Skip to content

Commit a053137

Browse files
authored
feature: add getLineRequestId() in the response of reply/push/multicast API (#151)
* feature: add getLineRequestId() in the response of reply/post/multicast API * docs: modify push/reply/multicast response type
1 parent 37e90bc commit a053137

File tree

6 files changed

+56
-16
lines changed

6 files changed

+56
-16
lines changed

docs/api-reference/client.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class Client {
1212
constructor(config: ClientConfig) {}
1313

1414
// Message
15-
pushMessage(to: string, messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>
16-
replyMessage(replyToken: string, messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>
17-
multicast(to: string[], messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>
15+
pushMessage(to: string, messages: Message | Message[], notificationDisabled: boolean = false): Promise<MessageAPIBasicResponse>
16+
replyMessage(replyToken: string, messages: Message | Message[], notificationDisabled: boolean = false): Promise<MessageAPIBasicResponse>
17+
multicast(to: string[], messages: Message | Message[], notificationDisabled: boolean = false): Promise<MessageAPIBasicResponse>
1818
broadcast(messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>
1919
getMessageContent(messageId: string): Promise<Readable>
2020

@@ -88,7 +88,7 @@ in [the Client guide](../guide/client.md).
8888

8989
### Message
9090

91-
#### `pushMessage(to: string, messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>`
91+
#### `pushMessage(to: string, messages: Message | Message[], notificationDisabled: boolean = false): Promise<MessageAPIBasicResponse>`
9292

9393
It corresponds to the [Push message](https://developers.line.me/en/docs/messaging-api/reference/#send-push-message) API.
9494

@@ -101,7 +101,7 @@ client.pushMessage('user_or_group_or_room_id', {
101101
})
102102
```
103103

104-
#### `replyMessage(replyToken: string, messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>`
104+
#### `replyMessage(replyToken: string, messages: Message | Message[], notificationDisabled: boolean = false): Promise<MessageAPIBasicResponse>`
105105

106106
It corresponds to the [Reply message](https://developers.line.me/en/docs/messaging-api/reference/#send-reply-message) API.
107107

@@ -116,7 +116,7 @@ client.replyMessage(event.replyToken, {
116116
})
117117
```
118118

119-
#### `multicast(to: string[], messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>`
119+
#### `multicast(to: string[], messages: Message | Message[], notificationDisabled: boolean = false): Promise<MessageAPIBasicResponse>`
120120

121121
It corresponds to the [Multicast](https://developers.line.me/en/docs/messaging-api/reference/#send-multicast-messages) API.
122122

lib/client.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,44 @@ export default class Client {
3535
);
3636
}
3737

38-
public async pushMessage(
38+
private setLineRequestId(response: object, lineRequestId: string): boolean {
39+
return Reflect.defineProperty(response, "getLineRequestId", {
40+
enumerable: false,
41+
value: (): string => {
42+
return lineRequestId;
43+
},
44+
});
45+
}
46+
47+
private async postMessagingAPI(
48+
url: string,
49+
body?: any,
50+
): Promise<Types.MessageAPIResponseBase> {
51+
const res = await this.http.postJson(url, body);
52+
// header names are lower-cased
53+
// https://nodejs.org/api/http.html#http_message_headers
54+
this.setLineRequestId(res.data, res.headers["x-line-request-id"]);
55+
return res.data as Types.MessageAPIResponseBase;
56+
}
57+
58+
public pushMessage(
3959
to: string,
4060
messages: Types.Message | Types.Message[],
4161
notificationDisabled: boolean = false,
42-
): Promise<any> {
43-
return this.http.post("/message/push", {
62+
): Promise<Types.MessageAPIResponseBase> {
63+
return this.postMessagingAPI("/message/push", {
4464
messages: toArray(messages),
4565
to,
4666
notificationDisabled,
4767
});
4868
}
4969

50-
public async replyMessage(
70+
public replyMessage(
5171
replyToken: string,
5272
messages: Types.Message | Types.Message[],
5373
notificationDisabled: boolean = false,
54-
): Promise<any> {
55-
return this.http.post("/message/reply", {
74+
): Promise<Types.MessageAPIResponseBase> {
75+
return this.postMessagingAPI("/message/reply", {
5676
messages: toArray(messages),
5777
replyToken,
5878
notificationDisabled,
@@ -63,8 +83,8 @@ export default class Client {
6383
to: string[],
6484
messages: Types.Message | Types.Message[],
6585
notificationDisabled: boolean = false,
66-
): Promise<any> {
67-
return this.http.post("/message/multicast", {
86+
): Promise<Types.MessageAPIResponseBase> {
87+
return this.postMessagingAPI("/message/multicast", {
6888
messages: toArray(messages),
6989
to,
7090
notificationDisabled,

lib/http.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios, { AxiosInstance, AxiosError } from "axios";
2+
import { AxiosResponse } from "axios";
23
import { Readable } from "stream";
34
import { HTTPError, ReadError, RequestError } from "./exceptions";
45
import * as fileType from "file-type";
@@ -35,10 +36,14 @@ export default class HTTPClient {
3536
return res.data as Readable;
3637
}
3738

38-
public async post<T>(url: string, body?: any): Promise<T> {
39-
const res = await this.instance.post(url, body, {
39+
public postJson(url: string, body?: any): Promise<AxiosResponse> {
40+
return this.instance.post(url, body, {
4041
headers: { "Content-Type": "application/json" },
4142
});
43+
}
44+
45+
public async post<T>(url: string, body?: any): Promise<T> {
46+
const res = await this.postJson(url, body);
4247
return res.data;
4348
}
4449

lib/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,3 +1800,7 @@ export type NumberOfSentBroadcastMessages = {
18001800
*/
18011801
success?: number;
18021802
};
1803+
1804+
export type MessageAPIResponseBase = {
1805+
getLineRequestId: () => string;
1806+
};

test/client.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe("client", () => {
5454
equal(req.body.replyToken, "test_reply_token");
5555
deepEqual(req.body.messages, [testMsg]);
5656
deepEqual(res, {});
57+
equal(res.getLineRequestId(), "X-Line-Request-Id");
5758
});
5859

5960
it("push", async () => {
@@ -65,6 +66,7 @@ describe("client", () => {
6566
equal(req.body.to, "test_user_id");
6667
deepEqual(req.body.messages, [testMsg]);
6768
deepEqual(res, {});
69+
equal(res.getLineRequestId(), "X-Line-Request-Id");
6870
});
6971

7072
it("multicast", async () => {
@@ -81,6 +83,7 @@ describe("client", () => {
8183
]);
8284
deepEqual(req.body.messages, [testMsg, testMsg]);
8385
deepEqual(res, {});
86+
equal(res.getLineRequestId(), "X-Line-Request-Id");
8487
});
8588

8689
it("broadcast", async () => {

test/helpers/test-server.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ function listen(port: number, middleware?: express.RequestHandler) {
8686
res.json(result);
8787
});
8888

89+
// Simulate the Message API
90+
app.use((req, res, next) => {
91+
if (req.url.startsWith("/message/")) {
92+
res.header("X-Line-Request-Id", "X-Line-Request-Id");
93+
}
94+
next();
95+
});
96+
8997
// return an empty object for others
9098
app.use((req, res) => res.json({}));
9199

0 commit comments

Comments
 (0)