Add activity

This commit is contained in:
rijkvanzanten
2020-10-27 12:56:39 +01:00
parent 672a3c9eaa
commit af9a2e8ddb
6 changed files with 147 additions and 12 deletions

View File

@@ -9,11 +9,11 @@
- [x] Read
- [x] Update
- [x] Delete
- [ ] Activity
- [ ] Read
- [ ] Create Comment
- [ ] Update Comment
- [ ] Delete Comment
- [x] Activity
- [x] Read
- [x] Create Comment
- [x] Update Comment
- [x] Delete Comment
- [ ] Assets??? (not sure if needed)
- [ ] Read
- [ ] Auth

View File

@@ -0,0 +1,42 @@
import { AxiosInstance } from 'axios';
import { ItemsHandler } from './items';
import { Query, PrimaryKey, Item, Response, Payload } from '../types';
export class ActivityHandler {
private axios: AxiosInstance;
private itemsHandler: ItemsHandler;
constructor(axios: AxiosInstance) {
this.axios = axios;
this.itemsHandler = new ItemsHandler('directus_activity', axios);
}
async read(query?: Query): Promise<Response<Item | Item[]>>;
async read(key: PrimaryKey, query?: Query): Promise<Response<Item>>;
async read(keys: PrimaryKey[], query?: Query): Promise<Response<Item | Item[]>>;
async read(
keysOrQuery?: PrimaryKey | PrimaryKey[] | Query,
query?: Query & { single: boolean }
): Promise<Response<Item | Item[]>> {
const result = await this.itemsHandler.read(keysOrQuery as any, query as any);
return result;
}
comments = {
create: async (payload: {
collection: string;
item: string;
comment: string;
}): Promise<Response<Item>> => {
const response = await this.axios.post('/activity/comments', payload);
return response.data;
},
update: async (key: PrimaryKey, payload: { comment: string }) => {
const response = await this.axios.patch(`/activity/comments/${key}`, payload);
return response.data;
},
delete: async (key: PrimaryKey) => {
await this.axios.delete(`/activity/comments/${key}`);
},
};
}

View File

@@ -1,3 +1,4 @@
export * from './items';
export * from './server';
export * from './utils';
export * from './activity';

View File

@@ -12,13 +12,7 @@ export class ItemsHandler {
: `/items/${collection}/`;
}
/**
* Create a single new item
*/
async create(payload: Payload, query?: Query): Promise<Response<Item>>;
/**
* Create multiple new items at once
*/
async create(payloads: Payload[], query?: Query): Promise<Response<Item | Item[]>>;
async create(payloads: Payload | Payload[], query?: Query): Promise<Response<Item | Item[]>> {
const result = await this.axios.post(this.endpoint, payloads, {

View File

@@ -1,5 +1,5 @@
import axios, { AxiosInstance } from 'axios';
import { ItemsHandler, ServerHandler, UtilsHandler } from './handlers';
import { ItemsHandler, ServerHandler, UtilsHandler, ActivityHandler } from './handlers';
export default class DirectusSDK {
axios: AxiosInstance;
@@ -33,4 +33,8 @@ export default class DirectusSDK {
get utils() {
return new UtilsHandler(this.axios);
}
get activity() {
return new ActivityHandler(this.axios);
}
}

View File

@@ -0,0 +1,94 @@
import { ActivityHandler } from '../../src/handlers/activity';
import { ItemsHandler } from '../../src/handlers/items';
import axios, { AxiosInstance } from 'axios';
import sinon, { SinonSandbox } from 'sinon';
import chai, { expect } from 'chai';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);
describe('ActivityHandler', () => {
let sandbox: SinonSandbox;
let axiosInstance: AxiosInstance;
let handler: ActivityHandler;
beforeEach(() => {
sandbox = sinon.createSandbox();
axiosInstance = axios.create();
handler = new ActivityHandler(axiosInstance);
});
afterEach(() => {
sandbox.restore();
});
describe('constructor', () => {
it('Initializes ItemHandler instance', () => {
expect(handler['itemsHandler']).to.be.instanceOf(ItemsHandler);
});
});
describe('read', () => {
it('Calls the ItemsHandler with the provided params', async () => {
const stub = sandbox
.stub(handler['itemsHandler'], 'read')
.returns(Promise.resolve({ data: {} }));
await handler.read();
expect(stub).to.have.been.calledWith();
await handler.read(15);
expect(stub).to.have.been.calledWith(15);
await handler.read([15, 41]);
expect(stub).to.have.been.calledWith([15, 41]);
await handler.read([15, 41], { fields: ['title'] });
expect(stub).to.have.been.calledWith([15, 41], { fields: ['title'] });
});
});
describe('comments.create', () => {
it('Calls the right endpoint', async () => {
const stub = sandbox
.stub(handler['axios'], 'post')
.returns(Promise.resolve({ data: {} }));
await handler.comments.create({
collection: 'articles',
item: '15',
comment: 'Hello World',
});
expect(stub).to.have.been.calledWith('/activity/comments', {
collection: 'articles',
item: '15',
comment: 'Hello World',
});
});
});
describe('comments.update', () => {
it('Calls the right endpoint', async () => {
const stub = sandbox
.stub(handler['axios'], 'patch')
.returns(Promise.resolve({ data: {} }));
await handler.comments.update(15, { comment: 'Hello Update' });
expect(stub).to.have.been.calledWith('/activity/comments/15', {
comment: 'Hello Update',
});
});
});
describe('comments.delete', () => {
it('Calls the right endpoint', async () => {
const stub = sandbox.stub(handler['axios'], 'delete').returns(Promise.resolve());
await handler.comments.delete(15);
expect(stub).to.have.been.calledWith('/activity/comments/15');
});
});
});