Skip to content

Commit d833bff

Browse files
authored
Merge pull request #444 from Easpe/next
feat: push,multicast accept customAggregationUnits as optional.
2 parents efba069 + feecf3e commit d833bff

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

lib/client.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ export default class Client {
6666
to: string,
6767
messages: Types.Message | Types.Message[],
6868
notificationDisabled: boolean = false,
69+
customAggregationUnits?: string[],
6970
): Promise<Types.MessageAPIResponseBase> {
7071
return this.http.post(
7172
`${MESSAGING_API_PREFIX}/message/push`,
7273
{
7374
messages: toArray(messages),
7475
to,
7576
notificationDisabled,
77+
customAggregationUnits,
7678
},
7779
this.generateRequestConfig(),
7880
);
@@ -94,13 +96,15 @@ export default class Client {
9496
to: string[],
9597
messages: Types.Message | Types.Message[],
9698
notificationDisabled: boolean = false,
99+
customAggregationUnits?: string[],
97100
): Promise<Types.MessageAPIResponseBase> {
98101
return this.http.post(
99102
`${MESSAGING_API_PREFIX}/message/multicast`,
100103
{
101104
messages: toArray(messages),
102105
to,
103106
notificationDisabled,
107+
customAggregationUnits,
104108
},
105109
this.generateRequestConfig(),
106110
);
@@ -196,6 +200,33 @@ export default class Client {
196200
);
197201
}
198202

203+
public validateCustomAggregationUnits(units: string[]): {
204+
messages: string[];
205+
valid: boolean;
206+
} {
207+
const messages: string[] = [];
208+
if (units.length > 1) {
209+
messages.push("customAggregationUnits can only contain one unit");
210+
}
211+
units.forEach((unit, index) => {
212+
if (unit.length > 30) {
213+
messages.push(
214+
`customAggregationUnits[${index}] must be less than or equal to 30 characters`,
215+
);
216+
}
217+
if (!/^[a-zA-Z0-9_]+$/.test(unit)) {
218+
messages.push(
219+
`customAggregationUnits[${index}] must be alphanumeric characters or underscores`,
220+
);
221+
}
222+
});
223+
224+
return {
225+
messages,
226+
valid: messages.length === 0,
227+
};
228+
}
229+
199230
public async getProfile(userId: string): Promise<Types.Profile> {
200231
const profile = await this.http.get<Types.Profile>(
201232
`${MESSAGING_API_PREFIX}/profile/${userId}`,

test/client.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,45 @@ describe("client", () => {
349349
strictEqual(res["x-line-request-id"], "X-Line-Request-Id");
350350
});
351351

352+
describe("validateCustomAggregationUnits", () => {
353+
it("should validate correctly when input is valid", () => {
354+
const units = ["promotion_A1"];
355+
const result = client.validateCustomAggregationUnits(units);
356+
strictEqual(result.valid, true);
357+
strictEqual(result.messages.length, 0);
358+
});
359+
360+
it("should return invalid when there is more than one unit", () => {
361+
const units = ["promotion_A1", "promotion_A2"];
362+
const result = client.validateCustomAggregationUnits(units);
363+
strictEqual(result.valid, false);
364+
strictEqual(
365+
result.messages[0],
366+
"customAggregationUnits can only contain one unit",
367+
);
368+
});
369+
370+
it("should return invalid when a unit has more than 30 characters", () => {
371+
const units = ["promotion_A1_with_a_very_long_name"];
372+
const result = client.validateCustomAggregationUnits(units);
373+
strictEqual(result.valid, false);
374+
strictEqual(
375+
result.messages[0],
376+
"customAggregationUnits[0] must be less than or equal to 30 characters",
377+
);
378+
});
379+
380+
it("should return invalid when a unit has invalid characters", () => {
381+
const units = ["promotion_A1!"];
382+
const result = client.validateCustomAggregationUnits(units);
383+
strictEqual(result.valid, false);
384+
strictEqual(
385+
result.messages[0],
386+
"customAggregationUnits[0] must be alphanumeric characters or underscores",
387+
);
388+
});
389+
});
390+
352391
it("getProfile", async () => {
353392
const scope = mockGet(MESSAGING_API_PREFIX, "/profile/test_user_id");
354393

0 commit comments

Comments
 (0)