20251: Fix flaky collection file browser by using race-free state update callback
[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 { camelCase, isPlainObject, isArray, snakeCase } from "lodash";
6 import { AxiosInstance, AxiosPromise, AxiosRequestConfig } from "axios";
7 import uuid from "uuid/v4";
8 import { ApiActions } from "services/api/api-actions";
9 import QueryString from "query-string";
10 import { Session } from "models/session";
11
12 interface Errors {
13     status: number;
14     errors: string[];
15     errorToken: string;
16 }
17
18 export interface ListArguments {
19     limit?: number;
20     offset?: number;
21     filters?: string;
22     order?: string;
23     select?: string[];
24     distinct?: boolean;
25     count?: string;
26     includeOldVersions?: boolean;
27 }
28
29 export interface ListResults<T> {
30     clusterId?: string;
31     kind: string;
32     offset: number;
33     limit: number;
34     items: T[];
35     itemsAvailable: number;
36 }
37
38 export class CommonService<T> {
39     protected serverApi: AxiosInstance;
40     protected resourceType: string;
41     protected actions: ApiActions;
42     protected readOnlyFields: string[];
43
44     constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
45         this.serverApi = serverApi;
46         this.resourceType = resourceType;
47         this.actions = actions;
48         this.readOnlyFields = readOnlyFields;
49     }
50
51     static mapResponseKeys = (response: { data: any }) =>
52         CommonService.mapKeys(camelCase)(response.data)
53
54     static mapKeys = (mapFn: (key: string) => string) =>
55         (value: any): any => {
56             switch (true) {
57                 case isPlainObject(value):
58                     return Object
59                         .keys(value)
60                         .map(key => [key, mapFn(key)])
61                         .reduce((newValue, [key, newKey]) => ({
62                             ...newValue,
63                             [newKey]: (key === 'items') ? CommonService.mapKeys(mapFn)(value[key]) : value[key]
64                         }), {});
65                 case isArray(value):
66                     return value.map(CommonService.mapKeys(mapFn));
67                 default:
68                     return value;
69             }
70         }
71
72     protected validateUuid(uuid: string) {
73         if (uuid === "") {
74             throw new Error('UUID cannot be empty string');
75         }
76     }
77
78     static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true, showErrors = true): Promise<R> {
79         const reqId = uuid();
80         actions.progressFn(reqId, true);
81         return promise
82             .then(data => {
83                 actions.progressFn(reqId, false);
84                 return data;
85             })
86             .then((response: { data: any }) => {
87                 return mapKeys ? CommonService.mapResponseKeys(response) : response.data;
88             })
89             .catch(({ response }) => {
90                 if (response) {
91                     actions.progressFn(reqId, false);
92                     const errors = CommonService.mapResponseKeys(response) as Errors;
93                     errors.status = response.status;
94                     actions.errorFn(reqId, errors, showErrors);
95                     throw errors;
96                 }
97             });
98     }
99
100     create(data?: Partial<T>, showErrors?: boolean) {
101         return CommonService.defaultResponse(
102             this.serverApi
103                 .post<T>(`/${this.resourceType}`, data && CommonService.mapKeys(snakeCase)(data)),
104             this.actions,
105             true, // mapKeys
106             showErrors
107         );
108     }
109
110     delete(uuid: string): Promise<T> {
111         this.validateUuid(uuid);
112         return CommonService.defaultResponse(
113             this.serverApi
114                 .delete(`/${this.resourceType}/${uuid}`),
115             this.actions
116         );
117     }
118
119     get(uuid: string, showErrors?: boolean, select?: string[], session?: Session) {
120         this.validateUuid(uuid);
121
122         const cfg: AxiosRequestConfig = {
123             params: {
124                 select: select
125                     ? `[${select.map(snakeCase).map(s => `"${s}"`).join(',')}]`
126                     : undefined
127             }
128         };
129         if (session) {
130             cfg.baseURL = session.baseUrl;
131             cfg.headers = { 'Authorization': 'Bearer ' + session.token };
132         }
133
134         return CommonService.defaultResponse(
135             this.serverApi
136                 .get<T>(`/${this.resourceType}/${uuid}`, cfg),
137             this.actions,
138             true, // mapKeys
139             showErrors
140         );
141     }
142
143     list(args: ListArguments = {}, showErrors?: boolean): Promise<ListResults<T>> {
144         const { filters, select, ...other } = args;
145         const params = {
146             ...CommonService.mapKeys(snakeCase)(other),
147             filters: filters ? `[${filters}]` : undefined,
148             select: select
149                 ? `[${select.map(snakeCase).map(s => `"${s}"`).join(', ')}]`
150                 : undefined
151         };
152
153         if (QueryString.stringify(params).length <= 1500) {
154             return CommonService.defaultResponse(
155                 this.serverApi.get(`/${this.resourceType}`, { params }),
156                 this.actions,
157                 true,
158                 showErrors
159             );
160         } else {
161             // Using the POST special case to avoid URI length 414 errors.
162             // We must use urlencoded post body since api doesn't support form data
163             // const formData = new FormData();
164             const formData = new URLSearchParams();
165             formData.append("_method", "GET");
166             Object.keys(params).forEach(key => {
167                 if (params[key] !== undefined) {
168                     formData.append(key, params[key]);
169                 }
170             });
171             return CommonService.defaultResponse(
172                 this.serverApi.post(`/${this.resourceType}`, formData, {}),
173                 this.actions,
174                 true,
175                 showErrors
176             );
177         }
178     }
179
180     update(uuid: string, data: Partial<T>, showErrors?: boolean) {
181         this.validateUuid(uuid);
182         return CommonService.defaultResponse(
183             this.serverApi
184                 .put<T>(`/${this.resourceType}/${uuid}`, data && CommonService.mapKeys(snakeCase)(data)),
185             this.actions,
186             undefined, // mapKeys
187             showErrors
188         );
189     }
190 }