Search code examples
angularhttpdependency-injectionaot

Angular: Providing Http in AoT


I'm using a custom http client that I provide for a function as a dependency for a providefactory.

{
  provide: TranslateLoader,
  useFactory: HttpLoaderFactory,
  deps: [Client, Url]
},

In JiT works fine but in AoT I get this error message:

ERROR in C:/PATH/src/$$_gendir/app/app.module.ngfactory.ts (1,1): Argument of type 'Client' is not assignable to parameter of type 'Http'.
  Property '_backend' is missing in type 'Client'.

But it shouldn't matter since it's protected and a property that wouldn't be accessed from outside, right?

What should I do? Adding those protected attributes to my Client won't fix the problem, and I don't know how to make my class extending http without breaking, since in the super call I need to pass a ConnectionBackend that I don't know how it works and how to get one.

EDIT: I've tried with the extend method but with no success: when extending, if I have the constructor like:

@Injectable()
export class Client extends Http {
constructor(private authService: AuthService,  providers: XHRBackend) {
    super(providers, null);
    this.onInit();
}

or like

@Injectable()
export class Client extends Http {
constructor(private authService: AuthService) {
        super(null, null);
        this.onInit();
    }

overwriting the http methots to ones that makes, for example

return super.get(url, this._getOptions(options)).catch((error: any) => this._handleError(error));

it will say:

zone.js:569 Unhandled Promise rejection: Cannot read property 'merge' of null ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot read property 'merge' of null
    at mergeOptions (http.es5.js:1767)

Solution

  • Add a provider

        useFactory: (
            backend: XHRBackend,
            defaultOptions: RequestOptions) =>
            new CustomHttp(backend, defaultOptions),
        deps: [XHRBackend, RequestOptions]
    

    See also How to override Http class in RC6?