1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
18 export interface ListArguments {
26 includeOldVersions?: boolean;
29 export interface ListResults<T> {
35 itemsAvailable: number;
38 export class CommonService<T> {
39 protected serverApi: AxiosInstance;
40 protected resourceType: string;
41 protected actions: ApiActions;
42 protected readOnlyFields: string[];
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;
51 static mapResponseKeys = (response: { data: any }) =>
52 CommonService.mapKeys(camelCase)(response.data)
54 static mapKeys = (mapFn: (key: string) => string) =>
55 (value: any): any => {
57 case isPlainObject(value):
60 .map(key => [key, mapFn(key)])
61 .reduce((newValue, [key, newKey]) => ({
63 [newKey]: (key === 'items') ? CommonService.mapKeys(mapFn)(value[key]) : value[key]
66 return value.map(CommonService.mapKeys(mapFn));
72 protected validateUuid(uuid: string) {
74 throw new Error('UUID cannot be empty string');
78 static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true, showErrors = true): Promise<R> {
80 actions.progressFn(reqId, true);
83 actions.progressFn(reqId, false);
86 .then((response: { data: any }) => {
87 return mapKeys ? CommonService.mapResponseKeys(response) : response.data;
89 .catch(({ response }) => {
90 actions.progressFn(reqId, false);
91 const errors = CommonService.mapResponseKeys(response) as Errors;
92 errors.status = response.status;
93 actions.errorFn(reqId, errors, showErrors);
98 create(data?: Partial<T>, showErrors?: boolean) {
99 return CommonService.defaultResponse(
101 .post<T>(`/${this.resourceType}`, data && CommonService.mapKeys(snakeCase)(data)),
108 delete(uuid: string): Promise<T> {
109 this.validateUuid(uuid);
110 return CommonService.defaultResponse(
112 .delete(`/${this.resourceType}/${uuid}`),
117 get(uuid: string, showErrors?: boolean, select?: string[], session?: Session) {
118 this.validateUuid(uuid);
120 const cfg: AxiosRequestConfig = {};
122 cfg.baseURL = session.baseUrl;
123 cfg.headers = { 'Authorization': 'Bearer ' + session.token };
126 return CommonService.defaultResponse(
128 .get<T>(`/${this.resourceType}/${uuid}`, session ? cfg : undefined),
135 list(args: ListArguments = {}, showErrors?: boolean): Promise<ListResults<T>> {
136 const { filters, select, ...other } = args;
138 ...CommonService.mapKeys(snakeCase)(other),
139 filters: filters ? `[${filters}]` : undefined,
141 ? `[${select.map(snakeCase).map(s => `"${s}"`).join(', ')}]`
145 if (QueryString.stringify(params).length <= 1500) {
146 return CommonService.defaultResponse(
147 this.serverApi.get(`/${this.resourceType}`, { params }),
152 // Using the POST special case to avoid URI length 414 errors.
153 const formData = new FormData();
154 formData.append("_method", "GET");
155 Object.keys(params).forEach(key => {
156 if (params[key] !== undefined) {
157 formData.append(key, params[key]);
160 return CommonService.defaultResponse(
161 this.serverApi.post(`/${this.resourceType}`, formData, {
172 update(uuid: string, data: Partial<T>) {
173 this.validateUuid(uuid);
174 return CommonService.defaultResponse(
176 .put<T>(`/${this.resourceType}/${uuid}`, data && CommonService.mapKeys(snakeCase)(data)),