axios-retry
Advanced tools
Comparing version 2.0.1 to 3.0.0
@@ -8,2 +8,15 @@ # Change Log | ||
## [3.0.0] - 2017-08-13 | ||
### Changed | ||
- Retried errors on idempotent requests (5xx with get, head, options, put and delete) by default, | ||
along with network errors. | ||
- Moved some hard-coded conditions to the default `retryCondition` function so users can define a | ||
custom function that overwrites them. The conditions that verify that the error is not a timeout or an unsafe network error have been moved to `isNetworkError`. | ||
### Added | ||
- Added additional pre-defined retry conditions: `isSafeRequestError`, `isIdempotentRequestError`. | ||
## [2.0.1] - 2017-06-19 | ||
### Fixed | ||
- Removed dependency from the `package.json` file. | ||
## [2.0.0] - 2017-06-15 | ||
@@ -13,4 +26,8 @@ ### Changed | ||
## [1.3.1] - 2017-06-19 | ||
### Fixed | ||
- Removed dependency from the `package.json` file. | ||
## [1.3.0] - 2017-06-15 | ||
### Added | ||
- Allowed per-request configuration using the `axios-retry` namespace. |
@@ -10,6 +10,46 @@ import isRetryAllowed from 'is-retry-allowed'; | ||
export function isNetworkError(error) { | ||
return !error.response; | ||
return !error.response | ||
&& Boolean(error.code) // Prevents retrying cancelled requests | ||
&& error.code !== 'ECONNABORTED' // Prevents retrying timed out requests | ||
&& isRetryAllowed(error); // Prevents retrying unsafe errors | ||
} | ||
const SAFE_HTTP_METHODS = ['get', 'head', 'options']; | ||
const IDEMPOTENT_HTTP_METHODS = SAFE_HTTP_METHODS.concat(['put', 'delete']); | ||
/** | ||
* @param {Error} error | ||
* @return {boolean} | ||
*/ | ||
export function isSafeRequestError(error) { | ||
if (!error.response || !error.config) { | ||
return false; | ||
} | ||
return error.response.status >= 500 && error.response.status <= 599 | ||
&& SAFE_HTTP_METHODS.indexOf(error.config.method) !== -1; | ||
} | ||
/** | ||
* @param {Error} error | ||
* @return {boolean} | ||
*/ | ||
export function isIdempotentRequestError(error) { | ||
if (!error.response || !error.config) { | ||
return false; | ||
} | ||
return error.response.status >= 500 && error.response.status <= 599 | ||
&& IDEMPOTENT_HTTP_METHODS.indexOf(error.config.method) !== -1; | ||
} | ||
/** | ||
* @param {Error} error | ||
* @return {boolean} | ||
*/ | ||
export function isNetworkOrIdempotentRequestError(error) { | ||
return isNetworkError(error) || isIdempotentRequestError(error); | ||
} | ||
/** | ||
* Initializes and returns the retry state for the given request/config | ||
@@ -89,3 +129,4 @@ * @param {AxiosRequestConfig} config | ||
* @param {number} [defaultOptions.retries=3] Number of retries | ||
* @param {number} [defaultOptions.retryCondition=isNetworkError] Number of retries | ||
* @param {number} [defaultOptions.retryCondition=isNetworkOrIdempotentRequestError] | ||
* A function to determine if the error can be retried | ||
*/ | ||
@@ -103,3 +144,3 @@ export default function axiosRetry(axios, defaultOptions) { | ||
retries = 3, | ||
retryCondition = isNetworkError | ||
retryCondition = isNetworkOrIdempotentRequestError | ||
} = getRequestOptions(config, defaultOptions); | ||
@@ -110,5 +151,3 @@ | ||
const shouldRetry = retryCondition(error) | ||
&& error.code !== 'ECONNABORTED' | ||
&& currentState.retryCount < retries | ||
&& isRetryAllowed(error); | ||
&& currentState.retryCount < retries; | ||
@@ -134,1 +173,7 @@ if (shouldRetry) { | ||
} | ||
// Compatibility with CommonJS | ||
axiosRetry.isNetworkError = isNetworkError; | ||
axiosRetry.isSafeRequestError = isSafeRequestError; | ||
axiosRetry.isIdempotentRequestError = isIdempotentRequestError; | ||
axiosRetry.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError; |
@@ -7,2 +7,5 @@ 'use strict'; | ||
exports.isNetworkError = isNetworkError; | ||
exports.isSafeRequestError = isSafeRequestError; | ||
exports.isIdempotentRequestError = isIdempotentRequestError; | ||
exports.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError; | ||
exports.default = axiosRetry; | ||
@@ -23,6 +26,43 @@ | ||
function isNetworkError(error) { | ||
return !error.response; | ||
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 | ||
} | ||
var SAFE_HTTP_METHODS = ['get', 'head', 'options']; | ||
var IDEMPOTENT_HTTP_METHODS = SAFE_HTTP_METHODS.concat(['put', 'delete']); | ||
/** | ||
* @param {Error} error | ||
* @return {boolean} | ||
*/ | ||
function isSafeRequestError(error) { | ||
if (!error.response || !error.config) { | ||
return false; | ||
} | ||
return error.response.status >= 500 && error.response.status <= 599 && SAFE_HTTP_METHODS.indexOf(error.config.method) !== -1; | ||
} | ||
/** | ||
* @param {Error} error | ||
* @return {boolean} | ||
*/ | ||
function isIdempotentRequestError(error) { | ||
if (!error.response || !error.config) { | ||
return false; | ||
} | ||
return error.response.status >= 500 && error.response.status <= 599 && IDEMPOTENT_HTTP_METHODS.indexOf(error.config.method) !== -1; | ||
} | ||
/** | ||
* @param {Error} error | ||
* @return {boolean} | ||
*/ | ||
function isNetworkOrIdempotentRequestError(error) { | ||
return isNetworkError(error) || isIdempotentRequestError(error); | ||
} | ||
/** | ||
* Initializes and returns the retry state for the given request/config | ||
@@ -102,3 +142,4 @@ * @param {AxiosRequestConfig} config | ||
* @param {number} [defaultOptions.retries=3] Number of retries | ||
* @param {number} [defaultOptions.retryCondition=isNetworkError] Number of retries | ||
* @param {number} [defaultOptions.retryCondition=isNetworkOrIdempotentRequestError] | ||
* A function to determine if the error can be retried | ||
*/ | ||
@@ -118,7 +159,7 @@ function axiosRetry(axios, defaultOptions) { | ||
_getRequestOptions$re2 = _getRequestOptions.retryCondition, | ||
retryCondition = _getRequestOptions$re2 === undefined ? isNetworkError : _getRequestOptions$re2; | ||
retryCondition = _getRequestOptions$re2 === undefined ? isNetworkOrIdempotentRequestError : _getRequestOptions$re2; | ||
var currentState = getCurrentState(config); | ||
var shouldRetry = retryCondition(error) && error.code !== 'ECONNABORTED' && currentState.retryCount < retries && (0, _isRetryAllowed2.default)(error); | ||
var shouldRetry = retryCondition(error) && currentState.retryCount < retries; | ||
@@ -144,2 +185,8 @@ if (shouldRetry) { | ||
} | ||
// Compatibility with CommonJS | ||
axiosRetry.isNetworkError = isNetworkError; | ||
axiosRetry.isSafeRequestError = isSafeRequestError; | ||
axiosRetry.isIdempotentRequestError = isIdempotentRequestError; | ||
axiosRetry.isNetworkOrIdempotentRequestError = isNetworkOrIdempotentRequestError; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "axios-retry", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"author": "Rubén Norte <ruben.norte@softonic.com>", | ||
@@ -5,0 +5,0 @@ "description": "Axios plugin that intercepts failed requests and retries them whenever posible.", |
@@ -55,3 +55,3 @@ # axios-retry | ||
| retries | `Number` | 3 | The number of times to retry before failing | | ||
| retryCondition | `Function` | `error => !error.response` | A callback to further control if a request should be retried. By default, it retries if the result did not have a response. | | ||
| 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). | | ||
@@ -58,0 +58,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
24189
312