Skip to content

Commit

Permalink
fix(node-fetch): support native File
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Dec 16, 2023
1 parent b931ed4 commit f844a4d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fluffy-mangos-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@whatwg-node/node-fetch": patch
---

Support native File
6 changes: 5 additions & 1 deletion packages/node-fetch/src/FormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
15 changes: 15 additions & 0 deletions packages/node-fetch/tests/FormData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!`);
});
}
});

0 comments on commit f844a4d

Please sign in to comment.