17319: Throws exceptions when attempting to use "" as uuid on service calls.
[arvados-workbench2.git] / src / services / common-service / common-service.ts
index 07ff398a4abdd7d4409bb67dd660e02f304af359..54c0edf6bf2c684346e0309103ad085c4f38a541 100644 (file)
@@ -6,8 +6,10 @@ import * as _ from "lodash";
 import { AxiosInstance, AxiosPromise } from "axios";
 import * as uuid from "uuid/v4";
 import { ApiActions } from "~/services/api/api-actions";
+import * as QueryString from "query-string";
 
 interface Errors {
+    status: number;
     errors: string[];
     errorToken: string;
 }
@@ -20,6 +22,7 @@ export interface ListArguments {
     select?: string[];
     distinct?: boolean;
     count?: string;
+    includeOldVersions?: boolean;
 }
 
 export interface ListResults<T> {
@@ -35,11 +38,13 @@ export class CommonService<T> {
     protected serverApi: AxiosInstance;
     protected resourceType: string;
     protected actions: ApiActions;
+    protected readOnlyFields: string[];
 
-    constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) {
+    constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
         this.serverApi = serverApi;
-        this.resourceType = '/' + resourceType + '/';
+        this.resourceType = '/' + resourceType;
         this.actions = actions;
+        this.readOnlyFields = readOnlyFields;
     }
 
     static mapResponseKeys = (response: { data: any }) =>
@@ -54,7 +59,7 @@ export class CommonService<T> {
                         .map(key => [key, mapFn(key)])
                         .reduce((newValue, [key, newKey]) => ({
                             ...newValue,
-                            [newKey]: CommonService.mapKeys(mapFn)(value[key])
+                            [newKey]: (key === 'items') ? CommonService.mapKeys(mapFn)(value[key]) : value[key]
                         }), {});
                 case _.isArray(value):
                     return value.map(CommonService.mapKeys(mapFn));
@@ -63,7 +68,13 @@ export class CommonService<T> {
             }
         }
 
-    static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true): Promise<R> {
+    private validateUuid(uuid: string) {
+        if (uuid === "") {
+            throw new Error('UUID cannot be empty string');
+        }
+    }
+
+    static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true, showErrors = true): Promise<R> {
         const reqId = uuid();
         actions.progressFn(reqId, true);
         return promise
@@ -77,7 +88,8 @@ export class CommonService<T> {
             .catch(({ response }) => {
                 actions.progressFn(reqId, false);
                 const errors = CommonService.mapResponseKeys(response) as Errors;
-                actions.errorFn(reqId, errors);
+                errors.status = response.status;
+                actions.errorFn(reqId, errors, showErrors);
                 throw errors;
             });
     }
@@ -91,41 +103,63 @@ export class CommonService<T> {
     }
 
     delete(uuid: string): Promise<T> {
+        this.validateUuid(uuid);
         return CommonService.defaultResponse(
             this.serverApi
-                .delete(this.resourceType + uuid),
+                .delete(this.resourceType + '/' + uuid),
             this.actions
         );
     }
 
-    get(uuid: string) {
+    get(uuid: string, showErrors?: boolean) {
+        this.validateUuid(uuid);
         return CommonService.defaultResponse(
             this.serverApi
-                .get<T>(this.resourceType + uuid),
-            this.actions
+                .get<T>(this.resourceType + '/' + uuid),
+            this.actions,
+            true, // mapKeys
+            showErrors
         );
     }
 
     list(args: ListArguments = {}): Promise<ListResults<T>> {
         const { filters, order, ...other } = args;
         const params = {
-            ...other,
+            ...CommonService.mapKeys(_.snakeCase)(other),
             filters: filters ? `[${filters}]` : undefined,
             order: order ? order : undefined
         };
-        return CommonService.defaultResponse(
-            this.serverApi
-                .get(this.resourceType, {
-                    params: CommonService.mapKeys(_.snakeCase)(params)
+
+        if (QueryString.stringify(params).length <= 1500) {
+            return CommonService.defaultResponse(
+                this.serverApi.get(this.resourceType, { params }),
+                this.actions
+            );
+        } else {
+            // Using the POST special case to avoid URI length 414 errors.
+            const formData = new FormData();
+            formData.append("_method", "GET");
+            Object.keys(params).map(key => {
+                if (params[key] !== undefined) {
+                    formData.append(key, params[key]);
+                }
+            });
+            return CommonService.defaultResponse(
+                this.serverApi.post(this.resourceType, formData, {
+                    params: {
+                        _method: 'GET'
+                    }
                 }),
-            this.actions
-        );
+                this.actions
+            );
+        }
     }
 
     update(uuid: string, data: Partial<T>) {
+        this.validateUuid(uuid);
         return CommonService.defaultResponse(
             this.serverApi
-                .put<T>(this.resourceType + uuid, data && CommonService.mapKeys(_.snakeCase)(data)),
+                .put<T>(this.resourceType + '/' + uuid, data && CommonService.mapKeys(_.snakeCase)(data)),
             this.actions
         );
     }