Skip to content

Commit c822b09

Browse files
committed
fix: ESM compatibility - avoid direct Response object mutation
Fixes #1429 In ESM environments with node-fetch v3, Response objects are read-only and cannot have new properties added. This change creates a wrapper object instead of mutating the Response directly, maintaining full backward compatibility while supporting modern ESM environments. The wrapper object: - Contains the custom data and error properties - Delegates all Response properties and methods to the original response - Works in both CommonJS and ESM environments - Maintains the same API surface for consumers
1 parent 291d9de commit c822b09

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

templates/base/http-clients/fetch-http-client.ejs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,29 @@ export class HttpClient<SecurityDataType = unknown> {
196196
body: typeof body === "undefined" || body === null ? null : payloadFormatter(body),
197197
}
198198
).then(async (response) => {
199-
const r = response as HttpResponse<T, E>;
200-
r.data = (null as unknown) as T;
201-
r.error = (null as unknown) as E;
199+
// Create a wrapper object that doesn't mutate the Response
200+
// This ensures compatibility with ESM environments where Response is read-only
201+
const r = {
202+
data: (null as unknown) as T,
203+
error: (null as unknown) as E,
204+
// Delegate Response properties
205+
ok: response.ok,
206+
status: response.status,
207+
statusText: response.statusText,
208+
headers: response.headers,
209+
url: response.url,
210+
redirected: response.redirected,
211+
type: response.type,
212+
body: response.body,
213+
bodyUsed: response.bodyUsed,
214+
// Delegate Response methods
215+
arrayBuffer: () => response.arrayBuffer(),
216+
blob: () => response.blob(),
217+
clone: () => response.clone(),
218+
formData: () => response.formData(),
219+
json: () => response.json(),
220+
text: () => response.text(),
221+
} as HttpResponse<T, E>;
202222

203223
const responseToParse = responseFormat ? response.clone() : response;
204224
const data = !responseFormat ? r : await responseToParse[responseFormat]()

0 commit comments

Comments
 (0)