In my library, I export a http interceptor.
Currently to activate the interceptor, the user must use the following code:
import { provideNgProgressHttp, progressInterceptor } from 'ngx-progressbar/http';
bootstrapApplication(AppComponent, {
providers: [
provideHttpClient(
withInterceptors([progressInterceptor])
),
provideNgProgressHttp()
]
});
I would like to avoid the withInterceptors([progressInterceptor])
part, and make it internally when user provide provideNgProgressHttp()
. is this possible?
Expected usage:
import { provideNgProgressHttp } from 'ngx-progressbar/http';
bootstrapApplication(AppComponent, {
providers: [
provideHttpClient(),
provideNgProgressHttp()
]
});
Here is my code:
The interceptor function
export function progressInterceptor(req: HttpRequest<unknown>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> {
return next(req).pipe(
finalize(() => {
// Some code
})
);
}
The provider function
export const NG_PROGRESS_HTTP_CONFIG: InjectionToken<NgProgressHttpConfig> = new InjectionToken<NgProgressHttpConfig>('ngProgressHttpConfig');
export function provideNgProgressHttp(config?: NgProgressHttpConfig): Provider {
return [
{
provide: NG_PROGRESS_HTTP_CONFIG,
useValue: config
}
];
}