Skip to content

Commit

Permalink
Fix: Refactor API
Browse files Browse the repository at this point in the history
Refactor eject interceptors to eject all interceptors as to not track the interceptor id.
  • Loading branch information
mickr committed Jul 18, 2019
1 parent d0bbc5e commit f26f015
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/lib/__tests__/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ describe('API helper', () => {
const responseInterceptor = sinon.stub();

api.addRequestInterceptor(requestInterceptor);
api.addRequestInterceptor(requestInterceptor);
api.addResponseInterceptor(responseInterceptor);
api.addResponseInterceptor(responseInterceptor);

api.ejectInterceptors();
Expand Down
17 changes: 7 additions & 10 deletions src/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ const handleError = ({ response }) => {
const transformTextResponse = (data) => data;

class Api {
responseInterceptorId;
requestInterceptorId;

/**
* [constructor]
*
Expand All @@ -79,7 +76,7 @@ class Api {
*/
addResponseInterceptor(responseInterceptor) {
if (typeof responseInterceptor === 'function') {
this.responseInterceptorId = this.client.interceptors.response.use(responseInterceptor);
this.client.interceptors.response.use(responseInterceptor);
}
}

Expand All @@ -92,7 +89,7 @@ class Api {
*/
addRequestInterceptor(requestInterceptor) {
if (typeof requestInterceptor === 'function') {
this.requestInterceptorId = this.client.interceptors.request.use(requestInterceptor);
this.client.interceptors.request.use(requestInterceptor);
}
}

Expand All @@ -103,11 +100,11 @@ class Api {
* @return {void}
*/
ejectInterceptors() {
this.client.interceptors.response.eject(this.responseInterceptorId);
this.client.interceptors.request.eject(this.requestInterceptorId);

this.requestInterceptorId = null;
this.responseInterceptorId = null;
['response', 'request'].forEach((interceptorType) => {
this.client.interceptors[interceptorType].handlers.forEach((interceptor, index) => {
this.client.interceptors[interceptorType].eject(index);
});
});
}

/**
Expand Down

0 comments on commit f26f015

Please sign in to comment.