@middy/http-router
Advanced tools
Comparing version 3.0.0-alpha.6 to 3.0.0-alpha.7
111
index.js
@@ -1,110 +0,3 @@ | ||
import { createError } from '@middy/util'; | ||
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)=>{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)}for(const route of routesDynamic[method]??[]){if(route.path.test(path)){return route.handler(event,context)}}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 | ||
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('[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) => { | ||
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('[http-router] Unknown http event format'); | ||
} | ||
const handler = (_routesStatic$method = routesStatic[method]) === null || _routesStatic$method === void 0 ? void 0 : _routesStatic$method[path]; | ||
if (handler !== undefined) { | ||
return handler(event, context); | ||
} | ||
for (const route of routesDynamic[method] ?? []) { | ||
if (route.path.test(path)) { | ||
return route.handler(event, context); | ||
} | ||
} | ||
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.0-alpha.6", | ||
"version": "3.0.0-alpha.7", | ||
"description": "http event router for the middy framework", | ||
@@ -52,8 +52,8 @@ "type": "module", | ||
"dependencies": { | ||
"@middy/util": "^3.0.0-alpha.6" | ||
"@middy/util": "^3.0.0-alpha.7" | ||
}, | ||
"devDependencies": { | ||
"@middy/core": "^3.0.0-alpha.6" | ||
"@middy/core": "^3.0.0-alpha.7" | ||
}, | ||
"gitHead": "176660ed3e0716d6bfb635c77251b301e0e24720" | ||
"gitHead": "5cef39ebe49c201f97d71bb0680004de4b82cb91" | ||
} |
@@ -1,25 +0,35 @@ | ||
# Middy http-router lambda handler | ||
<div align="center"> | ||
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/> | ||
</div> | ||
<div align="center"> | ||
<h1>Middy http-router lambda handler</h1> | ||
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/> | ||
<p><strong>HTTP router for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p> | ||
</div> | ||
<div align="center"> | ||
<p> | ||
<a href="http://badge.fury.io/js/%40middy%2Fhttp-router"> | ||
<a href="https://www.npmjs.com/package/@middy/http-router?activeTab=versions"> | ||
<img src="https://badge.fury.io/js/%40middy%2Fhttp-router.svg" alt="npm version" style="max-width:100%;"> | ||
</a> | ||
<a href="https://packagephobia.com/result?p=@middy/http-router"> | ||
<img src="https://packagephobia.com/badge?p=@middy/http-router" alt="npm install size" style="max-width:100%;"> | ||
</a> | ||
<a href="https://github.com/middyjs/middy/actions"> | ||
<img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;"> | ||
</a> | ||
<br/> | ||
<a href="https://standardjs.com/"> | ||
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;"> | ||
</a> | ||
<a href="https://snyk.io/test/github/middyjs/middy"> | ||
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;"> | ||
</a> | ||
<a href="https://standardjs.com/"> | ||
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;"> | ||
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript"> | ||
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;"> | ||
</a> | ||
<a href="https://bestpractices.coreinfrastructure.org/projects/5280"> | ||
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;"> | ||
</a> | ||
<br/> | ||
<a href="https://gitter.im/middyjs/Lobby"> | ||
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;"> | ||
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;"> | ||
</a> | ||
<a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052"> | ||
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;"> | ||
</a> | ||
</p> | ||
@@ -26,0 +36,0 @@ </div> |
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
9288
119
11
Updated@middy/util@^3.0.0-alpha.7