-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (30 loc) · 1.32 KB
/
index.js
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
module.exports = (axios) => {
axios.interceptors.response.use(undefined, function axiosRetryInterceptor(err) {
let config = err.config;
// If config does not exist or the retry option is not set, reject
if (!config) return Promise.reject(err);
// set default retry times
if (!config.retry) config.retry = 3;
// get error code or status
let errCode = err.code || (err.response && err.response.status);
// check retryCode
if (config.retryCode && config.retryCode.length > 0 && !config.retryCode.includes(errCode)) {
console.log('errCode', errCode);
return Promise.reject(err);
}
// Set the variable for keeping track of the retry count
config.__retryCount = config.__retryCount || 0;
// Check if we've maxed out the total number of retries
if (config.__retryCount >= config.retry) return Promise.reject(err);
// Increase the retry count
config.__retryCount += 1;
// Create new promise to handle exponential backoff
const backoff = new Promise(resolve => setTimeout(resolve, config.retryDelay || 50));
// Return the promise in which recalls axios to retry the request
return backoff.then(() => {
// run retryBeforeFn before request
if (config.retryBeforeFn) config.retryBeforeFn(err);
return axios(config);
});
});
}