Skip to content

Commit

Permalink
Merge 11cd07b into ca507ee
Browse files Browse the repository at this point in the history
  • Loading branch information
jonchurch committed Jul 2, 2020
2 parents ca507ee + 11cd07b commit 10d37c7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ Maps the specified path parameter `name` to a specialized param-capturing middle

This function positions the middleware in the same stack as `.use`.

If a `Promise` is returned from the `param_middleware` function, the router
will attach an `onRejected` callback using `.then(null, onRejected)`. If the promise is rejected,
`next` will be called with the rejected value as the error, or `new Error...` if the value is falsy.

Parameter mapping is used to provide pre-conditions to routes
which use normalized placeholders. For example a _:user_id_ parameter
could automatically load a user's information from the database without
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

var flatten = require('array-flatten').flatten
var isPromise = require('is-promise')
var Layer = require('./lib/layer')
var methods = require('methods')
var mixin = require('utils-merge')
Expand Down Expand Up @@ -634,7 +635,12 @@ function processParams (params, layer, called, req, res, done) {
if (!fn) return param()

try {
fn(req, res, paramCallback, paramVal, key.name)
var ret = fn(req, res, paramCallback, paramVal, key.name)
if (isPromise(ret)) {
ret.then(null, function (error) {
paramCallback(error || new Error('Rejected promise'))
})
}
} catch (e) {
paramCallback(e)
}
Expand Down
15 changes: 1 addition & 14 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @private
*/

var isPromise = require('is-promise')
var pathRegexp = require('path-to-regexp')

/**
Expand Down Expand Up @@ -187,20 +188,6 @@ function decodeParam (val) {
}
}

/**
* Returns true if the val is a Promise.
*
* @param {*} val
* @return {boolean}
* @private
*/

function isPromise (val) {
return val &&
typeof val === 'object' &&
typeof val.then === 'function'
}

/**
* Loosens the given path for path-to-regexp matching.
*/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"repository": "pillarjs/router",
"dependencies": {
"array-flatten": "3.0.0",
"is-promise": "4.0.0",
"methods": "~1.1.2",
"parseurl": "~1.3.3",
"path-to-regexp": "3.2.0",
Expand Down
22 changes: 21 additions & 1 deletion test/param.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var after = require('after')
var Router = require('..')
var utils = require('./support/utils')
Expand All @@ -10,6 +9,8 @@ var shouldNotHitHandle = utils.shouldNotHitHandle
var createServer = utils.createServer
var request = utils.request

var describePromises = global.Promise ? describe : describe.skip

describe('Router', function () {
describe('.param(name, fn)', function () {
it('should reject missing name', function () {
Expand Down Expand Up @@ -253,6 +254,25 @@ describe('Router', function () {
.get('/user/bob')
.expect(500, /Error: boom/, done)
})
describePromises('promise support', function () {
it('should catch rejected promises returned from fn', function (done) {
var router = new Router()
var server = createServer(router)

router.param('user', function parseUser (req, res, next, user) {
return Promise.reject(new Error('boom'))
})

router.get('/user/:user', function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('get user ' + req.params.id)
})

request(server)
.get('/user/bob')
.expect(500, /Error: boom/, done)
})
})

describe('next("route")', function () {
it('should cause route with param to be skipped', function (done) {
Expand Down
18 changes: 18 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,24 @@ describe('Router', function () {
.expect(200, 'saw Error: boom!', done)
})
})
it('should invoke error function after router.param returns rejected promise', function (done) {
var router = new Router()
var server = createServer(router)

router.param('user', function (req, res, next, user) {
return Promise.reject(new Error('boom!'))
})

router.get('/:user', function handle (req, res, next) {
res.end()
})

router.use(sawError)

request(server)
.get('/username')
.expect(200, 'saw Error: boom!', done)
})
})

describe('req.baseUrl', function () {
Expand Down

0 comments on commit 10d37c7

Please sign in to comment.