@middy/http-router 7.2.2 → 7.3.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.
Files changed (3) hide show
  1. package/index.d.ts +4 -0
  2. package/index.js +26 -9
  3. package/package.json +3 -6
package/index.d.ts CHANGED
@@ -51,4 +51,8 @@ declare function httpRouterHandler<
51
51
  },
52
52
  ): middy.MiddyfiedHandler<TEvent, TResult>;
53
53
 
54
+ export declare function httpRouterValidateOptions(
55
+ options?: Record<string, unknown>,
56
+ ): void;
57
+
54
58
  export default httpRouterHandler;
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: [],
@@ -12,6 +12,14 @@ const defaults = {
12
12
  },
13
13
  };
14
14
 
15
+ const optionSchema = {
16
+ routes: "array?",
17
+ notFoundResponse: "function?",
18
+ };
19
+
20
+ export const httpRouterValidateOptions = (options) =>
21
+ validateOptions("@middy/http-router", optionSchema, options);
22
+
15
23
  const methods = ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"]; // ANY excluded by design
16
24
 
17
25
  const httpRouteHandler = (opts = {}) => {
@@ -54,14 +62,20 @@ const httpRouteHandler = (opts = {}) => {
54
62
  const { method, path } = getVersionRoute[pickVersion(event)](event);
55
63
 
56
64
  if (!method) {
57
- throw new Error("Unknown HTTP event format", {
58
- cause: { package: "@middy/http-router", data: { method } },
59
- });
65
+ throw new Error(
66
+ "Unknown HTTP event format: missing HTTP method. Expected 'httpMethod' (v1), 'requestContext.http.method' (v2), or 'method' (VPC)",
67
+ {
68
+ cause: { package: "@middy/http-router", data: { method } },
69
+ },
70
+ );
60
71
  }
61
72
  if (!path) {
62
- throw new Error("Unknown HTTP event format", {
63
- cause: { package: "@middy/http-router", data: { path } },
64
- });
73
+ throw new Error(
74
+ "Unknown HTTP event format: missing path. Expected 'path' (v1), 'requestContext.http.path' (v2), or 'raw_path' (VPC)",
75
+ {
76
+ cause: { package: "@middy/http-router", data: { path } },
77
+ },
78
+ );
65
79
  }
66
80
 
67
81
  // Static
@@ -90,8 +104,9 @@ const httpRouteHandler = (opts = {}) => {
90
104
  return handler;
91
105
  };
92
106
 
93
- const regExpDynamicWildcards = /\/\{(proxy)\+\}$/;
94
- const regExpDynamicParameters = /\/\{([^/]+)\}/g;
107
+ const regExpEscapeChars = /[.+?^${}()|[\]\\]/g;
108
+ const regExpDynamicWildcards = /\/\\\{(proxy)\\\+\\\}$/;
109
+ const regExpDynamicParameters = /\/\\\{([^/]+)\\\}/g;
95
110
 
96
111
  const attachStaticRoute = (method, path, handler, routesType) => {
97
112
  if (method === "ANY") {
@@ -101,6 +116,7 @@ const attachStaticRoute = (method, path, handler, routesType) => {
101
116
  return;
102
117
  }
103
118
  routesType[method] ??= {};
119
+ // TODO v8 when duplicates throw error
104
120
  routesType[method][path] = handler;
105
121
  routesType[method][`${path}/`] = handler; // Optional `/`
106
122
  };
@@ -114,6 +130,7 @@ const attachDynamicRoute = (method, path, handler, routesType) => {
114
130
  }
115
131
  routesType[method] ??= [];
116
132
  const pathPartialRegExp = path
133
+ .replace(regExpEscapeChars, "\\$&")
117
134
  .replace(regExpDynamicWildcards, "/?(?<$1>.*)")
118
135
  .replace(regExpDynamicParameters, "/(?<$1>[^/]+)");
119
136
  // SAST Skipped: Not accessible by users
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-router",
3
- "version": "7.2.2",
3
+ "version": "7.3.0",
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.2.2"
65
+ "@middy/util": "7.3.0"
69
66
  },
70
67
  "devDependencies": {
71
- "@middy/core": "7.2.2",
68
+ "@middy/core": "7.3.0",
72
69
  "@types/aws-lambda": "^8.0.0",
73
70
  "@types/node": "^22.0.0"
74
71
  }