Comparing version 1.1.3 to 1.2.3
@@ -40,4 +40,4 @@ # Koa.js usage: | ||
app.use('echo'); | ||
// you can reach some function without class creation by passing as string | ||
app.use({ action: 'echo' }); | ||
// you can reach some function without class creation by passing only action | ||
// to `use` method | ||
@@ -52,3 +52,3 @@ | ||
While using router the story is almost the same: | ||
```javascript | ||
```javascript | ||
'use strict'; | ||
@@ -55,0 +55,0 @@ |
{ | ||
"name": "hekdi", | ||
"version": "1.1.3", | ||
"version": "1.2.3", | ||
"description": "Depedency injection framework for node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,3 +10,3 @@ /** | ||
/** | ||
* @param app {Object} | ||
* @param app {Koa.application} | ||
* @param original {Function} | ||
@@ -16,21 +16,21 @@ * @returns {Function} | ||
const diResolver = function(app, original) { | ||
/** @param diConfig {Function|String|Object<{controller: string, action: string, [params]: Array}>} */ | ||
/** @param diConfig {Function|String|Object<{[controller]: string, action: string, [params]: Array}>} */ | ||
return function(diConfig) { | ||
switch (typeof diConfig) { | ||
case 'string': | ||
original.call(app, app.context.di.resolve(diConfig)); | ||
break; | ||
case 'object': | ||
const { controller, action, params } = diConfig; | ||
original.call(app, async (ctx, next) => { | ||
const dependency = app.context.di.resolve(controller); | ||
if (next) { | ||
await dependency[action](ctx, next, params); | ||
} else { | ||
await dependency[action](ctx, params); | ||
} | ||
}); | ||
break; | ||
default: // function | ||
original.call(app, diConfig); | ||
if (typeof diConfig === 'object') { | ||
const { controller, action, params } = diConfig; | ||
if (!controller && !action) { | ||
throw new Error('Incorrect dependency config provided!'); | ||
} | ||
original.call(app, async (ctx, next) => { | ||
let dependency; | ||
if (!controller && action) { | ||
dependency = app.context.di.resolve(action); | ||
await dependency(ctx, next, params); | ||
} else { | ||
dependency = app.context.di.resolve(controller); | ||
await dependency[action](ctx, next, params); | ||
} | ||
}); | ||
} else { | ||
original.call(app, diConfig); | ||
} | ||
@@ -43,7 +43,7 @@ return app; | ||
* @param app {Object} | ||
* @param ctx {Object} | ||
* @param router {Router} | ||
* @param original {Function} | ||
* @returns {Function} | ||
*/ | ||
const diRouterResolver = function(app, ctx, original) { | ||
const diRouterResolver = function(app, router, original) { | ||
/** | ||
@@ -53,22 +53,31 @@ * path {string} | ||
*/ | ||
return function(path, diConfig) { | ||
switch (typeof diConfig) { | ||
case 'string': | ||
original.call(ctx, path, app.context.di.resolve(diConfig)); | ||
break; | ||
case 'object': | ||
const { controller, action, params } = diConfig; | ||
original.call(ctx, path, async (ctx, next) => { | ||
const dependency = app.context.di.resolve(controller); | ||
if (next) { | ||
return function(...args) { | ||
const [name, path] = args; | ||
const isRouteNamed = typeof name === 'string' && (typeof path === 'string' || path instanceof RegExp); | ||
const middlewares = args.slice(isRouteNamed ? 2 : 1).map(config => { | ||
if (typeof config === 'object') { | ||
const { controller, action, params } = config; | ||
if (!controller && !action) { | ||
throw new Error('Incorrect dependency config provided!'); | ||
} | ||
return async (ctx, next) => { | ||
let dependency; | ||
if (!controller && action) { | ||
dependency = app.context.di.resolve(action); | ||
await dependency(ctx, next, params); | ||
} else { | ||
dependency = app.context.di.resolve(controller); | ||
await dependency[action](ctx, next, params); | ||
} else { | ||
await dependency[action](ctx, params); | ||
} | ||
}); | ||
break; | ||
default: // function | ||
original.call(ctx, path, diConfig); | ||
} | ||
} | ||
return config; | ||
}); | ||
if (isRouteNamed) { | ||
original.call(router, name, path, ...middlewares); | ||
} else { | ||
original.call(router, name, ...middlewares); | ||
} | ||
return ctx; | ||
return router; | ||
}; | ||
@@ -75,0 +84,0 @@ }; |
Sorry, the diff of this file is not supported yet
18398
308