Project

General

Profile

1 61019 argiro.kok
import {Inject, Injectable, InjectionToken, PLATFORM_ID} from '@angular/core';
2 59741 argiro.kok
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
3
import { Observable } from 'rxjs';
4
import { timeout } from 'rxjs/operators';
5 61019 argiro.kok
import {isPlatformServer} from "@angular/common";
6 59741 argiro.kok
7
export const DEFAULT_TIMEOUT = new InjectionToken<number>('defaultTimeout');
8
9
@Injectable()
10
export class TimeoutInterceptor implements HttpInterceptor {
11 61019 argiro.kok
  constructor(@Inject(DEFAULT_TIMEOUT) protected defaultTimeout: number, @Inject(PLATFORM_ID) private platformId: any) {
12 59741 argiro.kok
  }
13
14
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
15 61003 konstantin
    if (req.method !== 'GET') {
16
      return next.handle(req);
17
    }
18
19 61019 argiro.kok
    const timeoutValue = isPlatformServer(this.platformId)?3000:6000;//req.headers.get('timeout') || this.defaultTimeout;
20 59741 argiro.kok
    const timeoutValueNumeric = Number(timeoutValue);
21
    return next.handle(req).pipe(timeout(timeoutValueNumeric));
22
  }
23
}