Skip to content

Commit e343948

Browse files
authored
doc deprecate bodymixin.formData (#2892)
* doc deprecate bodymixin.formData * fixup * add dedicated body mixin section
1 parent a3f494b commit e343948

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

docs/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ console.log('trailers', trailers)
6060

6161
The `body` mixins are the most common way to format the request/response body. Mixins include:
6262

63-
- [`.formData()`](https://fetch.spec.whatwg.org/#dom-body-formdata)
63+
- [`.arrayBuffer()`](https://fetch.spec.whatwg.org/#dom-body-arraybuffer)
64+
- [`.blob()`](https://fetch.spec.whatwg.org/#dom-body-blob)
6465
- [`.json()`](https://fetch.spec.whatwg.org/#dom-body-json)
6566
- [`.text()`](https://fetch.spec.whatwg.org/#dom-body-text)
6667

68+
> [!NOTE]
69+
> The body returned from `undici.request` does not implement `.formData()`.
70+
6771
Example usage:
6872

6973
```js
@@ -226,7 +230,7 @@ await fetch('https://example.com', { body: data, method: 'POST', duplex: 'half'
226230

227231
- half
228232

229-
In this implementation of fetch, `request.duplex` must be set if `request.body` is `ReadableStream` or `Async Iterables`. And fetch requests are currently always be full duplex. More detail refer to [Fetch Standard.](https://fetch.spec.whatwg.org/#dom-requestinit-duplex)
233+
In this implementation of fetch, `request.duplex` must be set if `request.body` is `ReadableStream` or `Async Iterables`, however, fetch requests are currently always full duplex. For more detail refer to the [Fetch Standard.](https://fetch.spec.whatwg.org/#dom-requestinit-duplex).
230234

231235
#### `response.body`
232236

docs/docs/api/Fetch.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ In Node versions v18.13.0 and above and v19.2.0 and above, undici will default t
1212

1313
## FormData
1414

15-
This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/FormData)
15+
This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/FormData).
16+
17+
If any parameters are passed to the FormData constructor other than `undefined`, an error will be thrown. Other parameters are ignored.
1618

1719
## Response
1820

@@ -25,3 +27,31 @@ This API is implemented as per the standard, you can find documentation on [MDN]
2527
## Header
2628

2729
This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
30+
31+
# Body Mixins
32+
33+
`Response` and `Request` body inherit body mixin methods. These methods include:
34+
35+
- [`.arrayBuffer()`](https://fetch.spec.whatwg.org/#dom-body-arraybuffer)
36+
- [`.blob()`](https://fetch.spec.whatwg.org/#dom-body-blob)
37+
- [`.formData()`](https://fetch.spec.whatwg.org/#dom-body-formdata)
38+
- [`.json()`](https://fetch.spec.whatwg.org/#dom-body-json)
39+
- [`.text()`](https://fetch.spec.whatwg.org/#dom-body-text)
40+
41+
There is an ongoing discussion regarding `.formData()` and its usefulness and performance in server environments. It is recommended to use a dedicated library for parsing `multipart/form-data` bodies, such as [Busboy](https://www.npmjs.com/package/busboy) or [@fastify/busboy](https://www.npmjs.com/package/@fastify/busboy).
42+
43+
These libraries can be interfaced with fetch with the following example code:
44+
45+
```mjs
46+
import { Busboy } from '@fastify/busboy'
47+
import { Readable } from 'node:stream'
48+
49+
const response = await fetch('...')
50+
const busboy = new Busboy({
51+
headers: {
52+
'content-type': response.headers.get('content-type')
53+
}
54+
})
55+
56+
Readable.fromWeb(response.body).pipe(busboy)
57+
```

types/fetch.d.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,29 @@ export type BodyInit =
2727
| null
2828
| string
2929

30-
export interface BodyMixin {
30+
export class BodyMixin {
3131
readonly body: ReadableStream | null
3232
readonly bodyUsed: boolean
3333

3434
readonly arrayBuffer: () => Promise<ArrayBuffer>
3535
readonly blob: () => Promise<Blob>
36+
/**
37+
* @deprecated This method is not recommended for parsing multipart/form-data bodies in server environments.
38+
* It is recommended to use a library such as [@fastify/busboy](https://www.npmjs.com/package/@fastify/busboy) as follows:
39+
*
40+
* @example
41+
* ```js
42+
* import { Busboy } from '@fastify/busboy'
43+
* import { Readable } from 'node:stream'
44+
*
45+
* const response = await fetch('...')
46+
* const busboy = new Busboy({ headers: { 'content-type': response.headers.get('content-type') } })
47+
*
48+
* // handle events emitted from `busboy`
49+
*
50+
* Readable.fromWeb(response.body).pipe(busboy)
51+
* ```
52+
*/
3653
readonly formData: () => Promise<FormData>
3754
readonly json: () => Promise<unknown>
3855
readonly text: () => Promise<string>
@@ -135,7 +152,7 @@ export type RequestRedirect = 'error' | 'follow' | 'manual'
135152

136153
export type RequestDuplex = 'half'
137154

138-
export declare class Request implements BodyMixin {
155+
export declare class Request extends BodyMixin {
139156
constructor (input: RequestInfo, init?: RequestInit)
140157

141158
readonly cache: RequestCache
@@ -153,15 +170,6 @@ export declare class Request implements BodyMixin {
153170
readonly signal: AbortSignal
154171
readonly duplex: RequestDuplex
155172

156-
readonly body: ReadableStream | null
157-
readonly bodyUsed: boolean
158-
159-
readonly arrayBuffer: () => Promise<ArrayBuffer>
160-
readonly blob: () => Promise<Blob>
161-
readonly formData: () => Promise<FormData>
162-
readonly json: () => Promise<unknown>
163-
readonly text: () => Promise<string>
164-
165173
readonly clone: () => Request
166174
}
167175

@@ -181,7 +189,7 @@ export type ResponseType =
181189

182190
export type ResponseRedirectStatus = 301 | 302 | 303 | 307 | 308
183191

184-
export declare class Response implements BodyMixin {
192+
export declare class Response extends BodyMixin {
185193
constructor (body?: BodyInit, init?: ResponseInit)
186194

187195
readonly headers: Headers
@@ -192,15 +200,6 @@ export declare class Response implements BodyMixin {
192200
readonly url: string
193201
readonly redirected: boolean
194202

195-
readonly body: ReadableStream | null
196-
readonly bodyUsed: boolean
197-
198-
readonly arrayBuffer: () => Promise<ArrayBuffer>
199-
readonly blob: () => Promise<Blob>
200-
readonly formData: () => Promise<FormData>
201-
readonly json: () => Promise<unknown>
202-
readonly text: () => Promise<string>
203-
204203
readonly clone: () => Response
205204

206205
static error (): Response

0 commit comments

Comments
 (0)