-
Notifications
You must be signed in to change notification settings - Fork 168
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
Open
yutak23
wants to merge
4
commits into
softonic:master
Choose a base branch
from
yutak23:feature/add-disableOtherResponseInterceptors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
75a63ca
feat: add disableOtherResponseInterceptors option
yutak23 7e7c61e
test: add testing for patterns of failure after retries
yutak23 e06b2db
fix: runWhen should be nullable
yutak23 8a4c4c1
fix: support before and after fullfilled, rejected
yutak23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat: add disableOtherResponseInterceptors option
- Loading branch information
commit 75a63caee9e05586a194488b4d075b2b2d71722a
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<{ | ||
fulfilled: ((value: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>) | null; | ||
rejected: ((error: any) => any) | null; | ||
synchronous: boolean; | ||
runWhen: (config: InternalAxiosRequestConfig) => boolean; | ||
}>; | ||
} | ||
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 extendedInterceptor = axiosInstance.interceptors | ||
.response as AxiosResponseInterceptorManagerExtended; | ||
const axiosRetryInterceptor = extendedInterceptor.handlers[responseInterceptorId]; | ||
extendedInterceptor.handlers = [axiosRetryInterceptor]; | ||
resolve(axiosInstance(config)); | ||
return; | ||
} | ||
resolve(axiosInstance(config)); | ||
}, delay); | ||
}); | ||
} | ||
return Promise.reject(error); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There is a
handlers
property in the axios request/response interceptor, but it is not present in the current axios type definition. Therefore, we are extending it on our own.However, if the following PRs are merged, this definition will no longer be necessary.
axios/axios#6138
But, it is not clear when the proposal will be adopted, so the
axios-retry
side would be quicker to merge first with this definition.