Repository: unjs/h3
Pull request #803 (2024-07-03 10:18:22)

fix(web, plain): keep params in `event.path`


Patch:
diff --git a/src/adapters/web/event.ts b/src/adapters/web/event.ts
index 82efc54..9c0d8df 100644
--- a/src/adapters/web/event.ts
+++ b/src/adapters/web/event.ts
@@ -7,6 +7,7 @@ export class WebEvent implements RawEvent {
   _req: Request;
 
   _path?: string;
+  _url?: URL;
   _originalPath?: string | undefined;
 
   _responseCode?: number;
@@ -35,7 +36,10 @@ export class WebEvent implements RawEvent {
 
   get path() {
     if (!this._path) {
-      this._path = this._originalPath = new URL(this._req.url).pathname;
+      if (!this._url) {
+        this._url = new URL(this._req.url);
+      }
+      this._path = this._originalPath = this._url.pathname + this._url.search;
     }
     return this._path;
   }


Test patch:
diff --git a/test/plain.test.ts b/test/plain.test.ts
index a0bef83..b713651 100644
--- a/test/plain.test.ts
+++ b/test/plain.test.ts
@@ -5,6 +5,7 @@ import {
   appendResponseHeader,
   setResponseStatus,
   getRequestHeaders,
+  getQuery,
 } from "../src";
 import { setupTest } from "./_setup";
 
@@ -32,6 +33,7 @@ describe("Plain handler", () => {
           headers: getRequestHeaders(event),
           body,
           contextKeys: Object.keys(event.context),
+          query: getQuery(event),
         };
       }),
     );
@@ -39,7 +41,7 @@ describe("Plain handler", () => {
     const res = await ctx.plainHandler(
       {
         method: "POST",
-        path: "/test/foo/bar",
+        path: "/test/foo/bar?test=123",
         headers: [["x-test", "true"]],
         body: "request body",
       },
@@ -61,13 +63,14 @@ describe("Plain handler", () => {
     expect(typeof res.body).toBe("string");
     expect(JSON.parse(res.body as string)).toMatchObject({
       method: "POST",
-      path: "/foo/bar",
+      path: "/foo/bar?test=123",
       body: "request body",
       headers: {
         "content-type": "text/plain;charset=UTF-8",
         "x-test": "true",
       },
       contextKeys: ["test"],
+      query: { test: "123" },
     });
   });
 });
diff --git a/test/web.test.ts b/test/web.test.ts
index 0203b7e..d10fd1a 100644
--- a/test/web.test.ts
+++ b/test/web.test.ts
@@ -4,6 +4,7 @@ import {
   readTextBody,
   setResponseStatus,
   getRequestHeaders,
+  getQuery,
 } from "../src";
 import { setupTest } from "./_setup";
 
@@ -20,6 +21,7 @@ describe("Web handler", () => {
           method: event.method,
           path: event.path,
           headers: getRequestHeaders(event),
+          query: getQuery(event),
           body,
           contextKeys: Object.keys(event.context),
         };
@@ -27,7 +29,7 @@ describe("Web handler", () => {
     );
 
     const res = await ctx.webHandler(
-      new Request(new URL("/test/foo/bar", "http://localhost"), {
+      new Request(new URL("/test/foo/bar?test=123", "http://localhost"), {
         method: "POST",
         headers: {
           "X-Test": "true",
@@ -47,12 +49,15 @@ describe("Web handler", () => {
 
     expect(await res.json()).toMatchObject({
       method: "POST",
-      path: "/foo/bar",
+      path: "/foo/bar?test=123",
       body: "request body",
       headers: {
         "content-type": "text/plain;charset=UTF-8",
         "x-test": "true",
       },
+      query: {
+        test: "123",
+      },
       contextKeys: ["test"],
     });
   });