import { AxiosInstance, AxiosPromise } from "axios";
import * as uuid from "uuid/v4";
import { ApiActions } from "~/services/api/api-actions";
+import * as QueryString from "query-string";
interface Errors {
+ status: number;
errors: string[];
errorToken: string;
}
select?: string[];
distinct?: boolean;
count?: string;
+ includeOldVersions?: boolean;
}
export interface ListResults<T> {
protected serverApi: AxiosInstance;
protected resourceType: string;
protected actions: ApiActions;
+ protected readOnlyFields: string[];
- constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) {
+ constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
this.serverApi = serverApi;
this.resourceType = '/' + resourceType;
this.actions = actions;
+ this.readOnlyFields = readOnlyFields;
}
static mapResponseKeys = (response: { data: any }) =>
}
}
- static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true): Promise<R> {
+ private validateUuid(uuid: string) {
+ if (uuid === "") {
+ throw new Error('UUID cannot be empty string');
+ }
+ }
+
+ static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true, showErrors = true): Promise<R> {
const reqId = uuid();
actions.progressFn(reqId, true);
return promise
.catch(({ response }) => {
actions.progressFn(reqId, false);
const errors = CommonService.mapResponseKeys(response) as Errors;
- actions.errorFn(reqId, errors);
+ errors.status = response.status;
+ actions.errorFn(reqId, errors, showErrors);
throw errors;
});
}
}
delete(uuid: string): Promise<T> {
+ this.validateUuid(uuid);
return CommonService.defaultResponse(
this.serverApi
.delete(this.resourceType + '/' + uuid),
);
}
- get(uuid: string) {
+ get(uuid: string, showErrors?: boolean) {
+ this.validateUuid(uuid);
return CommonService.defaultResponse(
this.serverApi
.get<T>(this.resourceType + '/' + uuid),
- this.actions
+ this.actions,
+ true, // mapKeys
+ showErrors
);
}
list(args: ListArguments = {}): Promise<ListResults<T>> {
const { filters, order, ...other } = args;
const params = {
- ...other,
+ ...CommonService.mapKeys(_.snakeCase)(other),
filters: filters ? `[${filters}]` : undefined,
order: order ? order : undefined
};
- return CommonService.defaultResponse(
- this.serverApi
- .get(this.resourceType, {
- params: CommonService.mapKeys(_.snakeCase)(params)
+
+ if (QueryString.stringify(params).length <= 1500) {
+ return CommonService.defaultResponse(
+ this.serverApi.get(this.resourceType, { params }),
+ this.actions
+ );
+ } else {
+ // Using the POST special case to avoid URI length 414 errors.
+ const formData = new FormData();
+ formData.append("_method", "GET");
+ Object.keys(params).map(key => {
+ if (params[key] !== undefined) {
+ formData.append(key, params[key]);
+ }
+ });
+ return CommonService.defaultResponse(
+ this.serverApi.post(this.resourceType, formData, {
+ params: {
+ _method: 'GET'
+ }
}),
- this.actions
- );
+ this.actions
+ );
+ }
}
update(uuid: string, data: Partial<T>) {
+ this.validateUuid(uuid);
return CommonService.defaultResponse(
this.serverApi
.put<T>(this.resourceType + '/' + uuid, data && CommonService.mapKeys(_.snakeCase)(data)),