@middy/http-router 5.0.0-alpha.0 → 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.
- package/README.md +3 -2
- package/index.d.ts +3 -11
- package/index.js +40 -8
- package/package.json +4 -10
- package/index.cjs +0 -102
package/README.md
CHANGED
|
@@ -19,8 +19,9 @@
|
|
|
19
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
20
20
|
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
|
|
21
21
|
</a>
|
|
22
|
-
<a href="https://
|
|
23
|
-
<img src="https://
|
|
22
|
+
<a href="https://github.com/middyjs/middy/actions/workflows/sast.yml">
|
|
23
|
+
<img src="https://github.com/middyjs/middy/actions/workflows/sast.yml/badge.svg
|
|
24
|
+
?branch=main&event=push" alt="CodeQL" style="max-width:100%;">
|
|
24
25
|
</a>
|
|
25
26
|
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
27
|
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
package/index.d.ts
CHANGED
|
@@ -5,21 +5,13 @@ import {
|
|
|
5
5
|
APIGatewayProxyEvent,
|
|
6
6
|
APIGatewayProxyEventV2,
|
|
7
7
|
APIGatewayProxyResult,
|
|
8
|
+
APIGatewayProxyResultV2,
|
|
8
9
|
Handler as LambdaHandler
|
|
9
10
|
} from 'aws-lambda'
|
|
10
11
|
|
|
11
|
-
export
|
|
12
|
-
Get = 'GET',
|
|
13
|
-
Post = 'POST',
|
|
14
|
-
Put = 'PUT',
|
|
15
|
-
Patch = 'PATCH',
|
|
16
|
-
Delete = 'DELETE',
|
|
17
|
-
Options = 'OPTIONS',
|
|
18
|
-
Head = 'HEAD',
|
|
19
|
-
Any = 'ANY'
|
|
20
|
-
}
|
|
12
|
+
export type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'ANY'
|
|
21
13
|
|
|
22
|
-
type TResult = ALBResult | APIGatewayProxyResult
|
|
14
|
+
type TResult = ALBResult | APIGatewayProxyResult | APIGatewayProxyResultV2
|
|
23
15
|
|
|
24
16
|
export interface Route<TEvent> {
|
|
25
17
|
method: Method
|
package/index.js
CHANGED
|
@@ -4,28 +4,43 @@ const httpRouteHandler = (routes)=>{
|
|
|
4
4
|
const routesDynamic = {};
|
|
5
5
|
const enumMethods = methods.concat('ANY');
|
|
6
6
|
for (const route of routes){
|
|
7
|
-
let { method
|
|
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.
|
|
8
9
|
if (!enumMethods.includes(method)) {
|
|
9
|
-
throw new Error('
|
|
10
|
+
throw new Error('Method not allowed', {
|
|
11
|
+
cause: {
|
|
12
|
+
package: '@middy/http-router'
|
|
13
|
+
}
|
|
14
|
+
});
|
|
10
15
|
}
|
|
16
|
+
// remove trailing slash, but not if it's the first one
|
|
11
17
|
if (path.endsWith('/') && path !== '/') {
|
|
12
18
|
path = path.substr(0, path.length - 1);
|
|
13
19
|
}
|
|
20
|
+
// Static
|
|
14
21
|
if (path.indexOf('{') < 0) {
|
|
15
22
|
attachStaticRoute(method, path, handler, routesStatic);
|
|
16
23
|
continue;
|
|
17
24
|
}
|
|
25
|
+
// Dynamic
|
|
18
26
|
attachDynamicRoute(method, path, handler, routesDynamic);
|
|
19
27
|
}
|
|
20
28
|
return (event, context, abort)=>{
|
|
21
|
-
const { method
|
|
29
|
+
const { method, path } = getVersionRoute[pickVersion(event)]?.(event);
|
|
22
30
|
if (!method) {
|
|
23
|
-
throw new Error('
|
|
31
|
+
throw new Error('Unknown http event format', {
|
|
32
|
+
cause: {
|
|
33
|
+
package: '@middy/http-router',
|
|
34
|
+
data: event
|
|
35
|
+
}
|
|
36
|
+
});
|
|
24
37
|
}
|
|
38
|
+
// Static
|
|
25
39
|
const handler = routesStatic[method]?.[path];
|
|
26
40
|
if (typeof handler !== 'undefined') {
|
|
27
41
|
return handler(event, context, abort);
|
|
28
42
|
}
|
|
43
|
+
// Dynamic
|
|
29
44
|
for (const route of routesDynamic[method] ?? []){
|
|
30
45
|
const match = path.match(route.path);
|
|
31
46
|
if (match) {
|
|
@@ -36,7 +51,13 @@ const httpRouteHandler = (routes)=>{
|
|
|
36
51
|
return route.handler(event, context, abort);
|
|
37
52
|
}
|
|
38
53
|
}
|
|
39
|
-
|
|
54
|
+
// Not Found
|
|
55
|
+
throw createError(404, 'Route does not exist', {
|
|
56
|
+
cause: {
|
|
57
|
+
pacakge: '@middy/http-router',
|
|
58
|
+
data: path
|
|
59
|
+
}
|
|
60
|
+
});
|
|
40
61
|
};
|
|
41
62
|
};
|
|
42
63
|
const regexpDynamicWildcards = /\/\{(proxy)\+\}$/;
|
|
@@ -49,7 +70,8 @@ const methods = [
|
|
|
49
70
|
'DELETE',
|
|
50
71
|
'OPTIONS',
|
|
51
72
|
'HEAD'
|
|
52
|
-
]
|
|
73
|
+
] // ANY excluded by design
|
|
74
|
+
;
|
|
53
75
|
const attachStaticRoute = (method, path, handler, routesType)=>{
|
|
54
76
|
if (method === 'ANY') {
|
|
55
77
|
for (const method of methods){
|
|
@@ -61,7 +83,8 @@ const attachStaticRoute = (method, path, handler, routesType)=>{
|
|
|
61
83
|
routesType[method] = {};
|
|
62
84
|
}
|
|
63
85
|
routesType[method][path] = handler;
|
|
64
|
-
routesType[method][path + '/'] = handler
|
|
86
|
+
routesType[method][path + '/'] = handler // Optional `/`
|
|
87
|
+
;
|
|
65
88
|
};
|
|
66
89
|
const attachDynamicRoute = (method, path, handler, routesType)=>{
|
|
67
90
|
if (method === 'ANY') {
|
|
@@ -74,12 +97,17 @@ const attachDynamicRoute = (method, path, handler, routesType)=>{
|
|
|
74
97
|
routesType[method] = [];
|
|
75
98
|
}
|
|
76
99
|
path = path.replace(regexpDynamicWildcards, '/?(?<$1>.*)').replace(regexpDynamicParameters, '/(?<$1>[^/]+)');
|
|
77
|
-
path = new RegExp(`^${path}/?$`)
|
|
100
|
+
path = new RegExp(`^${path}/?$`) // Adds in optional `/`
|
|
101
|
+
;
|
|
78
102
|
routesType[method].push({
|
|
79
103
|
path,
|
|
80
104
|
handler
|
|
81
105
|
});
|
|
82
106
|
};
|
|
107
|
+
const pickVersion = (event)=>{
|
|
108
|
+
// '1.0' is a safer default
|
|
109
|
+
return event.version ?? (event.method ? 'vpc' : '1.0');
|
|
110
|
+
};
|
|
83
111
|
const getVersionRoute = {
|
|
84
112
|
'1.0': (event)=>({
|
|
85
113
|
method: event.httpMethod,
|
|
@@ -88,6 +116,10 @@ const getVersionRoute = {
|
|
|
88
116
|
'2.0': (event)=>({
|
|
89
117
|
method: event.requestContext.http.method,
|
|
90
118
|
path: event.requestContext.http.path
|
|
119
|
+
}),
|
|
120
|
+
vpc: (event)=>({
|
|
121
|
+
method: event.method,
|
|
122
|
+
path: event.raw_path.split('?')[0]
|
|
91
123
|
})
|
|
92
124
|
};
|
|
93
125
|
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.
|
|
3
|
+
"version": "5.0.0-alpha.2",
|
|
4
4
|
"description": "HTTP event router for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,24 +10,18 @@
|
|
|
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": "5.0.0-alpha.
|
|
63
|
+
"@middy/util": "5.0.0-alpha.2"
|
|
70
64
|
},
|
|
71
65
|
"devDependencies": {
|
|
72
|
-
"@middy/core": "5.0.0-alpha.
|
|
66
|
+
"@middy/core": "5.0.0-alpha.2",
|
|
73
67
|
"@types/aws-lambda": "^8.10.97"
|
|
74
68
|
},
|
|
75
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
|
|
76
70
|
}
|
package/index.cjs
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(module, "exports", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: ()=>_default
|
|
8
|
-
});
|
|
9
|
-
const _util = require("@middy/util");
|
|
10
|
-
const httpRouteHandler = (routes)=>{
|
|
11
|
-
const routesStatic = {};
|
|
12
|
-
const routesDynamic = {};
|
|
13
|
-
const enumMethods = methods.concat('ANY');
|
|
14
|
-
for (const route of routes){
|
|
15
|
-
let { method , path , handler } = route;
|
|
16
|
-
if (!enumMethods.includes(method)) {
|
|
17
|
-
throw new Error('[http-router] Method not allowed');
|
|
18
|
-
}
|
|
19
|
-
if (path.endsWith('/') && path !== '/') {
|
|
20
|
-
path = path.substr(0, path.length - 1);
|
|
21
|
-
}
|
|
22
|
-
if (path.indexOf('{') < 0) {
|
|
23
|
-
attachStaticRoute(method, path, handler, routesStatic);
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
attachDynamicRoute(method, path, handler, routesDynamic);
|
|
27
|
-
}
|
|
28
|
-
return (event, context, abort)=>{
|
|
29
|
-
const { method , path } = getVersionRoute[event.version ?? '1.0']?.(event);
|
|
30
|
-
if (!method) {
|
|
31
|
-
throw new Error('[http-router] Unknown http event format');
|
|
32
|
-
}
|
|
33
|
-
const handler = routesStatic[method]?.[path];
|
|
34
|
-
if (typeof handler !== 'undefined') {
|
|
35
|
-
return handler(event, context, abort);
|
|
36
|
-
}
|
|
37
|
-
for (const route of routesDynamic[method] ?? []){
|
|
38
|
-
const match = path.match(route.path);
|
|
39
|
-
if (match) {
|
|
40
|
-
event.pathParameters = {
|
|
41
|
-
...match.groups,
|
|
42
|
-
...event.pathParameters
|
|
43
|
-
};
|
|
44
|
-
return route.handler(event, context, abort);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
throw (0, _util.createError)(404, 'Route does not exist');
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
const regexpDynamicWildcards = /\/\{(proxy)\+\}$/;
|
|
51
|
-
const regexpDynamicParameters = /\/\{([^/]+)\}/g;
|
|
52
|
-
const methods = [
|
|
53
|
-
'GET',
|
|
54
|
-
'POST',
|
|
55
|
-
'PUT',
|
|
56
|
-
'PATCH',
|
|
57
|
-
'DELETE',
|
|
58
|
-
'OPTIONS',
|
|
59
|
-
'HEAD'
|
|
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;
|
|
67
|
-
}
|
|
68
|
-
if (!routesType[method]) {
|
|
69
|
-
routesType[method] = {};
|
|
70
|
-
}
|
|
71
|
-
routesType[method][path] = handler;
|
|
72
|
-
routesType[method][path + '/'] = handler;
|
|
73
|
-
};
|
|
74
|
-
const attachDynamicRoute = (method, path, handler, routesType)=>{
|
|
75
|
-
if (method === 'ANY') {
|
|
76
|
-
for (const method of methods){
|
|
77
|
-
attachDynamicRoute(method, path, handler, routesType);
|
|
78
|
-
}
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
if (!routesType[method]) {
|
|
82
|
-
routesType[method] = [];
|
|
83
|
-
}
|
|
84
|
-
path = path.replace(regexpDynamicWildcards, '/?(?<$1>.*)').replace(regexpDynamicParameters, '/(?<$1>[^/]+)');
|
|
85
|
-
path = new RegExp(`^${path}/?$`);
|
|
86
|
-
routesType[method].push({
|
|
87
|
-
path,
|
|
88
|
-
handler
|
|
89
|
-
});
|
|
90
|
-
};
|
|
91
|
-
const getVersionRoute = {
|
|
92
|
-
'1.0': (event)=>({
|
|
93
|
-
method: event.httpMethod,
|
|
94
|
-
path: event.path
|
|
95
|
-
}),
|
|
96
|
-
'2.0': (event)=>({
|
|
97
|
-
method: event.requestContext.http.method,
|
|
98
|
-
path: event.requestContext.http.path
|
|
99
|
-
})
|
|
100
|
-
};
|
|
101
|
-
const _default = httpRouteHandler;
|
|
102
|
-
|