Parameter-names-based dependency injector for Node.js.
Returns a new instance of an injector function to work with.
Limitations
- minification of code is not supported (use
createInjectAnnotatedinstead) - not typesafe
- slower than a normal function call
Returns a new instance of an injector function that uses AngularJS-style array annotation for minification safety.
Instead of parsing parameter names, it expects dependencies to be specified as strings in an array before the function:
inject(["dep1", "dep2", (dep1, dep2) => { ... }]);This allows code to be minified since the dependency names are preserved as strings.
Note: The input array is mutated during injection (the function is removed via
pop()). Always use inline array literals or create a fresh array for each call if you need to reuse the dependency list.
Injects arguments into a function and invokes it.
function - required, function to inject parameters into and call (or annotated array for createInjectAnnotated)
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
});import { createInjectAnnotated } from "@slimlib/injector";
const inject = createInjectAnnotated();
inject([
"$provide",
($provide: Provider) => {
$provide("config", {
url: "http://example.com/json",
format: "json",
});
},
]);
inject([
"config",
async (config: Json) => {
const data = await fetch(config.url);
const result = config.json ? await data.json() : data;
// and so on
},
]);This style is similar to AngularJS's dependency injection annotation:
// Before minification
angular.module("App", []).controller("MyController", function ($scope) {
$scope.value = "test";
});
// After minification (broken)
angular.module("App", []).controller("MyController", function (a) {
a.value = "test";
});
// With array annotation (works after minification)
angular.module("App", []).controller("MyController", [
"$scope",
function (a) {
a.value = "test";
},
]);You can use createInject during development and swap to createInjectAnnotated at build time for production. This can be done with build tools like Rollup, Webpack, or esbuild by aliasing the import.
- 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?
createInjectwill not work after minification, butcreateInjectAnnotatedis designed to work with minified code.
- Is it good for Node.js applications?
- Only in some edge cases, please use singletons/factories/something else if possible.