I'm using HttpInterceptor
api of Angular's HttpClientModule
and now stuck with a situation. Here is my use-case.
In my application, if any service makes an http call, I want to make another silent http call before it, and want to store it's result locally. Once the silent call is completed, then only I'll proceed with the service's http call and will return its result.
Here is what I'm doing. (Using HttpInterceptor)
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.url.match(/api\//)) { // api call
http.post('/auth/silentCall', {user: 123}).subscribe(
(data) => {
console.log(data);
const clone = req.clone({ setHeaders: { 'Authorization ': tokenService.getToken() } });
return next.handle(clone).do((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
console.log('Service's call response');
}
}, (err: any) => {
if (err instanceof HttpErrorResponse) {
console.log('Network call err', err);
}
});
},
(err) => {
if (err instanceof HttpErrorResponse) {
return Observable.throw(err);
}
);
} else {
console.log('Not an api call..It's an auth call');
return next.handle(req); // call original auth request.
}
}
Now, problem with the above code is, it is executing internal silent call successfully, but never calls the original api call after that. So the service keeps waiting forever.