Skip to content

Commit a4d1370

Browse files
Update dependency typescript to v5.9.2 (#1391)
Resolve #1351 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-9.html#notable-behavioral-changes From TS5.9, more strict type check is added. Though warns are reported only in deprecated code, but let's fix to make build succeeded. --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
1 parent 046e478 commit a4d1370

File tree

3 files changed

+96
-17
lines changed

3 files changed

+96
-17
lines changed

lib/utils.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,35 @@ export function ensureJSON<T>(raw: T): T {
1313
}
1414
}
1515

16+
function toArrayBuffer(input: Uint8Array | Buffer): ArrayBuffer {
17+
if (input.buffer instanceof ArrayBuffer) {
18+
return input.buffer.slice(
19+
input.byteOffset,
20+
input.byteOffset + input.byteLength,
21+
);
22+
}
23+
const arrayBuffer = new ArrayBuffer(input.byteLength);
24+
new Uint8Array(arrayBuffer).set(input);
25+
return arrayBuffer;
26+
}
27+
1628
export function createMultipartFormData(
1729
this: FormData | void,
1830
formBody: Record<string, any>,
1931
): FormData {
2032
const formData = this instanceof FormData ? this : new FormData();
21-
Object.entries(formBody).forEach(([key, value]) => {
22-
if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
23-
formData.append(key, new Blob([value]));
33+
for (const [key, value] of Object.entries(formBody)) {
34+
if (value == null) continue;
35+
36+
if (value instanceof Blob) {
37+
formData.append(key, value);
38+
} else if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
39+
const arrayBuffer = toArrayBuffer(value);
40+
formData.append(key, new Blob([arrayBuffer]));
2441
} else {
2542
formData.append(key, String(value));
2643
}
27-
});
44+
}
45+
2846
return formData;
2947
}

package-lock.json

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

test/client.spec.ts

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { readFileSync } from "node:fs";
22
import { Buffer } from "node:buffer";
33
import { join } from "node:path";
4-
import { deepEqual, equal, ok, strictEqual } from "node:assert";
4+
import {
5+
deepEqual,
6+
deepStrictEqual,
7+
equal,
8+
ok,
9+
strictEqual,
10+
} from "node:assert";
511
import { URL } from "node:url";
612
import Client, { OAuth } from "../lib/client.js";
713
import * as Types from "../lib/types.js";
@@ -17,6 +23,7 @@ import {
1723

1824
import { describe, it, beforeAll, afterAll, afterEach } from "vitest";
1925
import { parseForm } from "./helpers/parse-form";
26+
import { Readable } from "node:stream";
2027

2128
const channelAccessToken = "test_channel_access_token";
2229

@@ -994,7 +1001,7 @@ describe("client", () => {
9941001
equal(scope.isDone(), true);
9951002
});
9961003

997-
it("createUploadAudienceGroupByFile", async () => {
1004+
it("createUploadAudienceGroupByFile with buffer", async () => {
9981005
const filepath = join(__dirname, "/helpers/line-icon.png");
9991006
const buffer = readFileSync(filepath);
10001007

@@ -1017,20 +1024,23 @@ describe("client", () => {
10171024
.startsWith(`multipart/form-data; boundary=`),
10181025
);
10191026

1020-
const blob = await request.blob();
1021-
const arrayBuffer = await blob.arrayBuffer();
1022-
const formData = parseForm(arrayBuffer);
1023-
equal(formData["description"], requestBody.description);
1027+
const formData = await request.formData();
1028+
equal(formData.get("description"), requestBody.description);
10241029
equal(
1025-
formData["isIfaAudience"],
1030+
formData.get("isIfaAudience"),
10261031
requestBody.isIfaAudience.toString(),
10271032
);
1028-
equal(formData["uploadDescription"], requestBody.uploadDescription);
10291033
equal(
1030-
Buffer.from(await (formData["file"] as Blob).arrayBuffer()),
1031-
requestBody.file.toString(),
1034+
formData.get("uploadDescription"),
1035+
requestBody.uploadDescription,
10321036
);
10331037

1038+
const filePart = formData.get("file");
1039+
ok(filePart instanceof Blob);
1040+
const sent = Buffer.from(await (filePart as Blob).arrayBuffer());
1041+
1042+
deepStrictEqual(sent, requestBody.file);
1043+
10341044
scope.done();
10351045
return HttpResponse.json({});
10361046
},
@@ -1041,6 +1051,57 @@ describe("client", () => {
10411051
equal(scope.isDone(), true);
10421052
});
10431053

1054+
it("createUploadAudienceGroupByFile with Readable stream", async () => {
1055+
const filepath = join(__dirname, "/helpers/line-icon.png");
1056+
const buffer = readFileSync(filepath);
1057+
1058+
const requestBody = {
1059+
description: "audienceGroupName",
1060+
isIfaAudience: true,
1061+
uploadDescription: "uploadDescription",
1062+
file: Readable.from(buffer),
1063+
};
1064+
1065+
const scope = new MSWResult();
1066+
server.use(
1067+
http.post(
1068+
DATA_API_PREFIX + "/audienceGroup/upload/byFile",
1069+
async ({ request }) => {
1070+
checkInterceptionOption(request, interceptionOption);
1071+
ok(
1072+
request.headers
1073+
.get("content-type")
1074+
.startsWith("multipart/form-data; boundary="),
1075+
);
1076+
1077+
const formData = await request.formData();
1078+
1079+
equal(formData.get("description"), requestBody.description);
1080+
equal(
1081+
formData.get("isIfaAudience"),
1082+
requestBody.isIfaAudience.toString(),
1083+
);
1084+
equal(
1085+
formData.get("uploadDescription"),
1086+
requestBody.uploadDescription,
1087+
);
1088+
1089+
const filePart = formData.get("file");
1090+
ok(filePart instanceof Blob);
1091+
1092+
const sent = Buffer.from(await (filePart as Blob).arrayBuffer());
1093+
deepStrictEqual(sent, buffer);
1094+
1095+
scope.done();
1096+
return HttpResponse.json({});
1097+
},
1098+
),
1099+
);
1100+
1101+
await client.createUploadAudienceGroupByFile(requestBody as any);
1102+
equal(scope.isDone(), true);
1103+
});
1104+
10441105
it("updateUploadAudienceGroup", async () => {
10451106
const requestBody = {
10461107
audienceGroupId: 4389303728991,

0 commit comments

Comments
 (0)