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

provide http interceptor via a provider function

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

  1. 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
    }
    ];
    }

    Continue reading...

Compartilhe esta Página