Use without global variables
See original GitHub issueHello,
I realize after using this library that using global variables is suboptimal for what I’m using it for.
In my case, I have a Node.js application. I need to run multiple processes with different auth (API tokens) for each, and having a global scope would limit my performance quite a bit (i have to run all in sequence to make sure that the right token is used for the request).
For quite some time, I used another library to generate code. However, that had classes for each service (like yours), but not with singletons. You had to construct each class and pass auth. E.g. new PostService('token')
.
Code that handled more than one service would have to construct a few classes. Also sub-optimal. IMO, for the best possible API, I’d like to have one class that reference others.
Example:
class MyApi {
private auth;
constructor(... some auth thing ...) {}
postService() {
return new PostService(auth);
}
}
In summary, I’d love to see a way where this library could a) generate classes without singletons and b) have a class that exports constructed services.
I realize looking at your code that this isn’t much work to do. Would you accept a pull request with that? I could also fork, but I would much rather help out 😃
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:6 (4 by maintainers)
Top GitHub Comments
@vitalybaev @cobraz There is a new version (0.16.0) that allows to create client instances to each client can have its own bearer token. You can specify the
--name
argument while generating the client. More info: https://github.com/ferdikoomen/openapi-typescript-codegen#generate-client-instance-with---name-optionI’d much rather pass around state expicitly than have an implicit global state. How about a constructor that takes an optional
config: OpenAPIConfig = OpenAPI
, best of both worlds, no?