2021-05-19 09:39:08 +02:00
|
|
|
import {Inject, Injectable, InjectionToken, PLATFORM_ID} from '@angular/core';
|
2020-11-02 18:11:31 +01:00
|
|
|
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
|
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
import { timeout } from 'rxjs/operators';
|
2021-05-19 09:39:08 +02:00
|
|
|
import {isPlatformServer} from "@angular/common";
|
2021-06-22 10:04:38 +02:00
|
|
|
import {properties} from "../../environments/environment";
|
2020-11-02 18:11:31 +01:00
|
|
|
|
|
|
|
export const DEFAULT_TIMEOUT = new InjectionToken<number>('defaultTimeout');
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class TimeoutInterceptor implements HttpInterceptor {
|
2021-09-07 09:51:07 +02:00
|
|
|
private static TIMEOUT_WHITELIST = [properties.csvAPIURL, properties.registryUrl];
|
2021-06-22 10:04:38 +02:00
|
|
|
|
2021-05-19 09:39:08 +02:00
|
|
|
constructor(@Inject(DEFAULT_TIMEOUT) protected defaultTimeout: number, @Inject(PLATFORM_ID) private platformId: any) {
|
2020-11-02 18:11:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
2021-06-22 10:04:38 +02:00
|
|
|
if (req.method !== 'GET' || this.isService(req, TimeoutInterceptor.TIMEOUT_WHITELIST)) {
|
2021-05-13 17:48:01 +02:00
|
|
|
return next.handle(req);
|
|
|
|
}
|
|
|
|
|
2022-05-17 11:15:15 +02:00
|
|
|
const timeoutValue = isPlatformServer(this.platformId)?3000:6000;//req.headers.get('timeout') || this.defaultTimeout;
|
2020-11-02 18:11:31 +01:00
|
|
|
const timeoutValueNumeric = Number(timeoutValue);
|
|
|
|
return next.handle(req).pipe(timeout(timeoutValueNumeric));
|
|
|
|
}
|
2021-06-22 10:04:38 +02:00
|
|
|
|
|
|
|
isService(req: HttpRequest<any>, service: string | string[]):boolean {
|
2021-09-07 09:51:07 +02:00
|
|
|
if(Array.isArray(service)) {
|
2021-06-22 10:04:38 +02:00
|
|
|
return !!service.find(element => req.url.indexOf(element) !== -1);
|
|
|
|
} else {
|
|
|
|
return req.url.indexOf(service) !== -1;
|
|
|
|
}
|
|
|
|
}
|
2020-11-02 18:11:31 +01:00
|
|
|
}
|