Added support for app.param(fn) to define param logic
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent d6ef90d · Browse files · 2 files·+46 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+46 added−13 removed
- other:+46 added−13 removed
Provenance
- Commit
b1a7310- Tree
908d4e4- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
d6ef90d
2 files changed+46 added−13 removed
modified:lib/router/index.js+39 added-12 removed
37 unmodified lines38 var self = this;39 this.app = app;40 this.routes = {};41 this.params = {};42+ this._params = [];43+44 this.middleware = function(req, res, next){45 self._dispatch(req, res, next);46 };47+48+ // treat functions with arity < 349+ // to simply return a value50+ this.param(function(name, fn){51+ if (fn.length < 3) {52+ return function(req, res, next){53+ var val = req.params[name];54+ val = req.params[name] = fn(val);55+ if (invalidParamReturnValue(val)) {56+ next('route');57+ } else {58+ next();59+ }60+ };61+ }62+ })63 }64 65 /**66 * Register a param callback `fn` for the given `name`.67 *50- * @param {String} name68+ * @param {String|Function} name69 * @param {Function} fn70 * @return {Router} for chaining71 * @api public72 */73 74 Router.prototype.param = function(name, fn){57- var callback = fn;75+ // param logic76+ if ('function' == typeof name) {77+ this._params.push(name);78+ return;79+ }80 59- if (fn.length < 3) {60- fn = function(req, res, next){61- var val = req.params[name];62- val = req.params[name] = callback(val);63- if (invalidParamReturnValue(val)) {64- next('route');65- } else {66- next();67- }68- };81+ // apply param functions82+ var params = this._params83+ , len = params.length84+ , ret;85+86+ for (var i = 0; i < len; ++i) {87+ if (ret = params[i](name, fn)) {88+ fn = ret;89+ }90 }91 92+ // ensure we end up with a93+ // middleware function94+ if ('function' != typeof fn) {95+ throw new Error('invalid param() call for ' + name + ', got ' + fn);96+ }97+98 this.params[name] = fn;99 return this;100 };101 316 unmodified linesmodified:lib/http.js+7 added-1 removed
346 unmodified lines347 *348 * Now every time ":userId" is present, the associated user object349 * will be loaded and assigned before the route handler is invoked.350 *351- * @param {String|Array} name351+ * @param {String|Array|Function} name352 * @param {Function} fn353 * @return {Server} for chaining354 * @api public355 */356 357 app.param = function(name, fn){358+ // array359 if (Array.isArray(name)) {360 name.forEach(function(name){361 this.param(name, fn);362 }, this);363+ // param logic364+ } else if ('function' == typeof name) {365+ this.routes.param(name);366+ // single367 } else {368 if (':' == name[0]) name = name.substr(1);369 this.routes.param(name, fn);370 }371+372 return this;373 };374 375 /**165 unmodified lines