Comparing version 1.3.4 to 1.3.5
{ | ||
"name": "hekdi", | ||
"version": "1.3.4", | ||
"version": "1.3.5", | ||
"description": "Depedency injection framework for node.js", | ||
@@ -13,3 +13,3 @@ "main": "index.js", | ||
"chai": "^4.1.1", | ||
"eslint": "^3.19.0", | ||
"eslint": "^4.5.0", | ||
"express": "^4.15.4", | ||
@@ -16,0 +16,0 @@ "hapi": "^16.5.2", |
@@ -125,3 +125,4 @@ [![Build Status](https://travis-ci.org/IvanProdaiko94/hekdi.svg?branch=master)](https://travis-ci.org/IvanProdaiko94/hekdi) | ||
{ name: 'PublicDependency', strategy: 'factory', value: class Y {} }, | ||
{ name: 'Arr', strategy: 'value', value: [1, 2, 3] } | ||
{ name: 'Arr', strategy: 'value', value: [1, 2, 3] }, | ||
{ name: 'ZProvider', strategy: 'provider', value: () => ({ name: 'Z', strategy: 'factory', value: class Z {} })} | ||
], | ||
@@ -139,2 +140,4 @@ exports: ['PublicDependency', 'Arr'], // if '*' set, module will export all of the dependencies including imported | ||
- `constant` - the same as `value` but can't be reassign. | ||
- `alias` - used to create an alias for some dependency. | ||
- `alias` - used to create an alias for some dependency. | ||
- `provider` - function that will be called, to get dependency config. | ||
Providers register dependencies before others do. Providers can't be exported from module. |
@@ -22,3 +22,3 @@ /** | ||
} | ||
original.call(app, async (ctx, next) => { | ||
original.call(app, async(ctx, next) => { | ||
let dependency; | ||
@@ -60,3 +60,3 @@ if (!controller && action) { | ||
} | ||
return async (ctx, next) => { | ||
return async(ctx, next) => { | ||
let dependency; | ||
@@ -70,3 +70,3 @@ if (!controller && action) { | ||
} | ||
} | ||
}; | ||
} | ||
@@ -94,3 +94,3 @@ return config; | ||
di.bootstrap(bootstrapModule); | ||
di.main.injector.register({name: 'App', strategy: 'constant', value: app}); | ||
di.main.injector.register({ name: 'App', strategy: 'constant', value: app }); | ||
app.context.di = di; | ||
@@ -105,4 +105,4 @@ | ||
router[methodName] = diRouterResolver(app, router, router[methodName]); | ||
}) | ||
}); | ||
} | ||
}; |
@@ -40,3 +40,24 @@ 'use strict'; | ||
Injector.prototype.register = function(...dependencies) { | ||
dependencies.map(config => { | ||
const providers = []; | ||
dependencies = dependencies.filter(config => { | ||
if (config.strategy === 'provider') { | ||
providers.push(config); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
const providersValues = providers.map(provider => { | ||
const dConf = provider.value(); | ||
if (dConf.strategy === 'provider') { | ||
throw new Error(errors.providerDoNotRegisterProviders(provider.name)); | ||
} | ||
return dConf; | ||
}); | ||
if (providersValues.length > 0) { | ||
this.register(...providersValues); | ||
} | ||
dependencies.forEach(config => { | ||
const inject = config.value.$inject || []; | ||
@@ -43,0 +64,0 @@ const dConf = this.getConfigOf(config.name); |
@@ -10,3 +10,4 @@ 'use strict'; | ||
`strategy ${strategy} is incorrect. Allowable values are ${Object.keys(strategies).join(', ')}` | ||
) | ||
), | ||
providerDoNotRegisterProviders: providerName => `Providers may not register providers ${providerName}` | ||
}; |
Sorry, the diff of this file is not supported yet
19005
319
141
13