Skip to content

Commit 10bb35e

Browse files
author
Hyunje Jun
committed
Update docs to guide cases with pre-parsed body
1 parent b66b14e commit 10bb35e

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

docs/pages/api-reference/client.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ corresponding to [messaging APIs](https://devdocs.line.me/en/#messaging-api).
77

88
``` typescript
99
class Client {
10-
public config: Config
10+
public config: ClientConfig
1111

12-
constructor(config: Config)
12+
constructor(config: ClientConfig)
1313

1414
pushMessage(to: string, messages: Message | Message[]): Promise<{}>
1515
replyMessage(replyToken: string, messages: Message | Message[]): Promise<{}>
@@ -25,12 +25,12 @@ class Client {
2525
refer to [Send message object](https://devdocs.line.me/en/#send-message-object)
2626
of the official document.
2727

28-
`Config` type is like below.
28+
`ClientConfig` type is like below, except that it also allows fields
29+
from [MiddlewareConfig](./middleware.md) too.
2930

3031
``` typescript
31-
type Config = {
32+
type ClientConfig = {
3233
channelAccessToken: string,
33-
channelSecret?: string,
3434
}
3535
```
3636

docs/pages/api-reference/middleware.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ by several Node.js web frameworks such as [Express](https://expressjs.com/).
66
#### Type signature
77

88
``` typescript
9-
function middleware(config: Config): Middleware
9+
function middleware(config: MiddlewareConfig): Middleware
1010
```
1111

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

1415
``` typescript
15-
type Config = {
16-
channelAccessToken?: string,
16+
type MiddlewareConfig = {
1717
channelSecret: string,
1818
}
1919
@@ -44,8 +44,12 @@ app.post('/webhook', middleware(config), (req, res) => {
4444

4545
The middleware returned by `middleware()` parses body and checks signature
4646
validation, so you do not need to use [`validateSignature()`](./validate-signature.md)
47-
directly. Also, you do not need to use [body-parser](https://github.com/expressjs/body-parser)
48-
to parse webhook events. If you have a reason to use body-parser for other
47+
directly.
48+
49+
You do not need to use [body-parser](https://github.com/expressjs/body-parser)
50+
to parse webhook events, as `middleware()` embeds body-parser and parses them to
51+
objects. Please keep in mind that it will not process requests without
52+
`X-Line-Signature` header. If you have a reason to use body-parser for other
4953
routes, *please do not use it before the LINE middleware*. body-parser parses
5054
the request body up and the LINE middleware cannot parse it afterwards.
5155

@@ -59,5 +63,9 @@ app.use(middleware(config))
5963
app.use(bodyParser.json())
6064
```
6165

66+
There are environments where `req.body` is pre-parsed, such as [Firebase Cloud Functions](https://firebase.google.com/docs/functions/http-events).
67+
If it parses the body into string or buffer, do not worry as the middleware will
68+
work just fine. If the pre-parsed body is an object, please use [`validateSignature()`](../api-reference/validate-signature.md)
69+
manually with the raw body.
6270

6371
About building webhook server, please refer to [Webhook](../guide/webhook.md).

docs/pages/guide/webhook.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ app.listen(8080)
7676

7777
We have imported `middleware` from the package and make the Express app to use
7878
the middleware. The middlware validates the request and parses webhook event
79-
object. It internally uses [body-parser](https://github.com/expressjs/body-parser),
80-
so please beware that it will not work with body parsed already by body-parser.
81-
Please put `middleware()` upper than any body-parser middleware.
79+
object. It embeds body-parser and parses them to objects. Please keep in mind
80+
that it will not process requests without `X-Line-Signature` header. If you have
81+
a reason to use body-parser for other routes, *please do not use it before the
82+
LINE middleware*. body-parser parses the request body up and the LINE middleware
83+
cannot parse it afterwards.
8284

8385
``` js
8486
// don't
@@ -90,16 +92,18 @@ app.use(middleware(config))
9092
app.use(bodyParser.json())
9193
```
9294

93-
`middleware()` will work only on requests with `X-Line-Signature`, so other body
94-
parsers will work well with requests without the signature header.
95+
There are environments where `req.body` is pre-parsed, such as [Firebase Cloud Functions](https://firebase.google.com/docs/functions/http-events).
96+
If it parses the body into string or buffer, do not worry as the middleware will
97+
work just fine. If the pre-parsed body is an object, please use [`validateSignature()`](../api-reference/validate-signature.md)
98+
manually with the raw body.
9599

96100
## Error handling
97101

98102
There are two types of errors thrown by the middleware, one is `SignatureValidationFailed`
99-
and the other is `JSONParseError`. `SignatureValidationFailed` is thrown when a
100-
request has a wrong signature, which usually means the request is not from the
101-
official LINE servers. `JSONParseError` occurs when a request body cannot be
102-
parsed as JSON.
103+
and the other is `JSONParseError`.
104+
105+
- `SignatureValidationFailed` is thrown when a request has a wrong signature.
106+
- `JSONParseError` occurs when a request body cannot be parsed as JSON.
103107

104108
For type references of the errors, please refer to [the API reference](../api-reference/exceptions.md).
105109

0 commit comments

Comments
 (0)