feat(http-client): added possibility to remove specific interceptors
This commit is contained in:
parent
8c818a46ab
commit
0823df33b5
@ -15,6 +15,18 @@ export class HttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
removeResponseInterceptor(interceptor) {
|
||||
//performs a reference check. if the interceptor is bound, when adding it, the reference of the bound function needs to be the same when removing it later.
|
||||
|
||||
if (typeof interceptor !== 'function') {
|
||||
throw new Error(`Cannot remove Interceptor ${interceptor}, because it is not of type function`);
|
||||
}
|
||||
if(this._responseInterceptors.filter(el => el == interceptor).length !== 1) {
|
||||
throw new Error(`Could not find Response Interceptor ${interceptor}.`);
|
||||
}
|
||||
this._responseInterceptors = this._responseInterceptors.filter(el => el != interceptor);
|
||||
}
|
||||
|
||||
_baseUrl;
|
||||
|
||||
setBaseUrl(baseUrl) {
|
||||
|
||||
@ -75,7 +75,7 @@ describe('HttpClient', () => {
|
||||
expect(httpClient._responseInterceptors.length).toBe(2);
|
||||
});
|
||||
|
||||
describe('get called', () => {
|
||||
describe('get called and removed', () => {
|
||||
let intercepted1;
|
||||
let intercepted2;
|
||||
const interceptors = {
|
||||
@ -111,6 +111,14 @@ describe('HttpClient', () => {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can be removed', () => {
|
||||
expect(httpClient._responseInterceptors.length).toBe(2);
|
||||
httpClient.removeResponseInterceptor(interceptors.interceptor1);
|
||||
expect(httpClient._responseInterceptors.length).toBe(1);
|
||||
expect(() => {httpClient.removeResponseInterceptor(interceptors.interceptor1);}).toThrow();
|
||||
expect(httpClient._responseInterceptors.length).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user