In my library, There is an interceptor that the user must provide in order for the component to work.
I want to throw an exception if the user forgets to provide the interceptor, maybe from a singleton service.
How can I achieve that?
export function progressInterceptor(req: HttpRequest<unknown>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> {
return next(req).pipe(
finalize(() => {
// some code
})
);
}
@Injectable({
providedIn: 'root'
})
export class NgProgressHttpManager {
constructor() {
// Some how I need to access the list of provided interceptors!
if (!interceptor instanceof NgProgressInterceptor) {
throw new Error('NgProgressInterceptor is not provided. Please add it to the HTTP client interceptors.');
}
}
}
bootstrapApplication(AppComponent, {
providers: [
provideHttpClient(
withInterceptors([
// in case user forgot to add 'progressInterceptor'
])
)
]
}).catch(err => console.error(err));
1