Comparing version 1.5.1 to 1.6.0
1.6.0 / 2017-01-20 | ||
================== | ||
* feat: controller support class (#42) | ||
1.5.1 / 2017-01-19 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -10,3 +10,5 @@ 'use strict'; | ||
const co = require('co'); | ||
const BaseContextClass = require('./utils/base_context_class'); | ||
const DEPRECATE = Symbol('EggCore#deprecate'); | ||
@@ -50,2 +52,11 @@ | ||
/** | ||
* BaseContextClass is a base class extended by classes(like service and controller), | ||
* it will be instantiated every request, and assign ctx and app to this. | ||
* | ||
* @member {BaseContextClass} EggCore#BaseContextClass | ||
* @since 1.0.0 | ||
*/ | ||
this.BaseContextClass = BaseContextClass; | ||
/** | ||
* @member {EggLoader} EggCore#loader | ||
@@ -162,2 +173,10 @@ * @since 1.0.0 | ||
get Controller() { | ||
return this.BaseContextClass; | ||
} | ||
get Service() { | ||
return this.BaseContextClass; | ||
} | ||
/** | ||
@@ -164,0 +183,0 @@ * @member {Function} |
@@ -85,2 +85,5 @@ 'use strict'; | ||
if (exports == null) continue; | ||
if (this.options.inject && is.class(exports)) { | ||
exports.prototype.pathName = utils.getPathName(fullpath, this.options.inject); | ||
} | ||
items.push({ fullpath, properties, exports }); | ||
@@ -98,2 +101,3 @@ debug('parse %s, properties %j, export %j', fullpath, properties, exports); | ||
module.exports.EXPORTS = EXPORTS; | ||
module.exports.FULLPATH = FULLPATH; | ||
@@ -100,0 +104,0 @@ // a/b/c.js => ['a', 'b', 'c'] |
'use strict'; | ||
const path = require('path'); | ||
const is = require('is-type-of'); | ||
const utility = require('utility'); | ||
const utils = require('../../utils'); | ||
@@ -13,3 +16,24 @@ module.exports = { | ||
loadController(opt) { | ||
opt = Object.assign({ lowercaseFirst: true }, opt); | ||
opt = Object.assign({ | ||
lowercaseFirst: true, | ||
initializer: (obj, opt) => { | ||
// return class if it exports a function | ||
// ```js | ||
// module.exports = app => { | ||
// return class HomeController extends app.Controller {}; | ||
// } | ||
// ``` | ||
if (is.function(obj) && !is.generatorFunction(obj) && !is.class(obj)) { | ||
obj = obj(this.app); | ||
} | ||
if (is.class(obj)) { | ||
obj.prototype.pathName = utils.getPathName(opt.path, this.app); | ||
return wrapClass(obj); | ||
} | ||
if (is.object(obj)) { | ||
return wrapObject(obj, opt.path); | ||
} | ||
return obj; | ||
}, | ||
}, opt); | ||
const controllerBase = path.join(this.options.baseDir, 'app/controller'); | ||
@@ -22,1 +46,54 @@ | ||
}; | ||
// wrap the class, yield a object with middlewares | ||
function wrapClass(Controller) { | ||
const keys = Object.getOwnPropertyNames(Controller.prototype); | ||
const ret = {}; | ||
for (const key of keys) { | ||
// getOwnPropertyNames will return constructor | ||
// that should be ignored | ||
if (key === 'constructor') { | ||
continue; | ||
} | ||
if (is.function(Controller.prototype[key])) { | ||
ret[key] = methodToMiddleware(Controller, key); | ||
} | ||
} | ||
return ret; | ||
function methodToMiddleware(Controller, key) { | ||
return function* classControllerMiddleware() { | ||
const controller = new Controller(this); | ||
const r = controller[key](this); | ||
// TODO: if we can check async function, then we can check it out of the middleware | ||
if (is.generator(r) || is.promise(r)) { | ||
yield r; | ||
} | ||
}; | ||
} | ||
} | ||
// wrap the method of the object, method can recieve ctx as it's first argument | ||
function wrapObject(obj, path) { | ||
const keys = Object.keys(obj); | ||
const ret = {}; | ||
for (const key of keys) { | ||
if (is.function(obj[key])) { | ||
const names = utility.getParamNames(obj[key]); | ||
if (names[0] === 'next') { | ||
throw new Error(`controller \`${key}\` should not use next as argument from file ${path}`); | ||
} | ||
ret[key] = functionToMiddleware(obj[key]); | ||
} | ||
} | ||
return ret; | ||
function functionToMiddleware(func) { | ||
return function* objectControllerMiddleware() { | ||
const r = func.call(this, this); | ||
if (is.generator(r) || is.promise(r)) { | ||
yield r; | ||
} | ||
}; | ||
} | ||
} |
@@ -35,2 +35,11 @@ 'use strict'; | ||
// rename fullpath | ||
// /path/to/app/controller/admin/config.js => controller.admin.config | ||
getPathName(path, app) { | ||
const baseDir = app.loader.appInfo.baseDir; | ||
return path | ||
.replace(`${baseDir}/app/`, '') | ||
.replace(/\//g, '.') | ||
.replace(/\.js$/, ''); | ||
}, | ||
}; |
{ | ||
"name": "egg-core", | ||
"version": "1.5.1", | ||
"version": "1.6.0", | ||
"description": "A core Pluggable framework based on koa", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
65777
21
1751