X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/6f8e509988756deb7e05a760e718d1ade164fd19..a9ba50241080a83734e8aea0f230cc3fc3f3b7ca:/src/services/common-service/common-resource-service.ts diff --git a/src/services/common-service/common-resource-service.ts b/src/services/common-service/common-resource-service.ts index 0ad6fbce1f..0c99e7d4be 100644 --- a/src/services/common-service/common-resource-service.ts +++ b/src/services/common-service/common-resource-service.ts @@ -6,7 +6,7 @@ import * as _ from "lodash"; import { AxiosInstance, AxiosPromise } from "axios"; import { Resource } from "src/models/resource"; import * as uuid from "uuid/v4"; -import { ProgressFn } from "~/services/api/api-progress"; +import { ApiActions } from "~/services/api/api-actions"; export interface ListArguments { limit?: number; @@ -41,7 +41,7 @@ export enum CommonResourceServiceError { export class CommonResourceService { - static mapResponseKeys = (response: { data: any }): Promise => + static mapResponseKeys = (response: { data: any }) => CommonResourceService.mapKeys(_.camelCase)(response.data) static mapKeys = (mapFn: (key: string) => string) => @@ -62,36 +62,38 @@ export class CommonResourceService { } } - static defaultResponse(promise: AxiosPromise, progressFn: ProgressFn): Promise { + static defaultResponse(promise: AxiosPromise, actions: ApiActions): Promise { const reqId = uuid(); - progressFn(reqId, true); + actions.progressFn(reqId, true); return promise .then(data => { - progressFn(reqId, false); + actions.progressFn(reqId, false); return data; }) .then(CommonResourceService.mapResponseKeys) .catch(({ response }) => { - progressFn(reqId, false); - Promise.reject(CommonResourceService.mapResponseKeys(response)); + actions.progressFn(reqId, false); + const errors = CommonResourceService.mapResponseKeys(response) as Errors; + actions.errorFn(reqId, errors); + throw errors; }); } protected serverApi: AxiosInstance; protected resourceType: string; - protected progressFn: ProgressFn; + protected actions: ApiActions; - constructor(serverApi: AxiosInstance, resourceType: string, onProgress: ProgressFn) { + constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) { this.serverApi = serverApi; this.resourceType = '/' + resourceType + '/'; - this.progressFn = onProgress; + this.actions = actions; } - create(data?: Partial | any) { + create(data?: Partial) { return CommonResourceService.defaultResponse( this.serverApi .post(this.resourceType, data && CommonResourceService.mapKeys(_.snakeCase)(data)), - this.progressFn + this.actions ); } @@ -99,7 +101,7 @@ export class CommonResourceService { return CommonResourceService.defaultResponse( this.serverApi .delete(this.resourceType + uuid), - this.progressFn + this.actions ); } @@ -107,7 +109,7 @@ export class CommonResourceService { return CommonResourceService.defaultResponse( this.serverApi .get(this.resourceType + uuid), - this.progressFn + this.actions ); } @@ -123,7 +125,7 @@ export class CommonResourceService { .get(this.resourceType, { params: CommonResourceService.mapKeys(_.snakeCase)(params) }), - this.progressFn + this.actions ); } @@ -131,7 +133,7 @@ export class CommonResourceService { return CommonResourceService.defaultResponse( this.serverApi .put(this.resourceType + uuid, data && CommonResourceService.mapKeys(_.snakeCase)(data)), - this.progressFn + this.actions ); } }