Merge branch 'master' of git.curoverse.com:arvados-workbench2 into 13827-structured...
[arvados-workbench2.git] / src / services / common-service / common-resource-service.ts
index 0ad6fbce1f3df525f4fd139af33e4a9c91c75fd6..70c1df0e2bc5e665a3c62c05b39b7a50a6705c62 100644 (file)
@@ -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<T extends Resource> {
 
-    static mapResponseKeys = (response: { data: any }): Promise<any> =>
+    static mapResponseKeys = (response: { data: any }) =>
         CommonResourceService.mapKeys(_.camelCase)(response.data)
 
     static mapKeys = (mapFn: (key: string) => string) =>
@@ -62,36 +62,40 @@ export class CommonResourceService<T extends Resource> {
             }
         }
 
-    static defaultResponse<R>(promise: AxiosPromise<R>, progressFn: ProgressFn): Promise<R> {
+    static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true): Promise<R> {
         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)
+            .then((response: { data: any }) => {
+                return mapKeys ? CommonResourceService.mapResponseKeys(response) : response.data;
+            })
             .catch(({ response }) => {
-                progressFn(reqId, false);
-                Promise.reject<Errors>(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<T> | any) {
+    create(data?: Partial<T>) {
         return CommonResourceService.defaultResponse(
             this.serverApi
                 .post<T>(this.resourceType, data && CommonResourceService.mapKeys(_.snakeCase)(data)),
-            this.progressFn
+            this.actions
         );
     }
 
@@ -99,7 +103,7 @@ export class CommonResourceService<T extends Resource> {
         return CommonResourceService.defaultResponse(
             this.serverApi
                 .delete(this.resourceType + uuid),
-            this.progressFn
+            this.actions
         );
     }
 
@@ -107,7 +111,7 @@ export class CommonResourceService<T extends Resource> {
         return CommonResourceService.defaultResponse(
             this.serverApi
                 .get<T>(this.resourceType + uuid),
-            this.progressFn
+            this.actions
         );
     }
 
@@ -123,7 +127,7 @@ export class CommonResourceService<T extends Resource> {
                 .get(this.resourceType, {
                     params: CommonResourceService.mapKeys(_.snakeCase)(params)
                 }),
-            this.progressFn
+            this.actions
         );
     }
 
@@ -131,7 +135,7 @@ export class CommonResourceService<T extends Resource> {
         return CommonResourceService.defaultResponse(
             this.serverApi
                 .put<T>(this.resourceType + uuid, data && CommonResourceService.mapKeys(_.snakeCase)(data)),
-            this.progressFn
+            this.actions
         );
     }
 }