@middy/http-router 3.0.1 → 3.0.4

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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
3
+ Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell) and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -9,8 +9,8 @@
9
9
  <a href="https://packagephobia.com/result?p=@middy/http-router">
10
10
  <img src="https://packagephobia.com/badge?p=@middy/http-router" alt="npm install size" style="max-width:100%;">
11
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%;">
12
+ <a href="https://github.com/middyjs/middy/actions/workflows/tests.yml">
13
+ <img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">
14
14
  </a>
15
15
  <br/>
16
16
  <a href="https://standardjs.com/">
@@ -33,6 +33,7 @@
33
33
  <img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
34
34
  </a>
35
35
  </p>
36
+ <p>You can read the documentation at: <a href="https://middy.js.org/docs/routers/http-router">https://middy.js.org/docs/routers/http-router</a></p>
36
37
  </div>
37
38
 
38
39
  This handler can route to requests to one of a nested hander based on `method` and `path` of an http event from API Gateway (REST or HTTP) or Elastic Load Balancer.
@@ -111,7 +112,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
111
112
 
112
113
  ## License
113
114
 
114
- Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
115
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
115
116
 
116
117
  <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
117
118
  <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
package/index.cjs CHANGED
@@ -1,3 +1,95 @@
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
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ module.exports = void 0;
6
+ var _util = require("@middy/util");
7
+ const httpRouteHandler = (routes)=>{
8
+ const routesStatic = {};
9
+ const routesDynamic = {};
10
+ const enumMethods = methods.concat('ANY');
11
+ for (const route1 of routes){
12
+ let { method , path , handler } = route1;
13
+ if (!enumMethods.includes(method)) {
14
+ throw new Error('[http-router] Method not allowed');
15
+ }
16
+ if (path.endsWith('/') && path !== '/') {
17
+ path = path.substr(0, path.length - 1);
18
+ }
19
+ if (path.indexOf('{') < 0) {
20
+ attachStaticRoute(method, path, handler, routesStatic);
21
+ continue;
22
+ }
23
+ attachDynamicRoute(method, path, handler, routesDynamic);
24
+ }
25
+ return (event, context, abort)=>{
26
+ const { method , path } = getVersionRoute[event.version ?? '1.0']?.(event);
27
+ if (!method) {
28
+ throw new Error('[http-router] Unknown http event format');
29
+ }
30
+ const handler = routesStatic[method]?.[path];
31
+ if (handler !== undefined) {
32
+ return handler(event, context, abort);
33
+ }
34
+ for (const route of routesDynamic[method] ?? []){
35
+ if (route.path.test(path)) {
36
+ return route.handler(event, context, abort);
37
+ }
38
+ }
39
+ throw (0, _util).createError(404, 'Route does not exist');
40
+ };
41
+ };
42
+ const regexpDynamicWildcards = /\/\{proxy\+\}/g;
43
+ const regexpDynamicParameters = /\/\{.+\}/g;
44
+ const methods = [
45
+ 'GET',
46
+ 'POST',
47
+ 'PUT',
48
+ 'PATCH',
49
+ 'DELETE',
50
+ 'OPTIONS'
51
+ ];
52
+ const attachStaticRoute = (method, path, handler, routesType)=>{
53
+ if (method === 'ANY') {
54
+ for (const method of methods){
55
+ attachStaticRoute(method, path, handler, routesType);
56
+ }
57
+ return;
58
+ }
59
+ if (!routesType[method]) {
60
+ routesType[method] = {};
61
+ }
62
+ routesType[method][path] = handler;
63
+ };
64
+ const attachDynamicRoute = (method, path, handler, routesType)=>{
65
+ if (method === 'ANY') {
66
+ for (const method of methods){
67
+ attachDynamicRoute(method, path, handler, routesType);
68
+ }
69
+ return;
70
+ }
71
+ if (!routesType[method]) {
72
+ routesType[method] = [];
73
+ }
74
+ path = path.replace(regexpDynamicWildcards, '/?.*').replace(regexpDynamicParameters, '/.+');
75
+ path = new RegExp(`^${path}$`);
76
+ routesType[method].push({
77
+ path,
78
+ handler
79
+ });
80
+ };
81
+ const getVersionRoute = {
82
+ '1.0': (event)=>({
83
+ method: event.httpMethod,
84
+ path: event.path
85
+ }),
86
+ '2.0': (event)=>({
87
+ method: event.requestContext.http.method,
88
+ path: event.requestContext.http.path
89
+ })
90
+ };
91
+ var _default = httpRouteHandler;
92
+ module.exports = _default;
93
+
2
94
 
3
95
  //# sourceMappingURL=index.cjs.map
package/index.d.ts CHANGED
@@ -1,5 +1,18 @@
1
- // import middy from '@middy/core'
1
+ import middy from '@middy/core'
2
+ import {
3
+ APIGatewayProxyEvent,
4
+ APIGatewayProxyResult,
5
+ Handler as LambdaHandler
6
+ } from 'aws-lambda'
2
7
 
3
- declare function httpRouterHandler (): any
8
+ type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'ANY'
9
+
10
+ interface Route {
11
+ method: Method
12
+ path: string
13
+ handler: LambdaHandler<APIGatewayProxyEvent, APIGatewayProxyResult>
14
+ }
15
+
16
+ declare function httpRouterHandler (routes: Route[]): middy.MiddlewareObj
4
17
 
5
18
  export default httpRouterHandler
package/index.js CHANGED
@@ -1,3 +1,89 @@
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
1
+ import { createError } from '@middy/util';
2
+ const httpRouteHandler = (routes)=>{
3
+ const routesStatic = {};
4
+ const routesDynamic = {};
5
+ const enumMethods = methods.concat('ANY');
6
+ for (const route1 of routes){
7
+ let { method , path , handler } = route1;
8
+ if (!enumMethods.includes(method)) {
9
+ throw new Error('[http-router] Method not allowed');
10
+ }
11
+ if (path.endsWith('/') && path !== '/') {
12
+ path = path.substr(0, path.length - 1);
13
+ }
14
+ if (path.indexOf('{') < 0) {
15
+ attachStaticRoute(method, path, handler, routesStatic);
16
+ continue;
17
+ }
18
+ attachDynamicRoute(method, path, handler, routesDynamic);
19
+ }
20
+ return (event, context, abort)=>{
21
+ const { method , path } = getVersionRoute[event.version ?? '1.0']?.(event);
22
+ if (!method) {
23
+ throw new Error('[http-router] Unknown http event format');
24
+ }
25
+ const handler = routesStatic[method]?.[path];
26
+ if (handler !== undefined) {
27
+ return handler(event, context, abort);
28
+ }
29
+ for (const route of routesDynamic[method] ?? []){
30
+ if (route.path.test(path)) {
31
+ return route.handler(event, context, abort);
32
+ }
33
+ }
34
+ throw createError(404, 'Route does not exist');
35
+ };
36
+ };
37
+ const regexpDynamicWildcards = /\/\{proxy\+\}/g;
38
+ const regexpDynamicParameters = /\/\{.+\}/g;
39
+ const methods = [
40
+ 'GET',
41
+ 'POST',
42
+ 'PUT',
43
+ 'PATCH',
44
+ 'DELETE',
45
+ 'OPTIONS'
46
+ ];
47
+ const attachStaticRoute = (method, path, handler, routesType)=>{
48
+ if (method === 'ANY') {
49
+ for (const method of methods){
50
+ attachStaticRoute(method, path, handler, routesType);
51
+ }
52
+ return;
53
+ }
54
+ if (!routesType[method]) {
55
+ routesType[method] = {};
56
+ }
57
+ routesType[method][path] = handler;
58
+ };
59
+ const attachDynamicRoute = (method, path, handler, routesType)=>{
60
+ if (method === 'ANY') {
61
+ for (const method of methods){
62
+ attachDynamicRoute(method, path, handler, routesType);
63
+ }
64
+ return;
65
+ }
66
+ if (!routesType[method]) {
67
+ routesType[method] = [];
68
+ }
69
+ path = path.replace(regexpDynamicWildcards, '/?.*').replace(regexpDynamicParameters, '/.+');
70
+ path = new RegExp(`^${path}$`);
71
+ routesType[method].push({
72
+ path,
73
+ handler
74
+ });
75
+ };
76
+ const getVersionRoute = {
77
+ '1.0': (event)=>({
78
+ method: event.httpMethod,
79
+ path: event.path
80
+ }),
81
+ '2.0': (event)=>({
82
+ method: event.requestContext.http.method,
83
+ path: event.requestContext.http.path
84
+ })
85
+ };
86
+ export default httpRouteHandler;
87
+
2
88
 
3
89
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-router",
3
- "version": "3.0.1",
3
+ "version": "3.0.4",
4
4
  "description": "HTTP event router for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -55,10 +55,11 @@
55
55
  },
56
56
  "homepage": "https://middy.js.org",
57
57
  "dependencies": {
58
- "@middy/util": "^3.0.1"
58
+ "@middy/util": "3.0.4"
59
59
  },
60
60
  "devDependencies": {
61
- "@middy/core": "^3.0.1"
61
+ "@middy/core": "3.0.4",
62
+ "@types/aws-lambda": "^8.10.97"
62
63
  },
63
- "gitHead": "797455e2c2be445867bc03597e0a2d5b0560c9de"
64
+ "gitHead": "3e9bc83e791f943c71cd7003fc27f0a3692d83a1"
64
65
  }