Skip to content

Commit 26cf8df

Browse files
EzraElletteimjoshinEzra EllettepiehLekoArts
authored
chore(docs): Add ability to configure body parsing in Gatsby Functions (#35569)
Co-authored-by: Josh Johnson <[email protected]> Co-authored-by: Ezra Ellette <[email protected]> Co-authored-by: Michal Piechowiak <[email protected]> Co-authored-by: Lennart <[email protected]>
1 parent 1535682 commit 26cf8df

File tree

1 file changed

+81
-3
lines changed

1 file changed

+81
-3
lines changed

docs/docs/reference/functions/middleware-and-helpers.md

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,85 @@ export default async function corsHandler(req, res) {
5151

5252
## Custom body parsing
5353

54-
Generally useful for file upload support.
54+
> Support for overriding default config added in `[email protected]`
55+
56+
By default, Gatsby is using following configuration defaults to parse request body and make it available as `req.body` field in appropriate format:
57+
58+
```js
59+
{
60+
bodyParser: {
61+
json: {
62+
type: "application/json",
63+
limit: "100kb"
64+
},
65+
raw: {
66+
type: "application/octet-stream",
67+
limit: "100kb"
68+
},
69+
text: {
70+
type: "text/plain",
71+
limit: "100kb"
72+
},
73+
urlencoded: {
74+
type: "application/x-www-form-urlencoded",
75+
limit: "100kb",
76+
extended: true
77+
}
78+
}
79+
}
80+
```
81+
82+
Those settings work in most cases, but sometimes you might need to adjust them to cover your use case. Gatsby allows exporting an object named `config` from your function handler module. This object allows you to control the `body-parser` middleware used by Gatsby Functions. Gatsby currently supports the `limit`, `type`, and `extended` options for the `bodyParser` configuration, which are documented by [body-parser](https://expressjs.com/en/resources/middleware/body-parser.html). The `limit` property will allow configuration of payload size up to **32mb**.
83+
84+
### Examples
85+
86+
#### Accessing body as a `Buffer`
87+
88+
You can modify what `Content-type` particular `body-parser` middleware can act on. Following configuration will force every request to use `raw` parser and result in function handler receiving `req.body` as a `Buffer`. A setup like this is useful if you are looking to verify signature of webhooks (e.g. https://stripe.com/docs/webhooks/signatures).
89+
90+
```js:title=src/api/some-function.js
91+
export const config = {
92+
bodyParser: {
93+
raw: {
94+
type: `*/*`,
95+
},
96+
},
97+
}
98+
99+
export default function MyAPIFunction(req, res) {
100+
// req.body will be a Buffer, no matter what's the `Content-type` header on request
101+
}
102+
```
103+
104+
#### Increasing or decreasing the payload limit
55105

56-
This is not yet supported. [Add a comment in the discussion if this is an
57-
important use case for you](https://github.com/gatsbyjs/gatsby/discussions/30735).
106+
By default, the limit is `100kb`. If the request body is larger than that it will result in automatic `413 Request Entity Too Large` response without executing function handler at all.
107+
108+
If your use case require a higher limit, you can bump it up in `config`.
109+
110+
```js:title=src/api/some-function.js
111+
// limit payload to 10mb
112+
export const config = {
113+
bodyParser: {
114+
json: {
115+
limit: `10mb`,
116+
},
117+
},
118+
}
119+
```
120+
121+
#### TypeScript (`config` object type)
122+
123+
You can import `GatsbyFunctionConfig` from `gatsby` to type your `config` export:
124+
125+
```ts:title=src/api/some-function.ts
126+
import { GatsbyFunctionConfig } from "gatsby"
127+
128+
export const config: GatsbyFunctionConfig = {
129+
bodyParser: {
130+
json: {
131+
limit: `10mb`,
132+
},
133+
},
134+
}
135+
```

0 commit comments

Comments
 (0)