Comparing version 2.0.1 to 2.1.0
2.1.0 / 2017-02-15 | ||
================== | ||
* feat: load plugin.default.js rather than plugin.js (#57) | ||
* refactor: seperate router api from app (#55) | ||
2.0.1 / 2017-02-15 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -12,2 +12,3 @@ 'use strict'; | ||
const utils = require('./utils'); | ||
const Router = require('./utils/router'); | ||
@@ -19,2 +20,3 @@ | ||
const CLOSE_PROMISE = Symbol('EggCore#closePromise'); | ||
const ROUTER = Symbol('EggCore#router'); | ||
@@ -270,4 +272,41 @@ class EggCore extends KoaApplication { | ||
} | ||
/** | ||
* get router | ||
* @member {Router} EggCore#router | ||
*/ | ||
get router() { | ||
if (this[ROUTER]) { | ||
return this[ROUTER]; | ||
} | ||
const router = this[ROUTER] = new Router({ sensitive: true }, this); | ||
// register router middleware | ||
this.use(router.middleware()); | ||
return router; | ||
} | ||
/** | ||
* Alias to {@link Router#url} | ||
* @param {String} name - Router name | ||
* @param {Object} params - more parameters | ||
* @return {String} url | ||
*/ | ||
url(name, params) { | ||
return this.router.url(name, params); | ||
} | ||
del(...args) { | ||
this.router.delete(...args); | ||
return this; | ||
} | ||
} | ||
// delegate all router method to application | ||
utils.methods.concat([ 'all', 'resources', 'register', 'redirect' ]).forEach(method => { | ||
EggCore.prototype[method] = function(...args) { | ||
this.router[method](...args); | ||
return this; | ||
}; | ||
}); | ||
module.exports = EggCore; | ||
@@ -274,0 +313,0 @@ |
@@ -59,7 +59,7 @@ 'use strict'; | ||
// loader plugins from application | ||
const appPlugins = this.readPluginConfigs(path.join(this.options.baseDir, 'config/plugin.js')); | ||
const appPlugins = this.readPluginConfigs(path.join(this.options.baseDir, 'config/plugin.default.js')); | ||
debug('Loaded app plugins: %j', Object.keys(appPlugins)); | ||
// loader plugins from framework | ||
const eggPluginConfigPaths = this.eggPaths.map(eggPath => path.join(eggPath, 'config/plugin.js')); | ||
const eggPluginConfigPaths = this.eggPaths.map(eggPath => path.join(eggPath, 'config/plugin.default.js')); | ||
const eggPlugins = this.readPluginConfigs(eggPluginConfigPaths); | ||
@@ -152,11 +152,16 @@ debug('Loaded egg plugins: %j', Object.keys(eggPlugins)); | ||
// read plugin.js and plugins.${env}.js | ||
// read plugin.default.js and plugins.${env}.js | ||
// note: can't use for-of | ||
for (let i = 0, l = configPaths.length; i < l; i++) { | ||
const configPath = configPaths[i]; | ||
configPaths.push(configPath.replace(/plugin.js$/, `plugin.${this.serverEnv}.js`)); | ||
configPaths.push(configPath.replace(/plugin\.default\.js$/, `plugin.${this.serverEnv}.js`)); | ||
} | ||
const plugins = {}; | ||
for (const configPath of configPaths) { | ||
for (let configPath of configPaths) { | ||
// let plugin.js compatible | ||
if (configPath.endsWith('plugin.default.js') && !fs.existsSync(configPath)) { | ||
configPath = configPath.replace(/plugin\.default\.js$/, 'plugin.js'); | ||
} | ||
if (!fs.existsSync(configPath)) { | ||
@@ -163,0 +168,0 @@ continue; |
'use strict'; | ||
const path = require('path'); | ||
const Router = require('../../utils/router'); | ||
@@ -15,8 +14,2 @@ module.exports = { | ||
loadRouter() { | ||
const app = this.app; | ||
const router = new Router({ sensitive: true }, app); | ||
// 注册 Router 的 Middleware | ||
app.use(router.middleware()); | ||
// 加载 router.js | ||
@@ -23,0 +16,0 @@ this.loadFile(path.join(this.options.baseDir, 'app/router.js')); |
@@ -50,4 +50,2 @@ 'use strict'; | ||
const slice = Array.prototype.slice; | ||
class Router extends KoaRouter { | ||
@@ -62,37 +60,31 @@ | ||
super(opts); | ||
this.app = app; | ||
// patch koa-router@5.x | ||
this.patchRouter(app); | ||
this.patchRouterMethod(); | ||
} | ||
patchRouter(app) { | ||
const router = this; | ||
router.app = app; | ||
app.url = router.url.bind(router); | ||
app.router = router; | ||
patchRouterMethod() { | ||
// patch router methods to support async function middleware and string controller | ||
methods.concat([ 'all', 'del', 'resources' ]).forEach(method => { | ||
const orignalMethod = router[method].bind(router); | ||
router[method] = function(...args) { | ||
methods.concat([ 'all' ]).forEach(method => { | ||
this[method] = (...args) => { | ||
const splited = splitedRouterParams(args); | ||
args = splited.prefix.concat(convertMiddlewares(splited.middlewares, app)); | ||
return orignalMethod(...args); | ||
args = splited.prefix.concat(convertMiddlewares(splited.middlewares, this.app)); | ||
return super[method](...args); | ||
}; | ||
}); | ||
} | ||
/** | ||
* Create and register a route. | ||
* @param {String} path - url path | ||
* @param {Array} methods - Array of HTTP verbs | ||
* @param {Array} middlewares - | ||
* @param {Object} opts - | ||
* @return {Route} this | ||
*/ | ||
register(path, methods, middlewares, opts) { | ||
// patch register to support async function middleware and string controller | ||
const register = router.register.bind(router); | ||
router.register = function(path, methods, middlewares, opts) { | ||
middlewares = Array.isArray(middlewares) ? middlewares : [ middlewares ]; | ||
middlewares = convertMiddlewares(middlewares, app); | ||
return register(path, methods, middlewares, opts); | ||
}; | ||
// delegate | ||
methods.concat([ 'all', 'del', 'resources', 'register', 'redirect' ]).forEach(method => { | ||
app[method] = function(...args) { | ||
router[method](...args); | ||
return this; | ||
}; | ||
}); | ||
middlewares = Array.isArray(middlewares) ? middlewares : [ middlewares ]; | ||
middlewares = convertMiddlewares(middlewares, this.app); | ||
return super.register(path, methods, middlewares, opts); | ||
} | ||
@@ -141,12 +133,17 @@ | ||
* ``` | ||
* @return {Route} return route object. | ||
* @return {Router} return route object. | ||
*/ | ||
resources(name, prefix, middleware) { | ||
const route = this; | ||
if (typeof prefix === 'string') { | ||
middleware = slice.call(arguments, 2); | ||
resources(...args) { | ||
const splited = splitedRouterParams(args); | ||
const middleware = convertMiddlewares(splited.middlewares, this.app); | ||
let name = ''; | ||
let prefix = ''; | ||
if (splited.prefix.length === 2) { | ||
// router.get('users', '/users') | ||
name = splited.prefix[0]; | ||
prefix = splited.prefix[1]; | ||
} else { | ||
middleware = slice.call(arguments, 1); | ||
prefix = name; | ||
name = ''; | ||
// router.get('/users') | ||
prefix = splited.prefix[0]; | ||
} | ||
@@ -173,6 +170,6 @@ | ||
const path = opts.suffix ? `${prefix}/${opts.suffix}` : prefix; | ||
route.register.call(this, path, [ opts.method ], middleware.concat(action), { name: formatedName }); | ||
this.register(path, [ opts.method ], middleware.concat(action), { name: formatedName }); | ||
} | ||
return route; | ||
return this; | ||
} | ||
@@ -179,0 +176,0 @@ |
{ | ||
"name": "egg-core", | ||
"version": "2.0.1", | ||
"version": "2.1.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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
72064
1913
1