Skip to content

Commit bc2a00b

Browse files
committed
add register function to close #11, rework base property
1 parent 4e0063e commit bc2a00b

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

index.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +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'
2424

25+
// Prefix stack w/ base
26+
this._prefix = this.parseRoute(this._base)
27+
2528
// Stores timers for debugging
2629
this._timers = {}
2730
this._procTimes = {}
@@ -56,6 +59,7 @@ class API {
5659

5760
// Testing flag
5861
this._test = false
62+
5963
} // end constructor
6064

6165
// GET: convenience method
@@ -86,8 +90,11 @@ class API {
8690
// METHOD: Adds method and handler to routes
8791
METHOD(method, path, handler) {
8892

93+
// Parse the path
94+
let parsedPath = this.parseRoute(path)
95+
8996
// Split the route and clean it up
90-
let route = path.trim().replace(/^\/(.*?)(\/)*$/,'$1').split('/')
97+
let route = this._prefix.concat(parsedPath)
9198

9299
// Keep track of path variables
93100
let pathVars = {}
@@ -106,7 +113,7 @@ class API {
106113
// Add the route to the global _routes
107114
this.setRoute(
108115
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('/') } } : {}),
110117
route.slice(0,i+1)
111118
);
112119

@@ -298,20 +305,11 @@ class API {
298305
// UTILITY FUNCTIONS
299306
//-------------------------------------------------------------------------//
300307

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() !== '')
313310
}
314311

312+
315313
setRoute(obj, value, path) {
316314
if (typeof path === "string") {
317315
let path = path.split('.')
@@ -354,9 +352,26 @@ class API {
354352
return this._app
355353
}
356354

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+
357374
} // end API class
358375

359376
// Export the API class
360377
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')

request.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ class REQUEST {
6767
// Extract path from event (strip querystring just in case)
6868
let path = app._event.path.trim().split('?')[0].replace(/^\/(.*?)(\/)*$/,'$1').split('/')
6969

70-
// Remove base if it exists
71-
if (app._base && app._base === path[0]) {
72-
path.shift()
73-
} // end remove base
70+
// // Remove base if it exists
71+
// if (app._base && app._base === path[0]) {
72+
// path.shift()
73+
// } // end remove base
7474

7575
// Init the route
7676
this.route = null

0 commit comments

Comments
 (0)