Project

General

Profile

1
import {Inject, Injectable, InjectionToken, PLATFORM_ID} from '@angular/core';
2
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
3
import { Observable } from 'rxjs';
4
import { timeout } from 'rxjs/operators';
5
import {isPlatformServer} from "@angular/common";
6

    
7
export const DEFAULT_TIMEOUT = new InjectionToken<number>('defaultTimeout');
8

    
9
@Injectable()
10
export class TimeoutInterceptor implements HttpInterceptor {
11
  constructor(@Inject(DEFAULT_TIMEOUT) protected defaultTimeout: number, @Inject(PLATFORM_ID) private platformId: any) {
12
  }
13

    
14
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
15
    if (req.method !== 'GET') {
16
      return next.handle(req);
17
    }
18

    
19
    const timeoutValue = isPlatformServer(this.platformId)?3000:6000;//req.headers.get('timeout') || this.defaultTimeout;
20
    const timeoutValueNumeric = Number(timeoutValue);
21
    return next.handle(req).pipe(timeout(timeoutValueNumeric));
22
  }
23
}
(4-4/4)