Skip to content

Commit

Permalink
fix: support before and after fullfilled, rejected
Browse files Browse the repository at this point in the history
  • Loading branch information
yutak23 committed Dec 22, 2023
1 parent e06b2db commit 8a4c4c1
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 26 deletions.
140 changes: 116 additions & 24 deletions spec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,17 +581,40 @@ describe('axiosRetry(axios, { disableOtherResponseInterceptors })', () => {
nock('http://example.com').get('/test').times(2).replyWithError(NETWORK_ERROR);
nock('http://example.com').get('/test').reply(200, 'It worked!');

let anotherInterceptorBeforeFulfilledCallCount = 0;
let anotherInterceptorBeforeRejectedCallCount = 0;
client.interceptors.response.use(
(result) => {
anotherInterceptorBeforeFulfilledCallCount += 1;
return result;
},
(error) => {
anotherInterceptorBeforeRejectedCallCount += 1;
return Promise.reject(error);
}
);

axiosRetry(client, { retries: 3, disableOtherResponseInterceptors: true });

let anotherInterceptorCallCount = 0;
client.interceptors.response.use((result) => {
anotherInterceptorCallCount += 1;
return result;
}, null);
let anotherInterceptorAfterFulfilledCallCount = 0;
let anotherInterceptorAfterRejectedCallCount = 0;
client.interceptors.response.use(
(result) => {
anotherInterceptorAfterFulfilledCallCount += 1;
return result;
},
(error) => {
anotherInterceptorAfterRejectedCallCount += 1;
return Promise.reject(error);
}
);

client.get('http://example.com/test').then((result) => {
expect(result.status).toBe(200);
expect(anotherInterceptorCallCount).toBe(1);
expect(anotherInterceptorBeforeFulfilledCallCount).toBe(1);
expect(anotherInterceptorBeforeRejectedCallCount).toBe(1);
expect(anotherInterceptorAfterFulfilledCallCount).toBe(1);
expect(anotherInterceptorAfterRejectedCallCount).toBe(0);
done();
}, done.fail);
});
Expand All @@ -601,17 +624,40 @@ describe('axiosRetry(axios, { disableOtherResponseInterceptors })', () => {
nock('http://example.com').get('/test').times(2).replyWithError(NETWORK_ERROR);
nock('http://example.com').get('/test').reply(200, 'It worked!');

let anotherInterceptorBeforeFulfilledCallCount = 0;
let anotherInterceptorBeforeRejectedCallCount = 0;
client.interceptors.response.use(
(result) => {
anotherInterceptorBeforeFulfilledCallCount += 1;
return result;
},
(error) => {
anotherInterceptorBeforeRejectedCallCount += 1;
return Promise.reject(error);
}
);

axiosRetry(client, { retries: 3, disableOtherResponseInterceptors: false });

let anotherInterceptorCallCount = 0;
client.interceptors.response.use((result) => {
anotherInterceptorCallCount += 1;
return result;
}, null);
let anotherInterceptorAfterFulfilledCallCount = 0;
let anotherInterceptorAfterRejectedCallCount = 0;
client.interceptors.response.use(
(result) => {
anotherInterceptorAfterFulfilledCallCount += 1;
return result;
},
(error) => {
anotherInterceptorAfterRejectedCallCount += 1;
return Promise.reject(error);
}
);

client.get('http://example.com/test').then((result) => {
expect(result.status).toBe(200);
expect(anotherInterceptorCallCount).toBe(3);
expect(anotherInterceptorBeforeFulfilledCallCount).toBe(1);
expect(anotherInterceptorBeforeRejectedCallCount).toBe(2);
expect(anotherInterceptorAfterFulfilledCallCount).toBe(3);
expect(anotherInterceptorAfterRejectedCallCount).toBe(0);
done();
}, done.fail);
});
Expand All @@ -622,20 +668,43 @@ describe('axiosRetry(axios, { disableOtherResponseInterceptors })', () => {
const client = axios.create();
nock('http://example.com').get('/test').times(3).replyWithError(NETWORK_ERROR);

let anotherInterceptorBeforeFulfilledCallCount = 0;
let anotherInterceptorBeforeRejectedCallCount = 0;
client.interceptors.response.use(
(result) => {
anotherInterceptorBeforeFulfilledCallCount += 1;
return result;
},
(error) => {
anotherInterceptorBeforeRejectedCallCount += 1;
return Promise.reject(error);
}
);

axiosRetry(client, { retries: 3, disableOtherResponseInterceptors: true });

let anotherInterceptorCallCount = 0;
client.interceptors.response.use(null, (error) => {
anotherInterceptorCallCount += 1;
return Promise.reject(error);
});
let anotherInterceptorAfterFulfilledCallCount = 0;
let anotherInterceptorAfterRejectedCallCount = 0;
client.interceptors.response.use(
(result) => {
anotherInterceptorAfterFulfilledCallCount += 1;
return result;
},
(error) => {
anotherInterceptorAfterRejectedCallCount += 1;
return Promise.reject(error);
}
);

client
.get('http://example.com/test')
.then(
() => done.fail(),
(error) => {
expect(anotherInterceptorCallCount).toBe(1);
expect(anotherInterceptorBeforeFulfilledCallCount).toBe(0);
expect(anotherInterceptorBeforeRejectedCallCount).toBe(1);
expect(anotherInterceptorAfterFulfilledCallCount).toBe(0);
expect(anotherInterceptorAfterRejectedCallCount).toBe(1);
done();
}
)
Expand All @@ -646,20 +715,43 @@ describe('axiosRetry(axios, { disableOtherResponseInterceptors })', () => {
const client = axios.create();
nock('http://example.com').get('/test').times(3).replyWithError(NETWORK_ERROR);

let anotherInterceptorBeforeFulfilledCallCount = 0;
let anotherInterceptorBeforeRejectedCallCount = 0;
client.interceptors.response.use(
(result) => {
anotherInterceptorBeforeFulfilledCallCount += 1;
return result;
},
(error) => {
anotherInterceptorBeforeRejectedCallCount += 1;
return Promise.reject(error);
}
);

axiosRetry(client, { retries: 3, disableOtherResponseInterceptors: false });

let anotherInterceptorCallCount = 0;
client.interceptors.response.use(null, (error) => {
anotherInterceptorCallCount += 1;
return Promise.reject(error);
});
let anotherInterceptorAfterFulfilledCallCount = 0;
let anotherInterceptorAfterRejectedCallCount = 0;
client.interceptors.response.use(
(result) => {
anotherInterceptorAfterFulfilledCallCount += 1;
return result;
},
(error) => {
anotherInterceptorAfterRejectedCallCount += 1;
return Promise.reject(error);
}
);

client
.get('http://example.com/test')
.then(
() => done.fail(),
(error) => {
expect(anotherInterceptorCallCount).toBe(4);
expect(anotherInterceptorBeforeFulfilledCallCount).toBe(0);
expect(anotherInterceptorBeforeRejectedCallCount).toBe(4);
expect(anotherInterceptorAfterFulfilledCallCount).toBe(0);
expect(anotherInterceptorAfterRejectedCallCount).toBe(4);
done();
}
)
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,14 @@ const axiosRetry: AxiosRetry = (axiosInstance, defaultOptions) => {
if (currentState.disableOtherResponseInterceptors && currentState.retryCount === 1) {
const responseInterceptors = axiosInstance.interceptors
.response as AxiosResponseInterceptorManagerExtended;
const axiosRetryInterceptor = responseInterceptors.handlers[responseInterceptorId];
responseInterceptors.handlers = [axiosRetryInterceptor];
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;
}
Expand Down

0 comments on commit 8a4c4c1

Please sign in to comment.