@middy/http-router
Advanced tools
Comparing version 3.0.0-alpha.2 to 3.0.0-alpha.3
107
index.js
@@ -1,62 +0,57 @@ | ||
import { createError } from '@middy/util'; | ||
import { createError } from '@middy/util' | ||
const httpRouteHandler = routes => { | ||
const routesStatic = {}; | ||
const routesDynamic = {}; | ||
const enumMethods = methods.concat('ANY'); | ||
const httpRouteHandler = (routes) => { | ||
const routesStatic = {} | ||
const routesDynamic = {} | ||
const enumMethods = methods.concat('ANY') | ||
for (const route of routes) { | ||
let { | ||
method, | ||
path, | ||
handler | ||
} = route; | ||
let { method, path, handler } = route | ||
// Prevents `routesType[method][path] = handler` from flagging: This assignment may alter Object.prototype if a malicious '__proto__' string is injected from library input. | ||
if (!enumMethods.includes(method)) { | ||
throw new Error('method not allowed'); | ||
throw new Error('method not allowed') | ||
} | ||
// remove trailing slash, but not if it's the first one | ||
if (path.endsWith('/') && path !== '/') { | ||
path = path.substr(0, path.length - 1); | ||
path = path.substr(0, path.length - 1) | ||
} | ||
// Static | ||
if (path.indexOf('{') < 0) { | ||
attachStaticRoute(method, path, handler, routesStatic); | ||
continue; | ||
attachStaticRoute(method, path, handler, routesStatic) | ||
continue | ||
} | ||
attachDynamicRoute(method, path, handler, routesDynamic); | ||
// Dynamic | ||
attachDynamicRoute(method, path, handler, routesDynamic) | ||
} | ||
return (event, context) => { | ||
var _getVersionRoute, _routesStatic$method; | ||
const { | ||
method, | ||
path | ||
} = (_getVersionRoute = getVersionRoute[event.version ?? '1.0']) === null || _getVersionRoute === void 0 ? void 0 : _getVersionRoute.call(getVersionRoute, event); | ||
const { method, path } = getVersionRoute[event.version ?? '1.0']?.(event) | ||
if (!method) { | ||
throw new Error('Unknown API Gateway Payload format'); | ||
throw new Error('Unknown API Gateway Payload format') | ||
} | ||
const handler = (_routesStatic$method = routesStatic[method]) === null || _routesStatic$method === void 0 ? void 0 : _routesStatic$method[path]; | ||
// Static | ||
const handler = routesStatic[method]?.[path] | ||
if (handler !== undefined) { | ||
return handler(event, context); | ||
return handler(event, context) | ||
} | ||
// Dynamic | ||
for (const route of routesDynamic[method] ?? []) { | ||
if (route.path.test(path)) { | ||
return route.handler(event, context); | ||
return route.handler(event, context) | ||
} | ||
} | ||
throw createError(404, 'Route does not exist'); | ||
}; | ||
}; | ||
// Not Found | ||
throw createError(404, 'Route does not exist') | ||
} | ||
} | ||
const regexpDynamicWildcards = /\/\{proxy\+\}/g; | ||
const regexpDynamicParameters = /\/\{.+\}/g; | ||
const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; | ||
const regexpDynamicWildcards = /\/\{proxy\+\}/g | ||
const regexpDynamicParameters = /\/\{.+\}/g | ||
const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'] | ||
@@ -66,46 +61,40 @@ const attachStaticRoute = (method, path, handler, routesType) => { | ||
for (const method of methods) { | ||
attachStaticRoute(method, path, handler, routesType); | ||
attachStaticRoute(method, path, handler, routesType) | ||
} | ||
return; | ||
return | ||
} | ||
if (!routesType[method]) { | ||
routesType[method] = {}; | ||
routesType[method] = {} | ||
} | ||
routesType[method][path] = handler | ||
} | ||
routesType[method][path] = handler; | ||
}; | ||
const attachDynamicRoute = (method, path, handler, routesType) => { | ||
if (method === 'ANY') { | ||
for (const method of methods) { | ||
attachDynamicRoute(method, path, handler, routesType); | ||
attachDynamicRoute(method, path, handler, routesType) | ||
} | ||
return; | ||
return | ||
} | ||
if (!routesType[method]) { | ||
routesType[method] = []; | ||
routesType[method] = [] | ||
} | ||
path = path | ||
.replace(regexpDynamicWildcards, '/?.*') // TODO update ot replaceAll for v4 | ||
.replace(regexpDynamicParameters, '/.+') | ||
path = new RegExp(`^${path}$`) | ||
routesType[method].push({ path, handler }) | ||
} | ||
path = path.replace(regexpDynamicWildcards, '/?.*').replace(regexpDynamicParameters, '/.+'); | ||
path = new RegExp(`^${path}$`); | ||
routesType[method].push({ | ||
path, | ||
handler | ||
}); | ||
}; | ||
const getVersionRoute = { | ||
'1.0': event => ({ | ||
'1.0': (event) => ({ | ||
method: event.httpMethod, | ||
path: event.path | ||
}), | ||
'2.0': event => ({ | ||
'2.0': (event) => ({ | ||
method: event.requestContext.http.method, | ||
path: event.requestContext.http.path | ||
}) | ||
}; | ||
export default httpRouteHandler; | ||
} | ||
export default httpRouteHandler |
{ | ||
"name": "@middy/http-router", | ||
"version": "3.0.0-alpha.2", | ||
"version": "3.0.0-alpha.3", | ||
"description": "http event router for the middy framework", | ||
@@ -21,3 +21,4 @@ "type": "module", | ||
"test": "npm run test:unit", | ||
"test:unit": "ava" | ||
"test:unit": "ava", | ||
"test:benchmark": "node __benchmarks__/index.js" | ||
}, | ||
@@ -52,8 +53,8 @@ "license": "MIT", | ||
"dependencies": { | ||
"@middy/util": "^3.0.0-alpha.2" | ||
"@middy/util": "^3.0.0-alpha.3" | ||
}, | ||
"devDependencies": { | ||
"@middy/core": "^3.0.0-alpha.2" | ||
"@middy/core": "^3.0.0-alpha.3" | ||
}, | ||
"gitHead": "de30419273ecbff08f367f47c7e320ec981cf145" | ||
"gitHead": "1441158711580313765e6d156046ef0fade0d156" | ||
} |
@@ -45,4 +45,4 @@ # Middy http-router lambda handler | ||
NOTES: | ||
- Errors should be handled as part of the router middleware stack **or** the baseHandler middleware stack. Handled errors in the later will trigger the `after` middleware stack of the former. | ||
- Shared middlewares, connected to the router middleware stack, can only be run before the baseHandler middleware stack. | ||
- Errors should be handled as part of the router middleware stack **or** the lambdaHandler middleware stack. Handled errors in the later will trigger the `after` middleware stack of the former. | ||
- Shared middlewares, connected to the router middleware stack, can only be run before the lambdaHandler middleware stack. | ||
@@ -49,0 +49,0 @@ ## Sample usage |
8960
88
Updated@middy/util@^3.0.0-alpha.3