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

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 +2 -2
  2. package/index.js +48 -59
  3. package/package.json +6 -5
package/README.md CHANGED
@@ -42,8 +42,8 @@ npm install --save @middy/http-router
42
42
  - `handler` (function) (required): Any `handler(event, context)` function
43
43
 
44
44
  NOTES:
45
- - Errors should be handled as part of the router middleware stack **or** the baseHandler middleware stack. Handled errors in the later will trigger the `after` middleware stack of the former.
46
- - Shared middlewares, connected to the router middleware stack, can only be run before the baseHandler middleware stack.
45
+ - Errors should be handled as part of the router middleware stack **or** the lambdaHandler middleware stack. Handled errors in the later will trigger the `after` middleware stack of the former.
46
+ - Shared middlewares, connected to the router middleware stack, can only be run before the lambdaHandler middleware stack.
47
47
 
48
48
  ## Sample usage
49
49
 
package/index.js CHANGED
@@ -1,110 +1,99 @@
1
- import { createError } from '@middy/util';
2
-
3
- const httpRouteHandler = routes => {
4
- const routesStatic = {};
5
- const routesDynamic = {};
6
- const enumMethods = methods.concat('ANY');
1
+ import { createError } from '@middy/util'
7
2
 
3
+ const httpRouteHandler = (routes) => {
4
+ const routesStatic = {}
5
+ const routesDynamic = {}
6
+ const enumMethods = methods.concat('ANY')
8
7
  for (const route of routes) {
9
- let {
10
- method,
11
- path,
12
- handler
13
- } = route;
8
+ let { method, path, handler } = route
14
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.
15
11
  if (!enumMethods.includes(method)) {
16
- throw new Error('method not allowed');
12
+ throw new Error('method not allowed')
17
13
  }
18
14
 
15
+ // remove trailing slash, but not if it's the first one
19
16
  if (path.endsWith('/') && path !== '/') {
20
- path = path.substr(0, path.length - 1);
17
+ path = path.substr(0, path.length - 1)
21
18
  }
22
19
 
20
+ // Static
23
21
  if (path.indexOf('{') < 0) {
24
- attachStaticRoute(method, path, handler, routesStatic);
25
- continue;
22
+ attachStaticRoute(method, path, handler, routesStatic)
23
+ continue
26
24
  }
27
25
 
28
- attachDynamicRoute(method, path, handler, routesDynamic);
26
+ // Dynamic
27
+ attachDynamicRoute(method, path, handler, routesDynamic)
29
28
  }
30
29
 
31
30
  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
-
31
+ const { method, path } = getVersionRoute[event.version ?? '1.0']?.(event)
39
32
  if (!method) {
40
- throw new Error('Unknown API Gateway Payload format');
33
+ throw new Error('Unknown API Gateway Payload format')
41
34
  }
42
35
 
43
- const handler = (_routesStatic$method = routesStatic[method]) === null || _routesStatic$method === void 0 ? void 0 : _routesStatic$method[path];
44
-
36
+ // Static
37
+ const handler = routesStatic[method]?.[path]
45
38
  if (handler !== undefined) {
46
- return handler(event, context);
39
+ return handler(event, context)
47
40
  }
48
41
 
42
+ // Dynamic
49
43
  for (const route of routesDynamic[method] ?? []) {
50
44
  if (route.path.test(path)) {
51
- return route.handler(event, context);
45
+ return route.handler(event, context)
52
46
  }
53
47
  }
54
48
 
55
- throw createError(404, 'Route does not exist');
56
- };
57
- };
49
+ // Not Found
50
+ throw createError(404, 'Route does not exist')
51
+ }
52
+ }
58
53
 
59
- const regexpDynamicWildcards = /\/\{proxy\+\}/g;
60
- const regexpDynamicParameters = /\/\{.+\}/g;
61
- const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
54
+ const regexpDynamicWildcards = /\/\{proxy\+\}/g
55
+ const regexpDynamicParameters = /\/\{.+\}/g
56
+ const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']
62
57
 
63
58
  const attachStaticRoute = (method, path, handler, routesType) => {
64
59
  if (method === 'ANY') {
65
60
  for (const method of methods) {
66
- attachStaticRoute(method, path, handler, routesType);
61
+ attachStaticRoute(method, path, handler, routesType)
67
62
  }
68
-
69
- return;
63
+ return
70
64
  }
71
-
72
65
  if (!routesType[method]) {
73
- routesType[method] = {};
66
+ routesType[method] = {}
74
67
  }
75
-
76
- routesType[method][path] = handler;
77
- };
68
+ routesType[method][path] = handler
69
+ }
78
70
 
79
71
  const attachDynamicRoute = (method, path, handler, routesType) => {
80
72
  if (method === 'ANY') {
81
73
  for (const method of methods) {
82
- attachDynamicRoute(method, path, handler, routesType);
74
+ attachDynamicRoute(method, path, handler, routesType)
83
75
  }
84
-
85
- return;
76
+ return
86
77
  }
87
-
88
78
  if (!routesType[method]) {
89
- routesType[method] = [];
79
+ routesType[method] = []
90
80
  }
91
-
92
- path = path.replace(regexpDynamicWildcards, '/?.*').replace(regexpDynamicParameters, '/.+');
93
- path = new RegExp(`^${path}$`);
94
- routesType[method].push({
95
- path,
96
- handler
97
- });
98
- };
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
+ }
99
87
 
100
88
  const getVersionRoute = {
101
- '1.0': event => ({
89
+ '1.0': (event) => ({
102
90
  method: event.httpMethod,
103
91
  path: event.path
104
92
  }),
105
- '2.0': event => ({
93
+ '2.0': (event) => ({
106
94
  method: event.requestContext.http.method,
107
95
  path: event.requestContext.http.path
108
96
  })
109
- };
110
- export default httpRouteHandler;
97
+ }
98
+
99
+ export default httpRouteHandler
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-router",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "http event router for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -18,7 +18,8 @@
18
18
  ],
19
19
  "scripts": {
20
20
  "test": "npm run test:unit",
21
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
22
23
  },
23
24
  "license": "MIT",
24
25
  "keywords": [
@@ -48,10 +49,10 @@
48
49
  },
49
50
  "homepage": "https://github.com/middyjs/middy#readme",
50
51
  "dependencies": {
51
- "@middy/util": "^3.0.0-alpha.2"
52
+ "@middy/util": "^3.0.0-alpha.3"
52
53
  },
53
54
  "devDependencies": {
54
- "@middy/core": "^3.0.0-alpha.2"
55
+ "@middy/core": "^3.0.0-alpha.3"
55
56
  },
56
- "gitHead": "de30419273ecbff08f367f47c7e320ec981cf145"
57
+ "gitHead": "1441158711580313765e6d156046ef0fade0d156"
57
58
  }