Skip to content
Myr

Remove app.param(fn) signature

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 3a1f27f · Browse files · 4 files+22 added−60 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+22 added−60 removed

  • tests:+10 added−40 removed
  • other:+12 added−20 removed

4 files+22 added−60 removed

modified:lib/router/index.js+10 added-20 removed
48 unmodified lines
49 // mixin Router class functions50 router.__proto__ = proto;5152 router.params = {};53 router._params = [];53 router.caseSensitive = opts.caseSensitive;54 router.mergeParams = opts.mergeParams;55 router.strict = opts.strict;56 router.stack = [];
35 unmodified lines
92 * @public93 */9495proto.param = function param(name, fn) {97 // param logic98 if (typeof name === 'function') {99 deprecate('router.param(fn): Refactor to use path params');100 this._params.push(name);101 return;96 if (!fn) {97 throw new TypeError('argument fn is required');98 }99104 // apply param functions105 var params = this._params;106 var len = params.length;107 var ret;108109 for (var i = 0; i < len; ++i) {110 if (ret = params[i](name, fn)) {111 fn = ret;112 }100 if (typeof fn !== 'function') {101 throw new TypeError('argument fn must be a function');102 }103115 // ensure we end up with a116 // middleware function117 if ('function' != typeof fn) {118 throw new Error('invalid param() call for ' + name + ', got ' + fn);104 var params = this.params[name];105106 if (!params) {107 params = this.params[name] = [];108 }109121 (this.params[name] = this.params[name] || []).push(fn);110 params.push(fn);111112 return this;113};114115/**
512 unmodified lines
modified:test/app.param.js-40 removed
1 unmodified line
2var express = require('../')3 , request = require('supertest');45describe('app', function(){6 describe('.param(fn)', function(){7 it('should map app.param(name, ...) logic', function(done){8 var app = express();910 app.param(function(name, regexp){11 if (Object.prototype.toString.call(regexp) == '[object RegExp]') { // See #155712 return function(req, res, next, val){13 var captures;14 if (captures = regexp.exec(String(val))) {15 req.params[name] = captures[1];16 next();17 } else {18 next('route');19 }20 }21 }22 })2324 app.param('name', /^([a-zA-Z]+)$/);2526 app.get('/user/:name', function(req, res){27 res.send(req.params.name);28 });2930 request(app)31 .get('/user/tj')32 .expect(200, 'tj', function (err) {33 if (err) return done(err);34 request(app)35 .get('/user/123')36 .expect(404, done);37 });38 })3940 it('should fail if not given fn', function(){41 var app = express();42 app.param.bind(app, 'name', 'bob').should.throw();43 })44 })456 describe('.param(names, fn)', function(){7 it('should map the array', function(done){8 var app = express();9
317 unmodified lines
modified:test/Router.js+10 added
375 unmodified lines
376 })377 })378379 describe('.param', function() {380 it('should require function', function () {381 var router = new Router();382 assert.throws(router.param.bind(router, 'id'), /argument fn is required/);383 });384385 it('should reject non-function', function () {386 var router = new Router();387 assert.throws(router.param.bind(router, 'id', 42), /argument fn must be a function/);388 });389390 it('should call param function when routing VERBS', function(done) {391 var router = new Router();392393 router.param('id', function(req, res, next, id) {
122 unmodified lines
modified:History.md+2 added
1 unmodified line
2===34This incorporates all changes after 4.10.1 up to 4.13.1.56 * remove:7 - `app.param(fn)`8 * change:9 - The leading `:` character in `name` for `app.param(name, fn)` is no longer removed10115.0.0-alpha.1 / 2014-11-06
3012 unmodified lines