@middy/http-router 3.0.2 → 3.1.0-rc.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/LICENSE +1 -1
- package/README.md +6 -5
- package/index.cjs +93 -1
- package/index.d.ts +15 -2
- package/index.js +87 -1
- package/package.json +14 -7
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
3
|
+
Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell) and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
<a href="https://packagephobia.com/result?p=@middy/http-router">
|
|
10
10
|
<img src="https://packagephobia.com/badge?p=@middy/http-router" alt="npm install size" style="max-width:100%;">
|
|
11
11
|
</a>
|
|
12
|
-
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
-
<img src="https://github.com/middyjs/middy/workflows/
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions/workflows/tests.yml">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">
|
|
14
14
|
</a>
|
|
15
15
|
<br/>
|
|
16
16
|
<a href="https://standardjs.com/">
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
34
34
|
</a>
|
|
35
35
|
</p>
|
|
36
|
+
<p>You can read the documentation at: <a href="https://middy.js.org/docs/routers/http-router">https://middy.js.org/docs/routers/http-router</a></p>
|
|
36
37
|
</div>
|
|
37
38
|
|
|
38
39
|
This handler can route to requests to one of a nested hander based on `method` and `path` of an http event from API Gateway (REST or HTTP) or Elastic Load Balancer.
|
|
@@ -62,7 +63,7 @@ import middy from '@middy/core'
|
|
|
62
63
|
import validatorMiddleware from '@middy/validator'
|
|
63
64
|
|
|
64
65
|
const getHandler = middy()
|
|
65
|
-
.use(validatorMiddleware({
|
|
66
|
+
.use(validatorMiddleware({eventSchema: {...} }))
|
|
66
67
|
.handler((event, context) => {
|
|
67
68
|
return {
|
|
68
69
|
statusCode: 200,
|
|
@@ -71,7 +72,7 @@ const getHandler = middy()
|
|
|
71
72
|
})
|
|
72
73
|
|
|
73
74
|
const postHandler = middy()
|
|
74
|
-
.use(validatorMiddleware({
|
|
75
|
+
.use(validatorMiddleware({eventSchema: {...} }))
|
|
75
76
|
.handler((event, context) => {
|
|
76
77
|
return {
|
|
77
78
|
statusCode: 200,
|
|
@@ -111,7 +112,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
111
112
|
|
|
112
113
|
## License
|
|
113
114
|
|
|
114
|
-
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
115
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
115
116
|
|
|
116
117
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
117
118
|
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
package/index.cjs
CHANGED
|
@@ -1,3 +1,95 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
module.exports = void 0;
|
|
6
|
+
var _util = require("@middy/util");
|
|
7
|
+
const httpRouteHandler = (routes)=>{
|
|
8
|
+
const routesStatic = {};
|
|
9
|
+
const routesDynamic = {};
|
|
10
|
+
const enumMethods = methods.concat('ANY');
|
|
11
|
+
for (const route1 of routes){
|
|
12
|
+
let { method , path , handler } = route1;
|
|
13
|
+
if (!enumMethods.includes(method)) {
|
|
14
|
+
throw new Error('[http-router] Method not allowed');
|
|
15
|
+
}
|
|
16
|
+
if (path.endsWith('/') && path !== '/') {
|
|
17
|
+
path = path.substr(0, path.length - 1);
|
|
18
|
+
}
|
|
19
|
+
if (path.indexOf('{') < 0) {
|
|
20
|
+
attachStaticRoute(method, path, handler, routesStatic);
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
attachDynamicRoute(method, path, handler, routesDynamic);
|
|
24
|
+
}
|
|
25
|
+
return (event, context, abort)=>{
|
|
26
|
+
const { method , path } = getVersionRoute[event.version ?? '1.0']?.(event);
|
|
27
|
+
if (!method) {
|
|
28
|
+
throw new Error('[http-router] Unknown http event format');
|
|
29
|
+
}
|
|
30
|
+
const handler = routesStatic[method]?.[path];
|
|
31
|
+
if (handler !== undefined) {
|
|
32
|
+
return handler(event, context, abort);
|
|
33
|
+
}
|
|
34
|
+
for (const route of routesDynamic[method] ?? []){
|
|
35
|
+
if (route.path.test(path)) {
|
|
36
|
+
return route.handler(event, context, abort);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw (0, _util).createError(404, 'Route does not exist');
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
const regexpDynamicWildcards = /\/\{proxy\+\}/g;
|
|
43
|
+
const regexpDynamicParameters = /\/\{.+\}/g;
|
|
44
|
+
const methods = [
|
|
45
|
+
'GET',
|
|
46
|
+
'POST',
|
|
47
|
+
'PUT',
|
|
48
|
+
'PATCH',
|
|
49
|
+
'DELETE',
|
|
50
|
+
'OPTIONS'
|
|
51
|
+
];
|
|
52
|
+
const attachStaticRoute = (method, path, handler, routesType)=>{
|
|
53
|
+
if (method === 'ANY') {
|
|
54
|
+
for (const method of methods){
|
|
55
|
+
attachStaticRoute(method, path, handler, routesType);
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (!routesType[method]) {
|
|
60
|
+
routesType[method] = {};
|
|
61
|
+
}
|
|
62
|
+
routesType[method][path] = handler;
|
|
63
|
+
};
|
|
64
|
+
const attachDynamicRoute = (method, path, handler, routesType)=>{
|
|
65
|
+
if (method === 'ANY') {
|
|
66
|
+
for (const method of methods){
|
|
67
|
+
attachDynamicRoute(method, path, handler, routesType);
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (!routesType[method]) {
|
|
72
|
+
routesType[method] = [];
|
|
73
|
+
}
|
|
74
|
+
path = path.replace(regexpDynamicWildcards, '/?.*').replace(regexpDynamicParameters, '/.+');
|
|
75
|
+
path = new RegExp(`^${path}$`);
|
|
76
|
+
routesType[method].push({
|
|
77
|
+
path,
|
|
78
|
+
handler
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
const getVersionRoute = {
|
|
82
|
+
'1.0': (event)=>({
|
|
83
|
+
method: event.httpMethod,
|
|
84
|
+
path: event.path
|
|
85
|
+
}),
|
|
86
|
+
'2.0': (event)=>({
|
|
87
|
+
method: event.requestContext.http.method,
|
|
88
|
+
path: event.requestContext.http.path
|
|
89
|
+
})
|
|
90
|
+
};
|
|
91
|
+
var _default = httpRouteHandler;
|
|
92
|
+
module.exports = _default;
|
|
93
|
+
|
|
2
94
|
|
|
3
95
|
//# sourceMappingURL=index.cjs.map
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
import middy from '@middy/core'
|
|
2
|
+
import {
|
|
3
|
+
APIGatewayProxyEvent,
|
|
4
|
+
APIGatewayProxyResult,
|
|
5
|
+
Handler as LambdaHandler
|
|
6
|
+
} from 'aws-lambda'
|
|
2
7
|
|
|
3
|
-
|
|
8
|
+
export type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'ANY'
|
|
9
|
+
|
|
10
|
+
export interface Route {
|
|
11
|
+
method: Method
|
|
12
|
+
path: string
|
|
13
|
+
handler: LambdaHandler<APIGatewayProxyEvent, APIGatewayProxyResult>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare function httpRouterHandler (routes: Route[]): middy.MiddlewareObj
|
|
4
17
|
|
|
5
18
|
export default httpRouterHandler
|
package/index.js
CHANGED
|
@@ -1,3 +1,89 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { createError } from '@middy/util';
|
|
2
|
+
const httpRouteHandler = (routes)=>{
|
|
3
|
+
const routesStatic = {};
|
|
4
|
+
const routesDynamic = {};
|
|
5
|
+
const enumMethods = methods.concat('ANY');
|
|
6
|
+
for (const route1 of routes){
|
|
7
|
+
let { method , path , handler } = route1;
|
|
8
|
+
if (!enumMethods.includes(method)) {
|
|
9
|
+
throw new Error('[http-router] Method not allowed');
|
|
10
|
+
}
|
|
11
|
+
if (path.endsWith('/') && path !== '/') {
|
|
12
|
+
path = path.substr(0, path.length - 1);
|
|
13
|
+
}
|
|
14
|
+
if (path.indexOf('{') < 0) {
|
|
15
|
+
attachStaticRoute(method, path, handler, routesStatic);
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
attachDynamicRoute(method, path, handler, routesDynamic);
|
|
19
|
+
}
|
|
20
|
+
return (event, context, abort)=>{
|
|
21
|
+
const { method , path } = getVersionRoute[event.version ?? '1.0']?.(event);
|
|
22
|
+
if (!method) {
|
|
23
|
+
throw new Error('[http-router] Unknown http event format');
|
|
24
|
+
}
|
|
25
|
+
const handler = routesStatic[method]?.[path];
|
|
26
|
+
if (handler !== undefined) {
|
|
27
|
+
return handler(event, context, abort);
|
|
28
|
+
}
|
|
29
|
+
for (const route of routesDynamic[method] ?? []){
|
|
30
|
+
if (route.path.test(path)) {
|
|
31
|
+
return route.handler(event, context, abort);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
throw createError(404, 'Route does not exist');
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
const regexpDynamicWildcards = /\/\{proxy\+\}/g;
|
|
38
|
+
const regexpDynamicParameters = /\/\{.+\}/g;
|
|
39
|
+
const methods = [
|
|
40
|
+
'GET',
|
|
41
|
+
'POST',
|
|
42
|
+
'PUT',
|
|
43
|
+
'PATCH',
|
|
44
|
+
'DELETE',
|
|
45
|
+
'OPTIONS'
|
|
46
|
+
];
|
|
47
|
+
const attachStaticRoute = (method, path, handler, routesType)=>{
|
|
48
|
+
if (method === 'ANY') {
|
|
49
|
+
for (const method of methods){
|
|
50
|
+
attachStaticRoute(method, path, handler, routesType);
|
|
51
|
+
}
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (!routesType[method]) {
|
|
55
|
+
routesType[method] = {};
|
|
56
|
+
}
|
|
57
|
+
routesType[method][path] = handler;
|
|
58
|
+
};
|
|
59
|
+
const attachDynamicRoute = (method, path, handler, routesType)=>{
|
|
60
|
+
if (method === 'ANY') {
|
|
61
|
+
for (const method of methods){
|
|
62
|
+
attachDynamicRoute(method, path, handler, routesType);
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!routesType[method]) {
|
|
67
|
+
routesType[method] = [];
|
|
68
|
+
}
|
|
69
|
+
path = path.replace(regexpDynamicWildcards, '/?.*').replace(regexpDynamicParameters, '/.+');
|
|
70
|
+
path = new RegExp(`^${path}$`);
|
|
71
|
+
routesType[method].push({
|
|
72
|
+
path,
|
|
73
|
+
handler
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
const getVersionRoute = {
|
|
77
|
+
'1.0': (event)=>({
|
|
78
|
+
method: event.httpMethod,
|
|
79
|
+
path: event.path
|
|
80
|
+
}),
|
|
81
|
+
'2.0': (event)=>({
|
|
82
|
+
method: event.requestContext.http.method,
|
|
83
|
+
path: event.requestContext.http.path
|
|
84
|
+
})
|
|
85
|
+
};
|
|
86
|
+
export default httpRouteHandler;
|
|
87
|
+
|
|
2
88
|
|
|
3
89
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-router",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.1.0-rc.0",
|
|
4
4
|
"description": "HTTP event router for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,11 +10,17 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
+
"main": "./index.cjs",
|
|
13
14
|
"exports": {
|
|
14
15
|
".": {
|
|
15
|
-
"import":
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"types": "./index.d.ts",
|
|
22
|
+
"default": "./index.cjs"
|
|
23
|
+
}
|
|
18
24
|
}
|
|
19
25
|
},
|
|
20
26
|
"types": "index.d.ts",
|
|
@@ -55,10 +61,11 @@
|
|
|
55
61
|
},
|
|
56
62
|
"homepage": "https://middy.js.org",
|
|
57
63
|
"dependencies": {
|
|
58
|
-
"@middy/util": "
|
|
64
|
+
"@middy/util": "3.1.0-rc.0"
|
|
59
65
|
},
|
|
60
66
|
"devDependencies": {
|
|
61
|
-
"@middy/core": "
|
|
67
|
+
"@middy/core": "3.1.0-rc.0",
|
|
68
|
+
"@types/aws-lambda": "^8.10.97"
|
|
62
69
|
},
|
|
63
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "03a8794d3cdb4319eca49ba4c55420bea5d66430"
|
|
64
71
|
}
|