Skip to content

Commit

Permalink
Use Node.js built-in FormData (#709)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dlackty and tokuhirom authored Feb 27, 2024
1 parent 34a2e56 commit 18dc03e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
10 changes: 4 additions & 6 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 6 additions & 2 deletions lib/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ export default class HTTPClient {
return res.data;
}

public async putFormMultipart<T>(url: string, form: FormData): Promise<T> {
const res = await this.instance.put<T>(url, form);
public async putFormMultipart<T>(
url: string,
form: FormData,
config?: Partial<AxiosRequestConfig>,
): Promise<T> {
const res = await this.instance.put<T>(url, form, config);
return res.data;
}

Expand Down
3 changes: 1 addition & 2 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { JSONParseError } from "./exceptions";
import * as FormData from "form-data";

export function toArray<T>(maybeArr: T | T[]): T[] {
return Array.isArray(maybeArr) ? maybeArr : [maybeArr];
Expand All @@ -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));
}
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 18dc03e

Please sign in to comment.