1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
16 export interface ListArguments {
26 export interface ListResults<T> {
32 itemsAvailable: number;
35 export class CommonService<T> {
36 protected serverApi: AxiosInstance;
37 protected resourceType: string;
38 protected actions: ApiActions;
39 protected readOnlyFields: string[];
41 constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
42 this.serverApi = serverApi;
43 this.resourceType = '/' + resourceType;
44 this.actions = actions;
45 this.readOnlyFields = readOnlyFields;
48 static mapResponseKeys = (response: { data: any }) =>
49 CommonService.mapKeys(_.camelCase)(response.data)
51 static mapKeys = (mapFn: (key: string) => string) =>
52 (value: any): any => {
54 case _.isPlainObject(value):
57 .map(key => [key, mapFn(key)])
58 .reduce((newValue, [key, newKey]) => ({
60 [newKey]: (key === 'items') ? CommonService.mapKeys(mapFn)(value[key]) : value[key]
62 case _.isArray(value):
63 return value.map(CommonService.mapKeys(mapFn));
69 static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true): Promise<R> {
71 actions.progressFn(reqId, true);
74 actions.progressFn(reqId, false);
77 .then((response: { data: any }) => {
78 return mapKeys ? CommonService.mapResponseKeys(response) : response.data;
80 .catch(({ response }) => {
81 actions.progressFn(reqId, false);
82 const errors = CommonService.mapResponseKeys(response) as Errors;
83 actions.errorFn(reqId, errors);
88 create(data?: Partial<T>) {
89 return CommonService.defaultResponse(
91 .post<T>(this.resourceType, data && CommonService.mapKeys(_.snakeCase)(data)),
96 delete(uuid: string): Promise<T> {
97 return CommonService.defaultResponse(
99 .delete(this.resourceType + '/' + uuid),
105 return CommonService.defaultResponse(
107 .get<T>(this.resourceType + '/' + uuid),
112 list(args: ListArguments = {}): Promise<ListResults<T>> {
113 const { filters, order, ...other } = args;
116 filters: filters ? `[${filters}]` : undefined,
117 order: order ? order : undefined
120 if (QueryString.stringify(params).length <= 1500) {
121 return CommonService.defaultResponse(
122 this.serverApi.get(this.resourceType, { params }),
126 // Using the POST special case to avoid URI length 414 errors.
127 const formData = new FormData();
128 formData.append("_method", "GET");
129 Object.keys(params).map(key => {
130 if (params[key] !== undefined) {
131 formData.append(key, params[key]);
134 return CommonService.defaultResponse(
135 this.serverApi.post(this.resourceType, formData, {
145 update(uuid: string, data: Partial<T>) {
146 return CommonService.defaultResponse(
148 .put<T>(this.resourceType + '/' + uuid, data && CommonService.mapKeys(_.snakeCase)(data)),