@middy/http-router
Advanced tools
Comparing version 3.0.3 to 3.0.4
88
index.js
@@ -1,3 +0,89 @@ | ||
import{createError}from"@middy/util";const httpRouteHandler=routes=>{const routesStatic={};const routesDynamic={};const enumMethods=methods.concat("ANY");for(const route1 of routes){let{method,path,handler}=route1;if(!enumMethods.includes(method)){throw new Error("[http-router] Method not allowed")}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)}return(event,context,abort)=>{const{method,path}=getVersionRoute[event.version??"1.0"]?.(event);if(!method){throw new Error("[http-router] Unknown http event format")}const handler=routesStatic[method]?.[path];if(handler!==undefined){return handler(event,context,abort)}for(const route of routesDynamic[method]??[]){if(route.path.test(path)){return route.handler(event,context,abort)}}throw createError(404,"Route does not exist")}};const regexpDynamicWildcards=/\/\{proxy\+\}/g;const regexpDynamicParameters=/\/\{.+\}/g;const methods=["GET","POST","PUT","PATCH","DELETE","OPTIONS"];const attachStaticRoute=(method,path,handler,routesType)=>{if(method==="ANY"){for(const method of methods){attachStaticRoute(method,path,handler,routesType)}return}if(!routesType[method]){routesType[method]={}}routesType[method][path]=handler};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,"/?.*").replace(regexpDynamicParameters,"/.+");path=new RegExp(`^${path}$`);routesType[method].push({path,handler})};const getVersionRoute={"1.0":event=>({method:event.httpMethod,path:event.path}),"2.0":event=>({method:event.requestContext.http.method,path:event.requestContext.http.path})};export default httpRouteHandler | ||
import { createError } from '@middy/util'; | ||
const httpRouteHandler = (routes)=>{ | ||
const routesStatic = {}; | ||
const routesDynamic = {}; | ||
const enumMethods = methods.concat('ANY'); | ||
for (const route1 of routes){ | ||
let { method , path , handler } = route1; | ||
if (!enumMethods.includes(method)) { | ||
throw new Error('[http-router] Method not allowed'); | ||
} | ||
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); | ||
} | ||
return (event, context, abort)=>{ | ||
const { method , path } = getVersionRoute[event.version ?? '1.0']?.(event); | ||
if (!method) { | ||
throw new Error('[http-router] Unknown http event format'); | ||
} | ||
const handler = routesStatic[method]?.[path]; | ||
if (handler !== undefined) { | ||
return handler(event, context, abort); | ||
} | ||
for (const route of routesDynamic[method] ?? []){ | ||
if (route.path.test(path)) { | ||
return route.handler(event, context, abort); | ||
} | ||
} | ||
throw createError(404, 'Route does not exist'); | ||
}; | ||
}; | ||
const regexpDynamicWildcards = /\/\{proxy\+\}/g; | ||
const regexpDynamicParameters = /\/\{.+\}/g; | ||
const methods = [ | ||
'GET', | ||
'POST', | ||
'PUT', | ||
'PATCH', | ||
'DELETE', | ||
'OPTIONS' | ||
]; | ||
const attachStaticRoute = (method, path, handler, routesType)=>{ | ||
if (method === 'ANY') { | ||
for (const method of methods){ | ||
attachStaticRoute(method, path, handler, routesType); | ||
} | ||
return; | ||
} | ||
if (!routesType[method]) { | ||
routesType[method] = {}; | ||
} | ||
routesType[method][path] = handler; | ||
}; | ||
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, '/?.*').replace(regexpDynamicParameters, '/.+'); | ||
path = new RegExp(`^${path}$`); | ||
routesType[method].push({ | ||
path, | ||
handler | ||
}); | ||
}; | ||
const getVersionRoute = { | ||
'1.0': (event)=>({ | ||
method: event.httpMethod, | ||
path: event.path | ||
}), | ||
'2.0': (event)=>({ | ||
method: event.requestContext.http.method, | ||
path: event.requestContext.http.path | ||
}) | ||
}; | ||
export default httpRouteHandler; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@middy/http-router", | ||
"version": "3.0.3", | ||
"version": "3.0.4", | ||
"description": "HTTP event router for the middy framework", | ||
@@ -58,9 +58,9 @@ "type": "module", | ||
"dependencies": { | ||
"@middy/util": "3.0.3" | ||
"@middy/util": "3.0.4" | ||
}, | ||
"devDependencies": { | ||
"@middy/core": "3.0.3", | ||
"@middy/core": "3.0.4", | ||
"@types/aws-lambda": "^8.10.97" | ||
}, | ||
"gitHead": "ea9e5e8cce754d0c467c7dd3ac9a7601149efea2" | ||
"gitHead": "3e9bc83e791f943c71cd7003fc27f0a3692d83a1" | ||
} |
Sorry, the diff of this file is not supported yet
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
13688
192
0
+ Added@middy/util@3.0.4(transitive)
- Removed@middy/util@3.0.3(transitive)
Updated@middy/util@3.0.4