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
Provenance
- Commit
1e2951a- Tree
697f4ab- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
3a1f27f
4 files changed+22 added−60 removed
modified:lib/router/index.js+10 added-20 removed
48 unmodified lines49 // mixin Router class functions50 router.__proto__ = proto;51 52 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 lines92 * @public93 */94 95 proto.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 }99 104- // apply param functions105- var params = this._params;106- var len = params.length;107- var ret;108-109- 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 }103 115- // 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];105+106+ if (!params) {107+ params = this.params[name] = [];108 }109 121- (this.params[name] = this.params[name] || []).push(fn);110+ params.push(fn);111+112 return this;113 };114 115 /**512 unmodified linesmodified:test/app.param.js-40 removed
1 unmodified line2 var express = require('../')3 , request = require('supertest');4 5 describe('app', function(){6- describe('.param(fn)', function(){7- it('should map app.param(name, ...) logic', function(done){8- var app = express();9-10- 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- })23-24- app.param('name', /^([a-zA-Z]+)$/);25-26- app.get('/user/:name', function(req, res){27- res.send(req.params.name);28- });29-30- 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- })39-40- it('should fail if not given fn', function(){41- var app = express();42- app.param.bind(app, 'name', 'bob').should.throw();43- })44- })45-6 describe('.param(names, fn)', function(){7 it('should map the array', function(done){8 var app = express();9 317 unmodified linesmodified:test/Router.js+10 added
375 unmodified lines376 })377 })378 379 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+ });384+385+ 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+ });389+390 it('should call param function when routing VERBS', function(done) {391 var router = new Router();392 393 router.param('id', function(req, res, next, id) {122 unmodified linesmodified:History.md+2 added
1 unmodified line2 ===3 4 This incorporates all changes after 4.10.1 up to 4.13.1.5 6+ * remove:7+ - `app.param(fn)`8 * change:9 - The leading `:` character in `name` for `app.param(name, fn)` is no longer removed10 11 5.0.0-alpha.1 / 2014-11-063012 unmodified lines