retry-axios
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -1,2 +0,2 @@ | ||
import axios from 'axios'; | ||
import axios, { isCancel, } from 'axios'; | ||
/** | ||
@@ -21,4 +21,4 @@ * Attach the interceptor to the Axios instance. | ||
} | ||
function onFulfilled(res) { | ||
return res; | ||
function onFulfilled(result) { | ||
return result; | ||
} | ||
@@ -40,18 +40,18 @@ /** | ||
*/ | ||
function normalizeArray(obj) { | ||
const arr = []; | ||
if (!obj) { | ||
function normalizeArray(object) { | ||
const array = []; | ||
if (!object) { | ||
return undefined; | ||
} | ||
if (Array.isArray(obj)) { | ||
return obj; | ||
if (Array.isArray(object)) { | ||
return object; | ||
} | ||
if (typeof obj === 'object') { | ||
Object.keys(obj).forEach(key => { | ||
if (typeof object === 'object') { | ||
for (const key of Object.keys(object)) { | ||
if (typeof key === 'number') { | ||
arr[key] = obj[key]; | ||
array[key] = object[key]; | ||
} | ||
}); | ||
} | ||
} | ||
return arr; | ||
return array; | ||
} | ||
@@ -77,7 +77,7 @@ /** | ||
} | ||
function onError(err) { | ||
if (axios.isCancel(err)) { | ||
return Promise.reject(err); | ||
async function onError(error) { | ||
if (isCancel(error)) { | ||
throw error; | ||
} | ||
const config = getConfig(err) || {}; | ||
const config = getConfig(error) || {}; | ||
config.currentRetryAttempt = config.currentRetryAttempt || 0; | ||
@@ -101,3 +101,5 @@ config.retry = typeof config.retry === 'number' ? config.retry : 3; | ||
config.maxRetryAfter = | ||
typeof config.maxRetryAfter === 'number' ? config.maxRetryAfter : 60000 * 5; | ||
typeof config.maxRetryAfter === 'number' | ||
? config.maxRetryAfter | ||
: 60000 * 5; | ||
// If this wasn't in the list of status codes where we want | ||
@@ -120,8 +122,9 @@ // to automatically retry, return. | ||
// Put the config back into the err | ||
err.config = err.config || {}; // allow for wider range of errors | ||
err.config.raxConfig = { ...config }; | ||
const axiosError = error; | ||
axiosError.config = axiosError.config || {}; // Allow for wider range of errors | ||
axiosError.config.raxConfig = { ...config }; | ||
// Determine if we should retry the request | ||
const shouldRetryFn = config.shouldRetry || shouldRetryRequest; | ||
if (!shouldRetryFn(err)) { | ||
return Promise.reject(err); | ||
if (!shouldRetryFn(axiosError)) { | ||
throw axiosError; | ||
} | ||
@@ -132,6 +135,4 @@ // Create a promise that invokes the retry after the backOffDelay | ||
// If enabled, check for 'Retry-After' header in response to use as delay | ||
if (config.checkRetryAfter && | ||
err.response && | ||
err.response.headers['retry-after']) { | ||
const retryAfter = parseRetryAfter(err.response.headers['retry-after']); | ||
if (config.checkRetryAfter && axiosError.response?.headers['retry-after']) { | ||
const retryAfter = parseRetryAfter(axiosError.response.headers['retry-after']); | ||
if (retryAfter && retryAfter > 0 && retryAfter <= config.maxRetryAfter) { | ||
@@ -141,3 +142,4 @@ delay = retryAfter; | ||
else { | ||
return reject(err); | ||
reject(axiosError); | ||
return; | ||
} | ||
@@ -158,5 +160,5 @@ } | ||
// below. | ||
err.config.raxConfig.currentRetryAttempt += 1; | ||
// store with shorter and more expressive variable name. | ||
const retrycount = err.config.raxConfig | ||
axiosError.config.raxConfig.currentRetryAttempt += 1; | ||
// Store with shorter and more expressive variable name. | ||
const retrycount = axiosError.config.raxConfig | ||
.currentRetryAttempt; | ||
@@ -166,3 +168,3 @@ // Calculate delay according to chosen strategy | ||
if (delay === 0) { | ||
// was not set by Retry-After logic | ||
// Was not set by Retry-After logic | ||
if (config.backoffType === 'linear') { | ||
@@ -179,3 +181,3 @@ // The delay between the first (actual) attempt and the first retry | ||
else { | ||
delay = ((Math.pow(2, retrycount) - 1) / 2) * 1000; | ||
delay = ((2 ** retrycount - 1) / 2) * 1000; | ||
} | ||
@@ -189,10 +191,11 @@ if (typeof config.maxRetryDelay === 'number') { | ||
// Notify the user if they added an `onRetryAttempt` handler | ||
const onRetryAttemptPromise = config.onRetryAttempt | ||
? Promise.resolve(config.onRetryAttempt(err)) | ||
: Promise.resolve(); | ||
if (config.onRetryAttempt) { | ||
config.onRetryAttempt(axiosError); | ||
} | ||
const onRetryAttemptPromise = Promise.resolve(); | ||
// Return the promise in which recalls axios to retry the request | ||
return Promise.resolve() | ||
.then(() => onBackoffPromise) | ||
.then(() => onRetryAttemptPromise) | ||
.then(() => config.instance.request(err.config)); | ||
.then(async () => onBackoffPromise) | ||
.then(async () => onRetryAttemptPromise) | ||
.then(async () => config.instance.request(axiosError.config)); | ||
} | ||
@@ -203,4 +206,4 @@ /** | ||
*/ | ||
export function shouldRetryRequest(err) { | ||
const config = err.config.raxConfig; | ||
export function shouldRetryRequest(error) { | ||
const config = error.config.raxConfig; | ||
// If there's no config, or retries are disabled, return. | ||
@@ -211,3 +214,3 @@ if (!config || config.retry === 0) { | ||
// Check if this error has no response (ETIMEDOUT, ENOTFOUND, etc) | ||
if (!err.response && | ||
if (!error.response && | ||
(config.currentRetryAttempt || 0) >= config.noResponseRetries) { | ||
@@ -217,4 +220,4 @@ return false; | ||
// Only retry with configured HttpMethods. | ||
if (!err.config.method || | ||
config.httpMethodsToRetry.indexOf(err.config.method.toUpperCase()) < 0) { | ||
if (!error.config?.method || | ||
!config.httpMethodsToRetry.includes(error.config.method.toUpperCase())) { | ||
return false; | ||
@@ -224,6 +227,6 @@ } | ||
// to automatically retry, return. | ||
if (err.response && err.response.status) { | ||
if (error.response?.status) { | ||
let isInRange = false; | ||
for (const [min, max] of config.statusCodesToRetry) { | ||
const status = err.response.status; | ||
const { status } = error.response; | ||
if (status >= min && status <= max) { | ||
@@ -249,8 +252,7 @@ isInRange = true; | ||
*/ | ||
export function getConfig(err) { | ||
if (err && err.config) { | ||
return err.config.raxConfig; | ||
export function getConfig(error) { | ||
if (error?.config) { | ||
return error.config.raxConfig; | ||
} | ||
return; | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "retry-axios", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Retry HTTP requests with Axios.", | ||
@@ -16,5 +16,4 @@ "exports": "./build/src/index.js", | ||
"scripts": { | ||
"lint": "gts check", | ||
"clean": "gts clean", | ||
"fix": "gts fix", | ||
"lint": "xo --prettier", | ||
"fix": "xo --prettier --fix", | ||
"compile": "tsc -p .", | ||
@@ -37,14 +36,14 @@ "test": "c8 mocha build/test", | ||
"devDependencies": { | ||
"@types/mocha": "^9.1.1", | ||
"@types/sinon": "^10.0.11", | ||
"@types/node": "^17.0.31", | ||
"axios": "^0.26.0", | ||
"c8": "^7.11.2", | ||
"gts": "^3.1.0", | ||
"js-green-licenses": "^3.0.1", | ||
"mocha": "^10.0.0", | ||
"nock": "^13.2.4", | ||
"semantic-release": "^19.0.2", | ||
"sinon": "^13.0.2", | ||
"typescript": "~4.6.4" | ||
"@types/mocha": "^10.0.1", | ||
"@types/node": "^18.15.9", | ||
"@types/sinon": "^10.0.13", | ||
"axios": "^1.2.1", | ||
"c8": "^8.0.0", | ||
"js-green-licenses": "^4.0.0", | ||
"mocha": "^10.2.0", | ||
"nock": "^13.3.0", | ||
"semantic-release": "^21.0.0", | ||
"sinon": "^15.0.2", | ||
"typescript": "~5.2.0", | ||
"xo": "^0.56.0" | ||
}, | ||
@@ -59,3 +58,9 @@ "files": [ | ||
] | ||
}, | ||
"xo": { | ||
"rules": { | ||
"@typescript-eslint/prefer-nullish-coalescing": "off", | ||
"@typescript-eslint/consistent-type-definitions": "off" | ||
} | ||
} | ||
} |
@@ -63,2 +63,3 @@ # retry-axios | ||
// Retry twice on errors that don't return a response (ENOTFOUND, ETIMEDOUT, etc). | ||
// 'noResponseRetries' is limited by the 'retry' value. | ||
noResponseRetries: 2, | ||
@@ -65,0 +66,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
172
0
33145
5
241