-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpTransformers.ts
57 lines (53 loc) · 1.73 KB
/
HttpTransformers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Response transformer for the API. It is used to transform the response from the API.
* @param response - Response from the API.
* @returns - Transformed response.
*/
import { AxiosError, AxiosResponse } from 'axios';
import { BadRequestError } from '../../utils/Errors/BadRequestError';
import { IError } from '../../utils/Errors/IError';
import { AuthorizationError } from '../../utils/Errors/AuthorizationError';
export function responseTransformer(response: any): any | null {
return response || null;
}
/**
* Error transformer for the API. It is used to transform the error from the API.
* @param error - Error from the API.
* @returns - Transformed error.
*/
export function errorTransformer(error: AxiosError) {
// The request was made and the server responded with a status code that falls out of the range of 2xx
if (error.response) {
return Promise.reject(handleErrorResponse(error.response));
}
return Promise.reject(error);
}
/**
* Response handler for the API. It is used to handle the error from the API.
* @param error - Error from the API.
* @param response
*/
function handleErrorResponse(response: AxiosResponse<unknown, any>): IError {
switch (response.status) {
case 400:
throw new BadRequestError(response.data);
case 401:
throw new AuthorizationError();
default:
throw new Error('Unknown error');
}
}
export function errorHandler(error: IError) {
switch (error.constructor) {
case AuthorizationError:
// Do something with the bad request error data
break;
case BadRequestError:
// Do something with the bad request error data
break;
default:
// Do something with the bad request error data
break;
}
return Promise.reject(error);
}