Skip to content

Commit 18dc03e

Browse files
dlacktytokuhirom
andauthored
Use Node.js built-in FormData (#709)
This should fix #708. There's FormData class built-in since Node.js 18, so we should just use that. Co-authored-by: Tokuhiro Matsuno <tokuhirom@gmail.com>
1 parent 34a2e56 commit 18dc03e

File tree

5 files changed

+13
-14
lines changed

5 files changed

+13
-14
lines changed

lib/client.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -642,14 +642,12 @@ export default class Client {
642642
}) {
643643
const file = await this.http.toBuffer(uploadAudienceGroup.file);
644644
const body = createMultipartFormData({ ...uploadAudienceGroup, file });
645-
const res = await this.http.post<{
645+
const res = await this.http.postFormMultipart<{
646646
audienceGroupId: number;
647647
type: "UPLOAD";
648648
description: string;
649649
created: number;
650-
}>(`${DATA_API_PREFIX}/audienceGroup/upload/byFile`, body, {
651-
headers: body.getHeaders(),
652-
});
650+
}>(`${DATA_API_PREFIX}/audienceGroup/upload/byFile`, body);
653651
return ensureJSON(res);
654652
}
655653

@@ -685,10 +683,10 @@ export default class Client {
685683
const file = await this.http.toBuffer(uploadAudienceGroup.file);
686684
const body = createMultipartFormData({ ...uploadAudienceGroup, file });
687685

688-
const res = await this.http.put<{}>(
686+
const res = await this.http.putFormMultipart<{}>(
689687
`${DATA_API_PREFIX}/audienceGroup/upload/byFile`,
690688
body,
691-
{ headers: body.getHeaders(), ...httpConfig },
689+
httpConfig,
692690
);
693691
return ensureJSON(res);
694692
}

lib/http.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,12 @@ export default class HTTPClient {
106106
return res.data;
107107
}
108108

109-
public async putFormMultipart<T>(url: string, form: FormData): Promise<T> {
110-
const res = await this.instance.put<T>(url, form);
109+
public async putFormMultipart<T>(
110+
url: string,
111+
form: FormData,
112+
config?: Partial<AxiosRequestConfig>,
113+
): Promise<T> {
114+
const res = await this.instance.put<T>(url, form, config);
111115
return res.data;
112116
}
113117

lib/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { JSONParseError } from "./exceptions";
2-
import * as FormData from "form-data";
32

43
export function toArray<T>(maybeArr: T | T[]): T[] {
54
return Array.isArray(maybeArr) ? maybeArr : [maybeArr];
@@ -20,7 +19,7 @@ export function createMultipartFormData(
2019
const formData = this instanceof FormData ? this : new FormData();
2120
Object.entries(formBody).forEach(([key, value]) => {
2221
if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
23-
formData.append(key, value);
22+
formData.append(key, new Blob([value]));
2423
} else {
2524
formData.append(key, String(value));
2625
}

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
"@types/node": "^20.0.0",
4343
"axios": "^1.0.0",
4444
"body-parser": "^1.20.0",
45-
"file-type": "^16.5.4",
46-
"form-data": "^4.0.0"
45+
"file-type": "^16.5.4"
4746
},
4847
"devDependencies": {
4948
"@types/express": "4.17.21",

0 commit comments

Comments
 (0)