Skip to content

Commit 7246a9a

Browse files
author
Hyunje Jun
authored
Merge pull request #31 from line/export-types
Export exceptions and types from top-level import
2 parents 15c64ff + 62fbec4 commit 7246a9a

22 files changed

+377
-278
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3+
- "4"
34
- "6"
4-
- "7"
55
- "8"

CONTRIBUTING.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# How to contribute to LINE Bot SDK for Node.js
22

33
First of all, thank you so much for taking your time to contribute! LINE Bot SDK
4-
for Node.js is not very different from any other open source projects you are
5-
aware of. It will be amazing if you could help us by doing any of the following:
4+
for Node.js is not very different from any other open source projects. It will
5+
be fantastic if you help us by doing any of the following:
66

77
- File an issue in [the issue tracker](https://github.com/line/line-bot-sdk-nodejs/issues)
88
to report bugs and propose new features and improvements.
@@ -17,7 +17,6 @@ Here are each top-level directory explained:
1717

1818
* `lib`: TypeScript source code. You may modify files under this directory.
1919
* `test`: Mocha test suites. Please add tests for modification if possible.
20-
* `types`: Project-level type declarations
2120
* `examples`: Example projects using this SDK
2221
* `docs`: [GitBook](https://www.gitbook.com/) markdowns for project documentation
2322
* `tools`: Useful tools

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
* [validateSignature](pages/api-reference/validate-signature.md)
1414
* [middleware](pages/api-reference/middleware.md)
1515
* [Exceptions](pages/api-reference/exceptions.md)
16+
* [Message and event objects](pages/api-reference/message-and-event-objects.md)
1617
* [Contributing](CONTRIBUTING.md)
1718
* [GitHub](https://github.com/line/line-bot-sdk-nodejs/)

docs/pages/api-reference.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# API Reference
22

3-
When imported via `require` or `import`, 3 interfaces are exposed.
3+
Please import the library via `require` or `import`.
44

55
``` js
66
// CommonJS
@@ -15,15 +15,5 @@ For the detailed API reference of each, please refer to their own pages.
1515
- [Client](api-reference/client.md)
1616
- [validateSignature](api-reference/validate-signature.md)
1717
- [middleware](api-reference/middleware.md)
18-
19-
Exceptions can be imported via `@line/bot-sdk/exceptions`.
20-
21-
``` js
22-
// CommonJS
23-
const JSONParseError = require('@line/bot-sdk/exceptions').JSONParseError;
24-
25-
// ES2015 modules or TypeScript
26-
import { JSONParseError } from '@line/bot-sdk/exceptions';
27-
```
28-
2918
- [Exceptions](api-reference/exceptions.md)
19+
- [Message and event objects](api-reference/message-and-event-objects.md)

docs/pages/api-reference/client.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ class Client {
2626
```
2727

2828
`Message` is a valid message object. About message object structure, please
29-
refer to [Send message object](https://developers.line.me/en/docs/messaging-api/reference/#message-objects)
30-
of the official documentation.
29+
refer to [Message and event objects](./message-and-event-objects.md) on this guide, or
30+
[Send message object](https://developers.line.me/en/docs/messaging-api/reference/#message-objects)
31+
on the official documentation.
3132

32-
`ClientConfig` type is like below, except that it also allows fields
33-
from [MiddlewareConfig](./middleware.md) too.
33+
`ClientConfig` type is like below.
3434

3535
``` typescript
36-
type ClientConfig = {
37-
channelAccessToken: string,
36+
interface ClientConfig {
37+
channelAccessToken: string;
38+
channelSecret?: string;
3839
}
3940
```
4041

docs/pages/api-reference/exceptions.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Exceptions
22

3-
Exception classes can be imported via `@line/bot-sdk/exceptions'`.
3+
Exception classes can also be imported from `@line/bot-sdk`.
44

55
``` js
6-
// CommonJS
7-
const HTTPError = require('@line/bot-sdk/exceptions').HTTPError;
8-
const JSONParseError = require('@line/bot-sdk/exceptions').JSONParseError;
9-
const ReadError = require('@line/bot-sdk/exceptions').ReadError;
10-
const RequestError = require('@line/bot-sdk/exceptions').RequestError;
11-
const SignatureValidationFailed = require('@line/bot-sdk/exceptions').SignatureValidationFailed;
6+
// CommonJS (destructuring can be used for Node.js >= 6)
7+
const HTTPError = require('@line/bot-sdk').HTTPError;
8+
const JSONParseError = require('@line/bot-sdk').JSONParseError;
9+
const ReadError = require('@line/bot-sdk').ReadError;
10+
const RequestError = require('@line/bot-sdk').RequestError;
11+
const SignatureValidationFailed = require('@line/bot-sdk').SignatureValidationFailed;
1212

1313
// ES2015 modules or TypeScript
1414
import {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Message and event objects
2+
3+
The message objects and event objects are plain JS objects with no
4+
abstraction. This SDK provides TypeScript types for them, which can be imported
5+
from `@line/bot-sdk`.
6+
7+
Please beware that the types only work in TypeScript, and will be removed when
8+
built into JavaScript.
9+
10+
``` typescript
11+
import {
12+
// webhook event objects
13+
WebhookEvent,
14+
MessageEvent,
15+
EventSource,
16+
VideoEventMessage,
17+
18+
// message event objects
19+
Message,
20+
TemplateMessage,
21+
TemplateContent,
22+
} from "@line/bot-sdk";
23+
```
24+
25+
For the actual type definitions, please refer to [types.ts](https://github.com/line/line-bot-sdk-nodejs/blob/master/lib/types.ts)
26+
directly.
27+
28+
You can also refer to the official specification:
29+
30+
- [Message objects](https://developers.line.me/en/docs/messaging-api/reference/#message-objects)
31+
- [Webhook event objects](https://developers.line.me/en/docs/messaging-api/reference/#webhook-event-objects)

docs/pages/api-reference/middleware.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ by several Node.js web frameworks such as [Express](https://expressjs.com/).
99
function middleware(config: MiddlewareConfig): Middleware
1010
```
1111

12-
The types of `MiddlewareConfig` and `Middleware` are like below, except that the config allows
13-
fields from [ClientConfig](./client.md) too.
12+
The types of `MiddlewareConfig` and `Middleware` are like below.
1413

1514
``` typescript
16-
type MiddlewareConfig = {
17-
channelSecret: string,
15+
interface MiddlewareConfig {
16+
channelAccessToken?: string;
17+
channelSecret: string;
1818
}
1919
2020
type Middleware =

docs/pages/guide/typescript.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Also, when building a complex message object, you can make use of types for
2626
its fields.
2727

2828
``` typescript
29-
const message: Line.TemplateMessage = {
29+
const message: TemplateMessage = {
3030
type: "template",
3131
altText: "cannot display template message",
3232
template: {
@@ -45,7 +45,7 @@ const message: Line.TemplateMessage = {
4545
```
4646

4747
The object above will be type-checked to have the type of
48-
`Line.TemplateMessage`, and thus ensured not to miss any required field.
48+
`TemplateMessage`, and thus ensured not to miss any required field.
4949

5050
Also, [literal type](https://www.typescriptlang.org/docs/handbook/advanced-types.html)
5151
is used for `type` fields, which means the compiler will complain if a wrong
@@ -57,9 +57,21 @@ The library is built to just-work with TypeScript too, so import the library and
5757
there you go.
5858

5959
``` typescript
60-
import { Client, validateSignature, middleware } from "@line/bot-sdk";
60+
import {
61+
// main APIs
62+
Client,
63+
middleware,
64+
65+
// exceptions
66+
JSONParseError,
67+
SignatureValidationFailed,
68+
69+
// types
70+
TemplateMessage,
71+
WebhookEvent,
72+
} from "@line/bot-sdk";
6173
```
6274

63-
Webhook event and message object types are defined in the `Line` namespace, e.g.
64-
`Line.WebhookEvent` or `Line.Message`. For declarations of the types, please
65-
refer to [global.d.ts](https://github.com/line/line-bot-sdk-nodejs/blob/master/types/global.d.ts).
75+
Message object and webhook event types can be also imported from `@line/bot-sdk`,
76+
e.g. `TemplateMessage` or `WebhookEvent`. For declarations of the types, please
77+
refer to [types.ts](https://github.com/line/line-bot-sdk-nodejs/blob/master/lib/types.ts).

docs/pages/guide/webhook.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ function to do this.
2020

2121
**Webhook event object parsing** is literally parsing webhook event objects,
2222
which contains information of each webhook event. The objects are provided as
23-
request body in JSON format, so any body parser will work here.
23+
request body in JSON format, so any body parser will work here. For interal
24+
object types in this SDK, please refer to [Message and event objects](../api-reference/message-and-event-objects.md).
2425

2526
There is a function to generate a [connect](https://github.com/senchalabs/connect) middleware,
2627
[`middleware()`](../api-reference/middleware.md), to conduct both of them. If
@@ -131,8 +132,8 @@ The errors can be handled with [error middleware](https://github.com/senchalabs/
131132
``` js
132133
const express = require('express')
133134
const middleware = require('@line/bot-sdk').middleware
134-
const JSONParseError = require('@line/bot-sdk/exceptions').JSONParseError
135-
const SignatureValidationFailed = require('@line/bot-sdk/exceptions').SignatureValidationFailed
135+
const JSONParseError = require('@line/bot-sdk').JSONParseError
136+
const SignatureValidationFailed = require('@line/bot-sdk').SignatureValidationFailed
136137

137138
const app = express()
138139

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.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 & 3 deletions
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 };
@@ -13,9 +14,7 @@ export type Middleware = (
1314
next: NextCallback,
1415
) => void;
1516

16-
export default function middleware(
17-
config: Line.Config & Line.MiddlewareConfig,
18-
): Middleware {
17+
export default function middleware(config: Types.MiddlewareConfig): Middleware {
1918
if (!config.channelSecret) {
2019
throw new Error("no channel secret");
2120
}

0 commit comments

Comments
 (0)