@@ -265,32 +265,33 @@ const createEndpoint = (args, verb) => {
265265 if ( ! args || ! args . length )
266266 throw new Error ( `Missing required argument. Impossible to create an ${ httpVerb } endpoint without any argument. Pass at least one function similar to (req, res) => ...` )
267267
268- const firstArgType = typeof ( args [ 0 ] )
269- const firstArgIsArray = firstArgType == 'object' && args [ 0 ] . length != undefined
268+ const firstArgument = args [ 0 ]
269+ const firstArgType = typeof ( firstArgument )
270+ const firstArgIsArray = firstArgType == 'object' && firstArgument . length != undefined
270271 // Case 1 - Single arguments. It must be a function
271272 if ( args . length == 1 ) {
272273 if ( firstArgType != 'function' && firstArgType != 'string' && ! firstArgIsArray )
273274 throw new Error ( `Wrong argument exception. When only one argument is passed to an ${ httpVerb } endpoint, then that argument must be either be a string (endpoint path), an array of strings (collection of endpoint path) or a function similar to (req, res) => ...` )
274275
275- if ( firstArgIsArray && args [ 0 ] . some ( x => typeof ( x ) != 'string' ) )
276+ if ( firstArgIsArray && firstArgument . some ( x => typeof ( x ) != 'string' ) )
276277 throw new Error ( `Wrong argument exception. When the first argument passed to create an ${ httpVerb } endpoint is an array, that array must be made of string only.` )
277278
278279 _endpoints . push ( {
279- routes : getRouteDetails ( firstArgType == 'function' ? '/' : args [ 0 ] ) ,
280+ routes : getRouteDetails ( firstArgType == 'function' ? '/' : firstArgument ) ,
280281 method : verb ,
281- execute : ( req , res ) => executeHandlers ( req , res , [ fnToPromise ( args [ 0 ] ) ] )
282+ execute : ( req , res ) => executeHandlers ( req , res , [ fnToPromise ( firstArgType != 'string' ? firstArgument : ( ) => null ) ] )
282283 } )
283284 }
284285 // Case 2 - Two or more arguments. Unless the first argument is a path ('string'), then they should all be functions
285286 else {
286- const firstArgType = typeof ( args [ 0 ] )
287- const firstArgIsArray = firstArgType == 'object' && args [ 0 ] . length != undefined
287+ const firstArgType = typeof ( firstArgument )
288+ const firstArgIsArray = firstArgType == 'object' && firstArgument . length != undefined
288289 let p , handlerFns , idxOffset
289290 if ( firstArgType == 'string' || firstArgIsArray ) {
290291 [ p , ...handlerFns ] = args
291292 idxOffset = 2
292293
293- if ( firstArgIsArray && args [ 0 ] . some ( x => typeof ( x ) != 'string' ) )
294+ if ( firstArgIsArray && firstArgument . some ( x => typeof ( x ) != 'string' ) )
294295 throw new Error ( `Wrong argument exception. When the first argument passed to create an ${ httpVerb } endpoint is an array, that array must be made of string only.` )
295296 }
296297 else {
@@ -336,6 +337,13 @@ const processEvent = (req, res, config={}, endpoints=[], handlers=[], requiredHe
336337 req . __receivedTime = Date . now ( )
337338 req . __transactionId = shortid . generate ( ) . replace ( / - / g, 'r' ) . replace ( / _ / g, '9' )
338339 req . __ellapsedMillis = ( ) => Date . now ( ) - req . __receivedTime
340+ let { propName :paramsPropName = 'params' , paramsMode= null } = config . params || { }
341+ // Ensure backward compatibility with version 0.12.1-alpha.0
342+ if ( config . paramsMode && ! paramsMode )
343+ paramsMode = config . paramsMode
344+
345+ if ( ! req [ paramsPropName ] || typeof ( req [ paramsPropName ] ) != 'object' )
346+ req [ paramsPropName ] = { }
339347
340348 // 1. Make sure the pre and post event methods exist
341349 if ( ! preEvent )
@@ -380,15 +388,13 @@ const processEvent = (req, res, config={}, endpoints=[], handlers=[], requiredHe
380388 return res . status ( 404 ) . send ( `Endpoint '${ pathname } ' for method ${ httpMethod } not found.` )
381389
382390 // 5.4. Extract all params from that request, including both the url route params and the payload params.
383- const paramsMode = PARAMSMODE [ config . paramsMode ] ? config . paramsMode : 'all'
384- const paramts = paramsMode == 'all' || paramsMode == 'route' ? Object . assign ( { } , endpoint . winningRoute . parameters ) : { }
385- const getParams = paramsMode == 'all' || paramsMode == 'body' ? reqUtil . getParams ( req ) : Promise . resolve ( { } )
391+ const validParamsMode = PARAMSMODE [ paramsMode ] ? paramsMode : 'all'
392+ const paramts = validParamsMode == 'all' || validParamsMode == 'route' ? Object . assign ( { } , endpoint . winningRoute . parameters ) : { }
393+ const getParams = validParamsMode == 'all' || validParamsMode == 'body' ? reqUtil . getParams ( req ) : Promise . resolve ( { } )
386394 return getParams . then ( parameters => Object . assign ( parameters , paramts ) )
387395 . then ( parameters => {
388396 // 5.5. Add all paramaters to the request object
389- if ( ! req . params || typeof ( req . params ) != 'object' )
390- req . params = { }
391- Object . assign ( req . params , parameters || { } )
397+ Object . assign ( req [ paramsPropName ] , parameters || { } )
392398 // 5.6. Process all global handlers
393399 return executeHandlers ( req , res , handlers )
394400 // 5.8. Process the endpoint
0 commit comments