@middy/http-router 5.1.0 → 5.2.0
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 +113 -107
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,115 +1,121 @@
|
|
|
1
|
-
import { createError } from '@middy/util'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (path.endsWith('/') && path !== '/') {
|
|
16
|
-
path = path.substr(0, path.length - 1);
|
|
17
|
-
}
|
|
18
|
-
if (path.indexOf('{') < 0) {
|
|
19
|
-
attachStaticRoute(method, path, handler, routesStatic);
|
|
20
|
-
continue;
|
|
21
|
-
}
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
cause: {
|
|
29
|
-
package: '@middy/http-router',
|
|
30
|
-
data: event
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
const handler = routesStatic[method]?.[path];
|
|
35
|
-
if (typeof handler !== 'undefined') {
|
|
36
|
-
return handler(event, context, abort);
|
|
37
|
-
}
|
|
38
|
-
for (const route of routesDynamic[method] ?? []){
|
|
39
|
-
const match = path.match(route.path);
|
|
40
|
-
if (match) {
|
|
41
|
-
event.pathParameters = {
|
|
42
|
-
...match.groups,
|
|
43
|
-
...event.pathParameters
|
|
44
|
-
};
|
|
45
|
-
return route.handler(event, context, abort);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
throw createError(404, 'Route does not exist', {
|
|
49
|
-
cause: {
|
|
50
|
-
package: '@middy/http-router',
|
|
51
|
-
data: path
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
const regexpDynamicWildcards = /\/\{(proxy)\+\}$/;
|
|
57
|
-
const regexpDynamicParameters = /\/\{([^/]+)\}/g;
|
|
58
|
-
const methods = [
|
|
59
|
-
'GET',
|
|
60
|
-
'POST',
|
|
61
|
-
'PUT',
|
|
62
|
-
'PATCH',
|
|
63
|
-
'DELETE',
|
|
64
|
-
'OPTIONS',
|
|
65
|
-
'HEAD'
|
|
66
|
-
];
|
|
67
|
-
const attachStaticRoute = (method, path, handler, routesType)=>{
|
|
68
|
-
if (method === 'ANY') {
|
|
69
|
-
for (const method of methods){
|
|
70
|
-
attachStaticRoute(method, path, handler, routesType);
|
|
71
|
-
}
|
|
72
|
-
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)
|
|
73
20
|
}
|
|
74
|
-
|
|
75
|
-
|
|
21
|
+
|
|
22
|
+
// Static
|
|
23
|
+
if (path.indexOf('{') < 0) {
|
|
24
|
+
attachStaticRoute(method, path, handler, routesStatic)
|
|
25
|
+
continue
|
|
76
26
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
|
84
53
|
}
|
|
85
|
-
return
|
|
54
|
+
return route.handler(event, context, abort)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Not Found
|
|
59
|
+
throw createError(404, 'Route does not exist', {
|
|
60
|
+
cause: { package: '@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)
|
|
86
74
|
}
|
|
87
|
-
|
|
88
|
-
|
|
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)
|
|
89
88
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
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
|
+
|
|
100
106
|
const getVersionRoute = {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
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
|
+
}
|
|
115
120
|
|
|
121
|
+
export default httpRouteHandler
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-router",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"description": "HTTP event router for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -60,11 +60,11 @@
|
|
|
60
60
|
"url": "https://github.com/sponsors/willfarrell"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@middy/util": "5.
|
|
63
|
+
"@middy/util": "5.2.0"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@middy/core": "5.
|
|
66
|
+
"@middy/core": "5.2.0",
|
|
67
67
|
"@types/aws-lambda": "^8.10.97"
|
|
68
68
|
},
|
|
69
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
|
|
70
70
|
}
|