export class CommonResourceService<T extends Resource> {
- static mapResponseKeys = (response: { data: any }): Promise<any> =>
+ static mapResponseKeys = (response: { data: any }) =>
CommonResourceService.mapKeys(_.camelCase)(response.data)
static mapKeys = (mapFn: (key: string) => string) =>
}
}
- static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions): Promise<R> {
+ static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true): Promise<R> {
const reqId = uuid();
actions.progressFn(reqId, true);
return promise
actions.progressFn(reqId, false);
return data;
})
- .then(CommonResourceService.mapResponseKeys)
+ .then((response: { data: any }) => {
+ return mapKeys ? CommonResourceService.mapResponseKeys(response) : response.data;
+ })
.catch(({ response }) => {
actions.progressFn(reqId, false);
- actions.errorFn(reqId, response.message);
- Promise.reject<Errors>(CommonResourceService.mapResponseKeys(response));
+ const errors = CommonResourceService.mapResponseKeys(response) as Errors;
+ actions.errorFn(reqId, errors);
+ throw errors;
});
}
this.actions = actions;
}
- create(data?: Partial<T> | any) {
+ create(data?: Partial<T>) {
return CommonResourceService.defaultResponse(
this.serverApi
.post<T>(this.resourceType, data && CommonResourceService.mapKeys(_.snakeCase)(data)),