Skip to content

Commit

Permalink
feat(interceptors): provide Request to response interceptors
Browse files Browse the repository at this point in the history
closes #33
  • Loading branch information
timbell committed Jan 23, 2016
1 parent 50eb3aa commit 2d24bea
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
13 changes: 7 additions & 6 deletions src/http-client.js
Expand Up @@ -105,12 +105,13 @@ export class HttpClient {
if (Response.prototype.isPrototypeOf(result)) {
response = result;
} else if (Request.prototype.isPrototypeOf(result)) {
request = Promise.resolve(result);
response = fetch(result);
} else {
throw new Error(`An invalid result was returned by the interceptor chain. Expected a Request or Response instance, but got [${result}]`);
}

return processResponse(response, this.interceptors);
return request.then(_request => processResponse(response, this.interceptors, _request));
});

return this::trackRequestEndWith(promise);
Expand Down Expand Up @@ -191,18 +192,18 @@ function processRequest(request, interceptors) {
return applyInterceptors(request, interceptors, 'request', 'requestError');
}

function processResponse(response, interceptors) {
return applyInterceptors(response, interceptors, 'response', 'responseError');
function processResponse(response, interceptors, request) {
return applyInterceptors(response, interceptors, 'response', 'responseError', request);
}

function applyInterceptors(input, interceptors, successName, errorName) {
function applyInterceptors(input, interceptors, successName, errorName, ...interceptorArgs) {
return (interceptors || [])
.reduce((chain, interceptor) => {
let successHandler = interceptor[successName];
let errorHandler = interceptor[errorName];

return chain.then(
successHandler && interceptor::successHandler,
errorHandler && interceptor::errorHandler);
successHandler && (value => interceptor::successHandler(value, ...interceptorArgs)),
errorHandler && (reason => interceptor::errorHandler(reason, ...interceptorArgs)));
}, Promise.resolve(input));
}
4 changes: 2 additions & 2 deletions src/interfaces.js
Expand Up @@ -29,7 +29,7 @@ interface Interceptor {
*
* @param response - The response.
*/
response?: (response: Response) => Response|Promise<Response>;
response?: (response: Response, request?: Request) => Response|Promise<Response>;

/**
* Handles fetch errors and errors generated by previous interceptors. This
Expand All @@ -39,7 +39,7 @@ interface Interceptor {
* @param error - The rejection value from the fetch request or from a
* previous interceptor.
*/
responseError?: (error: any) => Response|Promise<Response>;
responseError?: (error: any, request?: Request) => Response|Promise<Response>;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/http-client.spec.js
Expand Up @@ -172,7 +172,7 @@ describe('HttpClient', () => {

client.fetch('path')
.then(() => {
expect(interceptor.response).toHaveBeenCalledWith(jasmine.any(Response));
expect(interceptor.response).toHaveBeenCalledWith(jasmine.any(Response), jasmine.any(Request));
expect(interceptor.responseError).not.toHaveBeenCalled();
done();
});
Expand All @@ -189,7 +189,7 @@ describe('HttpClient', () => {
client.fetch('path')
.catch(() => {
expect(interceptor.response).not.toHaveBeenCalled();
expect(interceptor.responseError).toHaveBeenCalledWith(jasmine.any(Response));
expect(interceptor.responseError).toHaveBeenCalledWith(jasmine.any(Response), jasmine.any(Request));
done();
});
});
Expand Down

0 comments on commit 2d24bea

Please sign in to comment.