Sub topic 13
Class-based interceptors and migrating from HTTP_INTERCEPTORS
This chapter assumes you can write a functional interceptor with HttpInterceptorFn, register it with withInterceptors, and use inject(). Older Angular code uses a different style, so you will often need to read it, keep it running, or convert it.
The class-based style
Before Angular 15, an interceptor was a class that implemented the HttpInterceptor interface. It received the request and an HttpHandler, and it called next.handle(req) to pass the request on.
import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { AuthService } from './auth.service';
@Injectable()
export class LegacyAuthInterceptor implements HttpInterceptor {
constructor(private auth: AuthService) {}
intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
const token = this.auth.getToken();
const authReq = token
? req.clone({ setHeaders: { Authorization: `Bearer ${token}` } })
: req;
return next.handle(authReq);
}
}The class gets its dependencies through its constructor. Because it is an ordinary injectable class, it needs no inject() call.
Registering with HTTP_INTERCEPTORS
Class-based interceptors are registered with the HTTP_INTERCEPTORS multi-provider. In a modern provideHttpClient setup, you must also add withInterceptorsFromDi(). Without it, Angular ignores the providers.
import { bootstrapApplication } from '@angular/platform-browser';
import { HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { AppComponent } from './app/app.component';
import { LegacyAuthInterceptor } from './app/legacy-auth.interceptor';
bootstrapApplication(AppComponent, {
providers: [
provideHttpClient(withInterceptorsFromDi()),
{ provide: HTTP_INTERCEPTORS, useClass: LegacyAuthInterceptor, multi: true },
],
});The multi: true flag is required. It makes Angular collect every provider for the token into one array instead of letting the last one replace the others. Request order follows the order of the providers.
Migrating to a functional interceptor
Migrating a class takes three steps:
- Replace the class with a function of type
HttpInterceptorFn. - Replace constructor injection with
inject()at the top of the function. - Replace
next.handle(req)withnext(req), and register the function withwithInterceptors.
| Class-based | Functional |
|---|---|
| `implements HttpInterceptor` | `HttpInterceptorFn` |
| Constructor injection | `inject()` inside the function |
| `next.handle(req)` | `next(req)` |
| `HTTP_INTERCEPTORS` + `withInterceptorsFromDi()` | `withInterceptors([...])` |
The migrated version of the example above:
import { HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { AuthService } from './auth.service';
export const authInterceptor: HttpInterceptorFn = (req, next) => {
const token = inject(AuthService).getToken();
return next(token ? req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }) : req);
};Mixing both styles during a migration
You can pass both features to provideHttpClient while you convert interceptors one at a time. The position of each feature in the call determines where its interceptors sit in the chain.
provideHttpClient(
withInterceptors([authInterceptor]),
withInterceptorsFromDi(),
)Once the last class is converted, remove withInterceptorsFromDi() and the HTTP_INTERCEPTORS providers. Leftover providers do nothing without that feature, which makes them easy to miss.
Exercise
A project registers { provide: HTTP_INTERCEPTORS, useClass: LogInterceptor, multi: true } and calls provideHttpClient() with no arguments. The interceptor never runs. What is the fix, and what would you do to migrate it?
Show answer
Add withInterceptorsFromDi() to provideHttpClient, because DI-registered interceptors are ignored without it. To migrate, rewrite LogInterceptor as an HttpInterceptorFn that calls next(req), use inject() for any dependencies, register it with withInterceptors([logInterceptor]), and delete the HTTP_INTERCEPTORS provider.