0

I am currently creating a library which internally uses axios. Since I'm making use of dependency injection, I can't use the constructor for my services to initialize some data.

There are many ways to solve this in another way, e.g.

serviceWithOptions = container.Resolve(...);
serviceWithOptions.option1 = '';
serviceWithOptions.setOption2('');

..// etc

What I want to do instead, is something like tsconfig.json, webpack.config.js or eslintrc.json

I want users of my library to create a config file, which is parsed by my library and then internally use these things.

Now, I could just create a fileReader that reads a specific file, however, I don't know what will happen if users have my library as a node_module (the default way), since then, the root folder will be different?

Also, I found some libraries

What would be the best way and the best practice to achieve this?

1 Answer 1

0

I'm not entirely certain, but I think this isn't really doable in the way you might hope. The comparison things you offer are actually not running as a library within a program, but actually executed as their own executables outside of the program. In a sense, for them, the program is merely data being provided.

And given that it seems to be an HTTP library you'd want a program to consume like any other, this would seem to introduce a lot of challenges.

Of course, yes, you could ask users to provide data via the ENV as a means to circumvent this, but perhaps there is another way to provide the config?

In what way are you using DI? Is there perhaps a means by which a config object can be provided and then return some constructor that is created within a closure where it has access to that config object?

Sign up to request clarification or add additional context in comments.

5 Comments

Hm, okay. Sad. I was so used to do it this way from other pgoramming languages, that I thought it's easily do-able in JS as well. I'm using InversifyJS and you can use factories to create certain services from a specific configuration. But this would require the container, so to access the container, I would need to export it, which I wouldn't want to do (doesn't sound like good practice to me, to give the end-user the container). So, seems like I will simply create setter methods to set configuration values and throw an error, if no option has been passed. Thoughts?
What do you envision as being exported by your module? The constructed container? All the services it creates?
Currently, I'm simply importing the container in my index.ts and then resolve the services, but not all, only the ones to be used by the end-user and then I'm simply exporting the created services. So the end-user would just get PageClient for example, but not HttpClient or Container etc.
A pattern I've used in my own projects that works somewhat similarly, but does require a bit of user level work, but not terribly much is something like this. User imports a "make services" function from a module, that doesn't expose any of the internals. Just takes a config object and returns a set of services made by that. And then the user imports their own module that exports the constructed values. Essentially they have a module as config. It's a bit boilerplatey but not too but, and easily documentable.
Yeah, this is something I could imagine. Maybe a class called ClientConfiguration which I add in a SingletonScope, so all other services using this service get the same configuration. I think I would do it that way. Thanks for your input :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.