8e00c4ad1abd42897f84cc1634d34a22b5bd48f0
[arvados-workbench2.git] / src / services / common-service / common-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as _ from "lodash";
6 import { AxiosInstance, AxiosPromise } from "axios";
7 import * as uuid from "uuid/v4";
8 import { ApiActions } from "~/services/api/api-actions";
9 import * as QueryString from "query-string";
10
11 interface Errors {
12     status: number;
13     errors: string[];
14     errorToken: string;
15 }
16
17 export interface ListArguments {
18     limit?: number;
19     offset?: number;
20     filters?: string;
21     order?: string;
22     select?: string[];
23     distinct?: boolean;
24     count?: string;
25     includeOldVersions?: boolean;
26 }
27
28 export interface ListResults<T> {
29     clusterId?: string;
30     kind: string;
31     offset: number;
32     limit: number;
33     items: T[];
34     itemsAvailable: number;
35 }
36
37 export class CommonService<T> {
38     protected serverApi: AxiosInstance;
39     protected resourceType: string;
40     protected actions: ApiActions;
41     protected readOnlyFields: string[];
42
43     constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
44         this.serverApi = serverApi;
45         this.resourceType = '/' + resourceType;
46         this.actions = actions;
47         this.readOnlyFields = readOnlyFields;
48     }
49
50     static mapResponseKeys = (response: { data: any }) =>
51         CommonService.mapKeys(_.camelCase)(response.data)
52
53     static mapKeys = (mapFn: (key: string) => string) =>
54         (value: any): any => {
55             switch (true) {
56                 case _.isPlainObject(value):
57                     return Object
58                         .keys(value)
59                         .map(key => [key, mapFn(key)])
60                         .reduce((newValue, [key, newKey]) => ({
61                             ...newValue,
62                             [newKey]: (key === 'items') ? CommonService.mapKeys(mapFn)(value[key]) : value[key]
63                         }), {});
64                 case _.isArray(value):
65                     return value.map(CommonService.mapKeys(mapFn));
66                 default:
67                     return value;
68             }
69         }
70
71     static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true, showErrors = true): Promise<R> {
72         const reqId = uuid();
73         actions.progressFn(reqId, true);
74         return promise
75             .then(data => {
76                 actions.progressFn(reqId, false);
77                 return data;
78             })
79             .then((response: { data: any }) => {
80                 return mapKeys ? CommonService.mapResponseKeys(response) : response.data;
81             })
82             .catch(({ response }) => {
83                 actions.progressFn(reqId, false);
84                 const errors = CommonService.mapResponseKeys(response) as Errors;
85                 errors.status = response.status;
86                 actions.errorFn(reqId, errors, showErrors);
87                 throw errors;
88             });
89     }
90
91     create(data?: Partial<T>) {
92         return CommonService.defaultResponse(
93             this.serverApi
94                 .post<T>(this.resourceType, data && CommonService.mapKeys(_.snakeCase)(data)),
95             this.actions
96         );
97     }
98
99     delete(uuid: string): Promise<T> {
100         return CommonService.defaultResponse(
101             this.serverApi
102                 .delete(this.resourceType + '/' + uuid),
103             this.actions
104         );
105     }
106
107     get(uuid: string, showErrors?: boolean) {
108         return CommonService.defaultResponse(
109             this.serverApi
110                 .get<T>(this.resourceType + '/' + uuid),
111             this.actions,
112             true, // mapKeys
113             showErrors
114         );
115     }
116
117     list(args: ListArguments = {}): Promise<ListResults<T>> {
118         const { filters, order, ...other } = args;
119         const params = {
120             ...CommonService.mapKeys(_.snakeCase)(other),
121             filters: filters ? `[${filters}]` : undefined,
122             order: order ? order : undefined
123         };
124
125         if (QueryString.stringify(params).length <= 1500) {
126             return CommonService.defaultResponse(
127                 this.serverApi.get(this.resourceType, { params }),
128                 this.actions
129             );
130         } else {
131             // Using the POST special case to avoid URI length 414 errors.
132             const formData = new FormData();
133             formData.append("_method", "GET");
134             Object.keys(params).map(key => {
135                 if (params[key] !== undefined) {
136                     formData.append(key, params[key]);
137                 }
138             });
139             return CommonService.defaultResponse(
140                 this.serverApi.post(this.resourceType, formData, {
141                     params: {
142                         _method: 'GET'
143                     }
144                 }),
145                 this.actions
146             );
147         }
148     }
149
150     update(uuid: string, data: Partial<T>) {
151         return CommonService.defaultResponse(
152             this.serverApi
153                 .put<T>(this.resourceType + '/' + uuid, data && CommonService.mapKeys(_.snakeCase)(data)),
154             this.actions
155         );
156     }
157 }