Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sielay/ditoolkit
Set of classes and helpers for dependency injection in NodeJS
https://github.com/sielay/ditoolkit
Last synced: 1 day ago
JSON representation
Set of classes and helpers for dependency injection in NodeJS
- Host: GitHub
- URL: https://github.com/sielay/ditoolkit
- Owner: sielay
- License: mit
- Created: 2016-08-01T14:17:10.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-08-01T15:51:29.000Z (over 8 years ago)
- Last Synced: 2024-04-28T22:04:44.564Z (6 months ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DIToolkit
## Problem
There are various projects solving dependency injection for NodeJS. All of them are quire opinioneted and most apply Angular way - where target of injection decide about dependencies and they can be overriden in some config.
Also non of them appreciate that DI is most useful in node for promised/deferred dependencis.
## Installation
```sh
npm i --save ditoolkit
```## Usage
### Functions - on example of Express Middleware
```javascript
// module1module.export = function IWantToBeTestable(dependency1, dependancy2, dependancy3, req, res, next, id) {
...
}// module 2
const module1 = require('./module1');
require('ditoolkit');app.user(module1.injected(null, 123, {config:1}, Promise.resolve('whatever')));
```
Method `injected` and it synchronous version (not supporting promises) `inject` are added to `Function` prototype. Once you call them you'd get version of the function with bound `thisArg` and dependencies. Dependencies will be prefixed to later arguments (like you can see in function declaration).
### Classes
```javascript
const DI = require('ditoolkit');
class A {
test(depencency1, someArg) { ... }
}let a = new A();
DI.decorate(a, [sharedDependnecy1]);
class B {
test() { ... },
test2(dependency1) { ... },
test3(dependency1, dependency2, someArg) { ... }
}let B = new A();
DI.decorate(a, [sharedDependnecy1], {
test2: [],
test3: [sharedDepenency2]
});```
### Also
See test for details.
## License
MIT