Parameter names based dependency injector for nodejs.
Limitations
- does not work with default parameters
- minification of code is not supported
- not typesafe
- classes not supported
- slower than a normal function call
returns a new instance of an injector function to work with.
injects arguments into function and invokes it
function
- required, function to inject parameters and call
scope
- optional, default = {}
, this argument for the function
predefined injectable function
key
- string, required
value
- unknown
to get it, inject it into the function
inject(($provide: Provider) => {
$provide('service', service);
});
import createInject from '@slimlib/injector';
const inject = createInject();
inject(($provide: Provider) => {
$provide('config', {
url: 'http://example.com/json',
format: 'json'
});
});
inject(async (config: Json) => {
const data = await fetch(config.url);
const result = config.json ? await data.json() : data;
// and so on
});
- Is it a good solution to mock something in unit tests?
- no, please use jest, vitest, proxyquire, proxyrequire and other similar approaches to mock modules.
- Is it a good solution to use in frontend code?
- no, it will not work after minification
- Is it good for nodejs applications?
- only in some edge cases, please use singletons/factories/something else if possible