@middy/http-router 4.6.5 → 5.0.0-alpha.1

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/index.js +113 -104
  2. package/package.json +5 -11
  3. package/index.cjs +0 -122
package/index.js CHANGED
@@ -1,112 +1,121 @@
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 route of routes){
7
- let { method, path, handler } = route;
8
- // Prevents `routesType[method][path] = handler` from flagging: This assignment may alter Object.prototype if a malicious '__proto__' string is injected from library input.
9
- if (!enumMethods.includes(method)) {
10
- throw new Error('[http-router] Method not allowed');
11
- }
12
- // remove trailing slash, but not if it's the first one
13
- if (path.endsWith('/') && path !== '/') {
14
- path = path.substr(0, path.length - 1);
15
- }
16
- // Static
17
- if (path.indexOf('{') < 0) {
18
- attachStaticRoute(method, path, handler, routesStatic);
19
- continue;
20
- }
21
- // Dynamic
22
- attachDynamicRoute(method, path, handler, routesDynamic);
1
+ import { createError } from '@middy/util'
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
+ cause: { package: '@middy/http-router' }
14
+ })
23
15
  }
24
- return (event, context, abort)=>{
25
- const { method, path } = getVersionRoute[pickVersion(event)]?.(event);
26
- if (!method) {
27
- throw new Error('[http-router] Unknown http event format');
28
- }
29
- // Static
30
- const handler = routesStatic[method]?.[path];
31
- if (typeof handler !== 'undefined') {
32
- return handler(event, context, abort);
33
- }
34
- // Dynamic
35
- for (const route of routesDynamic[method] ?? []){
36
- const match = path.match(route.path);
37
- if (match) {
38
- event.pathParameters = {
39
- ...match.groups,
40
- ...event.pathParameters
41
- };
42
- return route.handler(event, context, abort);
43
- }
44
- }
45
- // Not Found
46
- throw createError(404, 'Route does not exist');
47
- };
48
- };
49
- const regexpDynamicWildcards = /\/\{(proxy)\+\}$/;
50
- const regexpDynamicParameters = /\/\{([^/]+)\}/g;
51
- const methods = [
52
- 'GET',
53
- 'POST',
54
- 'PUT',
55
- 'PATCH',
56
- 'DELETE',
57
- 'OPTIONS',
58
- 'HEAD'
59
- ] // ANY excluded by design
60
- ;
61
- const attachStaticRoute = (method, path, handler, routesType)=>{
62
- if (method === 'ANY') {
63
- for (const method of methods){
64
- attachStaticRoute(method, path, handler, routesType);
65
- }
66
- return;
16
+
17
+ // remove trailing slash, but not if it's the first one
18
+ if (path.endsWith('/') && path !== '/') {
19
+ path = path.substr(0, path.length - 1)
67
20
  }
68
- if (!routesType[method]) {
69
- routesType[method] = {};
21
+
22
+ // Static
23
+ if (path.indexOf('{') < 0) {
24
+ attachStaticRoute(method, path, handler, routesStatic)
25
+ continue
70
26
  }
71
- routesType[method][path] = handler;
72
- routesType[method][path + '/'] = handler // Optional `/`
73
- ;
74
- };
75
- const attachDynamicRoute = (method, path, handler, routesType)=>{
76
- if (method === 'ANY') {
77
- for (const method of methods){
78
- attachDynamicRoute(method, path, handler, routesType);
27
+
28
+ // Dynamic
29
+ attachDynamicRoute(method, path, handler, routesDynamic)
30
+ }
31
+
32
+ return (event, context, abort) => {
33
+ const { method, path } = getVersionRoute[pickVersion(event)]?.(event)
34
+ if (!method) {
35
+ throw new Error('Unknown http event format', {
36
+ cause: { package: '@middy/http-router', data: event }
37
+ })
38
+ }
39
+
40
+ // Static
41
+ const handler = routesStatic[method]?.[path]
42
+ if (typeof handler !== 'undefined') {
43
+ return handler(event, context, abort)
44
+ }
45
+
46
+ // Dynamic
47
+ for (const route of routesDynamic[method] ?? []) {
48
+ const match = path.match(route.path)
49
+ if (match) {
50
+ event.pathParameters = {
51
+ ...match.groups,
52
+ ...event.pathParameters
79
53
  }
80
- return;
54
+ return route.handler(event, context, abort)
55
+ }
56
+ }
57
+
58
+ // Not Found
59
+ throw createError(404, 'Route does not exist', {
60
+ cause: { pacakge: '@middy/http-router', data: path }
61
+ })
62
+ }
63
+ }
64
+
65
+ const regexpDynamicWildcards = /\/\{(proxy)\+\}$/
66
+ const regexpDynamicParameters = /\/\{([^/]+)\}/g
67
+
68
+ const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'] // ANY excluded by design
69
+
70
+ const attachStaticRoute = (method, path, handler, routesType) => {
71
+ if (method === 'ANY') {
72
+ for (const method of methods) {
73
+ attachStaticRoute(method, path, handler, routesType)
81
74
  }
82
- if (!routesType[method]) {
83
- routesType[method] = [];
75
+ return
76
+ }
77
+ if (!routesType[method]) {
78
+ routesType[method] = {}
79
+ }
80
+ routesType[method][path] = handler
81
+ routesType[method][path + '/'] = handler // Optional `/`
82
+ }
83
+
84
+ const attachDynamicRoute = (method, path, handler, routesType) => {
85
+ if (method === 'ANY') {
86
+ for (const method of methods) {
87
+ attachDynamicRoute(method, path, handler, routesType)
84
88
  }
85
- path = path.replace(regexpDynamicWildcards, '/?(?<$1>.*)').replace(regexpDynamicParameters, '/(?<$1>[^/]+)');
86
- path = new RegExp(`^${path}/?$`) // Adds in optional `/`
87
- ;
88
- routesType[method].push({
89
- path,
90
- handler
91
- });
92
- };
93
- const pickVersion = (event)=>{
94
- // '1.0' is a safer default
95
- return event.version ?? (event.method ? 'vpc' : '1.0');
96
- };
89
+ return
90
+ }
91
+ if (!routesType[method]) {
92
+ routesType[method] = []
93
+ }
94
+ path = path
95
+ .replace(regexpDynamicWildcards, '/?(?<$1>.*)')
96
+ .replace(regexpDynamicParameters, '/(?<$1>[^/]+)')
97
+ path = new RegExp(`^${path}/?$`) // Adds in optional `/`
98
+ routesType[method].push({ path, handler })
99
+ }
100
+
101
+ const pickVersion = (event) => {
102
+ // '1.0' is a safer default
103
+ return event.version ?? (event.method ? 'vpc' : '1.0')
104
+ }
105
+
97
106
  const getVersionRoute = {
98
- '1.0': (event)=>({
99
- method: event.httpMethod,
100
- path: event.path
101
- }),
102
- '2.0': (event)=>({
103
- method: event.requestContext.http.method,
104
- path: event.requestContext.http.path
105
- }),
106
- vpc: (event)=>({
107
- method: event.method,
108
- path: event.raw_path.split('?')[0]
109
- })
110
- };
111
- export default httpRouteHandler;
107
+ '1.0': (event) => ({
108
+ method: event.httpMethod,
109
+ path: event.path
110
+ }),
111
+ '2.0': (event) => ({
112
+ method: event.requestContext.http.method,
113
+ path: event.requestContext.http.path
114
+ }),
115
+ vpc: (event) => ({
116
+ method: event.method,
117
+ path: event.raw_path.split('?')[0]
118
+ })
119
+ }
112
120
 
121
+ export default httpRouteHandler
package/package.json CHANGED
@@ -1,33 +1,27 @@
1
1
  {
2
2
  "name": "@middy/http-router",
3
- "version": "4.6.5",
3
+ "version": "5.0.0-alpha.1",
4
4
  "description": "HTTP event router for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
7
- "node": ">=16"
7
+ "node": ">=18"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "./index.cjs",
14
13
  "module": "./index.js",
15
14
  "exports": {
16
15
  ".": {
17
16
  "import": {
18
17
  "types": "./index.d.ts",
19
18
  "default": "./index.js"
20
- },
21
- "require": {
22
- "types": "./index.d.ts",
23
- "default": "./index.cjs"
24
19
  }
25
20
  }
26
21
  },
27
22
  "types": "index.d.ts",
28
23
  "files": [
29
24
  "index.js",
30
- "index.cjs",
31
25
  "index.d.ts"
32
26
  ],
33
27
  "scripts": {
@@ -66,11 +60,11 @@
66
60
  "url": "https://github.com/sponsors/willfarrell"
67
61
  },
68
62
  "dependencies": {
69
- "@middy/util": "4.6.5"
63
+ "@middy/util": "5.0.0-alpha.1"
70
64
  },
71
65
  "devDependencies": {
72
- "@middy/core": "4.6.5",
66
+ "@middy/core": "5.0.0-alpha.1",
73
67
  "@types/aws-lambda": "^8.10.97"
74
68
  },
75
- "gitHead": "573d7b0bb243d8c5a9bcb00cf29d031aa7a0c606"
69
+ "gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
76
70
  }
package/index.cjs DELETED
@@ -1,122 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(module, "exports", {
6
- enumerable: true,
7
- get: function() {
8
- return _default;
9
- }
10
- });
11
- const _util = require("@middy/util");
12
- const httpRouteHandler = (routes)=>{
13
- const routesStatic = {};
14
- const routesDynamic = {};
15
- const enumMethods = methods.concat('ANY');
16
- for (const route of routes){
17
- let { method, path, handler } = route;
18
- // Prevents `routesType[method][path] = handler` from flagging: This assignment may alter Object.prototype if a malicious '__proto__' string is injected from library input.
19
- if (!enumMethods.includes(method)) {
20
- throw new Error('[http-router] Method not allowed');
21
- }
22
- // remove trailing slash, but not if it's the first one
23
- if (path.endsWith('/') && path !== '/') {
24
- path = path.substr(0, path.length - 1);
25
- }
26
- // Static
27
- if (path.indexOf('{') < 0) {
28
- attachStaticRoute(method, path, handler, routesStatic);
29
- continue;
30
- }
31
- // Dynamic
32
- attachDynamicRoute(method, path, handler, routesDynamic);
33
- }
34
- return (event, context, abort)=>{
35
- const { method, path } = getVersionRoute[pickVersion(event)]?.(event);
36
- if (!method) {
37
- throw new Error('[http-router] Unknown http event format');
38
- }
39
- // Static
40
- const handler = routesStatic[method]?.[path];
41
- if (typeof handler !== 'undefined') {
42
- return handler(event, context, abort);
43
- }
44
- // Dynamic
45
- for (const route of routesDynamic[method] ?? []){
46
- const match = path.match(route.path);
47
- if (match) {
48
- event.pathParameters = {
49
- ...match.groups,
50
- ...event.pathParameters
51
- };
52
- return route.handler(event, context, abort);
53
- }
54
- }
55
- // Not Found
56
- throw (0, _util.createError)(404, 'Route does not exist');
57
- };
58
- };
59
- const regexpDynamicWildcards = /\/\{(proxy)\+\}$/;
60
- const regexpDynamicParameters = /\/\{([^/]+)\}/g;
61
- const methods = [
62
- 'GET',
63
- 'POST',
64
- 'PUT',
65
- 'PATCH',
66
- 'DELETE',
67
- 'OPTIONS',
68
- 'HEAD'
69
- ] // ANY excluded by design
70
- ;
71
- const attachStaticRoute = (method, path, handler, routesType)=>{
72
- if (method === 'ANY') {
73
- for (const method of methods){
74
- attachStaticRoute(method, path, handler, routesType);
75
- }
76
- return;
77
- }
78
- if (!routesType[method]) {
79
- routesType[method] = {};
80
- }
81
- routesType[method][path] = handler;
82
- routesType[method][path + '/'] = handler // Optional `/`
83
- ;
84
- };
85
- const attachDynamicRoute = (method, path, handler, routesType)=>{
86
- if (method === 'ANY') {
87
- for (const method of methods){
88
- attachDynamicRoute(method, path, handler, routesType);
89
- }
90
- return;
91
- }
92
- if (!routesType[method]) {
93
- routesType[method] = [];
94
- }
95
- path = path.replace(regexpDynamicWildcards, '/?(?<$1>.*)').replace(regexpDynamicParameters, '/(?<$1>[^/]+)');
96
- path = new RegExp(`^${path}/?$`) // Adds in optional `/`
97
- ;
98
- routesType[method].push({
99
- path,
100
- handler
101
- });
102
- };
103
- const pickVersion = (event)=>{
104
- // '1.0' is a safer default
105
- return event.version ?? (event.method ? 'vpc' : '1.0');
106
- };
107
- const getVersionRoute = {
108
- '1.0': (event)=>({
109
- method: event.httpMethod,
110
- path: event.path
111
- }),
112
- '2.0': (event)=>({
113
- method: event.requestContext.http.method,
114
- path: event.requestContext.http.path
115
- }),
116
- vpc: (event)=>({
117
- method: event.method,
118
- path: event.raw_path.split('?')[0]
119
- })
120
- };
121
- const _default = httpRouteHandler;
122
-