Skip to content

Commit

Permalink
feat: support handler function and absoulte controller file path.
Browse files Browse the repository at this point in the history
  • Loading branch information
amazing-gao committed Mar 3, 2018
1 parent e81bfbe commit ca1c24a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[license-img]: http://img.shields.io/badge/license-MIT-green.svg
[license-url]: http://opensource.org/licenses/MIT

[node-image]: https://img.shields.io/badge/node.js-v6.0.0-blue.svg
[node-image]: https://img.shields.io/badge/node.js-v7.6.0-blue.svg
[node-url]: http://nodejs.org/download/

[npm-img]: https://img.shields.io/npm/v/koa-oai-router-middleware.svg
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-oai-router-middleware",
"version": "1.1.1",
"version": "1.1.2",
"description": "middleware loader plugin of koa-oai-router",
"main": "src/middleware.js",
"scripts": {
Expand Down
16 changes: 14 additions & 2 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MiddlewarePlugin extends Plugin {
}

loadHandler({ file, handler }) {
if (!file || !handler) throw new Error('invalid file and handler setted.');
assert(_.isFunction(handler) || (_.isString(file) && _.isString(handler)), 'invalid file and handler setted.');

const { args } = this;
let middlewareDir = '';
Expand All @@ -47,7 +47,19 @@ class MiddlewarePlugin extends Plugin {
throw Error(`args must be string or object, not ${typeof args}`);
}

const modulePath = path.resolve(middlewareDir, file);
// a middleware function.
if (_.isFunction(handler)) {
return handler;
}

// a absolute or relative file path
let modulePath = null;
if (path.isAbsolute(file)) {
modulePath = file;
} else {
modulePath = path.resolve(middlewareDir, file);
}

const module = require(modulePath);

debug('load from:', modulePath);
Expand Down

0 comments on commit ca1c24a

Please sign in to comment.