Comparing version 1.3.12 to 1.4.0
@@ -8,3 +8,3 @@ 'use strict'; | ||
createModule: Module.createModule, | ||
koaDI: require('./src/frameworks/koa') | ||
koaDI: require('./src/koa') | ||
}; |
{ | ||
"name": "hekdi", | ||
"version": "1.3.12", | ||
"description": "Depedency injection framework for node.js", | ||
"version": "1.4.0", | ||
"description": "Depedency injection framework for node integrated with koa.js", | ||
"main": "index.js", | ||
@@ -14,4 +14,2 @@ "scripts": { | ||
"eslint": "^4.5.0", | ||
"express": "^4.16.2", | ||
"hapi": "^16.5.2", | ||
"koa": "^2.3.0", | ||
@@ -28,2 +26,3 @@ "koa-body-parser": "^1.1.2", | ||
"nodeart", | ||
"framework", | ||
"di", | ||
@@ -36,7 +35,3 @@ "dependency", | ||
"koa", | ||
"koa.js", | ||
"express", | ||
"express.js", | ||
"hapi", | ||
"hapi.js" | ||
"koa.js" | ||
], | ||
@@ -43,0 +38,0 @@ "engines": { |
115
README.md
@@ -20,8 +20,2 @@ [![Build Status](https://travis-ci.org/IvanProdaiko94/hekdi.svg?branch=master)](https://travis-ci.org/IvanProdaiko94/hekdi) | ||
## `hekdi` popular frameworks integration: | ||
- [Hapi](./docs/hapi.md) | ||
- [Express](./docs/express.md) | ||
- [Koa](./docs/koa.md) | ||
## Basic usage: | ||
@@ -54,3 +48,3 @@ | ||
{ name: 'LocalDependency', strategy: 'singleton', value: Dependency1 }, | ||
{ name: 'PublicDependency', strategy: 'factory', value: Dependency2 }, | ||
{ name: 'PublicDependency', strategy: 'service', value: Dependency2 }, | ||
{ name: 'Arr', strategy: 'value', value: [1, 2, 3] } | ||
@@ -129,5 +123,5 @@ ], | ||
{ name: 'LocalDependency', strategy: 'singleton', value: class X {} }, | ||
{ name: 'PublicDependency', strategy: 'factory', value: class Y {} }, | ||
{ name: 'PublicDependency', strategy: 'service', value: class Y {} }, | ||
{ name: 'Arr', strategy: 'value', value: [1, 2, 3] }, | ||
{ name: 'ZProvider', strategy: 'provider', value: () => ({ name: 'Z', strategy: 'factory', value: class Z {} })} | ||
{ name: 'ZProvider', strategy: 'provider', value: () => ({ name: 'Z', strategy: 'service', value: class Z {} })} | ||
], | ||
@@ -141,3 +135,4 @@ exports: ['PublicDependency', 'Arr'], // if '*' set, module will export all of the dependencies including imported | ||
### Strategies: | ||
- `factory` - each time a new instance will be created. | ||
- `service` - each time a new instance will be created with `new` keyword. | ||
- `factory` - return the result of plain function call. | ||
- `singleton` - only one instance will be created. | ||
@@ -148,2 +143,100 @@ - `value` - just will be returned. | ||
- `provider` - function that will be called, to get dependency config. | ||
Providers register dependencies before others do. Providers can't be exported from module. | ||
Providers register dependencies before others do. Providers can't be exported from module. | ||
# Koa.js usage: | ||
`hekdi` can be integrated with [koa.js](https://github.com/koajs/koa). | ||
The main concept of framework integration is monkey patching of functions | ||
that are responsible for requests handling. | ||
While using koa hakdi monkey patches `use` method. | ||
#### Basic usage: | ||
```javascript | ||
const Koa = require('koa'); | ||
const { koaDI } = require('hekdi'); | ||
const app = new Koa(); | ||
const moduleToBootstrap = { | ||
name: 'MainModule', | ||
declarations: [ | ||
{ name: 'ctrl', strategy: 'singleton', value: SomeClass }, | ||
{ name: 'echo', | ||
strategy: 'value', | ||
value: async (ctx) => { | ||
ctx.body = ctx.request.body; | ||
} | ||
} | ||
], | ||
exports: '*' | ||
}; | ||
koaDI(moduleToBootstrap, app); | ||
// now di is already bootstrapped and ready to work. | ||
// In koa app you can reach di as `app.context.di` | ||
// In di you can get koa app as `App` dependency. | ||
app.use({ | ||
controller: 'ctrl', // if dependency is object | ||
action: 'middleware', // you tell which of its methods will be called | ||
params: [1, 2, 3] // also you can pass additional params to call if needed | ||
}); | ||
app.use({ action: 'echo' }); | ||
// you can reach some function without class creation by passing only action | ||
// to `use` method | ||
app.use(async (ctx) => { // you still can pass function to `use` method | ||
ctx.body = ctx.request.body; | ||
}); | ||
app.listen(3000) | ||
``` | ||
### Usage with router | ||
While using router the story is almost the same: | ||
```javascript | ||
'use strict'; | ||
const Koa = require('koa'); | ||
const Router = require('koa-router'); | ||
const bodyParser = require('koa-body-parser'); | ||
const { koaDI } = require('hekdi'); | ||
const app = new Koa(); | ||
const router = new Router(); | ||
const moduleToBootstrap = { | ||
name: 'MainModule', | ||
declarations: [ | ||
{ name: 'ctrl', strategy: 'singleton', value: SomeClass }, | ||
{ name: 'echo', | ||
strategy: 'value', | ||
value: async (ctx) => { | ||
ctx.body = ctx.request.body; | ||
} | ||
} | ||
], | ||
exports: '*' | ||
}; | ||
koaDI(moduleToBootstrap, app, router); | ||
app.use(bodyParser()); | ||
router | ||
.post(['/', '/test'], { action: 'echo'}) | ||
.get('/', { | ||
controller: 'ctrl', | ||
action: 'getHandler', | ||
params: [1, 2, 3] | ||
}).get('/test', async (ctx) => { | ||
ctx.body = 'handled'; | ||
}); | ||
app | ||
.use(router.routes()) | ||
.use(router.allowedMethods()); | ||
app.listen(3000); | ||
``` |
@@ -15,3 +15,3 @@ /** | ||
const resolveDependency = function(dependencyName) { | ||
const resolveDependency = function(dependencyName, strategy) { | ||
const config = this.getConfigOf(dependencyName); | ||
@@ -26,3 +26,5 @@ if (this.resolutionTrace.indexOf(dependencyName) !== -1) { | ||
this.resolutionTrace.push(dependencyName); | ||
const d = new config.value(...(config.value.$inject || []).map(name => resolveHelper.call(this, name))); | ||
const deps = (config.value.$inject || []).map(name => resolveHelper.call(this, name)); | ||
const isFactory = strategy === 'factory'; | ||
const d = isFactory ? config.value(...deps) : new config.value(...deps); | ||
this.resolutionTrace.pop(); | ||
@@ -34,7 +36,15 @@ return d; | ||
/** | ||
* creates a new instance each time | ||
* creates a new instance each time with `new` keyword | ||
* Explanation https://codeburst.io/javascript-for-beginners-the-new-operator-cee35beb669e | ||
* @param dependencyName {string} | ||
*/ | ||
service: dependencyName => function() { | ||
return resolveDependency.call(this, dependencyName, 'service'); | ||
}, | ||
/** | ||
* return the result of plain function call | ||
* @param dependencyName | ||
*/ | ||
factory: dependencyName => function() { | ||
return resolveDependency.call(this, dependencyName); | ||
return resolveDependency.call(this, dependencyName, 'factory'); | ||
}, | ||
@@ -49,3 +59,3 @@ /** | ||
if (!instance) { | ||
instance = resolveDependency.call(this, dependencyName); | ||
instance = resolveDependency.call(this, dependencyName, 'singleton'); | ||
} | ||
@@ -52,0 +62,0 @@ return instance; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19457
6
342
238
11