ihub-framework-js
Version:
Legacy version of iHub Framework written in Javascript
39 lines (30 loc) • 1.15 kB
JavaScript
const axios = require('axios');
const instance = axios.create({
timeout: 60000,
});
instance.interceptors.response.use(undefined, (err) => {
const { config } = err;
// If config does not exist or the retry option is not set or is a bad request (code 404) or not found (400) then reject immediately the request
if (!config || !config.retry || (err.response !== undefined
&& (err.response.status === 400 || err.response.status === 404))) {
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) {
// Reject with the error
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 || 1);
}));
// Return the promise in which recalls axios to retry the request
return backoff.then(() => axios(config));
});
module.exports = () => instance;