mirror of
https://github.com/directus/directus.git
synced 2026-01-28 16:48:02 -05:00
Updated routes for SDK Settings, Relations, Collections, and Fields (#7192)
* draft * enpoints tested(manually) and working * Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com> * removed optional params Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
This commit is contained in:
@@ -48,11 +48,11 @@ export class Directus<T extends TypeMap> implements IDirectus<T> {
|
||||
private _relations?: RelationsHandler<TypeOf<T, 'directus_relations'>>;
|
||||
private _revisions?: RevisionsHandler<TypeOf<T, 'directus_revisions'>>;
|
||||
private _roles?: RolesHandler<TypeOf<T, 'directus_roles'>>;
|
||||
private _settings?: SettingsHandler<TypeOf<T, 'directus_settings'>>;
|
||||
private _users?: UsersHandler<TypeOf<T, 'directus_users'>>;
|
||||
private _server?: ServerHandler;
|
||||
private _utils?: UtilsHandler;
|
||||
private _graphql?: GraphQLHandler;
|
||||
private _settings?: SettingsHandler<TypeOf<T, 'directus_settings'>>;
|
||||
|
||||
private _items: {
|
||||
[collection: string]: ItemsHandler<any>;
|
||||
@@ -132,14 +132,12 @@ export class Directus<T extends TypeMap> implements IDirectus<T> {
|
||||
return this._roles || (this._roles = new RolesHandler<TypeOf<T, 'directus_roles'>>(this.transport));
|
||||
}
|
||||
|
||||
get settings(): SettingsHandler<TypeOf<T, 'directus_settings'>> {
|
||||
return this._settings || (this._settings = new SettingsHandler<TypeOf<T, 'directus_settings'>>(this.transport));
|
||||
}
|
||||
|
||||
get users(): UsersHandler<TypeOf<T, 'directus_users'>> {
|
||||
return this._users || (this._users = new UsersHandler<TypeOf<T, 'directus_users'>>(this.transport));
|
||||
}
|
||||
|
||||
get settings(): SettingsHandler<TypeOf<T, 'directus_settings'>> {
|
||||
return this._settings || (this._settings = new SettingsHandler<TypeOf<T, 'directus_settings'>>(this.transport));
|
||||
}
|
||||
get server(): ServerHandler {
|
||||
return this._server || (this._server = new ServerHandler(this.transport));
|
||||
}
|
||||
|
||||
@@ -45,16 +45,16 @@ export interface IDirectus<T extends TypeMap> {
|
||||
|
||||
readonly activity: ActivityHandler<TypeOf<T, 'directus_activity'>>;
|
||||
readonly collections: CollectionsHandler<TypeOf<T, 'directus_collections'>>;
|
||||
readonly fields: FieldsHandler<TypeOf<T, 'directus_fields'>>;
|
||||
readonly files: FilesHandler<TypeOf<T, 'directus_files'>>;
|
||||
readonly fields: FieldsHandler<TypeOf<T, 'directus_fields'>>;
|
||||
readonly folders: FoldersHandler<TypeOf<T, 'directus_folders'>>;
|
||||
readonly permissions: PermissionsHandler<TypeOf<T, 'directus_permissions'>>;
|
||||
readonly presets: PresetsHandler<TypeOf<T, 'directus_presets'>>;
|
||||
readonly relations: RelationsHandler<TypeOf<T, 'directus_relations'>>;
|
||||
readonly revisions: RevisionsHandler<TypeOf<T, 'directus_revisions'>>;
|
||||
readonly relations: RelationsHandler<TypeOf<T, 'directus_relations'>>;
|
||||
readonly roles: RolesHandler<TypeOf<T, 'directus_roles'>>;
|
||||
readonly settings: SettingsHandler<TypeOf<T, 'directus_settings'>>;
|
||||
readonly users: UsersHandler<TypeOf<T, 'directus_users'>>;
|
||||
readonly settings: SettingsHandler<TypeOf<T, 'directus_settings'>>;
|
||||
readonly server: ServerHandler;
|
||||
readonly utils: UtilsHandler;
|
||||
readonly graphql: GraphQLHandler;
|
||||
|
||||
@@ -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<T = DefaultType> = CollectionType & T;
|
||||
|
||||
export class CollectionsHandler<T = DefaultType> extends ItemsHandler<CollectionItem<T>> {
|
||||
export class CollectionsHandler<T = CollectionItem> {
|
||||
transport: ITransport;
|
||||
constructor(transport: ITransport) {
|
||||
super('directus_collections', transport);
|
||||
this.transport = transport;
|
||||
}
|
||||
|
||||
async readOne(collection: string): Promise<OneItem<T>> {
|
||||
const response = await this.transport.get(`/collections/${collection}`);
|
||||
return response.data as T;
|
||||
}
|
||||
|
||||
async readAll(): Promise<ManyItems<T>> {
|
||||
const { data, meta } = await this.transport.get(`/collections`);
|
||||
|
||||
return {
|
||||
data,
|
||||
meta,
|
||||
};
|
||||
}
|
||||
|
||||
async createOne(collection: PartialItem<T>): Promise<OneItem<T>> {
|
||||
return (await this.transport.post<T>(`/collections`, collection)).data;
|
||||
}
|
||||
|
||||
async createMany(collections: PartialItem<T>[]): Promise<ManyItems<T>> {
|
||||
const { data, meta } = await this.transport.get(`/collections`, {
|
||||
params: collections,
|
||||
});
|
||||
|
||||
return {
|
||||
data,
|
||||
meta,
|
||||
};
|
||||
}
|
||||
|
||||
async updateOne(collection: string, item: PartialItem<T>, query?: QueryOne<T>): Promise<OneItem<T>> {
|
||||
return (
|
||||
await this.transport.patch<PartialItem<T>>(`/collections/${collection}`, item, {
|
||||
params: query,
|
||||
})
|
||||
).data;
|
||||
}
|
||||
|
||||
async deleteOne(collection: string): Promise<void> {
|
||||
await this.transport.delete(`/collections/${collection}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T = DefaultType> = FieldType & T;
|
||||
|
||||
export class FieldsHandler<T = DefaultType> extends ItemsHandler<FieldItem<T>> {
|
||||
export class FieldsHandler<T = FieldItem> {
|
||||
transport: ITransport;
|
||||
constructor(transport: ITransport) {
|
||||
super('directus_fields', transport);
|
||||
this.transport = transport;
|
||||
}
|
||||
|
||||
async readOne(collection: string, id: ID): Promise<OneItem<T>> {
|
||||
const response = await this.transport.get(`/fields/${collection}/${id}`);
|
||||
return response.data as T;
|
||||
}
|
||||
|
||||
async readMany(collection: string): Promise<ManyItems<T>> {
|
||||
const response = await this.transport.get(`/fields/${collection}`);
|
||||
return response.data as T;
|
||||
}
|
||||
|
||||
async readAll(): Promise<ManyItems<T>> {
|
||||
const response = await this.transport.get(`/fields`);
|
||||
return response.data as T;
|
||||
}
|
||||
|
||||
async createOne(collection: string, item: PartialItem<T>): Promise<OneItem<T>> {
|
||||
return (await this.transport.post<T>(`/fields/${collection}`, item)).data;
|
||||
}
|
||||
|
||||
async updateOne(collection: string, field: string, item: PartialItem<T>): Promise<OneItem<T>> {
|
||||
return (await this.transport.patch<PartialItem<T>>(`/fields/${collection}/${field}}`, item)).data;
|
||||
}
|
||||
|
||||
async deleteOne(collection: string, field: string): Promise<void> {
|
||||
await this.transport.delete(`/fields/${collection}/${field}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T = DefaultType> = RelationType & T;
|
||||
export class RelationsHandler<T = RelationItem> {
|
||||
transport: ITransport;
|
||||
|
||||
export class RelationsHandler<T = DefaultType> extends ItemsHandler<RelationItem<T>> {
|
||||
constructor(transport: ITransport) {
|
||||
super('directus_relations', transport);
|
||||
this.transport = transport;
|
||||
}
|
||||
|
||||
async readOne(collection: string, id: ID): Promise<OneItem<T>> {
|
||||
const response = await this.transport.get(`/relations/${collection}/${id}`);
|
||||
return response.data as T;
|
||||
}
|
||||
|
||||
async readMany(collection: string): Promise<ManyItems<T>> {
|
||||
const response = await this.transport.get(`/relations/${collection}`);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
async readAll(): Promise<ManyItems<T>> {
|
||||
const response = await this.transport.get(`/relations`);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
async createOne(item: PartialItem<T>): Promise<OneItem<T>> {
|
||||
return (await this.transport.post<T>(`/relations`, item)).data;
|
||||
}
|
||||
|
||||
async updateOne(collection: string, field: string, item: PartialItem<T>): Promise<OneItem<T>> {
|
||||
return (
|
||||
await this.transport.patch<PartialItem<T>>(`/relations/${collection}/${field}`, {
|
||||
params: item,
|
||||
})
|
||||
).data;
|
||||
}
|
||||
|
||||
async deleteOne(collection: string, field: string): Promise<void> {
|
||||
await this.transport.delete(`/relations/${collection}/${field}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T = DefaultType> = SettingType & T;
|
||||
|
||||
export class SettingsHandler<T = DefaultType> extends ItemsHandler<SettingItem<T>> {
|
||||
export class SettingsHandler<T = SettingItem> extends SingletonHandler<T> {
|
||||
constructor(transport: ITransport) {
|
||||
super('directus_settings', transport);
|
||||
}
|
||||
|
||||
@@ -5,23 +5,26 @@ import { ISingleton } from '../singleton';
|
||||
export class SingletonHandler<T> implements ISingleton<T> {
|
||||
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<T>): Promise<OneItem<T>> {
|
||||
const item = await this.transport.get<T>(`/items/${this.collection}`, {
|
||||
const item = await this.transport.get<T>(`${this.endpoint}`, {
|
||||
params: query,
|
||||
});
|
||||
return item.data;
|
||||
}
|
||||
|
||||
async update(data: PartialItem<T>, _query?: QueryOne<T>): Promise<OneItem<T>> {
|
||||
const item = await this.transport.patch<T>(`/items/${this.collection}`, data, {
|
||||
const item = await this.transport.patch<T>(`${this.endpoint}`, data, {
|
||||
params: _query,
|
||||
});
|
||||
|
||||
return item.data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user