Skip to content

Commit b33d2f8

Browse files
author
Hyunje Jun
committed
Make index script export all the types and exceptions
To simplify the interface, and also enable only types to be imported.
1 parent 15c64ff commit b33d2f8

File tree

12 files changed

+291
-228
lines changed

12 files changed

+291
-228
lines changed

exceptions.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

exceptions.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/client.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { get, post, stream } from "./http";
2+
import * as Types from "./types";
23
import * as URL from "./urls";
34
import { toArray } from "./util";
45

56
export default class Client {
6-
public config: Line.ClientConfig;
7+
public config: Types.ClientConfig;
78

8-
constructor(config: Line.Config & Line.ClientConfig) {
9+
constructor(config: Types.Config & Types.ClientConfig) {
910
if (!config.channelAccessToken) {
1011
throw new Error("no channel access token");
1112
}
@@ -15,7 +16,7 @@ export default class Client {
1516

1617
public pushMessage(
1718
to: string,
18-
messages: Line.Message | Line.Message[],
19+
messages: Types.Message | Types.Message[],
1920
): Promise<any> {
2021
return this.post(URL.push, {
2122
messages: toArray(messages),
@@ -25,7 +26,7 @@ export default class Client {
2526

2627
public replyMessage(
2728
replyToken: string,
28-
messages: Line.Message | Line.Message[],
29+
messages: Types.Message | Types.Message[],
2930
): Promise<any> {
3031
return this.post(URL.reply, {
3132
messages: toArray(messages),
@@ -35,29 +36,29 @@ export default class Client {
3536

3637
public multicast(
3738
to: string[],
38-
messages: Line.Message | Line.Message[],
39+
messages: Types.Message | Types.Message[],
3940
): Promise<any> {
4041
return this.post(URL.multicast, {
4142
messages: toArray(messages),
4243
to,
4344
});
4445
}
4546

46-
public getProfile(userId: string): Promise<Line.Profile> {
47+
public getProfile(userId: string): Promise<Types.Profile> {
4748
return this.get(URL.profile(userId));
4849
}
4950

5051
public getGroupMemberProfile(
5152
groupId: string,
5253
userId: string,
53-
): Promise<Line.Profile> {
54+
): Promise<Types.Profile> {
5455
return this.get(URL.groupMemberProfile(groupId, userId));
5556
}
5657

5758
public getRoomMemberProfile(
5859
roomId: string,
5960
userId: string,
60-
): Promise<Line.Profile> {
61+
): Promise<Types.Profile> {
6162
return this.get(URL.roomMemberProfile(roomId, userId));
6263
}
6364

lib/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ import middleware from "./middleware";
33
import validateSignature from "./validate-signature";
44

55
export { Client, middleware, validateSignature };
6+
7+
// re-export exceptions and types
8+
export * from "./exceptions";
9+
export * from "./types";

lib/middleware.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { raw } from "body-parser";
22
import * as http from "http";
33
import { JSONParseError, SignatureValidationFailed } from "./exceptions";
4+
import * as Types from "./types";
45
import validateSignature from "./validate-signature";
56

67
export type Request = http.IncomingMessage & { body: any };
@@ -14,7 +15,7 @@ export type Middleware = (
1415
) => void;
1516

1617
export default function middleware(
17-
config: Line.Config & Line.MiddlewareConfig,
18+
config: Types.Config & Types.MiddlewareConfig,
1819
): Middleware {
1920
if (!config.channelSecret) {
2021
throw new Error("no channel secret");

lib/types.ts

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
export type ClientConfig = {
2+
channelAccessToken: string;
3+
};
4+
5+
export type MiddlewareConfig = {
6+
channelSecret: string;
7+
};
8+
9+
export type Config = ClientConfig | MiddlewareConfig;
10+
11+
export type Profile = {
12+
displayName: string;
13+
userId: string;
14+
pictureUrl: string;
15+
statusMessage: string;
16+
};
17+
18+
export type WebhookEvent =
19+
| MessageEvent
20+
| FollowEvent
21+
| UnfollowEvent
22+
| JoinEvent
23+
| LeaveEvent
24+
| PostbackEvent
25+
| BeaconEvent;
26+
27+
export type EventBase = {
28+
timestamp: number;
29+
source: EventSource;
30+
};
31+
32+
export type EventSource = User | Group | Room;
33+
34+
export type User = { type: "user"; userId: string };
35+
36+
export type Group = { type: "group"; groupId: string; userId?: string };
37+
38+
export type Room = { type: "room"; roomId: string; userId?: string };
39+
40+
export type ReplyableEvent = EventBase & { replyToken: string };
41+
42+
export type MessageEvent = {
43+
type: "message";
44+
message: EventMessage;
45+
} & ReplyableEvent;
46+
47+
export type FollowEvent = { type: "follow" } & ReplyableEvent;
48+
49+
export type UnfollowEvent = { type: "unfollow" } & EventBase;
50+
51+
export type JoinEvent = { type: "join" } & ReplyableEvent;
52+
53+
export type LeaveEvent = { type: "leave" } & EventBase;
54+
55+
export type PostbackEvent = {
56+
type: "postback";
57+
postback: Postback;
58+
} & ReplyableEvent;
59+
60+
export type BeaconEvent = ReplyableEvent & {
61+
type: "beacon";
62+
beacon: {
63+
type: "enter" | "leave" | "banner";
64+
hwid: string;
65+
dm?: string;
66+
};
67+
};
68+
69+
export type EventMessage =
70+
| TextEventMessage
71+
| ImageEventMessage
72+
| VideoEventMessage
73+
| AudioEventMessage
74+
| LocationEventMessage
75+
| StickerEventMessage;
76+
77+
export type EventMessageBase = { id: string };
78+
79+
export type TextEventMessage = {
80+
type: "text";
81+
text: string;
82+
} & EventMessageBase;
83+
84+
export type ImageEventMessage = { type: "image" } & EventMessageBase;
85+
86+
export type VideoEventMessage = { type: "video" } & EventMessageBase;
87+
88+
export type AudioEventMessage = { type: "audio" } & EventMessageBase;
89+
90+
export type FileEventMessage = {
91+
type: "file";
92+
fileName: string;
93+
fileSize: string;
94+
} & EventMessageBase;
95+
96+
export type LocationEventMessage = {
97+
type: "location";
98+
title: string;
99+
address: string;
100+
latitude: number;
101+
longitude: number;
102+
} & EventMessageBase;
103+
104+
export type StickerEventMessage = {
105+
type: "sticker";
106+
packageId: string;
107+
stickerId: string;
108+
} & EventMessageBase;
109+
110+
export type Postback = {
111+
data: string;
112+
params?: {
113+
date?: string;
114+
time?: string;
115+
datetime?: string;
116+
};
117+
};
118+
119+
export type Message =
120+
| TextMessage
121+
| ImageMessage
122+
| VideoMessage
123+
| AudioMessage
124+
| LocationMessage
125+
| StickerMessage
126+
| ImageMapMessage
127+
| TemplateMessage;
128+
129+
export type TextMessage = {
130+
type: "text";
131+
text: string;
132+
};
133+
134+
export type ImageMessage = {
135+
type: "image";
136+
originalContentUrl: string;
137+
previewImageUrl: string;
138+
};
139+
140+
export type VideoMessage = {
141+
type: "video";
142+
originalContentUrl: string;
143+
previewImageUrl: string;
144+
};
145+
146+
export type AudioMessage = {
147+
type: "audio";
148+
originalContentUrl: string;
149+
duration: string;
150+
};
151+
152+
export type LocationMessage = {
153+
type: "location";
154+
title: string;
155+
address: string;
156+
latitude: number;
157+
longitude: number;
158+
};
159+
160+
export type StickerMessage = {
161+
type: "sticker";
162+
packageId: string;
163+
stickerId: string;
164+
};
165+
166+
export type ImageMapMessage = {
167+
type: "image";
168+
baseUrl: string;
169+
altText: string;
170+
baseSize: { width: number; height: number };
171+
actions: ImageMapAction[];
172+
};
173+
174+
export type TemplateMessage = {
175+
type: "template";
176+
altText: string;
177+
template: TemplateContent;
178+
};
179+
180+
export type ImageMapAction = ImageMapURIAction | ImageMapMessageAction;
181+
182+
export type ImageMapActionBase = { area: ImageMapArea };
183+
184+
export type ImageMapURIAction = {
185+
type: "uri";
186+
linkUri: string;
187+
} & ImageMapActionBase;
188+
189+
export type ImageMapMessageAction = {
190+
type: "message";
191+
text: string;
192+
} & ImageMapActionBase;
193+
194+
export type ImageMapArea = {
195+
x: number;
196+
y: number;
197+
width: number;
198+
height: number;
199+
};
200+
201+
export type TemplateContent =
202+
| TemplateButtons
203+
| TemplateConfirm
204+
| TemplateCarousel
205+
| TemplateImageCarousel;
206+
207+
export type TemplateButtons = {
208+
type: "buttons";
209+
thumbnailImageUrl?: string;
210+
title?: string;
211+
text: string;
212+
actions: TemplateAction<{ label: string }>[];
213+
};
214+
215+
export type TemplateConfirm = {
216+
type: "confirm";
217+
text: string;
218+
actions: TemplateAction<{ label: string }>[];
219+
};
220+
221+
export type TemplateCarousel = { type: "carousel"; columns: TemplateColumn[] };
222+
223+
export type TemplateColumn = {
224+
thumbnailImageUrl?: string;
225+
title?: string;
226+
text: string;
227+
actions: TemplateAction<{ label: string }>[];
228+
};
229+
230+
export type TemplateImageCarousel = {
231+
type: "image_carousel";
232+
columns: TemplateImageColumn;
233+
};
234+
235+
export type TemplateImageColumn = {
236+
imageUrl: string;
237+
action: TemplateAction<{ label?: string }>;
238+
};
239+
240+
export type TemplateAction<Label> =
241+
| TemplatePostbackAction & Label
242+
| TemplateMessageAction & Label
243+
| TemplateURIAction & Label
244+
| TemplateDatetimePickerAction & Label;
245+
246+
export type TemplatePostbackAction = {
247+
type: "postback";
248+
data: string;
249+
text?: string;
250+
};
251+
252+
export type TemplateMessageAction = {
253+
type: "message";
254+
text: string;
255+
};
256+
257+
export type TemplateURIAction = {
258+
type: "uri";
259+
uri: string;
260+
};
261+
262+
export type TemplateDatetimePickerAction = {
263+
type: "datetimepicker";
264+
data: string;
265+
mode: "date" | "time" | "datetime";
266+
initial?: string;
267+
max?: string;
268+
min?: string;
269+
};

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
"node": ">=4"
77
},
88
"main": "dist/index.js",
9-
"types": "types/index.d.ts",
9+
"types": "dist/index.d.ts",
1010
"files": [
1111
"dist",
1212
"docs",
13-
"exceptions.js",
14-
"exceptions.ts",
15-
"lib",
16-
"types"
13+
"lib"
1714
],
1815
"scripts": {
1916
"pretest": "npm run build",

test/client.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { deepEqual, equal } from "assert";
22
import Client from "../lib/client";
3+
import * as Types from "../lib/types";
34
import { getStreamData } from "./helpers/stream";
45
import { close, listen } from "./helpers/test-server";
56

@@ -14,7 +15,7 @@ describe("client", () => {
1415
before(() => listen(TEST_PORT));
1516
after(() => close());
1617

17-
const testMsg: Line.TextMessage = { type: "text", text: "hello" };
18+
const testMsg: Types.TextMessage = { type: "text", text: "hello" };
1819

1920
it("reply", () => {
2021
return client.replyMessage("test_reply_token", testMsg).then((res: any) => {

0 commit comments

Comments
 (0)