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";
17 export interface ListArguments {
25 includeOldVersions?: boolean;
28 export interface ListResults<T> {
34 itemsAvailable: number;
37 export class CommonService<T> {
38 protected serverApi: AxiosInstance;
39 protected resourceType: string;
40 protected actions: ApiActions;
41 protected readOnlyFields: string[];
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;
50 static mapResponseKeys = (response: { data: any }) =>
51 CommonService.mapKeys(_.camelCase)(response.data)
53 static mapKeys = (mapFn: (key: string) => string) =>
54 (value: any): any => {
56 case _.isPlainObject(value):
59 .map(key => [key, mapFn(key)])
60 .reduce((newValue, [key, newKey]) => ({
62 [newKey]: (key === 'items') ? CommonService.mapKeys(mapFn)(value[key]) : value[key]
64 case _.isArray(value):
65 return value.map(CommonService.mapKeys(mapFn));
71 static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true, showErrors = true): Promise<R> {
73 actions.progressFn(reqId, true);
76 actions.progressFn(reqId, false);
79 .then((response: { data: any }) => {
80 return mapKeys ? CommonService.mapResponseKeys(response) : response.data;
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);
91 create(data?: Partial<T>) {
92 return CommonService.defaultResponse(
94 .post<T>(this.resourceType, data && CommonService.mapKeys(_.snakeCase)(data)),
99 delete(uuid: string): Promise<T> {
100 return CommonService.defaultResponse(
102 .delete(this.resourceType + '/' + uuid),
107 get(uuid: string, showErrors?: boolean) {
108 return CommonService.defaultResponse(
110 .get<T>(this.resourceType + '/' + uuid),
117 list(args: ListArguments = {}): Promise<ListResults<T>> {
118 const { filters, order, ...other } = args;
120 ...CommonService.mapKeys(_.snakeCase)(other),
121 filters: filters ? `[${filters}]` : undefined,
122 order: order ? order : undefined
125 if (QueryString.stringify(params).length <= 1500) {
126 return CommonService.defaultResponse(
127 this.serverApi.get(this.resourceType, { params }),
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]);
139 return CommonService.defaultResponse(
140 this.serverApi.post(this.resourceType, formData, {
150 update(uuid: string, data: Partial<T>) {
151 return CommonService.defaultResponse(
153 .put<T>(this.resourceType + '/' + uuid, data && CommonService.mapKeys(_.snakeCase)(data)),