-
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?
feat: add disableOtherResponseInterceptors option #260
Conversation
import isRetryAllowed from 'is-retry-allowed'; | ||
|
||
interface AxiosResponseInterceptorManagerExtended extends AxiosInterceptorManager<AxiosResponse> { |
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.
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 comment
The 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.
https://github.com/axios/axios/blob/v1.6.2/lib/core/InterceptorManager.js#L23
@mindhells, I would appreciate your confirmation. |
I'm sorry @yutak23 I've been very busy lately. |
@mindhells , I apologize for rushing you. |
describe('failure after retry', () => { | ||
it('should not multiple response interceptor', (done) => { | ||
const client = axios.create(); | ||
nock('http://example.com').get('/test').times(3).replyWithError(NETWORK_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.
nock('http://example.com').get('/test').times(3).replyWithError(NETWORK_ERROR); | |
nock('http://example.com').get('/test').times(3).replyWithError(NETWORK_ERROR); | |
nock('http://example.com').get('/test').reply(200, 'It worked!'); |
I assumed that adding this line here wouldn't affect test results, but it did. Am I mistaken? :)
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); |
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.
@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:
diff --git a/node_modules/axios-retry/lib/cjs/index.js b/node_modules/axios-retry/lib/cjs/index.js
index 1cfaeed..3dbc698 100644
--- a/node_modules/axios-retry/lib/cjs/index.js
+++ b/node_modules/axios-retry/lib/cjs/index.js
@@ -335,10 +335,17 @@ function axiosRetry(axios, defaultOptions) {
config.transformRequest = [function (data) {
return data;
}];
+
onRetry(currentState.retryCount, error, config);
return _context.abrupt("return", new Promise(function (resolve) {
return setTimeout(function () {
- return resolve(axios(config));
+ var newClient = axios.create();
+
+ var retryInterceptors = axios.interceptors.response.handlers[responseInterceptorId];
+
+ newClient.interceptors.response = [retryInterceptors];
+
+ return resolve(newClient(config));
}, delay);
}));
We don't use custom interceptors before axios-retry so far, so your solution is more solid, and still looks better.
But could you confirm that your solution overcome this issue by providing new test cases?
@mindhells I apologize for creating any confusion, maybe my previous explanations in the issue were too abstract. I still think that TikTok Marketing API returns 200 mainly for all responses, wrapping everything in this structure
I want to achieve 2 things and still use
I expect that If |
Hi @lavagri, On the other hand, my concerns with having this implementation are:
But, given the feedback I'm seeing in #246, let's do the following: if axios/axios#6138 gets merged and @yutak23 confirms the use cases you are mentioning are covered, we'll merge this. |
This ought to fix #246.
As commented in the following issue, axios-retry calls a new axios in the response interceptor, so if it succeeds after a retry, for example, the following
console.log
will be executed many times.#246 (comment)
The proposal is to set the
disableOtherResponseInterceptors
option totrue
so that the only response interceptor for axios during the retry process is the one set in axios-retry, so if the retry is successful, the response interceptor set in axios for the first request is executed only once.