From e1dbcbc2a975901c99bef925e24b636750f97af2 Mon Sep 17 00:00:00 2001 From: rijkvanzanten Date: Mon, 26 Oct 2020 13:43:39 +0100 Subject: [PATCH] Add endpoint to items, add create test --- packages/sdk-js/src/handlers/items.ts | 17 ++++---- packages/sdk-js/src/index.ts | 6 ++- packages/sdk-js/tests/_index.ts | 4 ++ packages/sdk-js/tests/handlers/items.ts | 56 ++++++++++++++++++++----- 4 files changed, 63 insertions(+), 20 deletions(-) diff --git a/packages/sdk-js/src/handlers/items.ts b/packages/sdk-js/src/handlers/items.ts index 2728413ceb..958688b4c6 100644 --- a/packages/sdk-js/src/handlers/items.ts +++ b/packages/sdk-js/src/handlers/items.ts @@ -2,12 +2,14 @@ import { Query, Item, Payload, Response, PrimaryKey } from '../types'; import { AxiosInstance } from 'axios'; export class ItemsHandler { - private collection: string; private axios: AxiosInstance; + private endpoint: string; constructor(collection: string, axios: AxiosInstance) { - this.collection = collection; this.axios = axios; + this.endpoint = collection.startsWith('directus_') + ? `/${collection.substring(9)}/` + : `/items/${collection}/`; } /** @@ -19,7 +21,7 @@ export class ItemsHandler { */ async create(payloads: Payload[], query?: Query): Promise>; async create(payloads: Payload | Payload[], query?: Query): Promise> { - const result = await this.axios.post(`/items/${this.collection}/`, payloads, { + const result = await this.axios.post(this.endpoint, payloads, { params: query, }); @@ -56,7 +58,7 @@ export class ItemsHandler { params = keysOrQuery as Query; } - let endpoint = `/items/${this.collection}/`; + let endpoint = this.endpoint; if (keys) { endpoint += keys; @@ -85,14 +87,15 @@ export class ItemsHandler { const key = keyOrPayload as PrimaryKey | PrimaryKey[]; const payload = payloadOrQuery as Payload; - const result = await this.axios.patch(`/items/${this.collection}/${key}`, payload, { + const result = await this.axios.patch(`${this.endpoint}${key}`, payload, { params: query, }); return result.data; } else { - const result = await this.axios.patch(`/items/${this.collection}/`, keyOrPayload, { + const result = await this.axios.patch(`${this.endpoint}`, keyOrPayload, { params: payloadOrQuery, }); + return result.data; } } @@ -100,6 +103,6 @@ export class ItemsHandler { async delete(key: PrimaryKey): Promise; async delete(keys: PrimaryKey[]): Promise; async delete(keys: PrimaryKey | PrimaryKey[]): Promise { - await this.axios.delete(`/items/${this.collection}/${keys}`); + await this.axios.delete(`${this.endpoint}${keys}`); } } diff --git a/packages/sdk-js/src/index.ts b/packages/sdk-js/src/index.ts index 7715d019a5..36e0b4ea8d 100644 --- a/packages/sdk-js/src/index.ts +++ b/packages/sdk-js/src/index.ts @@ -19,6 +19,10 @@ export default class DirectusSDK { } items(collection: string) { + if (collection.startsWith('directus_')) { + throw new Error(`You can't read the "${collection}" collection directly.`); + } + return new ItemsHandler(collection, this.axios); } @@ -30,5 +34,3 @@ export default class DirectusSDK { return new UtilsHandler(this.axios); } } - -const directus = new DirectusSDK('https://example.com'); diff --git a/packages/sdk-js/tests/_index.ts b/packages/sdk-js/tests/_index.ts index fc7686dc60..e7fe5f9e26 100644 --- a/packages/sdk-js/tests/_index.ts +++ b/packages/sdk-js/tests/_index.ts @@ -31,6 +31,10 @@ describe('DirectusSDK', () => { expect(directus.items('articles')).to.be.instanceOf(ItemsHandler); }); + it('Errors when trying to read a system collection directly', () => { + expect(() => directus.items('directus_files')).to.throw(); + }); + it('Returns UtilsHandler instance for #utils', () => { expect(directus.utils).to.be.instanceOf(UtilsHandler); }); diff --git a/packages/sdk-js/tests/handlers/items.ts b/packages/sdk-js/tests/handlers/items.ts index dc8eb095de..b3bf82b077 100644 --- a/packages/sdk-js/tests/handlers/items.ts +++ b/packages/sdk-js/tests/handlers/items.ts @@ -22,19 +22,53 @@ describe('ItemsHandler', () => { }); describe('constructor', () => { - it('Sets the passed collection to this.collection', () => { + it('Sets the correct endpoint', () => { const handler = new ItemsHandler('test', axiosInstance); - expect(handler['collection']).to.equal('test'); + expect(handler['endpoint']).to.equal('/items/test/'); + + const handler2 = new ItemsHandler('directus_activity', axiosInstance); + expect(handler2['endpoint']).to.equal('/activity/'); }); }); - // describe('specs.oas', () => { - // it('Calls the /server/specs/oas endpoint', async () => { - // const stub = sandbox - // .stub(handler['axios'], 'get') - // .returns(Promise.resolve({ data: '' })); - // await handler.specs.oas(); - // expect(stub).to.have.been.calledWith('/server/specs/oas'); - // }); - // }); + describe('create', () => { + it('Calls the /items/:collection endpoint', async () => { + const stub = sandbox + .stub(handler['axios'], 'post') + .returns(Promise.resolve({ data: '' })); + await handler.create({}); + expect(stub).to.have.been.calledWith('/items/test/'); + }); + + it('Passes the payload(s)', async () => { + const stub = sandbox + .stub(handler['axios'], 'post') + .returns(Promise.resolve({ data: '' })); + + await handler.create({ title: 'new item' }); + expect(stub).to.have.been.calledWith('/items/test/', { title: 'new item' }); + + await handler.create([{ title: 'new item' }, { title: 'another new item' }]); + expect(stub).to.have.been.calledWith('/items/test/', [ + { title: 'new item' }, + { title: 'another new item' }, + ]); + }); + + it('Adds the optional query', async () => { + const stub = sandbox + .stub(handler['axios'], 'post') + .returns(Promise.resolve({ data: '' })); + + await handler.create({}); + expect(stub).to.have.been.calledWith('/items/test/', {}); + + await handler.create({}, { fields: ['title'] }); + expect(stub).to.have.been.calledWith( + '/items/test/', + {}, + { params: { fields: ['title'] } } + ); + }); + }); });