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

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