RXJS 6: new version of HttpInterceptor

后端 未结 2 1084
鱼传尺愫
鱼传尺愫 2020-12-31 11:50

I am in the process of adding rxjs_compat to my project in order to move to v6 of libraries.

However the existing HttpInterceptor for globa

相关标签:
2条回答
  • 2020-12-31 11:53
    1. import {EMPTY} from 'rxjs';

    2. Replace return Observable.empty() with

      return EMPTY;

    0 讨论(0)
  • 2020-12-31 12:17

    Couldn't figure out how to return something that the compiler accepts, but was at least able to throw an error. The code compiles and works, but as a beginner in Rxjs not sure if it is correct.

      intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        const xhr = req.clone({
          headers: req.headers.set('X-Requested-With', 'XMLHttpRequest')
        });
        return next.handle(xhr).pipe(
          catchError((err) => {
            if (err instanceof HttpErrorResponse) {
              if (err.status === 401) {
                this.router.navigate(['/login']);
              } else {
                this.snack.open('Communication error: ' + err.status + ' - ' + err.statusText, null,
                 {duration: 5000, panelClass: 'snack-error', verticalPosition: 'top'});
              }
              return throwError('backend comm error');
            }
          })
        );
      }
    
    0 讨论(0)
提交回复
热议问题