@@ -19,8 +19,12 @@ class API {
1919
2020 // Set the version and base paths
2121 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 ( ) : ''
2323 this . _callbackName = props && props . callback ? props . callback . trim ( ) : 'callback'
24+ this . _mimeTypes = props && props . mimeTypes && typeof props . mimeTypes === 'object' ? props . mimeTypes : { }
25+
26+ // Prefix stack w/ base
27+ this . _prefix = this . parseRoute ( this . _base )
2428
2529 // Stores timers for debugging
2630 this . _timers = { }
@@ -56,6 +60,7 @@ class API {
5660
5761 // Testing flag
5862 this . _test = false
63+
5964 } // end constructor
6065
6166 // GET: convenience method
@@ -73,6 +78,11 @@ class API {
7378 this . METHOD ( 'PUT' , path , handler )
7479 }
7580
81+ // PATCH: convenience method
82+ patch ( path , handler ) {
83+ this . METHOD ( 'PATCH' , path , handler )
84+ }
85+
7686 // DELETE: convenience method
7787 delete ( path , handler ) {
7888 this . METHOD ( 'DELETE' , path , handler )
@@ -86,8 +96,14 @@ class API {
8696 // METHOD: Adds method and handler to routes
8797 METHOD ( method , path , handler ) {
8898
99+ // Parse the path
100+ let parsedPath = this . parseRoute ( path )
101+
89102 // Split the route and clean it up
90- let route = path . trim ( ) . replace ( / ^ \/ ( .* ?) ( \/ ) * $ / , '$1' ) . split ( '/' )
103+ let route = this . _prefix . concat ( parsedPath )
104+
105+ // For root path support
106+ if ( route . length === 0 ) { route . push ( '' ) }
91107
92108 // Keep track of path variables
93109 let pathVars = { }
@@ -106,7 +122,7 @@ class API {
106122 // Add the route to the global _routes
107123 this . setRoute (
108124 this . _routes ,
109- ( i === route . length - 1 ? { [ '__' + method . toUpperCase ( ) ] : { vars : pathVars , handler : handler , route : path } } : { } ) ,
125+ ( i === route . length - 1 ? { [ '__' + method . toUpperCase ( ) ] : { vars : pathVars , handler : handler , route : '/' + parsedPath . join ( '/' ) } } : { } ) ,
110126 route . slice ( 0 , i + 1 )
111127 ) ;
112128
@@ -141,6 +157,12 @@ class API {
141157
142158 } ) . catch ( ( e ) => {
143159
160+ // Error messages should never be base64 encoded
161+ response . _isBase64 = false
162+
163+ // Strip the headers (TODO: find a better way to handle this)
164+ response . _headers = { }
165+
144166 let message ;
145167
146168 if ( e instanceof Error ) {
@@ -298,20 +320,11 @@ class API {
298320 // UTILITY FUNCTIONS
299321 //-------------------------------------------------------------------------//
300322
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
323+ parseRoute ( path ) {
324+ return path . trim ( ) . replace ( / ^ \/ ( .* ?) ( \/ ) * $ / , '$1' ) . split ( '/' ) . filter ( x => x . trim ( ) !== '' )
313325 }
314326
327+
315328 setRoute ( obj , value , path ) {
316329 if ( typeof path === "string" ) {
317330 let path = path . split ( '.' )
@@ -354,9 +367,26 @@ class API {
354367 return this . _app
355368 }
356369
370+
371+ // Register routes with options
372+ register ( fn , options ) {
373+
374+ // Extract Prefix
375+ let prefix = options . prefix && options . prefix . toString ( ) . trim ( ) !== '' ?
376+ this . parseRoute ( options . prefix ) : [ ]
377+
378+ // Concat to existing prefix
379+ this . _prefix = this . _prefix . concat ( prefix )
380+
381+ // Execute the routing function
382+ fn ( this , options )
383+
384+ // Remove the last prefix
385+ this . _prefix = this . _prefix . slice ( 0 , - ( prefix . length ) )
386+
387+ } // end register
388+
357389} // end API class
358390
359391// Export the API class
360392module . 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