diff --git a/resources/sdk/typescript/index.ts b/resources/sdk/typescript/index.ts index 5d63fde..7509e44 100644 --- a/resources/sdk/typescript/index.ts +++ b/resources/sdk/typescript/index.ts @@ -202,6 +202,21 @@ const request = } }; +// interface RequestWithData { +// method: 'POST' | 'PATCH' | 'PUT', +// resourceType: T, +// data: {}, +// search: {}, +// headers: {}, +// } + +// interface RequestWithoutData { +// method: 'GET' | 'DELETE' +// resourceType: T, +// search: {}, +// headers: {}, +// } + class Task { private readonly workers: Array; @@ -653,10 +668,25 @@ export class Client { head: request(this.client.head), }); + + + // request = (params: RequestWithData | RequestWithoutData) => { + + // } + + + private search = (resourceName: T) => { + return new GetResources(this.client, resourceName); + } + resource = { - list: (resourceName: T) => { - return new GetResources(this.client, resourceName); + processBundle: (data: Bundle): Promise => { + return this.client + .post(buildResourceUrl(''), { json: data }) + .json(); }, + search: this.search, + list: this.search, get: async ( resourceName: T, id: string, @@ -669,38 +699,39 @@ export class Client { resourceName: T, id: string, ): Promise> => { - return this.client - .delete(buildResourceUrl(resourceName, id)) - .json>(); + return new SearchResources((searchParams: URLSearchParams) => { + return this.client + .delete(buildResourceUrl(resourceName, id), { searchParams }); + }, resourceName) }, update: async ( resourceName: T, id: string, input: PartialResourceBody, ): Promise> => { - return this.client - .patch(buildResourceUrl(resourceName, id), { json: input }) - .json>(); + return new SearchResources((searchParams: URLSearchParams) => { + return this.client + .patch(buildResourceUrl(resourceName, id), { json: input, searchParams }); + }, resourceName) }, create: async ( resourceName: T, - input: SetOptional< - ResourceTypeMap[T] & { resourceType: string }, - "resourceType" - >, + input: SetOptional, ): Promise> => { - return this.client - .post(buildResourceUrl(resourceName), { json: input }) - .json>(); + return new SearchResources((searchParams: URLSearchParams) => { + return this.client + .post(buildResourceUrl(resourceName), { json: input, searchParams }); + }, resourceName) }, override: async ( resourceName: T, id: string, input: PartialResourceBody, ): Promise> => { - return this.client - .put(buildResourceUrl(resourceName, id), { json: input }) - .json>(); + return new SearchResources((searchParams: URLSearchParams) => { + return this.client + .put(buildResourceUrl(resourceName, id), { json: input, searchParams }); + }, resourceName) }, }; @@ -902,6 +933,79 @@ export class GetResources< } } +export class SearchResources implements PromiseLike> +{ + private searchParamsObject: URLSearchParams; + resourceName: T; + fun: (params: URLSearchParams) => ResponsePromise; + + constructor(fun: (params: URLSearchParams) => ResponsePromise, resourceName: T) { + this.searchParamsObject = new URLSearchParams(); + this.resourceName = resourceName; + this.fun = fun; + } + + where< + K extends keyof SearchParams[T], + SP extends SearchParams[T][K], + PR extends PrefixWithArray, + >(key: K | string, value: SP | SP[], prefix?: PR): this; + + where< + K extends keyof SearchParams[T], + SP extends SearchParams[T][K], + PR extends Exclude, + >(key: K | string, value: SP, prefix?: PR): this; + + where< + K extends keyof SearchParams[T], + SP extends SearchParams[T][K], + PR extends SP extends number ? Prefix : never, + >(key: K | string, value: SP | SP[], prefix?: Prefix | never): this { + if (Array.isArray(value)) { + const val = value as SP[]; + if (prefix) { + if (prefix === 'eq') { + this.searchParamsObject.append(key.toString(), val.join(',')); + return this; + } + + val.forEach((item) => { + this.searchParamsObject.append(key.toString(), `${prefix}${item}`); + }); + + return this; + } + + const queryValues = val.join(','); + this.searchParamsObject.append(key.toString(), queryValues); + + return this; + } + const queryValue = `${prefix ?? ''}${value}`; + + this.searchParamsObject.append(key.toString(), queryValue); + return this; + } + + then, TResult2 = never>( + onfulfilled?: + | ((value: BaseResponseResource) => PromiseLike | TResult1) + | undefined + | null, + _onrejected?: + | ((reason: unknown) => PromiseLike | TResult2) + | undefined + | null, + ): PromiseLike { + + return this.fun(this.searchParamsObject) + .then((response) => { + return onfulfilled ? onfulfilled(response.json()) : (response.json() as TResult1); + }); + } +} + type EventType = | "awf.workflow.event/workflow-init" | "awf.workflow.event/task-completed";