1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

How to check if an interceptor is provided in withInterceptors fucntion

Discussão em 'Angular' iniciado por Murhaf Sousli, Outubro 16, 2024.

  1. 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));

    Continue reading...

Compartilhe esta Página