Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multiple HTTP Interceptors to Angular Application

How to add multiple, independent HTTP interceptors to an Angular 4 application?

I tried to add them by extending the providers array with more than one interceptors. But only the last one is actually executed, Interceptor1 is ignored.

@NgModule({   declarations: [ /* ... */ ],   imports: [ /* ... */ HttpModule ],   providers: [     {       provide: Http,       useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions) =>         new Interceptor1(xhrBackend, requestOptions),       deps: [XHRBackend, RequestOptions],     },     {       provide: Http,       useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions) =>         new Interceptor2(xhrBackend, requestOptions),       deps: [XHRBackend, RequestOptions]     },   ],   bootstrap: [AppComponent] }) export class AppModule {} 

I could obviously combine them into a single Interceptor class and that should work. However, I would like to avoid that as these interceptors have completely different purposes (one for error handling, one to show a loading indicator).

So how can I add multiple interceptors?

like image 862
str Avatar asked Aug 11 '17 10:08

str


People also ask

Can I have multiple HTTP interceptors in angular?

So how can I add multiple interceptors? Http doesn't allow to have more than one custom implementation. But as @estus mentioned the Angular team has added a new HttpClient service recently (release 4.3) which supports multiple interceptors concept. You don't need to extend the HttpClient as you do with the old Http .

Can you register more than one interceptor?

TL;DR Yes, providing multiple interceptors is fine and they run in order.

How do I create an HTTP interceptor in angular 12?

The first step is to build the interceptor. To do this, create an injectable class that implements HttpInterceptor. Any interceptor we want to create needs to implement the HttpInterceptor interface. This means that our new class should have a method called intercept with parameters HttpRequest and HttpHandler.


1 Answers

Http doesn't allow to have more than one custom implementation. But as @estus mentioned the Angular team has added a new HttpClient service recently (release 4.3) which supports multiple interceptors concept. You don't need to extend the HttpClient as you do with the old Http. You can provide an implementation for HTTP_INTERCEPTORS instead which can be an array with the 'multi: true' option:

import {HTTP_INTERCEPTORS, HttpClientModule} from '@angular/common/http'; ...  @NgModule({   ...   imports: [     ... ,     HttpClientModule   ],   providers: [     ... ,     {       provide: HTTP_INTERCEPTORS,       useClass: InterceptorOne,       multi: true,     },     {       provide: HTTP_INTERCEPTORS,       useClass: InterceptorTwo,       multi: true,     }   ],   ... }) 

Interceptors:

import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http'; ...  @Injectable() export class InterceptorOne implements HttpInterceptor {    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {     console.log('InterceptorOne is working');     return next.handle(req);   } }  @Injectable() export class InterceptorTwo implements HttpInterceptor {    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {     console.log('InterceptorTwo is working');     return next.handle(req);   } } 

This server call will print both interceptors' log messages:

import {HttpClient} from '@angular/common/http'; ...  @Component({ ... }) export class SomeComponent implements OnInit {    constructor(private http: HttpClient) {}    ngOnInit(): void {     this.http.get('http://some_url').subscribe();   } } 
like image 71
hiper2d Avatar answered Sep 19 '22 16:09

hiper2d