2022-12-23 16:36:35 +01:00
|
|
|
import {Injectable, Inject, PLATFORM_ID, Optional} from '@angular/core';
|
|
|
|
import {HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpResponse, HttpHeaders} from '@angular/common/http';
|
|
|
|
import {Observable, of} from 'rxjs';
|
|
|
|
import {tap} from 'rxjs/operators';
|
|
|
|
import {TransferState, makeStateKey, StateKey} from '@angular/platform-browser';
|
|
|
|
import {isPlatformServer} from '@angular/common';
|
2021-11-08 14:01:31 +01:00
|
|
|
import {properties} from "../../environments/environment";
|
2022-12-23 16:36:35 +01:00
|
|
|
import {REQUEST} from "./utils/tokens";
|
|
|
|
import * as Xhr2 from 'xhr2';
|
|
|
|
|
|
|
|
Xhr2.prototype._restrictedHeaders = {};
|
2019-09-30 12:48:26 +02:00
|
|
|
|
2020-11-16 14:36:12 +01:00
|
|
|
@Injectable()
|
2019-09-30 12:48:26 +02:00
|
|
|
export class HttpInterceptorService implements HttpInterceptor {
|
2022-12-23 16:36:35 +01:00
|
|
|
private static HTTP_WHITELIST = [];
|
|
|
|
|
|
|
|
constructor(private transferState: TransferState, @Inject(PLATFORM_ID) private platformId: any, @Optional() @Inject(REQUEST) protected req: any) {
|
|
|
|
}
|
|
|
|
|
2019-09-30 12:48:26 +02:00
|
|
|
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
2021-11-08 14:01:31 +01:00
|
|
|
if (request.method !== 'GET' || this.isService(request, HttpInterceptorService.HTTP_WHITELIST)) {
|
2019-09-30 12:48:26 +02:00
|
|
|
return next.handle(request);
|
|
|
|
}
|
2022-12-23 16:36:35 +01:00
|
|
|
|
2019-09-30 12:48:26 +02:00
|
|
|
const key: StateKey<string> = makeStateKey<string>(request.url);
|
2022-12-23 16:36:35 +01:00
|
|
|
|
2019-09-30 12:48:26 +02:00
|
|
|
const storedResponse = this.transferState.get<any>(key, null);
|
|
|
|
//console.info(key, storedResponse);
|
|
|
|
if (storedResponse) {
|
|
|
|
const response = new HttpResponse({body: storedResponse, status: 200});
|
|
|
|
if (!isPlatformServer(this.platformId)) {
|
|
|
|
//console.info("browser remove: ", key);
|
|
|
|
this.transferState.remove(key);
|
|
|
|
}
|
|
|
|
return of(response);
|
|
|
|
} else {
|
|
|
|
if (isPlatformServer(this.platformId)) {
|
2023-05-27 21:48:13 +02:00
|
|
|
if(request.url.startsWith('/assets') || request.url.startsWith('assets')) {
|
|
|
|
return of(null);
|
|
|
|
}
|
2022-12-23 16:36:35 +01:00
|
|
|
let headers = new HttpHeaders();
|
2023-05-25 15:37:12 +02:00
|
|
|
if(request.withCredentials && !properties.hasMachineCache) {
|
2022-12-23 16:56:47 +01:00
|
|
|
headers = headers.set('Cookie', this.req.get('Cookie'));
|
|
|
|
}
|
2022-12-23 16:36:35 +01:00
|
|
|
const authReq = request.clone({
|
|
|
|
headers: headers
|
|
|
|
});
|
|
|
|
return next.handle(authReq).pipe(tap((event) => {
|
2019-09-30 12:48:26 +02:00
|
|
|
//console.info("server add: ", key);
|
|
|
|
this.transferState.set(key, (<HttpResponse<any>>event).body);
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
return next.handle(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-23 16:36:35 +01:00
|
|
|
|
|
|
|
isService(req: HttpRequest<any>, service: string | string[]): boolean {
|
|
|
|
if (Array.isArray(service)) {
|
2021-11-08 14:01:31 +01:00
|
|
|
return !!service.find(element => req.url.indexOf(element) !== -1);
|
|
|
|
} else {
|
|
|
|
return req.url.indexOf(service) !== -1;
|
|
|
|
}
|
|
|
|
}
|
2022-12-23 16:36:35 +01:00
|
|
|
|
2021-11-08 14:01:31 +01:00
|
|
|
// public cleanRequest(requestUrl: string) {
|
|
|
|
// console.log("cleaned");
|
|
|
|
// const key: StateKey<string> = makeStateKey<string>(requestUrl);
|
|
|
|
// this.transferState.remove(key);
|
|
|
|
// }
|
2020-11-16 14:36:12 +01:00
|
|
|
}
|