From f844a4da5ddc8888b105b509fee7794c6217fd98 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Sat, 16 Dec 2023 18:18:43 +0300 Subject: [PATCH] fix(node-fetch): support native File --- .changeset/fluffy-mangos-rescue.md | 5 +++++ packages/node-fetch/src/FormData.ts | 6 +++++- packages/node-fetch/tests/FormData.spec.ts | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 .changeset/fluffy-mangos-rescue.md diff --git a/.changeset/fluffy-mangos-rescue.md b/.changeset/fluffy-mangos-rescue.md new file mode 100644 index 00000000000..fdb67d5f53a --- /dev/null +++ b/.changeset/fluffy-mangos-rescue.md @@ -0,0 +1,5 @@ +--- +"@whatwg-node/node-fetch": patch +--- + +Support native File diff --git a/packages/node-fetch/src/FormData.ts b/packages/node-fetch/src/FormData.ts index 398740f4adf..9cdb7443be4 100644 --- a/packages/node-fetch/src/FormData.ts +++ b/packages/node-fetch/src/FormData.ts @@ -134,7 +134,11 @@ export function getStreamFromFormData( } function getNormalizedFile(name: string, blob: PonyfillBlob, fileName?: string) { - (blob as PonyfillFile).name = fileName || blob.name || name; + Object.defineProperty(blob as PonyfillFile, 'name', { + configurable: true, + enumerable: true, + value: fileName || blob.name || name, + }); return blob as PonyfillFile; } diff --git a/packages/node-fetch/tests/FormData.spec.ts b/packages/node-fetch/tests/FormData.spec.ts index 44bc02058f1..334f481352a 100644 --- a/packages/node-fetch/tests/FormData.spec.ts +++ b/packages/node-fetch/tests/FormData.spec.ts @@ -150,4 +150,19 @@ describe('Form Data', () => { expect(requestText).toContain(`Content-Type: text/plain`); expect(requestText).toContain(`Hello world!`); }); + if (globalThis.File) { + it('support native File', async () => { + const formData = new PonyfillFormData(); + const file = new globalThis.File(['Hello world!'], 'greetings.txt', { type: 'text/plain' }); + formData.append('greetings', file as any); + const request = new PonyfillRequest('http://localhost:8080', { + method: 'POST', + body: formData, + }); + const requestText = await request.text(); + expect(requestText).toContain(`Content-Disposition: form-data; name="greetings"`); + expect(requestText).toContain(`Content-Type: text/plain`); + expect(requestText).toContain(`Hello world!`); + }); + } });