Skip to content

Commit

Permalink
refactor routeParams and params property creation to be done in the r…
Browse files Browse the repository at this point in the history
…equest constructor

move logic to get request parameters for a route into a method on the route
  • Loading branch information
nrstott committed Feb 6, 2013
1 parent 363faac commit b6f8a9c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
9 changes: 9 additions & 0 deletions lib/request.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ module.exports = function(router, jsgiReq) {
get: function() { get: function() {
return !util.no(this.headers['x-requested-with']); return !util.no(this.headers['x-requested-with']);
} }
},
routeParams: {
value: {},
enumerable: true
},
params: {
get: function() {
return util.merge({}, this.routeParams, this.search, this.body);
}
} }
}); });
}; };
48 changes: 28 additions & 20 deletions lib/router.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Router.prototype.route = function(method, path /*, handlers... */) {
path = new RegExp("^"+path.replace(/\./, '\\.').replace(/\*/g, '(.+)').replace(PATH_PARAMETERS, PATH_PARAMETER_REPLACEMENT)+'$'); path = new RegExp("^"+path.replace(/\./, '\\.').replace(/\*/g, '(.+)').replace(PATH_PARAMETERS, PATH_PARAMETER_REPLACEMENT)+'$');
} }


route = { path: path, paramNames: paramNames, apps: apps, originalPath: originalPath }; route = makeRoute({ path: path, paramNames: paramNames, apps: apps, originalPath: originalPath });


this.emit(exports.bogartEvent.BEFORE_ADD_ROUTE, this, route); this.emit(exports.bogartEvent.BEFORE_ADD_ROUTE, this, route);


Expand Down Expand Up @@ -182,31 +182,13 @@ Router.prototype.respond = function(reqPromise) {


return when(reqPromise, function(req) { return when(reqPromise, function(req) {
var route = self.handler(req.method, req.pathInfo) var route = self.handler(req.method, req.pathInfo)
, routeParams = {}
, routeParamValues = null; , routeParamValues = null;


if (util.no(route)) { if (util.no(route)) {
return null; return null;
} }


routeParamValues = route.path.exec(req.pathInfo); routeParamValues = route.getParamValuesFromRequest(req);
if (routeParamValues) {
routeParamValues.shift(); // Remove the initial match

routeParamValues.forEach(function(val, indx) {
val = decodeURIComponent(val);
if (route.paramNames && route.paramNames.length > indx) {
routeParams[route.paramNames[indx]] = val;
} else if (val !== undefined) {
routeParams.splat = routeParams.splat || [];
routeParams.splat.push(val);
}
});
}

Object.defineProperty(req, 'routeParams', { value: routeParams, enumerable: true, readonly: true });

Object.defineProperty(req, 'params', { value: util.merge({}, req.routeParams, req.search, req.body), enumerable: true, readonly: true });


/* Invoke all 'before' functions and save any promises. */ /* Invoke all 'before' functions and save any promises. */
var promiseArray = []; var promiseArray = [];
Expand Down Expand Up @@ -293,4 +275,30 @@ function thenResolve(val) {
return function() { return function() {
return val; return val;
} }
}

function makeRoute(proto) {
return Object.create(proto, {
getParamValuesFromRequest: {
value: function(req) {
var route = this
, routeParamValues = route.path.exec(req.pathInfo);

if (routeParamValues) {
routeParamValues.shift(); // Remove the initial match

routeParamValues
.map(decodeURIComponent)
.forEach(function(val, indx) {
if (route.paramNames && route.paramNames.length > indx) {
req.routeParams[route.paramNames[indx]] = val;
} else if (val !== undefined) {
req.routeParams.splat = req.routeParams.splat || [];
req.routeParams.splat.push(val);
}
});
}
}
}
});
} }

0 comments on commit b6f8a9c

Please sign in to comment.