@middy/http-router 3.0.0-alpha.3 → 3.0.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +23 -13
  2. package/index.js +2 -98
  3. package/package.json +4 -4
package/README.md CHANGED
@@ -1,26 +1,36 @@
1
- # Middy http-router lambda handler
2
-
3
- <div align="center">
4
- <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
5
- </div>
6
-
7
1
  <div align="center">
2
+ <h1>Middy http-router lambda handler</h1>
3
+ <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
8
4
  <p><strong>HTTP router for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
9
- </div>
10
-
11
- <div align="center">
12
5
  <p>
13
- <a href="http://badge.fury.io/js/%40middy%2Fhttp-router">
6
+ <a href="https://www.npmjs.com/package/@middy/http-router?activeTab=versions">
14
7
  <img src="https://badge.fury.io/js/%40middy%2Fhttp-router.svg" alt="npm version" style="max-width:100%;">
15
8
  </a>
9
+ <a href="https://packagephobia.com/result?p=@middy/http-router">
10
+ <img src="https://packagephobia.com/badge?p=@middy/http-router" alt="npm install size" style="max-width:100%;">
11
+ </a>
12
+ <a href="https://github.com/middyjs/middy/actions">
13
+ <img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
14
+ </a>
15
+ <br/>
16
+ <a href="https://standardjs.com/">
17
+ <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
18
+ </a>
16
19
  <a href="https://snyk.io/test/github/middyjs/middy">
17
20
  <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%;">
18
21
  </a>
19
- <a href="https://standardjs.com/">
20
- <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
22
+ <a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
23
+ <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%;">
24
+ </a>
25
+ <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
26
+ <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
21
27
  </a>
28
+ <br/>
22
29
  <a href="https://gitter.im/middyjs/Lobby">
23
- <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
30
+ <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
31
+ </a>
32
+ <a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
33
+ <img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
24
34
  </a>
25
35
  </p>
26
36
  </div>
package/index.js CHANGED
@@ -1,99 +1,3 @@
1
- import { createError } from '@middy/util'
1
+ 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
2
2
 
3
- const httpRouteHandler = (routes) => {
4
- const routesStatic = {}
5
- const routesDynamic = {}
6
- const enumMethods = methods.concat('ANY')
7
- for (const route of routes) {
8
- let { method, path, handler } = route
9
-
10
- // Prevents `routesType[method][path] = handler` from flagging: This assignment may alter Object.prototype if a malicious '__proto__' string is injected from library input.
11
- if (!enumMethods.includes(method)) {
12
- throw new Error('method not allowed')
13
- }
14
-
15
- // remove trailing slash, but not if it's the first one
16
- if (path.endsWith('/') && path !== '/') {
17
- path = path.substr(0, path.length - 1)
18
- }
19
-
20
- // Static
21
- if (path.indexOf('{') < 0) {
22
- attachStaticRoute(method, path, handler, routesStatic)
23
- continue
24
- }
25
-
26
- // Dynamic
27
- attachDynamicRoute(method, path, handler, routesDynamic)
28
- }
29
-
30
- return (event, context) => {
31
- const { method, path } = getVersionRoute[event.version ?? '1.0']?.(event)
32
- if (!method) {
33
- throw new Error('Unknown API Gateway Payload format')
34
- }
35
-
36
- // Static
37
- const handler = routesStatic[method]?.[path]
38
- if (handler !== undefined) {
39
- return handler(event, context)
40
- }
41
-
42
- // Dynamic
43
- for (const route of routesDynamic[method] ?? []) {
44
- if (route.path.test(path)) {
45
- return route.handler(event, context)
46
- }
47
- }
48
-
49
- // Not Found
50
- throw createError(404, 'Route does not exist')
51
- }
52
- }
53
-
54
- const regexpDynamicWildcards = /\/\{proxy\+\}/g
55
- const regexpDynamicParameters = /\/\{.+\}/g
56
- const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']
57
-
58
- const attachStaticRoute = (method, path, handler, routesType) => {
59
- if (method === 'ANY') {
60
- for (const method of methods) {
61
- attachStaticRoute(method, path, handler, routesType)
62
- }
63
- return
64
- }
65
- if (!routesType[method]) {
66
- routesType[method] = {}
67
- }
68
- routesType[method][path] = handler
69
- }
70
-
71
- const attachDynamicRoute = (method, path, handler, routesType) => {
72
- if (method === 'ANY') {
73
- for (const method of methods) {
74
- attachDynamicRoute(method, path, handler, routesType)
75
- }
76
- return
77
- }
78
- if (!routesType[method]) {
79
- routesType[method] = []
80
- }
81
- path = path
82
- .replace(regexpDynamicWildcards, '/?.*') // TODO update ot replaceAll for v4
83
- .replace(regexpDynamicParameters, '/.+')
84
- path = new RegExp(`^${path}$`)
85
- routesType[method].push({ path, handler })
86
- }
87
-
88
- const getVersionRoute = {
89
- '1.0': (event) => ({
90
- method: event.httpMethod,
91
- path: event.path
92
- }),
93
- '2.0': (event) => ({
94
- method: event.requestContext.http.method,
95
- path: event.requestContext.http.path
96
- })
97
- }
98
-
99
- export default httpRouteHandler
3
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-router",
3
- "version": "3.0.0-alpha.3",
3
+ "version": "3.0.0-alpha.7",
4
4
  "description": "http event router for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -49,10 +49,10 @@
49
49
  },
50
50
  "homepage": "https://github.com/middyjs/middy#readme",
51
51
  "dependencies": {
52
- "@middy/util": "^3.0.0-alpha.3"
52
+ "@middy/util": "^3.0.0-alpha.7"
53
53
  },
54
54
  "devDependencies": {
55
- "@middy/core": "^3.0.0-alpha.3"
55
+ "@middy/core": "^3.0.0-alpha.7"
56
56
  },
57
- "gitHead": "1441158711580313765e6d156046ef0fade0d156"
57
+ "gitHead": "5cef39ebe49c201f97d71bb0680004de4b82cb91"
58
58
  }