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 { Resource } from "src/models/resource";
8 import * as uuid from "uuid/v4";
9 import { ApiActions } from "~/services/api/api-actions";
11 export interface ListArguments {
21 export interface ListResults<T> {
26 itemsAvailable: number;
29 export interface Errors {
34 export enum CommonResourceServiceError {
35 UNIQUE_VIOLATION = 'UniqueViolation',
36 OWNERSHIP_CYCLE = 'OwnershipCycle',
37 MODIFYING_CONTAINER_REQUEST_FINAL_STATE = 'ModifyingContainerRequestFinalState',
38 NAME_HAS_ALREADY_BEEN_TAKEN = 'NameHasAlreadyBeenTaken',
43 export class CommonResourceService<T extends Resource> {
45 static mapResponseKeys = (response: { data: any }) =>
46 CommonResourceService.mapKeys(_.camelCase)(response.data)
48 static mapKeys = (mapFn: (key: string) => string) =>
49 (value: any): any => {
51 case _.isPlainObject(value):
54 .map(key => [key, mapFn(key)])
55 .reduce((newValue, [key, newKey]) => ({
57 [newKey]: CommonResourceService.mapKeys(mapFn)(value[key])
59 case _.isArray(value):
60 return value.map(CommonResourceService.mapKeys(mapFn));
66 static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true): Promise<R> {
68 actions.progressFn(reqId, true);
71 actions.progressFn(reqId, false);
74 .then((response: { data: any }) => {
75 return mapKeys ? CommonResourceService.mapResponseKeys(response) : response.data;
77 .catch(({ response }) => {
78 actions.progressFn(reqId, false);
79 const errors = CommonResourceService.mapResponseKeys(response) as Errors;
80 actions.errorFn(reqId, errors);
85 protected serverApi: AxiosInstance;
86 protected resourceType: string;
87 protected actions: ApiActions;
89 constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) {
90 this.serverApi = serverApi;
91 this.resourceType = '/' + resourceType + '/';
92 this.actions = actions;
95 create(data?: Partial<T>) {
96 return CommonResourceService.defaultResponse(
98 .post<T>(this.resourceType, data && CommonResourceService.mapKeys(_.snakeCase)(data)),
103 delete(uuid: string): Promise<T> {
104 return CommonResourceService.defaultResponse(
106 .delete(this.resourceType + uuid),
112 return CommonResourceService.defaultResponse(
114 .get<T>(this.resourceType + uuid),
119 list(args: ListArguments = {}): Promise<ListResults<T>> {
120 const { filters, order, ...other } = args;
123 filters: filters ? `[${filters}]` : undefined,
124 order: order ? order : undefined
126 return CommonResourceService.defaultResponse(
128 .get(this.resourceType, {
129 params: CommonResourceService.mapKeys(_.snakeCase)(params)
135 update(uuid: string, data: Partial<T>) {
136 return CommonResourceService.defaultResponse(
138 .put<T>(this.resourceType + uuid, data && CommonResourceService.mapKeys(_.snakeCase)(data)),
144 export const getCommonResourceServiceError = (errorResponse: any) => {
145 if ('errors' in errorResponse && 'errorToken' in errorResponse) {
146 const error = errorResponse.errors.join('');
148 case /UniqueViolation/.test(error):
149 return CommonResourceServiceError.UNIQUE_VIOLATION;
150 case /ownership cycle/.test(error):
151 return CommonResourceServiceError.OWNERSHIP_CYCLE;
152 case /Mounts cannot be modified in state 'Final'/.test(error):
153 return CommonResourceServiceError.MODIFYING_CONTAINER_REQUEST_FINAL_STATE;
154 case /Name has already been taken/.test(error):
155 return CommonResourceServiceError.NAME_HAS_ALREADY_BEEN_TAKEN;
157 return CommonResourceServiceError.UNKNOWN;
160 return CommonResourceServiceError.NONE;