How use async service into angular httpClient interceptor

前端 未结 7 935
轻奢々
轻奢々 2020-12-09 18:53

Using Angular 4.3.1 and HttpClient, I need to modify the request and response by async service into the HttpInterceptor of httpClient,

Example for modifying the requ

7条回答
  •  佛祖请我去吃肉
    2020-12-09 19:19

    The answers above seem to be fine. I had same requirements but faced issues due to update in different dependencies and operators. Took me some time but I found one working solution to this specific issue.

    If you are using Angular 7 and RxJs version 6+ with requirements for Async Interceptor request then you can use this code which works with latest version of NgRx store and related dependencies:

    intercept(request: HttpRequest, next: HttpHandler): Observable> {
    
        let combRef = combineLatest(this.store.select(App.getAppName));
    
        return combRef.pipe( take(1), switchMap((result) => {
    
            // Perform any updates in the request here
            return next.handle(request).pipe(
                map((event: HttpEvent) => {
                    if (event instanceof HttpResponse) {
                        console.log('event--->>>', event);
                    }
                    return event;
                }),
                catchError((error: HttpErrorResponse) => {
                    let data = {};
                    data = {
                        reason: error && error.error.reason ? error.error.reason : '',
                        status: error.status
                    };
                    return throwError(error);
                }));
        }));
    

提交回复
热议问题