-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
normalize.ts
43 lines (34 loc) 路 1.2 KB
/
normalize.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
import {requestMethods} from '../core/constants.js';
import type {HttpMethod} from '../types/options.js';
import type {RetryOptions} from '../types/retry.js';
export const normalizeRequestMethod = (input: string): string =>
requestMethods.includes(input as HttpMethod) ? input.toUpperCase() : input;
const retryMethods = ['get', 'put', 'head', 'delete', 'options', 'trace'];
const retryStatusCodes = [408, 413, 429, 500, 502, 503, 504];
const retryAfterStatusCodes = [413, 429, 503];
const defaultRetryOptions: Required<RetryOptions> = {
limit: 2,
methods: retryMethods,
statusCodes: retryStatusCodes,
afterStatusCodes: retryAfterStatusCodes,
maxRetryAfter: Number.POSITIVE_INFINITY,
};
export const normalizeRetryOptions = (retry: number | RetryOptions = {}): Required<RetryOptions> => {
if (typeof retry === 'number') {
return {
...defaultRetryOptions,
limit: retry,
};
}
if (retry.methods && !Array.isArray(retry.methods)) {
throw new Error('retry.methods must be an array');
}
if (retry.statusCodes && !Array.isArray(retry.statusCodes)) {
throw new Error('retry.statusCodes must be an array');
}
return {
...defaultRetryOptions,
...retry,
afterStatusCodes: retryAfterStatusCodes,
};
};