-
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 all 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,23 @@ 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 interceptors = responseInterceptors.handlers.splice(0, responseInterceptorId + 1); | ||
|
||
// Disable only intercepter on rejected (do not disable fullfilled) | ||
responseInterceptors.handlers = interceptors.map((v, index) => { | ||
if (index === responseInterceptorId) return v; | ||
return { ...v, rejected: null }; | ||
}); | ||
|
||
resolve(axiosInstance(config)); | ||
return; | ||
} | ||
resolve(axiosInstance(config)); | ||
}, delay); | ||
}); | ||
Comment on lines
+254
to
+270
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. @yutak23 Thanks for your input! I tested your solution under all test cases we have at the moment I created issue #246 – all passed 🎉 Unfortunately, we have received more production cases and updated our patch. Our previous version was not working properly with API client contest requests. Your solution appears to make the same mistake by detaching interceptors from the global client. Here is seq diagram, to represent this simple contest use-case The main change we made was to clone (dirty) the Axios client, so the global detachment of handlers won't affect contest requests through the same client. Our new patch looks like this:
We don't use custom interceptors before axios-retry so far, so your solution is more solid, and still looks better. |
||
} | ||
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? :)