mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Interceptor clear (#4248)
* Adding interceptor clearing * Fixing spacing * changed clear to empty array * fixed interceptor clear test Co-authored-by: Andrew <Andrew@Andrews-MacBook-Pro.local> Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -638,6 +638,15 @@ const myInterceptor = axios.interceptors.request.use(function () {/*...*/});
|
|||||||
axios.interceptors.request.eject(myInterceptor);
|
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.
|
You can add interceptors to a custom instance of axios.
|
||||||
|
|
||||||
```js
|
```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
|
* Iterate over all the registered interceptors
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -571,4 +571,32 @@ describe('interceptors', function () {
|
|||||||
done();
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user