From e2eb7f10ff9cbac3171fa3a923fe5a49948a6cb0 Mon Sep 17 00:00:00 2001 From: Matthijs van Henten Date: Mon, 9 Mar 2015 00:33:48 +0000 Subject: [PATCH] move parameter munging in to a single place --- lib/route.js | 113 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 71 insertions(+), 42 deletions(-) diff --git a/lib/route.js b/lib/route.js index 7dcc48b..109ba4a 100644 --- a/lib/route.js +++ b/lib/route.js @@ -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) { @@ -54,6 +55,64 @@ 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. @@ -61,22 +120,19 @@ module.exports = function Route(route, options, handler, types, parent ) { * 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 + "'"); @@ -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;