From 1e16065bb05692d718e702daa9c3773b649550fa Mon Sep 17 00:00:00 2001 From: Jay Cammarano <67079013+jaycammarano@users.noreply.github.com> Date: Wed, 4 Aug 2021 16:00:05 -0400 Subject: [PATCH] Updated routes for SDK Settings, Relations, Collections, and Fields (#7192) * draft * enpoints tested(manually) and working * Co-authored-by: Rijk van Zanten * removed optional params Co-authored-by: Rijk van Zanten --- packages/sdk/src/base/directus.ts | 10 ++--- packages/sdk/src/directus.ts | 6 +-- packages/sdk/src/handlers/collections.ts | 48 ++++++++++++++++++++++-- packages/sdk/src/handlers/fields.ts | 36 ++++++++++++++++-- packages/sdk/src/handlers/relations.ts | 41 +++++++++++++++++--- packages/sdk/src/handlers/settings.ts | 5 +-- packages/sdk/src/handlers/singleton.ts | 7 +++- 7 files changed, 127 insertions(+), 26 deletions(-) diff --git a/packages/sdk/src/base/directus.ts b/packages/sdk/src/base/directus.ts index 0f8f366ec1..cca25abc27 100644 --- a/packages/sdk/src/base/directus.ts +++ b/packages/sdk/src/base/directus.ts @@ -48,11 +48,11 @@ export class Directus implements IDirectus { private _relations?: RelationsHandler>; private _revisions?: RevisionsHandler>; private _roles?: RolesHandler>; - private _settings?: SettingsHandler>; private _users?: UsersHandler>; private _server?: ServerHandler; private _utils?: UtilsHandler; private _graphql?: GraphQLHandler; + private _settings?: SettingsHandler>; private _items: { [collection: string]: ItemsHandler; @@ -132,14 +132,12 @@ export class Directus implements IDirectus { return this._roles || (this._roles = new RolesHandler>(this.transport)); } - get settings(): SettingsHandler> { - return this._settings || (this._settings = new SettingsHandler>(this.transport)); - } - get users(): UsersHandler> { return this._users || (this._users = new UsersHandler>(this.transport)); } - + get settings(): SettingsHandler> { + return this._settings || (this._settings = new SettingsHandler>(this.transport)); + } get server(): ServerHandler { return this._server || (this._server = new ServerHandler(this.transport)); } diff --git a/packages/sdk/src/directus.ts b/packages/sdk/src/directus.ts index 09e06e7b7c..e80a688b0c 100644 --- a/packages/sdk/src/directus.ts +++ b/packages/sdk/src/directus.ts @@ -45,16 +45,16 @@ export interface IDirectus { readonly activity: ActivityHandler>; readonly collections: CollectionsHandler>; - readonly fields: FieldsHandler>; readonly files: FilesHandler>; + readonly fields: FieldsHandler>; readonly folders: FoldersHandler>; readonly permissions: PermissionsHandler>; readonly presets: PresetsHandler>; - readonly relations: RelationsHandler>; readonly revisions: RevisionsHandler>; + readonly relations: RelationsHandler>; readonly roles: RolesHandler>; - readonly settings: SettingsHandler>; readonly users: UsersHandler>; + readonly settings: SettingsHandler>; readonly server: ServerHandler; readonly utils: UtilsHandler; readonly graphql: GraphQLHandler; diff --git a/packages/sdk/src/handlers/collections.ts b/packages/sdk/src/handlers/collections.ts index 2352a10cbb..44e3341329 100644 --- a/packages/sdk/src/handlers/collections.ts +++ b/packages/sdk/src/handlers/collections.ts @@ -2,14 +2,56 @@ * Collections handler */ -import { ItemsHandler } from '../base/items'; +import { ManyItems, OneItem, PartialItem, QueryOne } from '../items'; import { ITransport } from '../transport'; import { CollectionType, DefaultType } from '../types'; export type CollectionItem = CollectionType & T; -export class CollectionsHandler extends ItemsHandler> { +export class CollectionsHandler { + transport: ITransport; constructor(transport: ITransport) { - super('directus_collections', transport); + this.transport = transport; + } + + async readOne(collection: string): Promise> { + const response = await this.transport.get(`/collections/${collection}`); + return response.data as T; + } + + async readAll(): Promise> { + const { data, meta } = await this.transport.get(`/collections`); + + return { + data, + meta, + }; + } + + async createOne(collection: PartialItem): Promise> { + return (await this.transport.post(`/collections`, collection)).data; + } + + async createMany(collections: PartialItem[]): Promise> { + const { data, meta } = await this.transport.get(`/collections`, { + params: collections, + }); + + return { + data, + meta, + }; + } + + async updateOne(collection: string, item: PartialItem, query?: QueryOne): Promise> { + return ( + await this.transport.patch>(`/collections/${collection}`, item, { + params: query, + }) + ).data; + } + + async deleteOne(collection: string): Promise { + await this.transport.delete(`/collections/${collection}`); } } diff --git a/packages/sdk/src/handlers/fields.ts b/packages/sdk/src/handlers/fields.ts index b08d2d21a6..861529a094 100644 --- a/packages/sdk/src/handlers/fields.ts +++ b/packages/sdk/src/handlers/fields.ts @@ -2,14 +2,42 @@ * Fields handler */ -import { ItemsHandler } from '../base/items'; +import { ManyItems, OneItem, PartialItem } from '../items'; import { ITransport } from '../transport'; -import { FieldType, DefaultType } from '../types'; +import { FieldType, DefaultType, ID } from '../types'; export type FieldItem = FieldType & T; -export class FieldsHandler extends ItemsHandler> { +export class FieldsHandler { + transport: ITransport; constructor(transport: ITransport) { - super('directus_fields', transport); + this.transport = transport; + } + + async readOne(collection: string, id: ID): Promise> { + const response = await this.transport.get(`/fields/${collection}/${id}`); + return response.data as T; + } + + async readMany(collection: string): Promise> { + const response = await this.transport.get(`/fields/${collection}`); + return response.data as T; + } + + async readAll(): Promise> { + const response = await this.transport.get(`/fields`); + return response.data as T; + } + + async createOne(collection: string, item: PartialItem): Promise> { + return (await this.transport.post(`/fields/${collection}`, item)).data; + } + + async updateOne(collection: string, field: string, item: PartialItem): Promise> { + return (await this.transport.patch>(`/fields/${collection}/${field}}`, item)).data; + } + + async deleteOne(collection: string, field: string): Promise { + await this.transport.delete(`/fields/${collection}/${field}`); } } diff --git a/packages/sdk/src/handlers/relations.ts b/packages/sdk/src/handlers/relations.ts index 3e4a236bf3..3b064985dc 100644 --- a/packages/sdk/src/handlers/relations.ts +++ b/packages/sdk/src/handlers/relations.ts @@ -1,15 +1,46 @@ /** * Relations handler */ - -import { ItemsHandler } from '../base/items'; +import { ManyItems, OneItem, PartialItem } from '../items'; import { ITransport } from '../transport'; -import { RelationType, DefaultType } from '../types'; +import { RelationType, DefaultType, ID } from '../types'; export type RelationItem = RelationType & T; +export class RelationsHandler { + transport: ITransport; -export class RelationsHandler extends ItemsHandler> { constructor(transport: ITransport) { - super('directus_relations', transport); + this.transport = transport; + } + + async readOne(collection: string, id: ID): Promise> { + const response = await this.transport.get(`/relations/${collection}/${id}`); + return response.data as T; + } + + async readMany(collection: string): Promise> { + const response = await this.transport.get(`/relations/${collection}`); + return response.data; + } + + async readAll(): Promise> { + const response = await this.transport.get(`/relations`); + return response.data; + } + + async createOne(item: PartialItem): Promise> { + return (await this.transport.post(`/relations`, item)).data; + } + + async updateOne(collection: string, field: string, item: PartialItem): Promise> { + return ( + await this.transport.patch>(`/relations/${collection}/${field}`, { + params: item, + }) + ).data; + } + + async deleteOne(collection: string, field: string): Promise { + await this.transport.delete(`/relations/${collection}/${field}`); } } diff --git a/packages/sdk/src/handlers/settings.ts b/packages/sdk/src/handlers/settings.ts index 0c7eb4fe77..bbc487600c 100644 --- a/packages/sdk/src/handlers/settings.ts +++ b/packages/sdk/src/handlers/settings.ts @@ -1,14 +1,13 @@ /** * Settings handler */ - -import { ItemsHandler } from '../base/items'; import { ITransport } from '../transport'; import { SettingType, DefaultType } from '../types'; +import { SingletonHandler } from './singleton'; export type SettingItem = SettingType & T; -export class SettingsHandler extends ItemsHandler> { +export class SettingsHandler extends SingletonHandler { constructor(transport: ITransport) { super('directus_settings', transport); } diff --git a/packages/sdk/src/handlers/singleton.ts b/packages/sdk/src/handlers/singleton.ts index 57689d91ce..4ab1d3a1d5 100644 --- a/packages/sdk/src/handlers/singleton.ts +++ b/packages/sdk/src/handlers/singleton.ts @@ -5,23 +5,26 @@ import { ISingleton } from '../singleton'; export class SingletonHandler implements ISingleton { protected collection: string; protected transport: ITransport; + protected endpoint: string; constructor(collection: string, transport: ITransport) { this.collection = collection; this.transport = transport; + this.endpoint = collection.startsWith('directus_') ? `/${collection.substring(9)}` : `/items/${collection}`; } async read(query?: QueryOne): Promise> { - const item = await this.transport.get(`/items/${this.collection}`, { + const item = await this.transport.get(`${this.endpoint}`, { params: query, }); return item.data; } async update(data: PartialItem, _query?: QueryOne): Promise> { - const item = await this.transport.patch(`/items/${this.collection}`, data, { + const item = await this.transport.patch(`${this.endpoint}`, data, { params: _query, }); + return item.data; } }