diff --git a/README.md b/README.md index 173a4925f9..8658935b04 100755 --- a/README.md +++ b/README.md @@ -638,6 +638,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..c3e10ef8e9 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 = []; + } +}; + /** * Iterate over all the registered interceptors * diff --git a/test/specs/interceptors.spec.js b/test/specs/interceptors.spec.js index 36b12b761f..ecd1540ab4 100644 --- a/test/specs/interceptors.spec.js +++ b/test/specs/interceptors.spec.js @@ -571,4 +571,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.length).toBe(0); + }); + + 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.length).toBe(0); + }); });