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 private validateUuid(uuid: string) {
73 throw new Error('UUID cannot be empty string');
77 static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true, showErrors = true): Promise<R> {
79 actions.progressFn(reqId, true);
82 actions.progressFn(reqId, false);
85 .then((response: { data: any }) => {
86 return mapKeys ? CommonService.mapResponseKeys(response) : response.data;
88 .catch(({ response }) => {
89 actions.progressFn(reqId, false);
90 const errors = CommonService.mapResponseKeys(response) as Errors;
91 errors.status = response.status;
92 actions.errorFn(reqId, errors, showErrors);
97 create(data?: Partial<T>) {
98 return CommonService.defaultResponse(
100 .post<T>(this.resourceType, data && CommonService.mapKeys(_.snakeCase)(data)),
105 delete(uuid: string): Promise<T> {
106 this.validateUuid(uuid);
107 return CommonService.defaultResponse(
109 .delete(this.resourceType + '/' + uuid),
114 get(uuid: string, showErrors?: boolean) {
115 this.validateUuid(uuid);
116 return CommonService.defaultResponse(
118 .get<T>(this.resourceType + '/' + uuid),
125 list(args: ListArguments = {}): Promise<ListResults<T>> {
126 const { filters, order, ...other } = args;
128 ...CommonService.mapKeys(_.snakeCase)(other),
129 filters: filters ? `[${filters}]` : undefined,
130 order: order ? order : undefined
133 if (QueryString.stringify(params).length <= 1500) {
134 return CommonService.defaultResponse(
135 this.serverApi.get(this.resourceType, { params }),
139 // Using the POST special case to avoid URI length 414 errors.
140 const formData = new FormData();
141 formData.append("_method", "GET");
142 Object.keys(params).map(key => {
143 if (params[key] !== undefined) {
144 formData.append(key, params[key]);
147 return CommonService.defaultResponse(
148 this.serverApi.post(this.resourceType, formData, {
158 update(uuid: string, data: Partial<T>) {
159 this.validateUuid(uuid);
160 return CommonService.defaultResponse(
162 .put<T>(this.resourceType + '/' + uuid, data && CommonService.mapKeys(_.snakeCase)(data)),