@middy/http-router 3.0.0-alpha.6 → 3.0.0

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 (4) hide show
  1. package/README.md +23 -13
  2. package/index.cjs +3 -0
  3. package/index.js +2 -109
  4. package/package.json +15 -9
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.cjs ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _util=require("@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(0,_util).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})};var _default=httpRouteHandler;exports.default=_default
2
+
3
+ //# sourceMappingURL=index.cjs.map
package/index.js CHANGED
@@ -1,110 +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,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
2
2
 
3
- const httpRouteHandler = routes => {
4
- const routesStatic = {};
5
- const routesDynamic = {};
6
- const enumMethods = methods.concat('ANY');
7
-
8
- for (const route of routes) {
9
- let {
10
- method,
11
- path,
12
- handler
13
- } = route;
14
-
15
- if (!enumMethods.includes(method)) {
16
- throw new Error('[http-router] Method not allowed');
17
- }
18
-
19
- if (path.endsWith('/') && path !== '/') {
20
- path = path.substr(0, path.length - 1);
21
- }
22
-
23
- if (path.indexOf('{') < 0) {
24
- attachStaticRoute(method, path, handler, routesStatic);
25
- continue;
26
- }
27
-
28
- attachDynamicRoute(method, path, handler, routesDynamic);
29
- }
30
-
31
- return (event, context) => {
32
- var _getVersionRoute, _routesStatic$method;
33
-
34
- const {
35
- method,
36
- path
37
- } = (_getVersionRoute = getVersionRoute[event.version ?? '1.0']) === null || _getVersionRoute === void 0 ? void 0 : _getVersionRoute.call(getVersionRoute, event);
38
-
39
- if (!method) {
40
- throw new Error('[http-router] Unknown http event format');
41
- }
42
-
43
- const handler = (_routesStatic$method = routesStatic[method]) === null || _routesStatic$method === void 0 ? void 0 : _routesStatic$method[path];
44
-
45
- if (handler !== undefined) {
46
- return handler(event, context);
47
- }
48
-
49
- for (const route of routesDynamic[method] ?? []) {
50
- if (route.path.test(path)) {
51
- return route.handler(event, context);
52
- }
53
- }
54
-
55
- throw createError(404, 'Route does not exist');
56
- };
57
- };
58
-
59
- const regexpDynamicWildcards = /\/\{proxy\+\}/g;
60
- const regexpDynamicParameters = /\/\{.+\}/g;
61
- const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
62
-
63
- const attachStaticRoute = (method, path, handler, routesType) => {
64
- if (method === 'ANY') {
65
- for (const method of methods) {
66
- attachStaticRoute(method, path, handler, routesType);
67
- }
68
-
69
- return;
70
- }
71
-
72
- if (!routesType[method]) {
73
- routesType[method] = {};
74
- }
75
-
76
- routesType[method][path] = handler;
77
- };
78
-
79
- const attachDynamicRoute = (method, path, handler, routesType) => {
80
- if (method === 'ANY') {
81
- for (const method of methods) {
82
- attachDynamicRoute(method, path, handler, routesType);
83
- }
84
-
85
- return;
86
- }
87
-
88
- if (!routesType[method]) {
89
- routesType[method] = [];
90
- }
91
-
92
- path = path.replace(regexpDynamicWildcards, '/?.*').replace(regexpDynamicParameters, '/.+');
93
- path = new RegExp(`^${path}$`);
94
- routesType[method].push({
95
- path,
96
- handler
97
- });
98
- };
99
-
100
- const getVersionRoute = {
101
- '1.0': event => ({
102
- method: event.httpMethod,
103
- path: event.path
104
- }),
105
- '2.0': event => ({
106
- method: event.requestContext.http.method,
107
- path: event.requestContext.http.path
108
- })
109
- };
110
- export default httpRouteHandler;
3
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@middy/http-router",
3
- "version": "3.0.0-alpha.6",
4
- "description": "http event router for the middy framework",
3
+ "version": "3.0.0",
4
+ "description": "HTTP event router for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
7
7
  "node": ">=14"
@@ -10,10 +10,17 @@
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "exports": "./index.js",
13
+ "exports": {
14
+ ".": {
15
+ "import": "./index.js",
16
+ "require": "./index.cjs",
17
+ "types": "./index.d.ts"
18
+ }
19
+ },
14
20
  "types": "index.d.ts",
15
21
  "files": [
16
22
  "index.js",
23
+ "index.cjs",
17
24
  "index.d.ts"
18
25
  ],
19
26
  "scripts": {
@@ -32,8 +39,7 @@
32
39
  "Middy",
33
40
  "HTTP",
34
41
  "API",
35
- "Urlencode",
36
- "Body Parser"
42
+ "router"
37
43
  ],
38
44
  "author": {
39
45
  "name": "Middy contributors",
@@ -47,12 +53,12 @@
47
53
  "bugs": {
48
54
  "url": "https://github.com/middyjs/middy/issues"
49
55
  },
50
- "homepage": "https://github.com/middyjs/middy#readme",
56
+ "homepage": "https://middy.js.org",
51
57
  "dependencies": {
52
- "@middy/util": "^3.0.0-alpha.6"
58
+ "@middy/util": "^3.0.0"
53
59
  },
54
60
  "devDependencies": {
55
- "@middy/core": "^3.0.0-alpha.6"
61
+ "@middy/core": "^3.0.0"
56
62
  },
57
- "gitHead": "176660ed3e0716d6bfb635c77251b301e0e24720"
63
+ "gitHead": "01520fa8628a36c2f89e126cad656a16547ea0d6"
58
64
  }