diff --git a/src/abpHttpInterceptor.ts b/src/abpHttpInterceptor.ts index 1551a62..b1315e1 100644 --- a/src/abpHttpInterceptor.ts +++ b/src/abpHttpInterceptor.ts @@ -5,8 +5,9 @@ import { LogService } from './log/log.service'; import { TokenService } from './auth/token.service'; import { UtilsService } from './utils/utils.service'; import { HttpClient, HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse, HttpErrorResponse, HttpHeaders } from '@angular/common/http'; -import { switchMap, filter, take, catchError } from 'rxjs/operators'; +import { switchMap, filter, take, catchError, tap, map } from 'rxjs/operators'; import { throwError } from 'rxjs/internal/observable/throwError'; +declare const abp: any; export interface IValidationErrorInfo { @@ -232,35 +233,33 @@ export class AbpHttpInterceptor implements HttpInterceptor { } intercept(request: HttpRequest, next: HttpHandler): Observable> { - - var interceptObservable = new Subject>(); var modifiedRequest = this.normalizeRequestHeaders(request); - next.handle(modifiedRequest) + return next.handle(modifiedRequest) .pipe( catchError(error => { + debugger; if (error instanceof HttpErrorResponse && error.status === 401) { return this.tryAuthWithRefreshToken(request, next, error); } else { - return throwError(error); + return this.handleErrorResponse(error); } + }), + switchMap((event) => { + debugger; + return this.handleSuccessResponse(event); }) - ).subscribe( - (event: HttpEvent) => { - this.handleSuccessResponse(event, interceptObservable); - }, - (error: any) => { - return this.handleErrorResponse(error, interceptObservable); - } ); - - return interceptObservable; } protected tryGetRefreshTokenService(): Observable { - var _refreshTokenService = this._injector.get(RefreshTokenService, null); + try { + var _refreshTokenService = this._injector.get(RefreshTokenService); - if (_refreshTokenService) { - return _refreshTokenService.tryAuthWithRefreshToken(); + if (_refreshTokenService) { + return _refreshTokenService.tryAuthWithRefreshToken(); + } + } catch (error) { + return of(false); } return of(false); } @@ -363,67 +362,55 @@ export class AbpHttpInterceptor implements HttpInterceptor { return headers; } - protected handleSuccessResponse(event: HttpEvent, interceptObservable: Subject>): void { + protected handleSuccessResponse(event: HttpEvent): Observable> { var self = this; if (event instanceof HttpResponse) { if (event.body instanceof Blob && event.body.type && event.body.type.indexOf("application/json") >= 0) { - var clonedResponse = event.clone(); + return self.configuration.blobToText(event.body).pipe( + map( + json => { + const responseBody = json == "null" ? {} : JSON.parse(json); - self.configuration.blobToText(event.body).subscribe(json => { - const responseBody = json == "null" ? {} : JSON.parse(json); + var modifiedResponse = self.configuration.handleResponse(event.clone({ + body: responseBody + })); - var modifiedResponse = self.configuration.handleResponse(event.clone({ - body: responseBody - })); - - interceptObservable.next(modifiedResponse.clone({ - body: new Blob([JSON.stringify(modifiedResponse.body)], { type: 'application/json' }) - })); - - interceptObservable.complete(); - }); - } else { - interceptObservable.next(event); - interceptObservable.complete(); + return modifiedResponse.clone({ + body: new Blob([JSON.stringify(modifiedResponse.body)], { type: 'application/json' }) + }); + }) + ); } - } else { - interceptObservable.next(event); } + return of(event); } - protected handleErrorResponse(error: any, interceptObservable: Subject>): Observable { - var errorObservable = new Subject(); - + protected handleErrorResponse(error: any): Observable { if (!(error.error instanceof Blob)) { - interceptObservable.error(error); - interceptObservable.complete(); - return of({}); + return throwError(error); } - this.configuration.blobToText(error.error).subscribe(json => { - const errorBody = (json == "" || json == "null") ? {} : JSON.parse(json); - const errorResponse = new HttpResponse({ - headers: error.headers, - status: error.status, - body: errorBody - }); + return this.configuration.blobToText(error.error).pipe( + switchMap((json) => { + const errorBody = (json == "" || json == "null") ? {} : JSON.parse(json); + const errorResponse = new HttpResponse({ + headers: error.headers, + status: error.status, + body: errorBody + }); - var ajaxResponse = this.configuration.getAbpAjaxResponseOrNull(errorResponse); + var ajaxResponse = this.configuration.getAbpAjaxResponseOrNull(errorResponse); - if (ajaxResponse != null) { - this.configuration.handleAbpResponse(errorResponse, ajaxResponse); - } else { - this.configuration.handleNonAbpErrorResponse(errorResponse); - } - - errorObservable.complete(); + if (ajaxResponse != null) { + this.configuration.handleAbpResponse(errorResponse, ajaxResponse); + } else { + this.configuration.handleNonAbpErrorResponse(errorResponse); + } - interceptObservable.error(error); - interceptObservable.complete(); - }); - - return errorObservable; + return throwError(error); + }) + ); } private itemExists(items: T[], predicate: (item: T) => boolean): boolean {