Navigation Menu

Skip to content

Commit

Permalink
move parameter munging in to a single place
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthijs van Henten committed Mar 9, 2015
1 parent 5ebcff5 commit e2eb7f1
Showing 1 changed file with 71 additions and 42 deletions.
113 changes: 71 additions & 42 deletions lib/route.js
Expand Up @@ -37,14 +37,15 @@ module.exports = function Route(route, options, handler, types, parent ) {
var keys = [];
var params = options.params || {};


for ( var key in parent.params ){
if( params[key] ) continue;
params[key] = parent.params[key];
}

params = normalizeParams(params, 'body', types );
params = normalizeParams(params, types, 'url' );
var routeRe = normalizePath(options.route, keys, params);

function wrapHandler(handler) {
return function(req, res, next) {
handler(req.params || {}, function(err, json, headers, code) {
Expand All @@ -54,29 +55,84 @@ module.exports = function Route(route, options, handler, types, parent ) {
});
};
}

/**
* Param can be:
*
* - object with name: 'type',
* - a String where String === 'name', type: "string"
* - An object where { name: 'name', type: 'type' }
*/

function normalizeParam( def, name, source ){
var param;

source = source || 'url';


// if a simple string, this is the name
if( typeof def === 'string' && !name ){
param = { name: def, type: types.get('string'), source: source };
}
else if( typeof def === 'string' ){
console.log( def, name, '>>> def, name' );
param = { name: name, type: def, source: source };
}
else if ( def instanceof RegExp ){
param = { name: name, type: def, source: source };
}
else {
param = def;
param.source = source;
}

// apply defaults

param.optional = !!param.optional;

// allow regular expressions as types
if (param.type instanceof RegExp)
param.type = new RegExpType(param.type);

if (param.source == "query") {
// query params default to string
param.type = param.type || "string";
}
else if (!param.source || param.source == "body") {
// body params default to json
param.type = param.type || "json";
}
else if (param.source === "url") {
param.type = param.type || "string";
}
else {
throw new Error("parameter source muste be 'url', 'query' or 'body'");
}


return param;
}


/**
* Creates a rgular expression to match this route.
* Url param names are stored in `keys` and the `params` are completed with
* the default values for url parameters.
*/
function normalizePath(path, keys, params) {

path = path
.concat("/?")
.replace(/\/:([\w\.\-\_]+)(\*?)/g, function(match, key, wildcard) {
keys.push(key);

if (!params[key]) {
params[key] = {};
params[key] = normalizeParam( key );
}

// url params default to type string and optional=false
var param = params[key];
param.type = param.type || "string";
param.optional = false;

if (!param.source)
param.source = "url";



if (param.source !== "url")
throw new Error("Url parameters must have 'url' as source but found '" + param.source + "'");

Expand All @@ -92,43 +148,16 @@ module.exports = function Route(route, options, handler, types, parent ) {
return new RegExp('^' + path + '$');
}

function normalizeParams(params, source, types ) {
for (var name in params) {
var param = params[name];
if (typeof param == "string" || param instanceof RegExp)
params[name] = { type: param};
}
function normalizeParams(params, types, source ) {

param.source = param.source || source;


for (var name in params) {
var param = params[name];
var param = normalizeParam( params[name], name, source );


if (param.source == "query") {
// query params default to string
param.type = param.type || "string";
}
else if (!param.source || param.source == "body") {
// body params default to json
param.type = param.type || "json";
param.source = "url";
}
else if (param.source === "url") {
param.type = param.type || "string";
}
else {
throw new Error("parameter source muste be 'url', 'query' or 'body'");
}

// optional defaults to false
param.optional = !!param.optional;

// allow regular expressions as types
if (param.type instanceof RegExp)
param.type = new RegExpType(param.type);

// convert all types to type objects
param.type = types.get(param.type);
params[name] = param;
}

return params;
Expand Down

0 comments on commit e2eb7f1

Please sign in to comment.