@middy/http-router 3.0.3 → 3.1.0-rc.1
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.cjs +93 -1
- package/index.d.ts +3 -3
- package/index.js +87 -1
- package/package.json +13 -7
package/README.md
CHANGED
|
@@ -63,7 +63,7 @@ import middy from '@middy/core'
|
|
|
63
63
|
import validatorMiddleware from '@middy/validator'
|
|
64
64
|
|
|
65
65
|
const getHandler = middy()
|
|
66
|
-
.use(validatorMiddleware({
|
|
66
|
+
.use(validatorMiddleware({eventSchema: {...} }))
|
|
67
67
|
.handler((event, context) => {
|
|
68
68
|
return {
|
|
69
69
|
statusCode: 200,
|
|
@@ -72,7 +72,7 @@ const getHandler = middy()
|
|
|
72
72
|
})
|
|
73
73
|
|
|
74
74
|
const postHandler = middy()
|
|
75
|
-
.use(validatorMiddleware({
|
|
75
|
+
.use(validatorMiddleware({eventSchema: {...} }))
|
|
76
76
|
.handler((event, context) => {
|
|
77
77
|
return {
|
|
78
78
|
statusCode: 200,
|
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
|
@@ -5,14 +5,14 @@ import {
|
|
|
5
5
|
Handler as LambdaHandler
|
|
6
6
|
} from 'aws-lambda'
|
|
7
7
|
|
|
8
|
-
type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'ANY'
|
|
8
|
+
export type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'ANY'
|
|
9
9
|
|
|
10
|
-
interface Route {
|
|
10
|
+
export interface Route {
|
|
11
11
|
method: Method
|
|
12
12
|
path: string
|
|
13
13
|
handler: LambdaHandler<APIGatewayProxyEvent, APIGatewayProxyResult>
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
declare function httpRouterHandler (routes: Route[]): middy.
|
|
16
|
+
declare function httpRouterHandler (routes: Route[]): middy.MiddyfiedHandler
|
|
17
17
|
|
|
18
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.1",
|
|
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,11 +61,11 @@
|
|
|
55
61
|
},
|
|
56
62
|
"homepage": "https://middy.js.org",
|
|
57
63
|
"dependencies": {
|
|
58
|
-
"@middy/util": "3.0.
|
|
64
|
+
"@middy/util": "3.1.0-rc.1"
|
|
59
65
|
},
|
|
60
66
|
"devDependencies": {
|
|
61
|
-
"@middy/core": "3.0.
|
|
67
|
+
"@middy/core": "3.1.0-rc.1",
|
|
62
68
|
"@types/aws-lambda": "^8.10.97"
|
|
63
69
|
},
|
|
64
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "044c397e7a2b1de516b4b5c21ece2baffdbfa771"
|
|
65
71
|
}
|