1
|
import { Inject, Injectable, InjectionToken } 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
|
|
6
|
export const DEFAULT_TIMEOUT = new InjectionToken<number>('defaultTimeout');
|
7
|
|
8
|
@Injectable()
|
9
|
export class TimeoutInterceptor implements HttpInterceptor {
|
10
|
constructor(@Inject(DEFAULT_TIMEOUT) protected defaultTimeout: number) {
|
11
|
}
|
12
|
|
13
|
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
14
|
const timeoutValue = req.headers.get('timeout') || this.defaultTimeout;
|
15
|
const timeoutValueNumeric = Number(timeoutValue);
|
16
|
|
17
|
return next.handle(req).pipe(timeout(timeoutValueNumeric));
|
18
|
}
|
19
|
}
|