From 0b0efedbf57859b3569a04c94448a297f354f870 Mon Sep 17 00:00:00 2001 From: Will-hxw <1176843521@qq.com> Date: Thu, 23 Apr 2026 01:59:10 +0800 Subject: [PATCH] fix(client): preserve Request semantics in createProxyFetch When createProxyFetch() is called with a Request object instead of (url, init), the original implementation only forwarded request.url and dropped method, headers, and body. This caused proxy payloads to lose request semantics. Now extracts method, headers, and body from Request inputs, with init still able to override those values when provided. Fixes #1207 --- client/src/lib/proxyFetch.ts | 38 ++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/client/src/lib/proxyFetch.ts b/client/src/lib/proxyFetch.ts index a0ff5977f..54a7f5215 100644 --- a/client/src/lib/proxyFetch.ts +++ b/client/src/lib/proxyFetch.ts @@ -99,16 +99,36 @@ export function createProxyFetch(config: InspectorConfig): typeof fetch { ? input.url : input.toString(); - // Serialize body for JSON transport. URLSearchParams and similar don't - // JSON-serialize (they become {}), so we must convert to string first. - let serializedBody: string | undefined; + // Extract Request semantics when input is a Request object. + // init overrides take precedence so callers can still customize. + const method = + init?.method ?? (input instanceof Request ? input.method : undefined); + const headers = init?.headers + ? Object.fromEntries(new Headers(init.headers)) + : input instanceof Request + ? Object.fromEntries(new Headers(input.headers)) + : undefined; + let body: string | undefined; if (init?.body != null) { if (typeof init.body === "string") { - serializedBody = init.body; + body = init.body; } else if (init.body instanceof URLSearchParams) { - serializedBody = init.body.toString(); + body = init.body.toString(); + } else { + body = String(init.body); + } + } else if (input instanceof Request && input.body !== null) { + body = String(input.body); + } + + // Serialize body for JSON transport. URLSearchParams and similar don't + // JSON-serialize (they become {}), so we must convert to string first. + let serializedBody: string | undefined; + if (body != null) { + if (typeof body === "string") { + serializedBody = body; } else { - serializedBody = String(init.body); + serializedBody = String(body); } } @@ -121,10 +141,8 @@ export function createProxyFetch(config: InspectorConfig): typeof fetch { body: JSON.stringify({ url, init: { - method: init?.method, - headers: init?.headers - ? Object.fromEntries(new Headers(init.headers)) - : undefined, + method, + headers, body: serializedBody, }, }),