1

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]
    }),...]);
uksz
  • 18,239
  • 30
  • 94
  • 161

1 Answers1

2

Perhaps you just missed the private keyword at the level of the _eventEmitterService parameter of the CustomHttp constructor:

export class CustomHttp extends Http {
constructor(backend: ConnectionBackend,
            defaultOptions: RequestOptions,
            private _eventEmitterService:EventEmitterService) { // <----
  super(backend, defaultOptions);
}
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • So easy. I thought that public (default), and private in constructor were only important for TypeScript, not for the compiled javascript. – uksz Mar 17 '16 at 12:34