@esmkit/axios-retry
v0.1.0
Published
Axios plugin that intercepts failed requests and retries them whenever possible.
Readme
@esmkit/axios-retry
Axios plugin that intercepts failed requests and retries them whenever possible.
Usage
// CommonJS
// const axiosRetry = require('axios-retry').default;
// ES6
import axiosRetry from '@esmkit/axios-retry';
axiosRetry(axios, { retries: 3 });
axios.get('http://example.com/test') // The first request fails and the second returns 'ok'
.then(result => {
result.data; // 'ok'
});
// No retry delay
axiosRetry(axios, { retryDelay: axiosRetry.noDelay });
// Exponential back-off retry delay between requests
axiosRetry(axios, { retryDelay: axiosRetry.exponentialDelay });
// Linear retry delay between requests; note the different function signature
axiosRetry(axios, { retryDelay: axiosRetry.linearDelay() });
// Custom retry delay
axiosRetry(axios, { retryDelay: (retryCount) => {
return retryCount * 1000;
}});
// Exponential back-off retry delay with a custom initial delay
axiosRetry(axios, { retryDelay: (retryCount, error) => {
return axiosRetry.exponentialDelay(retryCount, error, 1000);
}});
// Works with custom axios instances
const client = axios.create({ baseURL: 'http://example.com' });
axiosRetry(client, { retries: 3 });
client.get('/test') // The first request fails and the second returns 'ok'
.then(result => {
result.data; // 'ok'
});
// Allows request-specific configuration
client
.get('/test', {
'axios-retry': {
retries: 0
}
})
.catch(error => { // The first request fails
error !== undefined
});Note: Unless shouldResetTimeout is set, the plugin interprets the request timeout as a global value, so it is not used for each retry but for the whole request lifecycle.
License
MIT © BILLGO. See LICENSE for details.
Migrate from npm package [email protected]
