axios-retry
Advanced tools
Comparing version 3.0.1 to 3.0.2
@@ -8,2 +8,8 @@ # Change Log | ||
## [3.0.2] - 2018-02-09 | ||
### Added | ||
- Now isRetryableError method is accessible. | ||
- Added delayStrategy option to be able to have exponential backoff for successive retries. | ||
## [3.0.1] - 2017-08-16 | ||
@@ -10,0 +16,0 @@ |
@@ -23,3 +23,3 @@ import isRetryAllowed from 'is-retry-allowed'; | ||
*/ | ||
function isRetryableError(error) { | ||
export function isRetryableError(error) { | ||
return error.code !== 'ECONNABORTED' | ||
@@ -64,2 +64,19 @@ && (!error.response || (error.response.status >= 500 && error.response.status <= 599)); | ||
/** | ||
* @return {number} - delay in milliseconds, always 0 | ||
*/ | ||
function noDelay() { | ||
return 0; | ||
} | ||
/** | ||
* @param {number} [retryNumber=0] | ||
* @return {number} - delay in milliseconds | ||
*/ | ||
export function exponentialDelay(retryNumber = 0) { | ||
const delay = Math.pow(2, retryNumber) * 100; | ||
const randomSum = delay * 0.2 * Math.random(); // 0-20% of the delay | ||
return delay + randomSum; | ||
} | ||
/** | ||
* Initializes and returns the retry state for the given request/config | ||
@@ -116,2 +133,10 @@ * @param {AxiosRequestConfig} config | ||
* | ||
* // Exponential back-off retry delay between requests | ||
* axiosRetry(axios, { retryDelay : axiosRetry.exponentialDelay}); | ||
* | ||
* // Custom retry delay | ||
* axiosRetry(axios, { retryDelay : (retryCount) => { | ||
* return retryCount * 1000; | ||
* }}); | ||
* | ||
* // Also works with custom axios instances | ||
@@ -140,4 +165,6 @@ * const client = axios.create({ baseURL: 'http://example.com' }); | ||
* @param {number} [defaultOptions.retries=3] Number of retries | ||
* @param {number} [defaultOptions.retryCondition=isNetworkOrIdempotentRequestError] | ||
* @param {Function} [defaultOptions.retryCondition=isNetworkOrIdempotentRequestError] | ||
* A function to determine if the error can be retried | ||
* @param {Function} [defaultOptions.retryDelay=noDelay] | ||
* A function to determine the delay between retry requests | ||
*/ | ||
@@ -161,3 +188,4 @@ export default function axiosRetry(axios, defaultOptions) { | ||
retries = 3, | ||
retryCondition = isNetworkOrIdempotentRequestError | ||
retryCondition = isNetworkOrIdempotentRequestError, | ||
retryDelay = noDelay | ||
} = getRequestOptions(config, defaultOptions); | ||
@@ -172,2 +200,3 @@ | ||
currentState.retryCount++; | ||
const delay = retryDelay(currentState.retryCount, error); | ||
@@ -181,6 +210,8 @@ // Axios fails merging this configuration to the default configuration because it has an issue | ||
// Minimum 1ms timeout (passing 0 or less to XHR means no timeout) | ||
config.timeout = Math.max(config.timeout - lastRequestDuration, 1); | ||
config.timeout = Math.max((config.timeout - lastRequestDuration) - delay, 1); | ||
} | ||
return axios(config); | ||
return new Promise((resolve) => | ||
setTimeout(() => resolve(axios(config)), delay) | ||
); | ||
} | ||
@@ -197,1 +228,2 @@ | ||
axiosRetry.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError; | ||
axiosRetry.exponentialDelay = exponentialDelay; |
@@ -7,5 +7,7 @@ 'use strict'; | ||
exports.isNetworkError = isNetworkError; | ||
exports.isRetryableError = isRetryableError; | ||
exports.isSafeRequestError = isSafeRequestError; | ||
exports.isIdempotentRequestError = isIdempotentRequestError; | ||
exports.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError; | ||
exports.exponentialDelay = exponentialDelay; | ||
exports.default = axiosRetry; | ||
@@ -26,4 +28,4 @@ | ||
function isNetworkError(error) { | ||
return !error.response && Boolean(error.code // Prevents retrying cancelled requests | ||
) && error.code !== 'ECONNABORTED' // Prevents retrying timed out requests | ||
return !error.response && Boolean(error.code) // Prevents retrying cancelled requests | ||
&& error.code !== 'ECONNABORTED' // Prevents retrying timed out requests | ||
&& (0, _isRetryAllowed2.default)(error); // Prevents retrying unsafe errors | ||
@@ -78,2 +80,21 @@ } | ||
/** | ||
* @return {number} - delay in milliseconds, always 0 | ||
*/ | ||
function noDelay() { | ||
return 0; | ||
} | ||
/** | ||
* @param {number} [retryNumber=0] | ||
* @return {number} - delay in milliseconds | ||
*/ | ||
function exponentialDelay() { | ||
var retryNumber = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
var delay = Math.pow(2, retryNumber) * 100; | ||
var randomSum = delay * 0.2 * Math.random(); // 0-20% of the delay | ||
return delay + randomSum; | ||
} | ||
/** | ||
* Initializes and returns the retry state for the given request/config | ||
@@ -130,2 +151,10 @@ * @param {AxiosRequestConfig} config | ||
* | ||
* // Exponential back-off retry delay between requests | ||
* axiosRetry(axios, { retryDelay : axiosRetry.exponentialDelay}); | ||
* | ||
* // Custom retry delay | ||
* axiosRetry(axios, { retryDelay : (retryCount) => { | ||
* return retryCount * 1000; | ||
* }}); | ||
* | ||
* // Also works with custom axios instances | ||
@@ -154,4 +183,6 @@ * const client = axios.create({ baseURL: 'http://example.com' }); | ||
* @param {number} [defaultOptions.retries=3] Number of retries | ||
* @param {number} [defaultOptions.retryCondition=isNetworkOrIdempotentRequestError] | ||
* @param {Function} [defaultOptions.retryCondition=isNetworkOrIdempotentRequestError] | ||
* A function to determine if the error can be retried | ||
* @param {Function} [defaultOptions.retryDelay=noDelay] | ||
* A function to determine the delay between retry requests | ||
*/ | ||
@@ -177,3 +208,5 @@ function axiosRetry(axios, defaultOptions) { | ||
_getRequestOptions$re2 = _getRequestOptions.retryCondition, | ||
retryCondition = _getRequestOptions$re2 === undefined ? isNetworkOrIdempotentRequestError : _getRequestOptions$re2; | ||
retryCondition = _getRequestOptions$re2 === undefined ? isNetworkOrIdempotentRequestError : _getRequestOptions$re2, | ||
_getRequestOptions$re3 = _getRequestOptions.retryDelay, | ||
retryDelay = _getRequestOptions$re3 === undefined ? noDelay : _getRequestOptions$re3; | ||
@@ -186,2 +219,3 @@ var currentState = getCurrentState(config); | ||
currentState.retryCount++; | ||
var delay = retryDelay(currentState.retryCount, error); | ||
@@ -195,6 +229,10 @@ // Axios fails merging this configuration to the default configuration because it has an issue | ||
// Minimum 1ms timeout (passing 0 or less to XHR means no timeout) | ||
config.timeout = Math.max(config.timeout - lastRequestDuration, 1); | ||
config.timeout = Math.max(config.timeout - lastRequestDuration - delay, 1); | ||
} | ||
return axios(config); | ||
return new Promise(function (resolve) { | ||
return setTimeout(function () { | ||
return resolve(axios(config)); | ||
}, delay); | ||
}); | ||
} | ||
@@ -211,2 +249,3 @@ | ||
axiosRetry.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError; | ||
axiosRetry.exponentialDelay = exponentialDelay; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "axios-retry", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"author": "Rubén Norte <ruben.norte@softonic.com>", | ||
@@ -12,3 +12,4 @@ "description": "Axios plugin that intercepts failed requests and retries them whenever posible.", | ||
"lib", | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -15,0 +16,0 @@ "scripts": { |
@@ -27,2 +27,10 @@ # axios-retry | ||
// Exponential back-off retry delay between requests | ||
axiosRetry(axios, { retryDelay: axiosRetry.exponentialDelay}); | ||
// Custom retry delay | ||
axiosRetry(axios, { retryDelay: (retryCount) => { | ||
return retryCount * 1000; | ||
}}); | ||
// Works with custom axios instances | ||
@@ -57,2 +65,3 @@ const client = axios.create({ baseURL: 'http://example.com' }); | ||
| retryCondition | `Function` | `isNetworkOrIdempotentRequestError` | A callback to further control if a request should be retried. By default, it retries if it is a network error or a 5xx error on an idempotent request (GET, HEAD, OPTIONS, PUT or DELETE). | | ||
| retryDelay | `Function` | `0` | A callback to further control the delay between retried requests. By default there is no delay between retries. Another option is exponentialDelay ([Exponential Backoff](https://developers.google.com/analytics/devguides/reporting/core/v3/errors#backoff)). The function is passed `retryCount` and `error`. | | ||
@@ -59,0 +68,0 @@ ## Testing |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31932
9
436
82