2021-05-19 09:39:08 +02:00
|
|
|
import {Inject, Injectable, InjectionToken, PLATFORM_ID} from '@angular/core';
|
2023-06-14 14:10:49 +02: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-06-22 10:04:38 +02:00
|
|
|
|
2023-06-14 14:10:49 +02:00
|
|
|
private static TIMEOUT_WHITELIST = [
|
|
|
|
properties.csvAPIURL, properties.registryUrl, properties.claimsAPIURL,
|
|
|
|
properties.searchCrossrefAPIURL, properties.searchDataciteAPIURL,
|
2024-02-23 13:20:19 +01:00
|
|
|
properties.statisticsAPIURL, properties.monitorStatsFrameUrl];
|
2020-11-02 18:11:31 +01:00
|
|
|
|
2023-06-14 14:10:49 +02:00
|
|
|
constructor(@Inject(DEFAULT_TIMEOUT) protected defaultTimeout: number, @Inject(PLATFORM_ID) private platformId: any) {
|
2021-05-13 17:48:01 +02:00
|
|
|
}
|
|
|
|
|
2023-06-14 14:10:49 +02:00
|
|
|
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
|
|
|
if (req.method !== 'GET' || this.isService(req, TimeoutInterceptor.TIMEOUT_WHITELIST)) {
|
|
|
|
return next.handle(req);
|
|
|
|
}
|
2021-06-22 10:04:38 +02:00
|
|
|
|
2023-06-14 14:10:49 +02:00
|
|
|
let serverTime = properties.environment == "production" ? 3000 : 4000;
|
|
|
|
let clientTime = properties.environment == "production" ? 6000 : 12000;
|
|
|
|
const timeoutValue = isPlatformServer(this.platformId) ? serverTime : clientTime;//req.headers.get('timeout') || this.defaultTimeout;
|
|
|
|
const timeoutValueNumeric = Number(timeoutValue);
|
|
|
|
return next.handle(req).pipe(timeout(timeoutValueNumeric));
|
|
|
|
}
|
|
|
|
|
|
|
|
isService(req: HttpRequest<any>, service: string | string[]): boolean {
|
|
|
|
if (Array.isArray(service)) {
|
|
|
|
return !!service.find(element => req.url.indexOf(element) !== -1);
|
|
|
|
} else {
|
|
|
|
return req.url.indexOf(service) !== -1;
|
|
|
|
}
|
2021-06-22 10:04:38 +02:00
|
|
|
}
|
2020-11-02 18:11:31 +01:00
|
|
|
}
|