X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/b6a5b173cd4e9f325f371d26204dfe156d911c20..964b7f2cea81f087bbaddc94c9eeb08bab945742:/src/common/api/common-resource-service.ts diff --git a/src/common/api/common-resource-service.ts b/src/common/api/common-resource-service.ts index 2541feab02..caa4d760c9 100644 --- a/src/common/api/common-resource-service.ts +++ b/src/common/api/common-resource-service.ts @@ -3,16 +3,14 @@ // SPDX-License-Identifier: AGPL-3.0 import * as _ from "lodash"; -import { FilterBuilder } from "./filter-builder"; -import { OrderBuilder } from "./order-builder"; -import { AxiosInstance } from "axios"; -import { Resource } from "../../models/resource"; +import { AxiosInstance, AxiosPromise } from "axios"; +import { Resource } from "~/models/resource"; export interface ListArguments { limit?: number; offset?: number; - filters?: FilterBuilder; - order?: OrderBuilder; + filters?: string; + order?: string; select?: string[]; distinct?: boolean; count?: string; @@ -26,6 +24,11 @@ export interface ListResults { itemsAvailable: number; } +export interface Errors { + errors: string[]; + errorToken: string; +} + export class CommonResourceService { static mapResponseKeys = (response: any): Promise => @@ -49,6 +52,12 @@ export class CommonResourceService { } } + static defaultResponse(promise: AxiosPromise): Promise { + return promise + .then(CommonResourceService.mapResponseKeys) + .catch(({ response }) => Promise.reject(CommonResourceService.mapResponseKeys(response))); + } + protected serverApi: AxiosInstance; protected resourceType: string; @@ -57,40 +66,43 @@ export class CommonResourceService { this.resourceType = '/' + resourceType + '/'; } - create(data: Partial) { - return this.serverApi - .post(this.resourceType, CommonResourceService.mapKeys(_.snakeCase)(data)) - .then(CommonResourceService.mapResponseKeys); + create(data?: Partial | any) { + return CommonResourceService.defaultResponse( + this.serverApi + .post(this.resourceType, data && CommonResourceService.mapKeys(_.snakeCase)(data))); } delete(uuid: string): Promise { - return this.serverApi - .delete(this.resourceType + uuid) - .then(CommonResourceService.mapResponseKeys); + return CommonResourceService.defaultResponse( + this.serverApi + .delete(this.resourceType + uuid)); } get(uuid: string) { - return this.serverApi - .get(this.resourceType + uuid) - .then(CommonResourceService.mapResponseKeys); + return CommonResourceService.defaultResponse( + this.serverApi + .get(this.resourceType + uuid)); } list(args: ListArguments = {}): Promise> { const { filters, order, ...other } = args; const params = { ...other, - filters: filters ? filters.serialize() : undefined, - order: order ? order.getOrder() : undefined + filters: filters ? `[${filters}]` : undefined, + order: order ? order : undefined }; - return this.serverApi - .get(this.resourceType, { - params: CommonResourceService.mapKeys(_.snakeCase)(params) - }) - .then(CommonResourceService.mapResponseKeys); + return CommonResourceService.defaultResponse( + this.serverApi + .get(this.resourceType, { + params: CommonResourceService.mapKeys(_.snakeCase)(params) + })); } - update(uuid: string) { - throw new Error("Not implemented"); + update(uuid: string, data: any) { + return CommonResourceService.defaultResponse( + this.serverApi + .put(this.resourceType + uuid, data)); + } }