-
-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathapi-utils.ts
More file actions
119 lines (103 loc) · 3.22 KB
/
api-utils.ts
File metadata and controls
119 lines (103 loc) · 3.22 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import type { AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios';
import axios from 'axios';
import { networkErrorMessages } from 'common/constants/messages';
import { apiUrl, resourcesAPIURL } from 'common/config/environment';
const baseAxiosConfig = {
baseURL: apiUrl,
timeout: 15000,
};
const resourcesAxiosConfig = {
baseURL: resourcesAPIURL,
timeout: 15000,
};
export const OperationCodeAPI = axios.create(baseAxiosConfig);
/**
* @see https://github.com/OperationCode/resources_api
*/
export const ResourcesAPI = axios.create(resourcesAxiosConfig);
/**
* @description These pieces allow us to throw errors on connection timeouts
* @see https://github.com/axios/axios/issues/647#issuecomment-459517694
*/
const getRequestAbortionPieces = () => {
const abort = axios.CancelToken.source();
const connectionTimeout = setTimeout(
() => abort.cancel(`Connection timeout of ${baseAxiosConfig.timeout}ms.`),
baseAxiosConfig.timeout,
);
return { abort, connectionTimeout };
};
export const get = async (
path: string,
{ parameters }: { parameters?: Record<string, AxiosRequestConfig['params']> } = {},
axiosClient = OperationCodeAPI,
) => {
const { abort, connectionTimeout } = getRequestAbortionPieces();
return axiosClient
.get(path, {
cancelToken: abort.token,
params: parameters,
})
.then(response => {
clearTimeout(connectionTimeout);
return response;
})
.catch(error => {
clearTimeout(connectionTimeout);
throw error;
});
};
export const post = async (path: string, body: object, axiosClient: AxiosInstance = axios) => {
const { abort, connectionTimeout } = getRequestAbortionPieces();
return axiosClient
.post(path, body, {
cancelToken: abort.token,
})
.then(response => {
clearTimeout(connectionTimeout);
return response;
})
.catch(error => {
clearTimeout(connectionTimeout);
throw error;
});
};
export const patch = async (path: string, body: object, axiosClient: AxiosInstance = axios) => {
const { abort, connectionTimeout } = getRequestAbortionPieces();
return axiosClient
.patch(path, body, {
cancelToken: abort.token,
})
.then(response => {
clearTimeout(connectionTimeout);
return response;
})
.catch(error => {
clearTimeout(connectionTimeout);
throw error;
});
};
export const put = async (path: string, body: object, axiosClient: AxiosInstance = axios) => {
const { abort, connectionTimeout } = getRequestAbortionPieces();
return axiosClient
.put(path, body, {
cancelToken: abort.token,
})
.then(response => {
clearTimeout(connectionTimeout);
return response;
})
.catch(error => {
clearTimeout(connectionTimeout);
throw error;
});
};
/**
* @description Take an expected server error object and return its error.
* If object is unexpected, assume the server is down and return a relavant error message.
*/
export const getServerErrorMessage = (errorObject: AxiosError | Error | unknown) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const abstractError = errorObject as any;
return abstractError?.response?.data?.error ?? networkErrorMessages.serverDown;
};