X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/47e0dc87fa82bac593c53518e556ba7c55410288..dd89200ad6fdbfa337fdbab5f54def8712c6746c:/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 2541feab..36017f0f 100644 --- a/src/common/api/common-resource-service.ts +++ b/src/common/api/common-resource-service.ts @@ -5,8 +5,8 @@ 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; @@ -26,6 +26,11 @@ export interface ListResults { itemsAvailable: number; } +export interface Errors { + errors: string[]; + errorToken: string; +} + export class CommonResourceService { static mapResponseKeys = (response: any): Promise => @@ -49,6 +54,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,22 +68,22 @@ 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> { @@ -82,15 +93,18 @@ export class CommonResourceService { filters: filters ? filters.serialize() : undefined, order: order ? order.getOrder() : 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)); + } }