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.
An interceptor handles cross-cutting concerns for requests/responses: attaching auth tokens, refreshing tokens, correlation ids, logging, retry, error normalization or loading indicators.
Example 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]))Keep interceptors thin; business transformations belong in API services.