Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test Changelog CI (should not run) #259

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion docs/api-reference/client.md
Expand Up @@ -93,15 +93,36 @@ class Client {
created: number;
requestId: string;
}>
createUploadAudienceGroupByFile(uploadAudienceGroup: {
description: string;
isIfaAudience?: boolean;
uploadDescription?: string;
file: Buffer | Readable;
}) : Promise<{
audienceGroupId: number;
type: "UPLOAD";
description: string;
created: number;
}>
updateUploadAudienceGroup(
uploadAudienceGroup: {
audienceGroupId: number;
description?: string;
uploadDescription?: string;
audiences: { id: string }[];
},
// for set request timeout
httpConfig?: Partial<AxiosRequestConfig>,
): Promise<{}>
) : Promise<{}>
createUploadAudienceGroupByFile(
uploadAudienceGroup: {
audienceGroupId: number;
uploadDescription?: string;
file: Buffer | Readable;
},
// for set request timeout
httpConfig?: Partial<AxiosRequestConfig>,
}) : Promise<{}>
createClickAudienceGroup(clickAudienceGroup: {
description: string;
requestId: string;
Expand Down
42 changes: 40 additions & 2 deletions lib/client.ts
Expand Up @@ -2,8 +2,7 @@ import { Readable } from "stream";
import HTTPClient from "./http";
import * as Types from "./types";
import { AxiosResponse, AxiosRequestConfig } from "axios";

import { ensureJSON, toArray } from "./utils";
import { createMultipartFormData, ensureJSON, toArray } from "./utils";

type ChatType = "group" | "room";
type RequestOption = {
Expand Down Expand Up @@ -470,6 +469,25 @@ export default class Client {
return ensureJSON(res);
}

public async createUploadAudienceGroupByFile(uploadAudienceGroup: {
description: string;
isIfaAudience?: boolean;
uploadDescription?: string;
file: Buffer | Readable;
}) {
const file = await this.http.toBuffer(uploadAudienceGroup.file);
const body = createMultipartFormData({ ...uploadAudienceGroup, file });
const res = await this.http.post<{
audienceGroupId: number;
type: "UPLOAD";
description: string;
created: number;
}>(`${DATA_API_PREFIX}/audienceGroup/upload/byFile`, body, {
headers: body.getHeaders(),
});
return ensureJSON(res);
}

public async updateUploadAudienceGroup(
uploadAudienceGroup: {
audienceGroupId: number;
Expand All @@ -490,6 +508,26 @@ export default class Client {
return ensureJSON(res);
}

public async updateUploadAudienceGroupByFile(
uploadAudienceGroup: {
audienceGroupId: number;
uploadDescription?: string;
file: Buffer | Readable;
},
// for set request timeout
httpConfig?: Partial<AxiosRequestConfig>,
) {
const file = await this.http.toBuffer(uploadAudienceGroup.file);
const body = createMultipartFormData({ ...uploadAudienceGroup, file });

const res = await this.http.put<{}>(
`${DATA_API_PREFIX}/audienceGroup/upload/byFile`,
body,
{ headers: body.getHeaders(), ...httpConfig },
);
return ensureJSON(res);
}

public async createClickAudienceGroup(clickAudienceGroup: {
description: string;
requestId: string;
Expand Down
38 changes: 20 additions & 18 deletions lib/http.ts
Expand Up @@ -96,29 +96,31 @@ export default class HTTPClient {
return res.data;
}

public async toBuffer(data: Buffer | Readable) {
if (Buffer.isBuffer(data)) {
return data;
} else if (data instanceof Readable) {
return await new Promise<Buffer>((resolve, reject) => {
const buffers: Buffer[] = [];
let size = 0;
data.on("data", (chunk: Buffer) => {
buffers.push(chunk);
size += chunk.length;
});
data.on("end", () => resolve(Buffer.concat(buffers, size)));
data.on("error", reject);
});
} else {
throw new Error("invalid data type for binary data");
}
}

public async postBinary<T>(
url: string,
data: Buffer | Readable,
contentType?: string,
): Promise<T> {
const buffer = await (async (): Promise<Buffer> => {
if (Buffer.isBuffer(data)) {
return data;
} else if (data instanceof Readable) {
return new Promise<Buffer>((resolve, reject) => {
const buffers: Buffer[] = [];
let size = 0;
data.on("data", (chunk: Buffer) => {
buffers.push(chunk);
size += chunk.length;
});
data.on("end", () => resolve(Buffer.concat(buffers, size)));
data.on("error", reject);
});
} else {
throw new Error("invalid data type for postBinary");
}
})();
const buffer = await this.toBuffer(data);

const res = await this.instance.post(url, buffer, {
headers: {
Expand Down
16 changes: 16 additions & 0 deletions lib/utils.ts
@@ -1,4 +1,5 @@
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 @@ -11,3 +12,18 @@ export function ensureJSON<T>(raw: T): T {
throw new JSONParseError("Failed to parse response body as JSON", raw);
}
}

export function createMultipartFormData(
this: FormData | void,
formBody: Record<string, any>,
): FormData {
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);
} else {
formData.append(key, String(value));
}
});
return formData;
}
27 changes: 17 additions & 10 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -39,7 +39,8 @@
"@types/node": "^14.10.0",
"axios": "^0.20.0",
"body-parser": "^1.19.0",
"file-type": "^15.0.0"
"file-type": "^15.0.0",
"form-data": "^3.0.0"
},
"devDependencies": {
"@types/express": "^4.17.8",
Expand Down
69 changes: 68 additions & 1 deletion test/client.spec.ts
Expand Up @@ -12,6 +12,8 @@ import {
OAUTH_BASE_PREFIX_V2_1,
DATA_API_PREFIX,
} from "../lib/endpoints";
import * as FormData from "form-data";
import { createMultipartFormData } from "../lib/utils";

const pkg = require("../package.json");

Expand Down Expand Up @@ -106,6 +108,18 @@ describe("client", () => {
.reply(responseFn);
};

const multipartFormDataMatcher = (expectedBody: Record<string, any>) => (
body: any,
) => {
const decoded = Buffer.from(body, "hex");
const boundary = decoded.toString("utf-8").match(/^--(.+)/)[1];
const data = new FormData();
//@ts-ignore
data._boundary = boundary;
createMultipartFormData.call(data, expectedBody);
return data.getBuffer().compare(decoded) === 0;
};

const mockPut = (
prefix: string,
path: string,
Expand Down Expand Up @@ -692,6 +706,34 @@ describe("client", () => {
equal(scope.isDone(), true);
});

it("createUploadAudienceGroupByFile", async () => {
const filepath = join(__dirname, "/helpers/line-icon.png");
const buffer = readFileSync(filepath);

const requestBody = {
description: "audienceGroupName",
isIfaAudience: false,
uploadDescription: "uploadDescription",
file: buffer,
};

const scope = nock(DATA_API_PREFIX, {
reqheaders: {
...interceptionOption.reqheaders,
"content-type": value =>
value.startsWith(`multipart/form-data; boundary=`),
},
})
.post(
"/audienceGroup/upload/byFile",
multipartFormDataMatcher(requestBody),
)
.reply(responseFn);

await client.createUploadAudienceGroupByFile(requestBody);
equal(scope.isDone(), true);
});

it("updateUploadAudienceGroup", async () => {
const requestBody = {
audienceGroupId: 4389303728991,
Expand All @@ -716,6 +758,31 @@ describe("client", () => {
equal(scope.isDone(), true);
});

it("updateUploadAudienceGroupByFile", async () => {
const filepath = join(__dirname, "/helpers/line-icon.png");
const buffer = readFileSync(filepath);
const requestBody = {
audienceGroupId: 4389303728991,
uploadDescription: "fileName",
file: buffer,
};
const scope = nock(DATA_API_PREFIX, {
reqheaders: {
...interceptionOption.reqheaders,
"content-type": value =>
value.startsWith(`multipart/form-data; boundary=`),
},
})
.put(
"/audienceGroup/upload/byFile",
multipartFormDataMatcher(requestBody),
)
.reply(responseFn);

await client.updateUploadAudienceGroupByFile(requestBody);
equal(scope.isDone(), true);
});

it("createClickAudienceGroup", async () => {
const requestBody = {
description: "audienceGroupName",
Expand Down Expand Up @@ -890,7 +957,7 @@ describe("client", () => {
await client.setRichMenuImage("test_rich_menu_id", null);
ok(false);
} catch (err) {
equal(err.message, "invalid data type for postBinary");
equal(err.message, "invalid data type for binary data");
}
});
});
Expand Down