axios-retry
Advanced tools
Comparing version 4.2.0 to 4.3.0
@@ -1,2 +0,2 @@ | ||
import type { AxiosError, AxiosRequestConfig, AxiosInstance, AxiosStatic } from 'axios'; | ||
import type { AxiosError, AxiosRequestConfig, AxiosInstance, AxiosStatic, AxiosResponse } from 'axios'; | ||
export interface IAxiosRetryConfig { | ||
@@ -31,2 +31,7 @@ /** | ||
onMaxRetryTimesExceeded?: (error: AxiosError, retryCount: number) => Promise<void> | void; | ||
/** | ||
* A callback to define whether a response should be resolved or rejected. If null is passed, it will fallback to | ||
* the axios default (only 2xx status codes are resolved). | ||
*/ | ||
validateResponse?: ((response: AxiosResponse) => boolean) | null; | ||
} | ||
@@ -33,0 +38,0 @@ export interface IAxiosRetryConfigExtended extends IAxiosRetryConfig { |
@@ -96,3 +96,4 @@ "use strict"; | ||
onRetry: () => { }, | ||
onMaxRetryTimesExceeded: () => { } | ||
onMaxRetryTimesExceeded: () => { }, | ||
validateResponse: null | ||
}; | ||
@@ -140,2 +141,25 @@ function getRequestOptions(config, defaultOptions) { | ||
} | ||
function handleRetry(axiosInstance, currentState, error, config) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
currentState.retryCount += 1; | ||
const { retryDelay, shouldResetTimeout, onRetry } = currentState; | ||
const delay = retryDelay(currentState.retryCount, error); | ||
// Axios fails merging this configuration to the default configuration because it has an issue | ||
// with circular structures: https://github.com/mzabriskie/axios/issues/370 | ||
fixConfig(axiosInstance, config); | ||
if (!shouldResetTimeout && config.timeout && currentState.lastRequestTime) { | ||
const lastRequestDuration = Date.now() - currentState.lastRequestTime; | ||
const timeout = config.timeout - lastRequestDuration - delay; | ||
if (timeout <= 0) { | ||
return Promise.reject(error); | ||
} | ||
config.timeout = timeout; | ||
} | ||
config.transformRequest = [(data) => data]; | ||
yield onRetry(currentState.retryCount, error, config); | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(axiosInstance(config)), delay); | ||
}); | ||
}); | ||
} | ||
function handleMaxRetryTimesExceeded(currentState, error) { | ||
@@ -149,6 +173,12 @@ return __awaiter(this, void 0, void 0, function* () { | ||
const requestInterceptorId = axiosInstance.interceptors.request.use((config) => { | ||
var _a; | ||
setCurrentState(config, defaultOptions); | ||
if ((_a = config[exports.namespace]) === null || _a === void 0 ? void 0 : _a.validateResponse) { | ||
// by setting this, all HTTP responses will be go through the error interceptor first | ||
config.validateStatus = () => false; | ||
} | ||
return config; | ||
}); | ||
const responseInterceptorId = axiosInstance.interceptors.response.use(null, (error) => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a; | ||
const { config } = error; | ||
@@ -160,22 +190,8 @@ // If we have no information to retry the request | ||
const currentState = setCurrentState(config, defaultOptions); | ||
if (error.response && ((_a = currentState.validateResponse) === null || _a === void 0 ? void 0 : _a.call(currentState, error.response))) { | ||
// no issue with response | ||
return error.response; | ||
} | ||
if (yield shouldRetry(currentState, error)) { | ||
currentState.retryCount += 1; | ||
const { retryDelay, shouldResetTimeout, onRetry } = currentState; | ||
const delay = retryDelay(currentState.retryCount, error); | ||
// Axios fails merging this configuration to the default configuration because it has an issue | ||
// with circular structures: https://github.com/mzabriskie/axios/issues/370 | ||
fixConfig(axiosInstance, config); | ||
if (!shouldResetTimeout && config.timeout && currentState.lastRequestTime) { | ||
const lastRequestDuration = Date.now() - currentState.lastRequestTime; | ||
const timeout = config.timeout - lastRequestDuration - delay; | ||
if (timeout <= 0) { | ||
return Promise.reject(error); | ||
} | ||
config.timeout = timeout; | ||
} | ||
config.transformRequest = [(data) => data]; | ||
yield onRetry(currentState.retryCount, error, config); | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(axiosInstance(config)), delay); | ||
}); | ||
return handleRetry(axiosInstance, currentState, error, config); | ||
} | ||
@@ -182,0 +198,0 @@ yield handleMaxRetryTimesExceeded(currentState, error); |
@@ -1,2 +0,2 @@ | ||
import type { AxiosError, AxiosRequestConfig, AxiosInstance, AxiosStatic } from 'axios'; | ||
import type { AxiosError, AxiosRequestConfig, AxiosInstance, AxiosStatic, AxiosResponse } from 'axios'; | ||
export interface IAxiosRetryConfig { | ||
@@ -31,2 +31,7 @@ /** | ||
onMaxRetryTimesExceeded?: (error: AxiosError, retryCount: number) => Promise<void> | void; | ||
/** | ||
* A callback to define whether a response should be resolved or rejected. If null is passed, it will fallback to | ||
* the axios default (only 2xx status codes are resolved). | ||
*/ | ||
validateResponse?: ((response: AxiosResponse) => boolean) | null; | ||
} | ||
@@ -33,0 +38,0 @@ export interface IAxiosRetryConfigExtended extends IAxiosRetryConfig { |
@@ -71,3 +71,4 @@ import isRetryAllowed from 'is-retry-allowed'; | ||
onRetry: () => { }, | ||
onMaxRetryTimesExceeded: () => { } | ||
onMaxRetryTimesExceeded: () => { }, | ||
validateResponse: null | ||
}; | ||
@@ -113,2 +114,23 @@ function getRequestOptions(config, defaultOptions) { | ||
} | ||
async function handleRetry(axiosInstance, currentState, error, config) { | ||
currentState.retryCount += 1; | ||
const { retryDelay, shouldResetTimeout, onRetry } = currentState; | ||
const delay = retryDelay(currentState.retryCount, error); | ||
// Axios fails merging this configuration to the default configuration because it has an issue | ||
// with circular structures: https://github.com/mzabriskie/axios/issues/370 | ||
fixConfig(axiosInstance, config); | ||
if (!shouldResetTimeout && config.timeout && currentState.lastRequestTime) { | ||
const lastRequestDuration = Date.now() - currentState.lastRequestTime; | ||
const timeout = config.timeout - lastRequestDuration - delay; | ||
if (timeout <= 0) { | ||
return Promise.reject(error); | ||
} | ||
config.timeout = timeout; | ||
} | ||
config.transformRequest = [(data) => data]; | ||
await onRetry(currentState.retryCount, error, config); | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(axiosInstance(config)), delay); | ||
}); | ||
} | ||
async function handleMaxRetryTimesExceeded(currentState, error) { | ||
@@ -121,2 +143,6 @@ if (currentState.retryCount >= currentState.retries) | ||
setCurrentState(config, defaultOptions); | ||
if (config[namespace]?.validateResponse) { | ||
// by setting this, all HTTP responses will be go through the error interceptor first | ||
config.validateStatus = () => false; | ||
} | ||
return config; | ||
@@ -131,22 +157,8 @@ }); | ||
const currentState = setCurrentState(config, defaultOptions); | ||
if (error.response && currentState.validateResponse?.(error.response)) { | ||
// no issue with response | ||
return error.response; | ||
} | ||
if (await shouldRetry(currentState, error)) { | ||
currentState.retryCount += 1; | ||
const { retryDelay, shouldResetTimeout, onRetry } = currentState; | ||
const delay = retryDelay(currentState.retryCount, error); | ||
// Axios fails merging this configuration to the default configuration because it has an issue | ||
// with circular structures: https://github.com/mzabriskie/axios/issues/370 | ||
fixConfig(axiosInstance, config); | ||
if (!shouldResetTimeout && config.timeout && currentState.lastRequestTime) { | ||
const lastRequestDuration = Date.now() - currentState.lastRequestTime; | ||
const timeout = config.timeout - lastRequestDuration - delay; | ||
if (timeout <= 0) { | ||
return Promise.reject(error); | ||
} | ||
config.timeout = timeout; | ||
} | ||
config.transformRequest = [(data) => data]; | ||
await onRetry(currentState.retryCount, error, config); | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(axiosInstance(config)), delay); | ||
}); | ||
return handleRetry(axiosInstance, currentState, error, config); | ||
} | ||
@@ -153,0 +165,0 @@ await handleMaxRetryTimesExceeded(currentState, error); |
{ | ||
"name": "axios-retry", | ||
"version": "4.2.0", | ||
"version": "4.3.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.", |
@@ -70,2 +70,3 @@ # axios-retry | ||
| onMaxRetryTimesExceeded | `Function` | `function onMaxRetryTimesExceeded(error, retryCount) { return; }` | After all the retries are failed, this callback will be called with the last error before throwing the error. | | ||
| validateResponse | `Function \| null` | `null` | A callback to define whether a response should be resolved or rejected. If null is passed, it will fallback to the axios default (only 2xx status codes are resolved). | | ||
@@ -72,0 +73,0 @@ ## Testing |
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
30122
541
88