import {Inject, Injectable, InjectionToken, PLATFORM_ID} from '@angular/core'; import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; import { Observable } from 'rxjs'; import { timeout } from 'rxjs/operators'; import {isPlatformServer} from "@angular/common"; export const DEFAULT_TIMEOUT = new InjectionToken('defaultTimeout'); @Injectable() export class TimeoutInterceptor implements HttpInterceptor { constructor(@Inject(DEFAULT_TIMEOUT) protected defaultTimeout: number, @Inject(PLATFORM_ID) private platformId: any) { } intercept(req: HttpRequest, next: HttpHandler): Observable> { if (req.method !== 'GET') { return next.handle(req); } const timeoutValue = isPlatformServer(this.platformId)?3000:6000;//req.headers.get('timeout') || this.defaultTimeout; const timeoutValueNumeric = Number(timeoutValue); return next.handle(req).pipe(timeout(timeoutValueNumeric)); } }