@middy/http-router 7.3.0 → 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.
Files changed (2) hide show
  1. package/index.js +56 -5
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -12,15 +12,66 @@ const defaults = {
12
12
  },
13
13
  };
14
14
 
15
+ const methods = ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"]; // ANY excluded by design
16
+
15
17
  const optionSchema = {
16
- routes: "array?",
17
- notFoundResponse: "function?",
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,
18
40
  };
19
41
 
20
- export const httpRouterValidateOptions = (options) =>
21
- validateOptions("@middy/http-router", optionSchema, options);
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
+ };
22
51
 
23
- const methods = ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"]; // ANY excluded by design
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
+ };
24
75
 
25
76
  const httpRouteHandler = (opts = {}) => {
26
77
  let options;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-router",
3
- "version": "7.3.0",
3
+ "version": "7.3.1",
4
4
  "description": "HTTP event router for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -62,10 +62,10 @@
62
62
  "url": "https://github.com/sponsors/willfarrell"
63
63
  },
64
64
  "dependencies": {
65
- "@middy/util": "7.3.0"
65
+ "@middy/util": "7.3.1"
66
66
  },
67
67
  "devDependencies": {
68
- "@middy/core": "7.3.0",
68
+ "@middy/core": "7.3.1",
69
69
  "@types/aws-lambda": "^8.0.0",
70
70
  "@types/node": "^22.0.0"
71
71
  }