-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add disableOtherResponseInterceptors option #260
base: master
Are you sure you want to change the base?
Changes from 3 commits
75a63ca
7e7c61e
e06b2db
8a4c4c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,23 @@ | ||
import type { AxiosError, AxiosRequestConfig, AxiosInstance, AxiosStatic } from 'axios'; | ||
import type { | ||
AxiosError, | ||
AxiosRequestConfig, | ||
AxiosInstance, | ||
AxiosStatic, | ||
AxiosInterceptorManager, | ||
AxiosResponse, | ||
InternalAxiosRequestConfig | ||
} from 'axios'; | ||
import isRetryAllowed from 'is-retry-allowed'; | ||
|
||
interface AxiosResponseInterceptorManagerExtended extends AxiosInterceptorManager<AxiosResponse> { | ||
handlers: Array<{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a However, if the following PRs are merged, this definition will no longer be necessary. But, it is not clear when the proposal will be adopted, so the |
||
fulfilled: ((value: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>) | null; | ||
rejected: ((error: any) => any) | null; | ||
synchronous: boolean; | ||
runWhen: (config: InternalAxiosRequestConfig) => boolean | null; | ||
}>; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have checked the following JavaScrip implementation on my end and defined the types. |
||
|
||
export interface IAxiosRetryConfig { | ||
/** | ||
* The number of times to retry before failing | ||
|
@@ -12,6 +29,11 @@ export interface IAxiosRetryConfig { | |
* default: false | ||
*/ | ||
shouldResetTimeout?: boolean; | ||
/** | ||
* Disable other response interceptors when axios-retry is retrying the request | ||
* default: false | ||
*/ | ||
disableOtherResponseInterceptors?: boolean; | ||
/** | ||
* A callback to further control if a request should be retried. | ||
* default: it retries if it is a network error or a 5xx error on an idempotent request (GET, HEAD, OPTIONS, PUT or DELETE). | ||
|
@@ -141,6 +163,7 @@ export const DEFAULT_OPTIONS: Required<IAxiosRetryConfig> = { | |
retryCondition: isNetworkOrIdempotentRequestError, | ||
retryDelay: noDelay, | ||
shouldResetTimeout: false, | ||
disableOtherResponseInterceptors: false, | ||
onRetry: () => {} | ||
}; | ||
|
||
|
@@ -196,13 +219,14 @@ async function shouldRetry( | |
return shouldRetryOrPromise; | ||
} | ||
|
||
let responseInterceptorId: number; | ||
const axiosRetry: AxiosRetry = (axiosInstance, defaultOptions) => { | ||
const requestInterceptorId = axiosInstance.interceptors.request.use((config) => { | ||
setCurrentState(config, defaultOptions); | ||
return config; | ||
}); | ||
|
||
const responseInterceptorId = axiosInstance.interceptors.response.use(null, async (error) => { | ||
responseInterceptorId = axiosInstance.interceptors.response.use(null, async (error) => { | ||
const { config } = error; | ||
// If we have no information to retry the request | ||
if (!config) { | ||
|
@@ -227,7 +251,17 @@ const axiosRetry: AxiosRetry = (axiosInstance, defaultOptions) => { | |
config.transformRequest = [(data) => data]; | ||
await onRetry(currentState.retryCount, error, config); | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(axiosInstance(config)), delay); | ||
setTimeout(() => { | ||
if (currentState.disableOtherResponseInterceptors && currentState.retryCount === 1) { | ||
const responseInterceptors = axiosInstance.interceptors | ||
.response as AxiosResponseInterceptorManagerExtended; | ||
const axiosRetryInterceptor = responseInterceptors.handlers[responseInterceptorId]; | ||
responseInterceptors.handlers = [axiosRetryInterceptor]; | ||
resolve(axiosInstance(config)); | ||
return; | ||
} | ||
resolve(axiosInstance(config)); | ||
}, delay); | ||
}); | ||
} | ||
return Promise.reject(error); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assumed that adding this line here wouldn't affect test results, but it did. Am I mistaken? :)