mirror of
https://github.com/zebrajr/express.git
synced 2025-12-06 00:19:48 +01:00
Remove app.param(fn) signature
This commit is contained in:
parent
3a1f27fcde
commit
1e2951a832
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
This incorporates all changes after 4.10.1 up to 4.13.1.
|
||||
|
||||
* remove:
|
||||
- `app.param(fn)`
|
||||
* change:
|
||||
- The leading `:` character in `name` for `app.param(name, fn)` is no longer removed
|
||||
|
||||
|
|
|
|||
|
|
@ -50,7 +50,6 @@ var proto = module.exports = function(options) {
|
|||
router.__proto__ = proto;
|
||||
|
||||
router.params = {};
|
||||
router._params = [];
|
||||
router.caseSensitive = opts.caseSensitive;
|
||||
router.mergeParams = opts.mergeParams;
|
||||
router.strict = opts.strict;
|
||||
|
|
@ -94,31 +93,22 @@ var proto = module.exports = function(options) {
|
|||
*/
|
||||
|
||||
proto.param = function param(name, fn) {
|
||||
// param logic
|
||||
if (typeof name === 'function') {
|
||||
deprecate('router.param(fn): Refactor to use path params');
|
||||
this._params.push(name);
|
||||
return;
|
||||
if (!fn) {
|
||||
throw new TypeError('argument fn is required');
|
||||
}
|
||||
|
||||
// apply param functions
|
||||
var params = this._params;
|
||||
var len = params.length;
|
||||
var ret;
|
||||
|
||||
for (var i = 0; i < len; ++i) {
|
||||
if (ret = params[i](name, fn)) {
|
||||
fn = ret;
|
||||
}
|
||||
if (typeof fn !== 'function') {
|
||||
throw new TypeError('argument fn must be a function');
|
||||
}
|
||||
|
||||
// ensure we end up with a
|
||||
// middleware function
|
||||
if ('function' != typeof fn) {
|
||||
throw new Error('invalid param() call for ' + name + ', got ' + fn);
|
||||
var params = this.params[name];
|
||||
|
||||
if (!params) {
|
||||
params = this.params[name] = [];
|
||||
}
|
||||
|
||||
(this.params[name] = this.params[name] || []).push(fn);
|
||||
params.push(fn);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -377,6 +377,16 @@ describe('Router', function(){
|
|||
})
|
||||
|
||||
describe('.param', function() {
|
||||
it('should require function', function () {
|
||||
var router = new Router();
|
||||
assert.throws(router.param.bind(router, 'id'), /argument fn is required/);
|
||||
});
|
||||
|
||||
it('should reject non-function', function () {
|
||||
var router = new Router();
|
||||
assert.throws(router.param.bind(router, 'id', 42), /argument fn must be a function/);
|
||||
});
|
||||
|
||||
it('should call param function when routing VERBS', function(done) {
|
||||
var router = new Router();
|
||||
|
||||
|
|
|
|||
|
|
@ -3,46 +3,6 @@ var express = require('../')
|
|||
, request = require('supertest');
|
||||
|
||||
describe('app', function(){
|
||||
describe('.param(fn)', function(){
|
||||
it('should map app.param(name, ...) logic', function(done){
|
||||
var app = express();
|
||||
|
||||
app.param(function(name, regexp){
|
||||
if (Object.prototype.toString.call(regexp) == '[object RegExp]') { // See #1557
|
||||
return function(req, res, next, val){
|
||||
var captures;
|
||||
if (captures = regexp.exec(String(val))) {
|
||||
req.params[name] = captures[1];
|
||||
next();
|
||||
} else {
|
||||
next('route');
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
app.param('name', /^([a-zA-Z]+)$/);
|
||||
|
||||
app.get('/user/:name', function(req, res){
|
||||
res.send(req.params.name);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/user/tj')
|
||||
.expect(200, 'tj', function (err) {
|
||||
if (err) return done(err);
|
||||
request(app)
|
||||
.get('/user/123')
|
||||
.expect(404, done);
|
||||
});
|
||||
})
|
||||
|
||||
it('should fail if not given fn', function(){
|
||||
var app = express();
|
||||
app.param.bind(app, 'name', 'bob').should.throw();
|
||||
})
|
||||
})
|
||||
|
||||
describe('.param(names, fn)', function(){
|
||||
it('should map the array', function(done){
|
||||
var app = express();
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user