diff --git a/package.json b/package.json index 9c73953..4a861a1 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "semantic-release": "^23.0.0", "sinon": "^17.0.0", "typescript": "~5.3.0", - "xo": "^0.56.0" + "xo": "^0.58.0" }, "files": [ "build/src" diff --git a/src/index.ts b/src/index.ts index a42662f..d91ec1d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -89,7 +89,7 @@ export type RaxConfig = { * @returns The id of the interceptor attached to the axios instance. */ export function attach(instance?: AxiosInstance) { - instance = instance || axios; + instance ||= axios; return instance.interceptors.response.use( onFulfilled, async (error: AxiosError) => onError(instance!, error), @@ -102,7 +102,7 @@ export function attach(instance?: AxiosInstance) { * @param instance The axios instance using this interceptor. */ export function detach(interceptorId: number, instance?: AxiosInstance) { - instance = instance || axios; + instance ||= axios; instance.interceptors.response.eject(interceptorId); } @@ -175,12 +175,12 @@ async function onError(instance: AxiosInstance, error: AxiosError) { } const config = getConfig(error) || {}; - config.currentRetryAttempt = config.currentRetryAttempt || 0; + config.currentRetryAttempt ||= 0; config.retry = typeof config.retry === 'number' ? config.retry : 3; config.retryDelay = typeof config.retryDelay === 'number' ? config.retryDelay : 100; - config.instance = config.instance || instance; - config.backoffType = config.backoffType || 'exponential'; + config.instance ||= instance; + config.backoffType ||= 'exponential'; config.httpMethodsToRetry = normalizeArray(config.httpMethodsToRetry) || [ 'GET', 'HEAD', @@ -221,8 +221,8 @@ async function onError(instance: AxiosInstance, error: AxiosError) { (axiosError.config as RaxConfig).raxConfig = {...config}; // Determine if we should retry the request - const shouldRetryFn = config.shouldRetry || shouldRetryRequest; - if (!shouldRetryFn(axiosError)) { + const shouldRetryFunction = config.shouldRetry || shouldRetryRequest; + if (!shouldRetryFunction(axiosError)) { throw axiosError; } @@ -345,7 +345,7 @@ export function shouldRetryRequest(error: AxiosError) { } // If we are out of retry attempts, return - config.currentRetryAttempt = config.currentRetryAttempt || 0; + config.currentRetryAttempt ||= 0; if (config.currentRetryAttempt >= config.retry!) { return false; } diff --git a/test/index.ts b/test/index.ts index f9b32f3..8467fe5 100644 --- a/test/index.ts +++ b/test/index.ts @@ -324,8 +324,8 @@ describe('retry-axios', () => { ax.defaults.raxConfig = { retry: 3, instance: ax, - onRetryAttempt(evt) { - console.log(`attempt #${evt.config!.raxConfig?.currentRetryAttempt}`); + onRetryAttempt(event) { + console.log(`attempt #${event.config!.raxConfig?.currentRetryAttempt}`); }, }; interceptorId = rax.attach(ax); @@ -534,15 +534,15 @@ describe('retry-axios', () => { interceptorId = rax.attach(); try { // eslint-disable-next-line import/no-named-as-default-member - const src = axios.CancelToken.source(); + const source = axios.CancelToken.source(); const cfg: rax.RaxConfig = { url, raxConfig: {retry: 2}, - cancelToken: src.token, + cancelToken: source.token, }; const request = axios(cfg); setTimeout(() => { - src.cancel(); + source.cancel(); }, 10); await request; throw new Error('The canceled request completed.');