From 18dc03e28dfff8c18dc626d952139bb29fe8a982 Mon Sep 17 00:00:00 2001 From: Richard Lee <14349+dlackty@users.noreply.github.com> Date: Tue, 27 Feb 2024 09:59:17 +0800 Subject: [PATCH] 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 --- lib/client.ts | 10 ++++------ lib/http.ts | 8 ++++++-- lib/utils.ts | 3 +-- package-lock.json | 3 +-- package.json | 3 +-- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/client.ts b/lib/client.ts index 1f5a2c6cd..5b66804ed 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -642,14 +642,12 @@ export default class Client { }) { const file = await this.http.toBuffer(uploadAudienceGroup.file); const body = createMultipartFormData({ ...uploadAudienceGroup, file }); - const res = await this.http.post<{ + const res = await this.http.postFormMultipart<{ audienceGroupId: number; type: "UPLOAD"; description: string; created: number; - }>(`${DATA_API_PREFIX}/audienceGroup/upload/byFile`, body, { - headers: body.getHeaders(), - }); + }>(`${DATA_API_PREFIX}/audienceGroup/upload/byFile`, body); return ensureJSON(res); } @@ -685,10 +683,10 @@ export default class Client { const file = await this.http.toBuffer(uploadAudienceGroup.file); const body = createMultipartFormData({ ...uploadAudienceGroup, file }); - const res = await this.http.put<{}>( + const res = await this.http.putFormMultipart<{}>( `${DATA_API_PREFIX}/audienceGroup/upload/byFile`, body, - { headers: body.getHeaders(), ...httpConfig }, + httpConfig, ); return ensureJSON(res); } diff --git a/lib/http.ts b/lib/http.ts index aca540e1d..6d4e99966 100644 --- a/lib/http.ts +++ b/lib/http.ts @@ -106,8 +106,12 @@ export default class HTTPClient { return res.data; } - public async putFormMultipart(url: string, form: FormData): Promise { - const res = await this.instance.put(url, form); + public async putFormMultipart( + url: string, + form: FormData, + config?: Partial, + ): Promise { + const res = await this.instance.put(url, form, config); return res.data; } diff --git a/lib/utils.ts b/lib/utils.ts index 571e092da..973032352 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,5 +1,4 @@ import { JSONParseError } from "./exceptions"; -import * as FormData from "form-data"; export function toArray(maybeArr: T | T[]): T[] { return Array.isArray(maybeArr) ? maybeArr : [maybeArr]; @@ -20,7 +19,7 @@ export function createMultipartFormData( const formData = this instanceof FormData ? this : new FormData(); Object.entries(formBody).forEach(([key, value]) => { if (Buffer.isBuffer(value) || value instanceof Uint8Array) { - formData.append(key, value); + formData.append(key, new Blob([value])); } else { formData.append(key, String(value)); } diff --git a/package-lock.json b/package-lock.json index 67a0684f7..1947c733c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,8 +13,7 @@ "@types/node": "^20.0.0", "axios": "^1.0.0", "body-parser": "^1.20.0", - "file-type": "^16.5.4", - "form-data": "^4.0.0" + "file-type": "^16.5.4" }, "devDependencies": { "@types/express": "4.17.21", diff --git a/package.json b/package.json index 6e9526172..e4673cdc5 100644 --- a/package.json +++ b/package.json @@ -42,8 +42,7 @@ "@types/node": "^20.0.0", "axios": "^1.0.0", "body-parser": "^1.20.0", - "file-type": "^16.5.4", - "form-data": "^4.0.0" + "file-type": "^16.5.4" }, "devDependencies": { "@types/express": "4.17.21",