From 9020f05de2051ec5f9c80674ddb86d3a860c725d Mon Sep 17 00:00:00 2001 From: Andrew Landis Date: Thu, 4 Nov 2021 17:32:23 -0400 Subject: [PATCH 1/4] Adding interceptor clearing --- README.md | 9 +++++++++ lib/core/InterceptorManager.js | 9 +++++++++ test/specs/interceptors.spec.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/README.md b/README.md index 8ce16091de..ac706d73bb 100755 --- a/README.md +++ b/README.md @@ -610,6 +610,15 @@ const myInterceptor = axios.interceptors.request.use(function () {/*...*/}); axios.interceptors.request.eject(myInterceptor); ``` +You can also clear all interceptors for requests or responses. +```js +const instance = axios.create(); +instance.interceptors.request.use(function () {/*...*/}); +instance.interceptors.request.clear(); // Removes interceptors from requests +instance.interceptors.response.use(function () {/*...*/}); +instance.interceptors.response.clear(); // Removes interceptors from responses +``` + You can add interceptors to a custom instance of axios. ```js diff --git a/lib/core/InterceptorManager.js b/lib/core/InterceptorManager.js index 900f44880d..8e5960da44 100644 --- a/lib/core/InterceptorManager.js +++ b/lib/core/InterceptorManager.js @@ -35,6 +35,15 @@ InterceptorManager.prototype.eject = function eject(id) { } }; +/** + * Clear all interceptors from the stack + */ + InterceptorManager.prototype.clear = function clear() { + if (this.handlers) { + this.handlers = null; + } +}; + /** * Iterate over all the registered interceptors * diff --git a/test/specs/interceptors.spec.js b/test/specs/interceptors.spec.js index d8768f911e..786de4d579 100644 --- a/test/specs/interceptors.spec.js +++ b/test/specs/interceptors.spec.js @@ -464,4 +464,32 @@ describe('interceptors', function () { done(); }); }); + + it('should clear all request interceptors', function () { + var instance = axios.create({ + baseURL: 'http://test.com/' + }); + + instance.interceptors.request.use(function (config) { + return config + }); + + instance.interceptors.request.clear(); + + expect(instance.interceptors.request.handlers).toBe(null); + }); + + it('should clear all response interceptors', function () { + var instance = axios.create({ + baseURL: 'http://test.com/' + }); + + instance.interceptors.response.use(function (config) { + return config + }); + + instance.interceptors.response.clear(); + + expect(instance.interceptors.response.handlers).toBe(null); + }); }); From 1ec93e85b3f14b2e50a72b2b69bad72338f9cc77 Mon Sep 17 00:00:00 2001 From: Andrew Landis Date: Thu, 4 Nov 2021 17:34:29 -0400 Subject: [PATCH 2/4] Fixing spacing --- lib/core/InterceptorManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/InterceptorManager.js b/lib/core/InterceptorManager.js index 8e5960da44..3c8c6b375d 100644 --- a/lib/core/InterceptorManager.js +++ b/lib/core/InterceptorManager.js @@ -38,7 +38,7 @@ InterceptorManager.prototype.eject = function eject(id) { /** * Clear all interceptors from the stack */ - InterceptorManager.prototype.clear = function clear() { +InterceptorManager.prototype.clear = function clear() { if (this.handlers) { this.handlers = null; } From d3d34d0a210e978831798cf45fdac70308bf3c23 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 25 Nov 2021 13:32:56 -0500 Subject: [PATCH 3/4] changed clear to empty array --- lib/core/InterceptorManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/InterceptorManager.js b/lib/core/InterceptorManager.js index 3c8c6b375d..c3e10ef8e9 100644 --- a/lib/core/InterceptorManager.js +++ b/lib/core/InterceptorManager.js @@ -40,7 +40,7 @@ InterceptorManager.prototype.eject = function eject(id) { */ InterceptorManager.prototype.clear = function clear() { if (this.handlers) { - this.handlers = null; + this.handlers = []; } }; From b41006d88060f885e362ac182e1d669f28dea339 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 30 Nov 2021 15:32:32 -0500 Subject: [PATCH 4/4] fixed interceptor clear test --- test/specs/interceptors.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/specs/interceptors.spec.js b/test/specs/interceptors.spec.js index 786de4d579..71a5d5a89a 100644 --- a/test/specs/interceptors.spec.js +++ b/test/specs/interceptors.spec.js @@ -476,7 +476,7 @@ describe('interceptors', function () { instance.interceptors.request.clear(); - expect(instance.interceptors.request.handlers).toBe(null); + expect(instance.interceptors.request.handlers.length).toBe(0); }); it('should clear all response interceptors', function () { @@ -490,6 +490,6 @@ describe('interceptors', function () { instance.interceptors.response.clear(); - expect(instance.interceptors.response.handlers).toBe(null); + expect(instance.interceptors.response.handlers.length).toBe(0); }); });