@middy/http-router
Advanced tools
Comparing version 5.1.0 to 5.2.0
220
index.js
@@ -1,115 +0,121 @@ | ||
import { createError } from '@middy/util'; | ||
const httpRouteHandler = (routes)=>{ | ||
const routesStatic = {}; | ||
const routesDynamic = {}; | ||
const enumMethods = methods.concat('ANY'); | ||
for (const route of routes){ | ||
let { method, path, handler } = route; | ||
if (!enumMethods.includes(method)) { | ||
throw new Error('Method not allowed', { | ||
cause: { | ||
package: '@middy/http-router' | ||
} | ||
}); | ||
} | ||
if (path.endsWith('/') && path !== '/') { | ||
path = path.substr(0, path.length - 1); | ||
} | ||
if (path.indexOf('{') < 0) { | ||
attachStaticRoute(method, path, handler, routesStatic); | ||
continue; | ||
} | ||
attachDynamicRoute(method, path, handler, routesDynamic); | ||
import { createError } from '@middy/util' | ||
const httpRouteHandler = (routes) => { | ||
const routesStatic = {} | ||
const routesDynamic = {} | ||
const enumMethods = methods.concat('ANY') | ||
for (const route of routes) { | ||
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', { | ||
cause: { package: '@middy/http-router' } | ||
}) | ||
} | ||
return (event, context, abort)=>{ | ||
const { method, path } = getVersionRoute[pickVersion(event)]?.(event); | ||
if (!method) { | ||
throw new Error('Unknown http event format', { | ||
cause: { | ||
package: '@middy/http-router', | ||
data: event | ||
} | ||
}); | ||
} | ||
const handler = routesStatic[method]?.[path]; | ||
if (typeof handler !== 'undefined') { | ||
return handler(event, context, abort); | ||
} | ||
for (const route of routesDynamic[method] ?? []){ | ||
const match = path.match(route.path); | ||
if (match) { | ||
event.pathParameters = { | ||
...match.groups, | ||
...event.pathParameters | ||
}; | ||
return route.handler(event, context, abort); | ||
} | ||
} | ||
throw createError(404, 'Route does not exist', { | ||
cause: { | ||
package: '@middy/http-router', | ||
data: path | ||
} | ||
}); | ||
}; | ||
}; | ||
const regexpDynamicWildcards = /\/\{(proxy)\+\}$/; | ||
const regexpDynamicParameters = /\/\{([^/]+)\}/g; | ||
const methods = [ | ||
'GET', | ||
'POST', | ||
'PUT', | ||
'PATCH', | ||
'DELETE', | ||
'OPTIONS', | ||
'HEAD' | ||
]; | ||
const attachStaticRoute = (method, path, handler, routesType)=>{ | ||
if (method === 'ANY') { | ||
for (const method of methods){ | ||
attachStaticRoute(method, path, handler, routesType); | ||
} | ||
return; | ||
// remove trailing slash, but not if it's the first one | ||
if (path.endsWith('/') && path !== '/') { | ||
path = path.substr(0, path.length - 1) | ||
} | ||
if (!routesType[method]) { | ||
routesType[method] = {}; | ||
// Static | ||
if (path.indexOf('{') < 0) { | ||
attachStaticRoute(method, path, handler, routesStatic) | ||
continue | ||
} | ||
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); | ||
// Dynamic | ||
attachDynamicRoute(method, path, handler, routesDynamic) | ||
} | ||
return (event, context, abort) => { | ||
const { method, path } = getVersionRoute[pickVersion(event)]?.(event) | ||
if (!method) { | ||
throw new Error('Unknown http event format', { | ||
cause: { package: '@middy/http-router', data: event } | ||
}) | ||
} | ||
// Static | ||
const handler = routesStatic[method]?.[path] | ||
if (typeof handler !== 'undefined') { | ||
return handler(event, context, abort) | ||
} | ||
// Dynamic | ||
for (const route of routesDynamic[method] ?? []) { | ||
const match = path.match(route.path) | ||
if (match) { | ||
event.pathParameters = { | ||
...match.groups, | ||
...event.pathParameters | ||
} | ||
return; | ||
return route.handler(event, context, abort) | ||
} | ||
} | ||
if (!routesType[method]) { | ||
routesType[method] = []; | ||
// Not Found | ||
throw createError(404, 'Route does not exist', { | ||
cause: { package: '@middy/http-router', data: path } | ||
}) | ||
} | ||
} | ||
const regexpDynamicWildcards = /\/\{(proxy)\+\}$/ | ||
const regexpDynamicParameters = /\/\{([^/]+)\}/g | ||
const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'] // ANY excluded by design | ||
const attachStaticRoute = (method, path, handler, routesType) => { | ||
if (method === 'ANY') { | ||
for (const method of methods) { | ||
attachStaticRoute(method, path, handler, routesType) | ||
} | ||
path = path.replace(regexpDynamicWildcards, '/?(?<$1>.*)').replace(regexpDynamicParameters, '/(?<$1>[^/]+)'); | ||
path = new RegExp(`^${path}/?$`); | ||
routesType[method].push({ | ||
path, | ||
handler | ||
}); | ||
}; | ||
const pickVersion = (event)=>{ | ||
return event.version ?? (event.method ? 'vpc' : '1.0'); | ||
}; | ||
return | ||
} | ||
if (!routesType[method]) { | ||
routesType[method] = {} | ||
} | ||
routesType[method][path] = handler | ||
routesType[method][path + '/'] = handler // Optional `/` | ||
} | ||
const attachDynamicRoute = (method, path, handler, routesType) => { | ||
if (method === 'ANY') { | ||
for (const method of methods) { | ||
attachDynamicRoute(method, path, handler, routesType) | ||
} | ||
return | ||
} | ||
if (!routesType[method]) { | ||
routesType[method] = [] | ||
} | ||
path = path | ||
.replace(regexpDynamicWildcards, '/?(?<$1>.*)') | ||
.replace(regexpDynamicParameters, '/(?<$1>[^/]+)') | ||
path = new RegExp(`^${path}/?$`) // Adds in optional `/` | ||
routesType[method].push({ path, handler }) | ||
} | ||
const pickVersion = (event) => { | ||
// '1.0' is a safer default | ||
return event.version ?? (event.method ? 'vpc' : '1.0') | ||
} | ||
const getVersionRoute = { | ||
'1.0': (event)=>({ | ||
method: event.httpMethod, | ||
path: event.path | ||
}), | ||
'2.0': (event)=>({ | ||
method: event.requestContext.http.method, | ||
path: event.requestContext.http.path | ||
}), | ||
vpc: (event)=>({ | ||
method: event.method, | ||
path: event.raw_path.split('?')[0] | ||
}) | ||
}; | ||
export default httpRouteHandler; | ||
'1.0': (event) => ({ | ||
method: event.httpMethod, | ||
path: event.path | ||
}), | ||
'2.0': (event) => ({ | ||
method: event.requestContext.http.method, | ||
path: event.requestContext.http.path | ||
}), | ||
vpc: (event) => ({ | ||
method: event.method, | ||
path: event.raw_path.split('?')[0] | ||
}) | ||
} | ||
export default httpRouteHandler |
{ | ||
"name": "@middy/http-router", | ||
"version": "5.1.0", | ||
"version": "5.2.0", | ||
"description": "HTTP event router for the middy framework", | ||
@@ -63,9 +63,9 @@ "type": "module", | ||
"dependencies": { | ||
"@middy/util": "5.1.0" | ||
"@middy/util": "5.2.0" | ||
}, | ||
"devDependencies": { | ||
"@middy/core": "5.1.0", | ||
"@middy/core": "5.2.0", | ||
"@types/aws-lambda": "^8.10.97" | ||
}, | ||
"gitHead": "bbdaf5843914921804ba085dd58117273febe6b5" | ||
"gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9701
129
+ Added@middy/util@5.2.0(transitive)
- Removed@middy/util@5.1.0(transitive)
Updated@middy/util@5.2.0