Common HTTP Interceptors for Angular Applications. Available as HTTP Inteceptor class or function.
Using npm:
npm i ngx-interceptors
Using yarn:
yarn add ngx-interceptors
- AuthInterceptor
- HeaderInterceptor
- LoggingInterceptor
- RetryInterceptor
- MockInterceptor
- CachingInterceptor
Adding one or multiple interceptors to your application is done by registering them as providers. Either in your module class or for standalone projects in the bootstrap application function.
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: RetryInterceptor, multi: true }
],
bootstrap: [AppComponent]
})
export class AppModule { }
bootstrapApplication(AppComponent, {
providers: [
provideHttpClient(
withInterceptorsFromDi()
),
{ provide: HTTP_INTERCEPTORS, useClass: RetryInterceptor, multi: true }
]
})
.catch((err) => console.error(err));
bootstrapApplication(AppComponent, {
providers: [
provideHttpClient(
withInterceptors([retryInterceptor])
)
]
})
.catch((err) => console.error(err));
Each interceptor comes with a custom configuration. You can overwrite the default configuration and provide your own values.
providers: [
{
provide: RETRY_INTERCEPTOR_CONFIG,
useValue: {
retries: 3,
delay: 1000
}
}
]
If no or incomplete configurations are provided, the default values will be used.