Skip to content

Commit 839fb08

Browse files
committed
fix: ESM compatibility - avoid direct Response object mutation
When using fetch in ESM environments (e.g., node-fetch v3+), the Response object is read-only and attempting to set properties like 'data' and 'error' directly throws an error. This fix creates a wrapper object that delegates to the original Response while allowing the assignment of custom properties, ensuring compatibility with both CommonJS and ESM environments. Fixes compatibility with node-fetch v3+ and other ESM-only fetch implementations.
1 parent 291d9de commit 839fb08

File tree

29 files changed

+2645
-345
lines changed

29 files changed

+2645
-345
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)