Skip to content

Commit 78c44fc

Browse files
committed
chore: FormData supports filename
1 parent afbd717 commit 78c44fc

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/nodes/Web.FormData.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type P = {
77
type R = any;
88

99
export const module: ModuleDefinition<P, R> = {
10-
version: '1.0.0',
10+
version: '1.1.4',
1111
moduleName: 'Web / Form Data',
1212
description: `
1313
Creates a Form Data compatible with Fetch request body.
@@ -30,15 +30,25 @@ export const module: ModuleDefinition<P, R> = {
3030
export const compute: ModuleCompute<P, R> = params => {
3131
const formData = new FormData();
3232
for (const [key, value] of Object.entries(params.params)) {
33-
if (value == null) {
34-
continue;
35-
}
3633
const arr = Array.isArray(value) ? value : [value];
3734
for (const value of arr) {
38-
if (value instanceof ArrayBuffer) {
35+
if (value == null) {
36+
continue;
37+
}
38+
if (typeof value === 'object' && typeof value.filename === 'string') {
39+
// Data must be a Blob in this case
40+
const data = typeof value.data === 'string' ?
41+
new TextEncoder().encode(value.data).buffer : value.data;
42+
if (!(data instanceof ArrayBuffer)) {
43+
throw new TypeError('Value must be a string, ArrayBuffer or an object with { filename: string, data: string | ArrayBuffer }');
44+
}
45+
formData.append(key, new Blob([data]), value.filename);
46+
} else if (typeof value === 'string') {
47+
formData.append(key, value);
48+
} else if (value instanceof ArrayBuffer) {
3949
formData.append(key, new Blob([value]));
4050
} else {
41-
formData.append(key, value);
51+
throw new TypeError('Value must be a string, ArrayBuffer or an object with { filename: string, data: string | ArrayBuffer }');
4252
}
4353
}
4454
}

0 commit comments

Comments
 (0)