Skip to content

Commit afbd717

Browse files
committed
chore: add support for outgoing multipart/form-data
1 parent 5d10ca6 commit afbd717

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

src/lib/web.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ export function mergeUrlQuery(url: string, query: Record<string, string | string
2929
return parsedUrl.toString();
3030
}
3131

32-
export function determineRequestBody(method: FetchMethod, body: any): [string | undefined, string | undefined] {
32+
export function determineRequestBody(method: FetchMethod, body: any): [string | FormData | undefined, string | undefined] {
3333
switch (true) {
3434
case method === FetchMethod.GET:
3535
return [undefined, undefined];
3636
case (body instanceof URLSearchParams):
3737
return [body.toString(), 'application/x-www-form-urlencoded'];
38+
case (body instanceof FormData):
39+
return [body, undefined];
3840
case (body instanceof ArrayBuffer || body instanceof TypedArray): {
3941
return [body, undefined];
4042
}

src/nodes/Web.Fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type P = {
2121
type R = Promise<unknown>;
2222

2323
export const module: ModuleDefinition<P, R> = {
24-
version: '1.7.2',
24+
version: '1.7.3',
2525
moduleName: 'Web / Fetch',
2626
description: `
2727
Sends an HTTP request using natively available Fetch API.

src/nodes/Web.FormData.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { ModuleCompute, ModuleDefinition } from '@nodescript/core/types';
2+
3+
type P = {
4+
params: Record<string, any>;
5+
};
6+
7+
type R = any;
8+
9+
export const module: ModuleDefinition<P, R> = {
10+
version: '1.0.0',
11+
moduleName: 'Web / Form Data',
12+
description: `
13+
Creates a Form Data compatible with Fetch request body.
14+
Used to send multipart/form-data requests.
15+
`,
16+
params: {
17+
params: {
18+
schema: {
19+
type: 'object',
20+
properties: {},
21+
additionalProperties: { type: 'any' },
22+
},
23+
},
24+
},
25+
result: {
26+
schema: { type: 'any' },
27+
},
28+
};
29+
30+
export const compute: ModuleCompute<P, R> = params => {
31+
const formData = new FormData();
32+
for (const [key, value] of Object.entries(params.params)) {
33+
if (value == null) {
34+
continue;
35+
}
36+
const arr = Array.isArray(value) ? value : [value];
37+
for (const value of arr) {
38+
if (value instanceof ArrayBuffer) {
39+
formData.append(key, new Blob([value]));
40+
} else {
41+
formData.append(key, value);
42+
}
43+
}
44+
}
45+
return formData;
46+
};

src/nodes/Web.HttpRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type P = {
2929
type R = Promise<unknown>;
3030

3131
export const module: ModuleDefinition<P, R> = {
32-
version: '2.6.2',
32+
version: '2.6.3',
3333
moduleName: 'Web / Http Request',
3434
description: `
3535
Sends an HTTP request using backend-powered HTTP client.

0 commit comments

Comments
 (0)