@middy/http-router 7.2.3 → 7.3.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/index.d.ts +4 -0
- package/index.js +60 -1
- package/package.json +3 -6
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
|
-
import { createError } from "@middy/util";
|
|
3
|
+
import { createError, validateOptions } from "@middy/util";
|
|
4
4
|
|
|
5
5
|
const defaults = {
|
|
6
6
|
routes: [],
|
|
@@ -14,6 +14,65 @@ const defaults = {
|
|
|
14
14
|
|
|
15
15
|
const methods = ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"]; // ANY excluded by design
|
|
16
16
|
|
|
17
|
+
const optionSchema = {
|
|
18
|
+
type: "object",
|
|
19
|
+
properties: {
|
|
20
|
+
routes: {
|
|
21
|
+
type: "array",
|
|
22
|
+
items: {
|
|
23
|
+
type: "object",
|
|
24
|
+
required: ["method", "path", "handler"],
|
|
25
|
+
properties: {
|
|
26
|
+
method: { type: "string", enum: [...methods, "ANY"] },
|
|
27
|
+
path: {
|
|
28
|
+
type: "string",
|
|
29
|
+
pattern: "^/",
|
|
30
|
+
examples: ["/", "/users", "/users/{id}"],
|
|
31
|
+
},
|
|
32
|
+
handler: { instanceof: "Function" },
|
|
33
|
+
},
|
|
34
|
+
additionalProperties: false,
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
notFoundResponse: { instanceof: "Function" },
|
|
38
|
+
},
|
|
39
|
+
additionalProperties: false,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Normalize a route path for duplicate detection: drop non-root trailing slash
|
|
43
|
+
// and rewrite `{name}`/`{name+}` to `{_}`/`{_+}` so two routes that only
|
|
44
|
+
// differ by parameter name collide (they match the same requests).
|
|
45
|
+
// Inner class excludes `{` to keep matches linear (avoids polynomial
|
|
46
|
+
// backtracking on pathological inputs like `{{{{...`).
|
|
47
|
+
const normalizeRoutePath = (path) => {
|
|
48
|
+
if (path.endsWith("/") && path !== "/") path = path.slice(0, -1);
|
|
49
|
+
return path.replace(/\{[^/{}]+(\+?)\}/g, "{_$1}");
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const httpRouterValidateOptions = (options) => {
|
|
53
|
+
validateOptions("@middy/http-router", optionSchema, options);
|
|
54
|
+
const routes = options?.routes;
|
|
55
|
+
if (routes === undefined) return;
|
|
56
|
+
const seen = new Set();
|
|
57
|
+
for (const { method, path } of routes) {
|
|
58
|
+
const expanded = method === "ANY" ? methods : [method];
|
|
59
|
+
const normalized = normalizeRoutePath(path);
|
|
60
|
+
for (const m of expanded) {
|
|
61
|
+
const key = `${m} ${normalized}`;
|
|
62
|
+
if (seen.has(key)) {
|
|
63
|
+
throw new Error("Duplicate route", {
|
|
64
|
+
cause: {
|
|
65
|
+
package: "@middy/http-router",
|
|
66
|
+
data: { method: m, path },
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
seen.add(key);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return options;
|
|
74
|
+
};
|
|
75
|
+
|
|
17
76
|
const httpRouteHandler = (opts = {}) => {
|
|
18
77
|
let options;
|
|
19
78
|
if (Array.isArray(opts)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-router",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.3.1",
|
|
4
4
|
"description": "HTTP event router for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -17,9 +17,6 @@
|
|
|
17
17
|
"import": {
|
|
18
18
|
"types": "./index.d.ts",
|
|
19
19
|
"default": "./index.js"
|
|
20
|
-
},
|
|
21
|
-
"require": {
|
|
22
|
-
"default": "./index.js"
|
|
23
20
|
}
|
|
24
21
|
}
|
|
25
22
|
},
|
|
@@ -65,10 +62,10 @@
|
|
|
65
62
|
"url": "https://github.com/sponsors/willfarrell"
|
|
66
63
|
},
|
|
67
64
|
"dependencies": {
|
|
68
|
-
"@middy/util": "7.
|
|
65
|
+
"@middy/util": "7.3.1"
|
|
69
66
|
},
|
|
70
67
|
"devDependencies": {
|
|
71
|
-
"@middy/core": "7.
|
|
68
|
+
"@middy/core": "7.3.1",
|
|
72
69
|
"@types/aws-lambda": "^8.0.0",
|
|
73
70
|
"@types/node": "^22.0.0"
|
|
74
71
|
}
|