-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathline.ts
137 lines (133 loc) · 3.51 KB
/
line.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Elysia, t } from "elysia";
import { defineApi } from "../defineApi";
import { EventStore } from "../EventStore";
import { decodeAccessToken, generateAccessToken } from "./oauth";
interface Events {
push: {
body: {
messages: any[];
};
sentMessages: { id: string; quoteToken: string }[];
};
}
function getEventStore(userId: string) {
return new EventStore<Events>(`line:${userId}`);
}
const elysia = new Elysia({ prefix: "/line", tags: ["LINE"] })
.post(
"/v2/bot/message/push",
async ({ body, set }) => {
const { messages } = body;
const sentMessages = messages.map((_, index) => ({
id: `${Date.now()}${index}`,
quoteToken: Math.random().toString(36).substring(2, 15),
}));
const eventStore = getEventStore(body.to);
const topic = `line:${body.to}`;
set.headers["x-mockapis-topic"] = topic;
await eventStore.add("push", { body, sentMessages });
return {
sentMessages,
};
},
{
body: t.Object({
to: t.String(),
messages: t.Array(t.Any()),
notificationDisabled: t.Optional(t.Boolean()),
customAggregationUnits: t.Optional(t.Array(t.String())),
}),
response: t.Object({
sentMessages: t.Array(
t.Object({
id: t.String(),
quoteToken: t.String(),
})
),
}),
detail: { summary: "Send push message" },
}
)
.get(
"/_test/messages",
async ({ query }) => {
const eventStore = getEventStore(query.uid);
return (await eventStore.get())
.filter((e) => e.type === "push")
.map((event) => {
const { body, sentMessages } = event.payload;
return body.messages.map((message, index) => ({
id: sentMessages[index].id,
message,
}));
});
},
{
query: t.Object({
uid: t.String(),
}),
response: t.Array(
t.Array(
t.Object({
id: t.String(),
message: t.Any(),
})
)
),
detail: { summary: "[Test] Get messages sent to a user" },
}
)
.post(
"_test/v2/profile",
({ body }) => {
return {
access_token: generateAccessToken(body),
token_type: "Bearer",
expires_in: 3600,
scope: "profile",
};
},
{
body: t.Object({
userId: t.String(),
displayName: t.String(),
pictureUrl: t.String(),
statusMessage: t.String(),
}),
response: t.Object({
access_token: t.String(),
token_type: t.String(),
expires_in: t.Number(),
scope: t.String(),
}),
detail: { summary: "[Test] Add user profile" },
}
)
.get(
"/v2/profile",
({ headers }) => {
const authHeader = headers.authorization;
if (!authHeader || !authHeader.startsWith("Bearer ")) {
return { error: "invalid_token" };
}
const token = authHeader.split(" ")[1];
return decodeAccessToken(token) as any;
},
{
headers: t.Object({
authorization: t.String(),
}),
response: t.Object({
userId: t.String(),
displayName: t.String(),
pictureUrl: t.String(),
statusMessage: t.String(),
}),
detail: { summary: "Get user profile" },
}
);
export const line = defineApi({
tag: "LINE",
description: `A mock API that implements a subset of the [LINE Messaging API](https://developers.line.biz/en/reference/messaging-api/).`,
elysia,
});