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