Angular interceptors order with interceptor in imported module

Viewed 630

I undertand anguler interceptors are executed according to their declaration order in a module.

@NgModule({
  imports: [...],
  declarations: [...],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: FirstInterceptor,
      multi: true,
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: SecondInterceptor,
      multi: true,
    }
  ]
})
export class AppModule {
  constructor() {

  }
}

FirstInterceptor will be executed before SecondInterceptor.

But what about the order when an interceptor is in a imported Module ?

@NgModule({
  declarations: [...],
  imports: [...],
  exports: [...],
})
export class ImportedModule{
  static forRoot() {
    return {
      ngModule: ImportedModule,
      providers: [
        ...,
        {
          provide: HTTP_INTERCEPTORS,
          useClass: ThirdInterceptor,
          multi: true,
        }
      ],
    };
  }
}

@NgModule({
  imports: [
    ...,
    ImportedModule.forRoot(),
  ],
  declarations: [...],
  providers: [
  ...
    {
      provide: HTTP_INTERCEPTORS,
      useClass: FirstInterceptor,
      multi: true,
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: SecondInterceptor,
      multi: true,
    }
  ]
})
export class AppModule {
  constructor() {
  }
}

According to my tests, the imported module interceptor (ThirdInterceptor) is executed after the AppModule interceptors.

In my situation, it's not the expected behavior, I want the imported module interceptor to be executed first, is it possible ?

0 Answers
Related