I have created custom interceptor for all Http requests:
import {EventEmitterService} from "./EventEmitter.service";
@Injectable()
export class CustomHttp extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, _eventEmitterService:EventEmitterService) {
super(backend, defaultOptions);
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.get(url,{headers: interceptorHeaders}).catch((res)=>{
if (res.status === 403){
console.log("Interceptor here")
this._eventEmitterService.logout.emit("403");
}
return Observable.of(res);
});
}
}
Which works great - whenever I am receiving a 403 response from the server I get :
Interceptor here
in my console.
However, there is an issue with injecting EventEmitterService
into the catch function. Whenever I am inside of it, I cant access CustomHttp
- I only have access to some Observable
, even though when I debug my constructor - I can see the EventEmitterService
has been injected.
This is how I inject EventEmitterService
:
bootstrap(App,[...,
EventEmitterService,
new Provider(Http, {
useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, _eventEmitterService:EventEmitterService) => new CustomHttp(backend, defaultOptions,_eventEmitterService),
deps: [XHRBackend, RequestOptions,EventEmitterService]
}),...]);