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