Extended Angular 2 Http service cannot be injected

180 Views Asked by At

I am trying to extend the Http Service in Angular 2 to handle some required headers I need to include on each request. I was able to successfully extend it, add the headers I want to, and then call the super.x() operations to send the requests. I resolved the problem with adding the provider using this answer , resulting in this code:

providers: [
    OAuthService,
    { provide: LoggerOptions, useValue: { level: environment.LogLevel }},
    Logger,
    {
      provide: APIClient,
      useFactory: (backend: ConnectionBackend, options: RequestOptions, oauthService: OAuthService, logger: Logger) => { return new APIClient(oauthService, backend, options, logger); },
      deps: [XHRBackend, RequestOptions, OAuthService, Logger]
    }, ...

I am now getting a compile error though:

ERROR in Error encountered resolving symbol values statically. Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function (pos
ition 45:19 in the original .ts file), resolving symbol AppModule in [directory]/src/app/app.module.ts

How can I inject my extension to the HTTP service and resolve this error?

Thanks.

Update I was able to get the app to compile using the following code:

  providers: [
    OAuthService,
    { provide: LoggerOptions, useValue: { level: environment.LogLevel }},
    Logger,
    {
      provide: APIClient,
      useFactory: apiClient,
      deps: [XHRBackend, RequestOptions, OAuthService, Logger]
    },
    IdentityService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

export function apiClient(backend: XHRBackend, options: RequestOptions, oauthService: OAuthService, logger: Logger): APIClient {
  return new APIClient(oauthService, backend, options, logger);
}

but I am not sure why I needed to create a function to wrap the constructor for my API Client. I can move forward like this but would prefer to fix this properly.

1

There are 1 best solutions below

0
On BEST ANSWER

Angulars ahead of time compilation currently just can't parse closures (() =>).

See also https://github.com/qdouble/angular-webpack2-starter#aot--donts