@@ -19,9 +19,12 @@ class API {
19
19
20
20
// Set the version and base paths
21
21
this . _version = props && props . version ? props . version : 'v1'
22
- this . _base = props && props . base ? props . base . trim ( ) : ''
22
+ this . _base = props && props . base && typeof props . base === 'string' ? props . base . trim ( ) : ''
23
23
this . _callbackName = props && props . callback ? props . callback . trim ( ) : 'callback'
24
24
25
+ // Prefix stack w/ base
26
+ this . _prefix = this . parseRoute ( this . _base )
27
+
25
28
// Stores timers for debugging
26
29
this . _timers = { }
27
30
this . _procTimes = { }
@@ -56,6 +59,7 @@ class API {
56
59
57
60
// Testing flag
58
61
this . _test = false
62
+
59
63
} // end constructor
60
64
61
65
// GET: convenience method
@@ -86,8 +90,11 @@ class API {
86
90
// METHOD: Adds method and handler to routes
87
91
METHOD ( method , path , handler ) {
88
92
93
+ // Parse the path
94
+ let parsedPath = this . parseRoute ( path )
95
+
89
96
// Split the route and clean it up
90
- let route = path . trim ( ) . replace ( / ^ \/ ( . * ? ) ( \/ ) * $ / , '$1' ) . split ( '/' )
97
+ let route = this . _prefix . concat ( parsedPath )
91
98
92
99
// Keep track of path variables
93
100
let pathVars = { }
@@ -106,7 +113,7 @@ class API {
106
113
// Add the route to the global _routes
107
114
this . setRoute (
108
115
this . _routes ,
109
- ( i === route . length - 1 ? { [ '__' + method . toUpperCase ( ) ] : { vars : pathVars , handler : handler , route : path } } : { } ) ,
116
+ ( i === route . length - 1 ? { [ '__' + method . toUpperCase ( ) ] : { vars : pathVars , handler : handler , route : '/' + parsedPath . join ( '/' ) } } : { } ) ,
110
117
route . slice ( 0 , i + 1 )
111
118
) ;
112
119
@@ -298,20 +305,11 @@ class API {
298
305
// UTILITY FUNCTIONS
299
306
//-------------------------------------------------------------------------//
300
307
301
- deepFind ( obj , path ) {
302
- let paths = path //.split('.'),
303
- let current = obj
304
-
305
- for ( let i = 0 ; i < paths . length ; ++ i ) {
306
- if ( current [ paths [ i ] ] == undefined ) {
307
- return undefined
308
- } else {
309
- current = current [ paths [ i ] ]
310
- }
311
- }
312
- return current
308
+ parseRoute ( path ) {
309
+ return path . trim ( ) . replace ( / ^ \/ ( .* ?) ( \/ ) * $ / , '$1' ) . split ( '/' ) . filter ( x => x . trim ( ) !== '' )
313
310
}
314
311
312
+
315
313
setRoute ( obj , value , path ) {
316
314
if ( typeof path === "string" ) {
317
315
let path = path . split ( '.' )
@@ -354,9 +352,26 @@ class API {
354
352
return this . _app
355
353
}
356
354
355
+
356
+ // Register routes with options
357
+ register ( fn , options ) {
358
+
359
+ // Extract Prefix
360
+ let prefix = options . prefix && options . prefix . toString ( ) . trim ( ) !== '' ?
361
+ this . parseRoute ( options . prefix ) : [ ]
362
+
363
+ // Concat to existing prefix
364
+ this . _prefix = this . _prefix . concat ( prefix )
365
+
366
+ // Execute the routing function
367
+ fn ( this , options )
368
+
369
+ // Remove the last prefix
370
+ this . _prefix = this . _prefix . slice ( 0 , - ( prefix . length ) )
371
+
372
+ } // end register
373
+
357
374
} // end API class
358
375
359
376
// Export the API class
360
377
module . exports = opts => new API ( opts )
361
-
362
- // console.error('DEPRECATED: constructor method. Use require(\'lambda-api\')({ version: \'v1.0\', base: \'v1\' }) to initialize the framework instead')
0 commit comments