I have created a generic service for my HTTP APIs and I want to provide this service for different endpoint without creating a class for each of my endpoints. My service looks like this
export class ApiService<T> {
constructor(private httpClient: HttpClient, private otherDependency: OtherDependency, private subPath: string, private defaultHeaders = []) { }
}
And I am injecting it using tokens and providers like these ones
import { InjectionToken } from '@angular/core';
export const XXXToken = new InjectionToken<ApiService<XXX>('MyService');
export const XXXProvider = {
provide: XXXToken,
useFactory: (httpClient: HttpClient, dep: OtherDependency) => new ApiService<XXX>(httpClient, dep, 'xxx'),
deps: [ HttpClient, OtherDependency]
};
Previous code will actually fail when trying to use my provider in the declarations of a NgModule and building in production mode (only in production mode). Error looks like
Error during template compile of 'AppModule'
Consider changing the function expression into an exported function.
Issue is coming from (httpClient: HttpClient, dep: OtherDependency) => new ApiService<XXX>(httpClient, dep, 'xxx')
I can make it work if I do something like
export function myFactory(httpClient: HttpClient, dep: OtherDependency) => new ApiService<XXX>(httpClient, dep, 'xxx');
export const XXXProvider = {
provide: XXXToken,
useFactory: myFactory,
deps: [ HttpClient, OtherDependency]
};
However, because the provider and factory is more complicated than that, I've created an helper function to not have to repeat that code any time I am adding a new endpoint.
export func getProvider(token, subPath) {
return {
provide: token,
useFactory: (httpClient: HttpClient, dep: OtherDependency) => new ApiService<XXX>(httpClient, dep, subPath),
deps: [ HttpClient, OtherDependency]
}
}
And in there I cannot use the export function trick because I want my subpath to be different everytime I am creating a new service.
Is there a way to get around this or am I stuck to repeating my provider creation every time I need it. I would be very sad since that would mean for example that I would have to change every provider creation anytime I need a new dependency on that service. === Edit === I want to be able to inject my services easily in any service/component without having to bin those other objects to the service creation or implementation detailes and there can be many services in one component. Ideally I would like something like that
export class MyComponent implements OnInit {
constructor(private userService: ApiService<User>, private countryService: ApiService<countryService>) { }
ngOnInit() {
userService.get();
countryService.get();
}
}
But because javascript does not realy support generics, this cannot work because ApiService and ApiService are actually the same class. Therefore I currently need injection token anyway so I would be OK with
export class MyComponent implements OnInit {
constructor(@Inject(UserToken) private userService: ApiService<User>, @Inject(CountryToken) private countryService: ApiService<countryService>) { }