claudia-api-builder
Advanced tools
Comparing version 0.2.0 to 0.3.0
{ | ||
"name": "claudia-api-builder", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Simplify AWS ApiGateway handling", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -8,23 +8,26 @@ /*global module */ | ||
['GET', 'POST', 'PUT'].forEach(function (method) { | ||
self[method.toLowerCase()] = function (route, handler) { | ||
var pathPart = route.replace(/^\//, ''); | ||
self[method.toLowerCase()] = function (route, handler, options) { | ||
var pathPart = route.replace(/^\//, '').toLowerCase(), | ||
canonicalRoute = route.toLowerCase(); | ||
if (!/^\//.test(canonicalRoute)) { | ||
canonicalRoute = '/' + route; | ||
} | ||
if (!methodConfigurations[pathPart]) { | ||
methodConfigurations[pathPart] = { methods: [] }; | ||
methodConfigurations[pathPart] = {} ; | ||
} | ||
if (methodConfigurations[pathPart].methods.indexOf(method) === -1) { | ||
methodConfigurations[pathPart].methods.push(method); | ||
methodConfigurations[pathPart][method] = (options || {}); | ||
if (!routes[canonicalRoute]) { | ||
routes[canonicalRoute] = {}; | ||
} | ||
if (!routes[route]) { | ||
routes[route] = {}; | ||
} | ||
routes[route][method] = handler; | ||
routes[canonicalRoute][method] = handler; | ||
}; | ||
}); | ||
self.apiConfig = function () { | ||
return methodConfigurations; | ||
return {version: 2, routes: methodConfigurations}; | ||
}; | ||
self.router = function (event, context) { | ||
var handler, result; | ||
var handler, result, path; | ||
if (event && event.context && event.context.path && event.context.method) { | ||
handler = routes[event.context.path] && routes[event.context.path][event.context.method]; | ||
path = event.context.path.toLowerCase(); | ||
handler = routes[path] && routes[path][event.context.method]; | ||
if (handler) { | ||
@@ -31,0 +34,0 @@ try { |
5357
56