Skip to content

Commit

Permalink
Removed app.param() callback with retval support
Browse files Browse the repository at this point in the history
this is now abstract and will be going into express-params
along with some other cases
  • Loading branch information
tj committed May 23, 2011
1 parent 4d12292 commit e78dc18
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 92 deletions.
30 changes: 3 additions & 27 deletions lib/http.js
Expand Up @@ -315,30 +315,9 @@ app.dynamicHelpers = function(obj){
* Map the given param placeholder `name`(s) to the given callback `fn`.
*
* Param mapping is used to provide pre-conditions to routes
* which us normalized placeholders. For example ":user_id" may
* attempt to load the user from the database, where as ":num" may
* pass the value through `parseInt(num, 10)`.
*
* When the callback function accepts only a single argument, the
* value of placeholder is passed:
*
* app.param('page', function(n){ return parseInt(n, 10); });
*
* If we do not with to supply the `parseInt` radix, we could simply do:
*
* app.param('page', parseInt);
*
* Or use `Number`:
*
* app.param('page', Number);
*
* After which "/users/:page" would automatically provide us with
* an integer for `req.params.page`. If desired we could use the callback
* signature shown below, and immediately `next(new Error('invalid page'))`
* when `parseInt` fails.
*
* Alternatively the callback may accept the request, response, next, and
* the value, acting like middlware:
* which us normalized placeholders. This callback has the same
* signature as regular middleware, for example below when ":userId"
* is used this function will be invoked in an attempt to load the user.
*
* app.param('userId', function(req, res, next, id){
* User.find(id, function(err, user){
Expand All @@ -353,9 +332,6 @@ app.dynamicHelpers = function(obj){
* });
* });
*
* Now every time ":userId" is present, the associated user object
* will be loaded and assigned before the route handler is invoked.
*
* @param {String|Array|Function} name
* @param {Function} fn
* @return {Server} for chaining
Expand Down
16 changes: 0 additions & 16 deletions lib/router/index.js
Expand Up @@ -44,22 +44,6 @@ function Router(app) {
this.middleware = function(req, res, next){
self._dispatch(req, res, next);
};

// treat functions with arity < 3
// to simply return a value
this.param(function(name, fn){
if (fn.length < 3) {
return function(req, res, next){
var val = req.params[name];
val = req.params[name] = fn(val);
if (invalidParamReturnValue(val)) {
next('route');
} else {
next();
}
};
}
})
}

/**
Expand Down
49 changes: 0 additions & 49 deletions test/router.test.js
Expand Up @@ -117,9 +117,6 @@ module.exports = {
, { name: 'bandit' }
];

function integer(n){ return parseInt(n, 10); };
app.param(['to', 'from'], integer);

app.param('user', function(req, res, next, id){
if (req.user = users[id]) {
next();
Expand All @@ -132,59 +129,13 @@ module.exports = {
res.send('user ' + req.user.name);
});

app.get('/users/:from-:to', function(req, res, next){
var names = users.slice(req.params.from, req.params.to).map(function(user){
return user.name;
});
res.send('users ' + names.join(', '));
});

assert.response(app,
{ url: '/user/0' },
{ body: 'user tj' });

assert.response(app,
{ url: '/user/1' },
{ body: 'user tobi' });

assert.response(app,
{ url: '/users/0-3' },
{ body: 'users tj, tobi, loki' });
},

'test .param() ret val': function(){
var app = express.createServer();

app.param('uid', parseInt);

app.get('/user/:uid', function(req, res, next){
var id = req.params.uid;
res.send('loaded user ' + id + ' as typeof ' + typeof id);
});

app.param('name', function(name){
var captures;
if (captures = /^\w[\w\d]+/.exec(name)) {
return captures[0];
}
});

app.get('/user/:name', function(req, res, next){
var name = req.params.name;
res.send('loaded user ' + name);
});

assert.response(app,
{ url: '/user/0' },
{ body: 'loaded user 0 as typeof number' });

assert.response(app,
{ url: '/user/tj' },
{ body: 'loaded user tj' });

assert.response(app,
{ url: '/user/t' },
{ status: 404 });
},

'test OPTIONS': function(){
Expand Down

0 comments on commit e78dc18

Please sign in to comment.