Interceptor xử lý cross-cutting concern cho request/response: attach auth token, refresh token, correlation id, logging, retry, error normalization hoặc loading indicator.
Ví dụ functional interceptor:
typescript
export const authInterceptor: HttpInterceptorFn = (req, next) => {
const token = inject(AuthService).token()
const authReq = req.clone({ setHeaders: { Authorization: `Bearer ${token}` } })
return next(authReq)
}
provideHttpClient(withInterceptors([authInterceptor]))Nên giữ interceptor mỏng; business transform để trong API service.