-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
54 lines (48 loc) · 1.33 KB
/
index.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
import { AxiosError } from 'axios';
const RETRYABLE_ERRORS = [
'ETIMEDOUT',
'ECONNRESET',
'EADDRINUSE',
'ESOCKETTIMEDOUT',
'ECONNREFUSED',
'EPIPE'
];
const SAFE_HTTP_METHODS = ['GET', 'HEAD', 'OPTIONS'];
const IDEMPOTENT_HTTP_METHODS = SAFE_HTTP_METHODS.concat(['PUT', 'DELETE']);
export function isRetryable(error: AxiosError) {
if (error.code === undefined) {
return false;
}
return RETRYABLE_ERRORS.indexOf(error.code) !== -1;
}
export function isNetError(error: AxiosError) {
return (
error.response === undefined &&
isRetryable(error)
);
}
export function isServerError(error: AxiosError) {
return (
error.response !== undefined &&
error.response.status >= 500 &&
error.response.status <= 600 &&
isRetryable(error)
);
}
export function isIdempotent(error: AxiosError) {
return (
error.config !== undefined &&
error.config.method !== undefined &&
isRetryable(error) &&
IDEMPOTENT_HTTP_METHODS.indexOf(error.config.method) !== -1
);
}
export function isSafe(error: AxiosError) {
return (
error.config !== undefined &&
error.config.method !== undefined &&
isRetryable(error) &&
SAFE_HTTP_METHODS.indexOf(error.config.method) !== -1
);
}
export default isRetryable;