mirror of
https://github.com/directus/directus.git
synced 2026-01-28 11:58:11 -05:00
Add endpoint to items, add create test
This commit is contained in:
@@ -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<Response<Item | Item[]>>;
|
||||
async create(payloads: Payload | Payload[], query?: Query): Promise<Response<Item | Item[]>> {
|
||||
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<void>;
|
||||
async delete(keys: PrimaryKey[]): Promise<void>;
|
||||
async delete(keys: PrimaryKey | PrimaryKey[]): Promise<void> {
|
||||
await this.axios.delete(`/items/${this.collection}/${keys}`);
|
||||
await this.axios.delete(`${this.endpoint}${keys}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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'] } }
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user