Update abpHttpInterceptor.ts
This commit is contained in:
Родитель
be3f65e0f4
Коммит
ff557b1d37
|
@ -5,8 +5,9 @@ import { LogService } from './log/log.service';
|
||||||
import { TokenService } from './auth/token.service';
|
import { TokenService } from './auth/token.service';
|
||||||
import { UtilsService } from './utils/utils.service';
|
import { UtilsService } from './utils/utils.service';
|
||||||
import { HttpClient, HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
|
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';
|
import { throwError } from 'rxjs/internal/observable/throwError';
|
||||||
|
declare const abp: any;
|
||||||
|
|
||||||
export interface IValidationErrorInfo {
|
export interface IValidationErrorInfo {
|
||||||
|
|
||||||
|
@ -232,35 +233,33 @@ export class AbpHttpInterceptor implements HttpInterceptor {
|
||||||
}
|
}
|
||||||
|
|
||||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||||
|
|
||||||
var interceptObservable = new Subject<HttpEvent<any>>();
|
|
||||||
var modifiedRequest = this.normalizeRequestHeaders(request);
|
var modifiedRequest = this.normalizeRequestHeaders(request);
|
||||||
next.handle(modifiedRequest)
|
return next.handle(modifiedRequest)
|
||||||
.pipe(
|
.pipe(
|
||||||
catchError(error => {
|
catchError(error => {
|
||||||
|
debugger;
|
||||||
if (error instanceof HttpErrorResponse && error.status === 401) {
|
if (error instanceof HttpErrorResponse && error.status === 401) {
|
||||||
return this.tryAuthWithRefreshToken(request, next, error);
|
return this.tryAuthWithRefreshToken(request, next, error);
|
||||||
} else {
|
} else {
|
||||||
return throwError(error);
|
return this.handleErrorResponse(error);
|
||||||
}
|
}
|
||||||
|
}),
|
||||||
|
switchMap((event) => {
|
||||||
|
debugger;
|
||||||
|
return this.handleSuccessResponse(event);
|
||||||
})
|
})
|
||||||
).subscribe(
|
|
||||||
(event: HttpEvent<any>) => {
|
|
||||||
this.handleSuccessResponse(event, interceptObservable);
|
|
||||||
},
|
|
||||||
(error: any) => {
|
|
||||||
return this.handleErrorResponse(error, interceptObservable);
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return interceptObservable;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected tryGetRefreshTokenService(): Observable<boolean> {
|
protected tryGetRefreshTokenService(): Observable<boolean> {
|
||||||
var _refreshTokenService = this._injector.get(RefreshTokenService, null);
|
try {
|
||||||
|
var _refreshTokenService = this._injector.get(RefreshTokenService);
|
||||||
|
|
||||||
if (_refreshTokenService) {
|
if (_refreshTokenService) {
|
||||||
return _refreshTokenService.tryAuthWithRefreshToken();
|
return _refreshTokenService.tryAuthWithRefreshToken();
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return of(false);
|
||||||
}
|
}
|
||||||
return of(false);
|
return of(false);
|
||||||
}
|
}
|
||||||
|
@ -363,67 +362,55 @@ export class AbpHttpInterceptor implements HttpInterceptor {
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected handleSuccessResponse(event: HttpEvent<any>, interceptObservable: Subject<HttpEvent<any>>): void {
|
protected handleSuccessResponse(event: HttpEvent<any>): Observable<HttpEvent<any>> {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
if (event instanceof HttpResponse) {
|
if (event instanceof HttpResponse) {
|
||||||
if (event.body instanceof Blob && event.body.type && event.body.type.indexOf("application/json") >= 0) {
|
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 => {
|
var modifiedResponse = self.configuration.handleResponse(event.clone({
|
||||||
const responseBody = json == "null" ? {} : JSON.parse(json);
|
body: responseBody
|
||||||
|
}));
|
||||||
|
|
||||||
var modifiedResponse = self.configuration.handleResponse(event.clone({
|
return modifiedResponse.clone({
|
||||||
body: responseBody
|
body: new Blob([JSON.stringify(modifiedResponse.body)], { type: 'application/json' })
|
||||||
}));
|
});
|
||||||
|
})
|
||||||
interceptObservable.next(modifiedResponse.clone({
|
);
|
||||||
body: new Blob([JSON.stringify(modifiedResponse.body)], { type: 'application/json' })
|
|
||||||
}));
|
|
||||||
|
|
||||||
interceptObservable.complete();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
interceptObservable.next(event);
|
|
||||||
interceptObservable.complete();
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
interceptObservable.next(event);
|
|
||||||
}
|
}
|
||||||
|
return of(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected handleErrorResponse(error: any, interceptObservable: Subject<HttpEvent<any>>): Observable<any> {
|
protected handleErrorResponse(error: any): Observable<never> {
|
||||||
var errorObservable = new Subject<any>();
|
|
||||||
|
|
||||||
if (!(error.error instanceof Blob)) {
|
if (!(error.error instanceof Blob)) {
|
||||||
interceptObservable.error(error);
|
return throwError(error);
|
||||||
interceptObservable.complete();
|
|
||||||
return of({});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.configuration.blobToText(error.error).subscribe(json => {
|
return this.configuration.blobToText(error.error).pipe(
|
||||||
const errorBody = (json == "" || json == "null") ? {} : JSON.parse(json);
|
switchMap((json) => {
|
||||||
const errorResponse = new HttpResponse({
|
const errorBody = (json == "" || json == "null") ? {} : JSON.parse(json);
|
||||||
headers: error.headers,
|
const errorResponse = new HttpResponse({
|
||||||
status: error.status,
|
headers: error.headers,
|
||||||
body: errorBody
|
status: error.status,
|
||||||
});
|
body: errorBody
|
||||||
|
});
|
||||||
|
|
||||||
var ajaxResponse = this.configuration.getAbpAjaxResponseOrNull(errorResponse);
|
var ajaxResponse = this.configuration.getAbpAjaxResponseOrNull(errorResponse);
|
||||||
|
|
||||||
if (ajaxResponse != null) {
|
if (ajaxResponse != null) {
|
||||||
this.configuration.handleAbpResponse(errorResponse, ajaxResponse);
|
this.configuration.handleAbpResponse(errorResponse, ajaxResponse);
|
||||||
} else {
|
} else {
|
||||||
this.configuration.handleNonAbpErrorResponse(errorResponse);
|
this.configuration.handleNonAbpErrorResponse(errorResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
errorObservable.complete();
|
|
||||||
|
|
||||||
interceptObservable.error(error);
|
return throwError(error);
|
||||||
interceptObservable.complete();
|
})
|
||||||
});
|
);
|
||||||
|
|
||||||
return errorObservable;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private itemExists<T>(items: T[], predicate: (item: T) => boolean): boolean {
|
private itemExists<T>(items: T[], predicate: (item: T) => boolean): boolean {
|
||||||
|
|
Загрузка…
Ссылка в новой задаче