Skip to content

Commit

Permalink
router.use(middlewareArray) should work
Browse files Browse the repository at this point in the history
  • Loading branch information
nervgh committed May 4, 2017
1 parent 057bfe7 commit ff6f6ee
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
13 changes: 7 additions & 6 deletions lib/Router.js
Expand Up @@ -11,6 +11,7 @@ const {
isFunction,
isObject,
isString,
isUndefined,
noop
} = require('./lang')

Expand Down Expand Up @@ -45,11 +46,11 @@ class Router {
* @returns {Router}
*/
addRoute(method, paths, ...middleware) {
if (isFunction(paths) || (isArray(paths) && isFunction(paths[0]))) {
middleware.push(...flatten([paths]))
this[__ADD_ROUTE](method, undefined, middleware)
} else {
if (isString(paths) || (isArray(paths) && isString(paths[0]))) {
this[__ADD_ROUTE](method, paths, middleware)
} else {
middleware.push(paths)
this[__ADD_ROUTE](method, undefined, middleware)
}
return this
}
Expand Down Expand Up @@ -84,7 +85,7 @@ class Router {
}
/**
* @param {String} method
* @param {String|Array<String>} paths
* @param {String|Array<String>|undefined} paths
* @param {Function|Array<Function>} middleware
*/
[__ADD_ROUTE](method, paths, middleware) {
Expand All @@ -97,7 +98,7 @@ class Router {
return
}

if (method === 'ANY' || !paths) {
if (isUndefined(paths)) {
this.methods.add(method, stack)
return
}
Expand Down
8 changes: 8 additions & 0 deletions lib/lang.js
Expand Up @@ -19,6 +19,13 @@ function isObject(any) {
function isString(any) {
return typeof any === 'string'
}
/**
* @param {*} any
* @returns {Boolean}
*/
function isUndefined(any) {
return undefined === any
}
/**
* No operations
*/
Expand All @@ -35,4 +42,5 @@ exports.isArray = isArray
exports.isFunction = isFunction
exports.isObject = isObject
exports.isString = isString
exports.isUndefined = isUndefined
exports.noop = noop
4 changes: 2 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "koa-trie-router",
"description": "Trie-routing for Koa",
"version": "2.1.2",
"version": "2.1.3",
"author": {
"name": "Jonathan Ong",
"email": "me@jongleberry.com",
Expand All @@ -22,7 +22,7 @@
"devDependencies": {
"assert-request": "^1.0.6",
"istanbul": "^0.4.5",
"koa": "^2.0.0-alpha.8",
"koa": "^2.2.0",
"koa-mount": "^2.0.0",
"mocha": "^3.2.0",
"should": "^11.2.0"
Expand Down
36 changes: 36 additions & 0 deletions test/routes.js
Expand Up @@ -83,6 +83,24 @@ describe('router.use()', function () {
.get('/')
.status(204)
})
it('should work with an array of middleware', function () {
router.use([
function (ctx, next) {
ctx.status = 202
next()
},
function (ctx, next) {
ctx.status += 1
next()
},
function (ctx) {
ctx.status += 1
}
])
return request
.get('/one')
.status(204)
})
it('should working with ctx.params if middleware with params were defined', function () {
router.use(function (ctx) {
ctx.params.a.should.equal('one')
Expand Down Expand Up @@ -137,6 +155,24 @@ describe('router[method]()', function () {
.get('/one')
.status(204)
})
it('should work with an array of middleware', function () {
router.get([
function (ctx, next) {
ctx.status = 202
next()
},
function (ctx, next) {
ctx.status += 1
next()
},
function (ctx) {
ctx.status += 1
}
])
return request
.get('/one')
.status(204)
})
})


Expand Down

0 comments on commit ff6f6ee

Please sign in to comment.