Sub topic 2
What an interceptor does and where it sits
This chapter assumes you know TypeScript, Angular components, services and dependency injection, how to make GET and POST requests with HttpClient, and basic RxJS (pipe, tap, catchError).
The problem interceptors solve
Most apps repeat the same HTTP work in many places. Every request needs an Authorization header, every failure should be logged, and every call might need timing. If you write that code in each service, it gets copied dozens of times and someone forgets it in one of them.
An HTTP interceptor is a function that HttpClient runs for every request that passes through it. It can look at the outgoing request, change it, and look at or change what comes back. You write the shared logic once and register it once.
Where an interceptor sits
An interceptor sits between your code and the HTTP backend. The backend is the Angular layer that actually sends the request to the network. Your component or service calls http.get(...). HttpClient builds a request object and passes it through the chain of interceptors. The last interceptor hands it to the backend, which talks to the server.
The response travels back along the same path in reverse. Each interceptor sees it on the way out of the chain.
The shape of a functional interceptor
In current Angular, an interceptor is a plain function of type HttpInterceptorFn. It receives two arguments:
req: theHttpRequestabout to be sent.next: a function that passes a request to the next step in the chain and returns anObservableof events.
The smallest working interceptor does nothing except forward the request.
import { HttpInterceptorFn } from '@angular/common/http';
export const noopInterceptor: HttpInterceptorFn = (req, next) => {
return next(req);
};The call to next(req) is the important part. Code you put before it runs on the request path. Code you attach to the returned observable, with operators such as tap, runs on the response path.
What an interceptor can do
An interceptor can do four things with a request.
- Forward it unchanged, as the example above does.
- Replace it with a modified copy, for example one with an added header.
- Return a response without calling
next, which can be useful for a cached answer. This is rare and easy to get wrong. - Transform the result by piping the observable that
nextreturns.
Typical uses are:
| Use | Where the logic runs |
|---|---|
| Add an auth token | Request path |
| Log method, URL and duration | Both paths |
| Show a global error message | Response path |
| Add a base URL or default header | Request path |
Limits to keep in mind
Interceptors only see requests made through HttpClient. Code that calls fetch or XMLHttpRequest directly bypasses the chain completely. Some libraries do this internally, so a missing header on a request from third-party code often means the request never reached your interceptor.
Also, an interceptor that never calls next stops the request. The caller's observable will not emit what you expect, and the request is never sent. Later chapters cover request immutability, registration, ordering and these pitfalls in detail.
Exercise
A service calls http.get('/api/items') and two interceptors are registered. Describe the path the request and the response take, and say what happens if the first interceptor never calls next.
Show answer
The request goes from HttpClient through the first interceptor, then the second, then the HTTP backend and the server. The response comes back through the second interceptor, then the first, then to the caller. If the first interceptor never calls next, the request is never sent to the second interceptor or the backend, so no network call is made.