@whatwg-node/fetch 0.4.4 → 0.4.5-alpha-20220925233002-c4d7b92
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/getFormDataMethod.js +2 -2
- package/package.json +1 -1
- package/tests/getFormDataMethod.spec.ts +43 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @whatwg-node/fetch
|
|
2
2
|
|
|
3
|
+
## 0.4.5-alpha-20220925233002-c4d7b92
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#140](https://github.com/ardatan/whatwg-node/pull/140) [`77b4592`](https://github.com/ardatan/whatwg-node/commit/77b4592a6ee5aa35ff92c4f8a76c2f93127d8045) Thanks [@ardatan](https://github.com/ardatan)! - Fix Request.formData method
|
|
8
|
+
|
|
3
9
|
## 0.4.4
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -16,13 +16,13 @@ module.exports = function getFormDataMethod(File, limits) {
|
|
|
16
16
|
reject(new Error(`File size limit exceeded: ${limits.fileSize} bytes`));
|
|
17
17
|
})
|
|
18
18
|
fileStream.on('data', (chunk) => {
|
|
19
|
-
chunks.push(chunk);
|
|
19
|
+
chunks.push(...chunk);
|
|
20
20
|
})
|
|
21
21
|
fileStream.on('close', () => {
|
|
22
22
|
if (fileStream.truncated) {
|
|
23
23
|
reject(new Error(`File size limit exceeded: ${limits.fileSize} bytes`));
|
|
24
24
|
}
|
|
25
|
-
const file = new File(chunks, filename, { type: mimeType });
|
|
25
|
+
const file = new File([new Uint8Array(chunks)], filename, { type: mimeType });
|
|
26
26
|
formData.set(name, file);
|
|
27
27
|
resolve(file);
|
|
28
28
|
});
|
package/package.json
CHANGED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createFetch } from '@whatwg-node/fetch';
|
|
2
|
+
|
|
3
|
+
describe('getFormDataMethod', () => {
|
|
4
|
+
['fieldsFirst:true', 'fieldsFirst:false'].forEach(fieldsFirstFlag => {
|
|
5
|
+
const fetchAPI = createFetch({
|
|
6
|
+
formDataLimits: {
|
|
7
|
+
fieldsFirst: fieldsFirstFlag === 'fieldsFirst:true',
|
|
8
|
+
},
|
|
9
|
+
});
|
|
10
|
+
describe(fieldsFirstFlag, () => {
|
|
11
|
+
it('should parse fields correctly', async () => {
|
|
12
|
+
const formData = new fetchAPI.FormData();
|
|
13
|
+
formData.append('greetings', 'Hello world!');
|
|
14
|
+
formData.append('bye', 'Goodbye world!');
|
|
15
|
+
const request = new fetchAPI.Request('http://localhost:8080', {
|
|
16
|
+
method: 'POST',
|
|
17
|
+
body: formData,
|
|
18
|
+
});
|
|
19
|
+
const formdata = await request.formData();
|
|
20
|
+
expect(formdata.get('greetings')).toBe('Hello world!');
|
|
21
|
+
expect(formdata.get('bye')).toBe('Goodbye world!');
|
|
22
|
+
});
|
|
23
|
+
it('should parse and receive text files correctly', async () => {
|
|
24
|
+
const formData = new fetchAPI.FormData();
|
|
25
|
+
const greetingsFile = new fetchAPI.File(['Hello world!'], 'greetings.txt', { type: 'text/plain' });
|
|
26
|
+
const byeFile = new fetchAPI.File(['Goodbye world!'], 'bye.txt', { type: 'text/plain' });
|
|
27
|
+
formData.append('greetings', greetingsFile);
|
|
28
|
+
formData.append('bye', byeFile);
|
|
29
|
+
const request = new fetchAPI.Request('http://localhost:8080', {
|
|
30
|
+
method: 'POST',
|
|
31
|
+
body: formData,
|
|
32
|
+
});
|
|
33
|
+
const formdata = await request.formData();
|
|
34
|
+
const receivedGreetingsFile = formdata.get('greetings') as File;
|
|
35
|
+
const receivedGreetingsText = await receivedGreetingsFile.text();
|
|
36
|
+
expect(receivedGreetingsText).toBe('Hello world!');
|
|
37
|
+
const receivedByeFile = formdata.get('bye') as File;
|
|
38
|
+
const receivedByeText = await receivedByeFile.text();
|
|
39
|
+
expect(receivedByeText).toBe('Goodbye world!');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
});
|