UNPKG

1.09 kBJavaScriptView Raw
1const calculateRetryDelay = ({ attemptCount, retryOptions, error, retryAfter, computedValue, }) => {
2 if (error.name === 'RetryError') {
3 return 1;
4 }
5 if (attemptCount > retryOptions.limit) {
6 return 0;
7 }
8 const hasMethod = retryOptions.methods.includes(error.options.method);
9 const hasErrorCode = retryOptions.errorCodes.includes(error.code);
10 const hasStatusCode = error.response && retryOptions.statusCodes.includes(error.response.statusCode);
11 if (!hasMethod || (!hasErrorCode && !hasStatusCode)) {
12 return 0;
13 }
14 if (error.response) {
15 if (retryAfter) {
16 // In this case `computedValue` is `options.request.timeout`
17 if (retryAfter > computedValue) {
18 return 0;
19 }
20 return retryAfter;
21 }
22 if (error.response.statusCode === 413) {
23 return 0;
24 }
25 }
26 const noise = Math.random() * retryOptions.noise;
27 return Math.min(((2 ** (attemptCount - 1)) * 1000), retryOptions.backoffLimit) + noise;
28};
29export default calculateRetryDelay;