think-router
Advanced tools
Comparing version 1.3.2 to 1.3.3
{ | ||
"name": "think-router", | ||
"description": "Router for ThinkJS", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "welefen", |
@@ -176,4 +176,3 @@ const helper = require('think-helper'); | ||
pathname = pos === -1 ? '' : pathname.slice(pos + 1); | ||
} | ||
if (m === '' || controllers[m] === undefined) { | ||
} else { | ||
m = this.options.defaultModule; | ||
@@ -191,3 +190,5 @@ } | ||
let controller = ''; | ||
// only check multiple layer controller, because single layer can get controller from pathname | ||
for (const name in controllers) { | ||
if (name.indexOf('/') === -1) break; // if single layer, break the loop | ||
if (name === pathname || pathname.indexOf(`${name}/`) === 0) { | ||
@@ -199,5 +200,8 @@ controller = name; | ||
} | ||
if ((controller && controllers[controller] === undefined) || !controller) { | ||
controller = this.options.defaultController; | ||
if (controller === '') { | ||
const pos = pathname.indexOf('/'); | ||
controller = pos === -1 ? pathname : pathname.slice(0, pos); | ||
pathname = pos === -1 ? '' : pathname.slice(pos + 1); | ||
} | ||
controller = controller || this.options.defaultController; | ||
return { controller, pathname }; | ||
@@ -209,9 +213,7 @@ } | ||
*/ | ||
parseAction({ pathname, controllers, controller, ruleMethod }) { | ||
parseAction({ pathname, ruleMethod }) { | ||
let action = ''; | ||
pathname = pathname.split('/'); | ||
action = ruleMethod === 'REST' ? this.ctxMethod.toLowerCase() : pathname[0]; | ||
if (action === '' || (controller && controllers[controller] === undefined) || (controller && action && this.controllers[controller] && this.controllers[controller][`${action}Action`] === undefined)) { | ||
action = this.options.defaultAction; | ||
} | ||
action = action || this.options.defaultAction; | ||
return { action }; | ||
@@ -250,16 +252,14 @@ } | ||
} | ||
// parse module when in multi module | ||
let controllers = this.controllers; | ||
const parseModuleResult = this.parseModule({ pathname, controllers }); | ||
const { m } = parseModuleResult; | ||
controllers = parseModuleResult.controllers; | ||
// parse module | ||
const parseModuleResult = this.parseModule({ pathname, controllers: this.controllers }); | ||
const { m, controllers } = parseModuleResult; | ||
pathname = parseModuleResult.pathname; | ||
// parse controller | ||
let controller = ''; | ||
const parseControllerResult = this.parseController({ pathname, controllers }); | ||
controller = parseControllerResult.controller; | ||
const { controller } = parseControllerResult; | ||
pathname = parseControllerResult.pathname; | ||
// parse action | ||
const { action } = this.parseAction({ pathname, controllers, controller, ruleMethod }); | ||
const { action } = this.parseAction({ pathname, ruleMethod }); | ||
// wirte back to ctx | ||
@@ -266,0 +266,0 @@ this.ctx.module = m; |
@@ -717,5 +717,5 @@ /* | ||
t.deepEqual(RESULT, { | ||
module: 'defaultModule', | ||
controller: 'defaultController', | ||
action: 'defaultAction' | ||
module: 'admin', | ||
controller: 'article', | ||
action: 'list' | ||
}); | ||
@@ -984,3 +984,2 @@ t.end(); | ||
test.serial.cb('use defaultAction', t => { | ||
@@ -1309,1 +1308,35 @@ const options = helper.extend({}, defaultOptions); | ||
}); | ||
test.serial.cb('emptycontroller', t => { | ||
const options = helper.extend({}, defaultOptions, { | ||
defaultController: '' | ||
}); | ||
const ctx = helper.extend({}, defaultCtx); | ||
const app = helper.extend({}, defaultApp, { | ||
modules: [], | ||
controllers: { | ||
'admin/article/list2/': { | ||
// articleAction() {} | ||
}, | ||
admin: { | ||
}, | ||
admin2: { | ||
} | ||
}, | ||
routers: [ | ||
['^/admin/article/list', 'admin/article/list', 'get'] | ||
] | ||
}); | ||
ctx.app = app; | ||
parseRouter(options, app)(ctx, next).then(data => { | ||
t.deepEqual(RESULT, { | ||
module: '', | ||
controller: 'admin', | ||
action: 'article' | ||
}); | ||
t.end(); | ||
}); | ||
}); |
44318
1629