question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use without global variables

See original GitHub issue

Hello,

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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ferdikoomencommented, Jan 25, 2022

@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-option

0reactions
silasdaviscommented, Oct 15, 2022

otherwise you will need to pass around this MyApi class everywhere.

I’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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Are The Alternatives To Using Global Variables?
It is well known that global variables are generally a bad thing. Although they can seem convenient, they have a number of major...
Read more >
Are global variables bad? - c++ - Stack Overflow
The "no global variables" rule is there because most of the time, global variables make the meaning of code less clear. However, like...
Read more >
Why Is Using Global Variables Considered a Bad Practice?
A global variable is a variable that is declared outside any function and is accessible to all routines in our program. So, its...
Read more >
Alternatives To Global Variables - Anupam Chugh - Medium
Global variables are accessible from anywhere which makes them convenient and useful but also dangerous as they can be misused.
Read more >
Why should we avoid using global variables in C/C++?
Testing in programs using global variables can be a huge pain as it is difficult to decouple them when testing.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found