Search code examples
angularngrxangular-http-interceptorsangular13

(Angular13) How can add conditions to include HTTP_INTERCEPTORS based on where the http call is called


I am working with Angular13, ngrx 13.

I am executing some API calls from app.module.ts

providers: [
{
            provide: HTTP_INTERCEPTORS,
            useClass: MyHttpInterceptor,
            multi: true,
        }
{
            provide: APP_INITIALIZER,
            useFactory: (store: Store<IStore>) => {
                return () => store.dispatch(MyAction.getData());
            },
            deps: [Store],
            multi: true
        },

]

MyHttpInterceptor

      @Injectable()
      export class MyHttpInterceptor implements HttpInterceptor {
         constructor() {}
         public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    
    // Here I need add something conditionally.
    // I mean if the Http call is based on App initializer
    // For normal Http API calls from components using ngrx can ignore this condition
    if(isAppInitialize == true) {
    }

    return next.handle(req).pipe(
        finalize(() => {
            
        }),
    );
  }
}

I tired with 2 different Interceptor but still couldn't figure it out how i can use a specific Interceptor for each purpose


Solution

  • You cannot disable interceptors based on the caller, the one way is to filter by an API path.

    Another way is to HttpContext https://dev.to/angular/what-the-heck-is-httpcontext-in-angular-4n3c