I would like to reuse some Angular 6 API services in a node script and am facing some troubles initialising everything properly.
The API services are generated using Swagger Codegen (-l typescript-angular
), which gives me for example:
@Injectable()
export class UserService {
constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
...
}
}
These services work perfect in my Angular 6 application and I would now like to use them in a node for some scripting. I'm aware that Swagger Codegen is also able to generate a pure Typescript client, but would still prefer to reuse the existing Angular services to keep the code base more in line.
The challenge I'm then facing is how to call this constructor without dependency injection.
It appears to be very hard to get a valid HttpClient
object without dependency injection. In AngularJS I used to rely on Axios for this, but that library does not provide the same interface as HttpClient
any more it seems (still promises instead of the newer observables in Angular 6).
It seems to me there would be two options:
- Somehow get an
HttpClient
object -> can't get this to work. - Inject another HTTP client object that exposes the same interface -> can't seem to find one.
Does anybody know how to properly tackle this?
Cheers,
M.