The request/response interceptor chains that let the workbench act on HTTP traffic before requests are sent or after responses are received.
The system can intercept HTTP requests and responses done by the HttpService. This allows us to perform certain actions upon requests, before they are sent, or after a response has been received.
There are two chains of interceptors. A request interceptor chain and a response interceptor chain. Once a request is triggered it will first be processed by the request interceptor chain: InterceptorService#preProcess The finished request object will then be used to form the actual HTTP Request to the backend in the HttpService The resulting response will be processed by the response interceptor chain, before being finally returned to the caller.
Currently, there is no explicit mechanism for cancelling a request/response, but throwing an error, or rejecting the promise will stop either chain.
An example can be made with the AuthRequestInterceptor, which is used to add auth headers to each request, before it is sent to the backend
Interceptors are executed based on their priority. Each interceptor has a priority property, which defaults to 0.
Higher priority indicates execution priority as well. The highest number will be executed first, lower numbers will be next.
HttpRequest in the generic type, whereas response interceptors should provide ResponseExample request interceptor:
import {HttpInterceptor} from './http-interceptor';
import {HttpRequest} from './http-request';
class MyRequestInterceptor extends HttpInterceptor<HttpRequest> {
shouldProcess(request: HttpRequest) {
// implementation
}
process(request: HttpRequest) {
// implementation
}
}
Example response interceptor:
import {HttpInterceptor} from './http-interceptor';
class MyResponseInterceptor extends HttpInterceptor<Response> {
shouldProcess(response: Response) {
// implementation
}
process(response: Response) {
// implementation
}
}
/**
registerRequestInterceptors and/or registerResponseInterceptors in
interceptor.service.tsExample:
import {ServiceProvider} from './service.provider';
const interceptorService = ServiceProvider.get(InterceptorService);
interceptorService.registerRequestInterceptors(new HttpInterceptorList([new MyRequestInterceptor()]));
interceptorService.registerResponseInterceptors(new HttpInterceptorList([new MyResponseInterceptor()]));
See also: Developers Guide