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