@middy/http-router
Advanced tools
Comparing version 3.0.0-alpha.3 to 3.0.0-alpha.4
107
index.js
@@ -1,57 +0,62 @@ | ||
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; | ||
} | ||
// Dynamic | ||
attachDynamicRoute(method, path, handler, routesDynamic) | ||
attachDynamicRoute(method, path, handler, routesDynamic); | ||
} | ||
return (event, context) => { | ||
const { method, path } = getVersionRoute[event.version ?? '1.0']?.(event) | ||
var _getVersionRoute, _routesStatic$method; | ||
const { | ||
method, | ||
path | ||
} = (_getVersionRoute = getVersionRoute[event.version ?? '1.0']) === null || _getVersionRoute === void 0 ? void 0 : _getVersionRoute.call(getVersionRoute, event); | ||
if (!method) { | ||
throw new Error('Unknown API Gateway Payload format') | ||
throw new Error('Unknown API Gateway Payload format'); | ||
} | ||
// Static | ||
const handler = routesStatic[method]?.[path] | ||
const handler = (_routesStatic$method = routesStatic[method]) === null || _routesStatic$method === void 0 ? void 0 : _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); | ||
} | ||
} | ||
// Not Found | ||
throw createError(404, 'Route does not exist') | ||
} | ||
} | ||
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']; | ||
@@ -61,40 +66,46 @@ 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.3", | ||
"version": "3.0.0-alpha.4", | ||
"description": "http event router for the middy framework", | ||
@@ -52,8 +52,8 @@ "type": "module", | ||
"dependencies": { | ||
"@middy/util": "^3.0.0-alpha.3" | ||
"@middy/util": "^3.0.0-alpha.4" | ||
}, | ||
"devDependencies": { | ||
"@middy/core": "^3.0.0-alpha.3" | ||
"@middy/core": "^3.0.0-alpha.4" | ||
}, | ||
"gitHead": "1441158711580313765e6d156046ef0fade0d156" | ||
"gitHead": "d4bea7f4e21f6a9bbb1f6f6908361169598b9e53" | ||
} |
90
8944
Updated@middy/util@^3.0.0-alpha.4