-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdj-interceptor.ts
38 lines (33 loc) · 1.09 KB
/
dj-interceptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
import { finalize } from 'rxjs/operators';
import { RequestCounterService } from './request-counter.service';
/**
* global interceptor that adds a simple basic auth header to all REST calls
*/
@Injectable()
export class DjInterceptor implements HttpInterceptor {
/**
* track request count
*/
constructor(private counter: RequestCounterService) { }
/**
* set basic auth
*/
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
setTimeout(() => {
this.counter.requestCounter++;
}, 0);
request = request.clone({
setHeaders: {
Authorization: 'Basic ' + sessionStorage.token
}
});
return next.handle(request).pipe(finalize(() => {
setTimeout(() => {
this.counter.requestCounter--;
}, 0);
}));
}
}