Skip to content

Commit 27b113d

Browse files
lifeisfooRafaelGSS
authored andcommitted
doc: improved fetch docs
PR-URL: #57296 Refs: https://undici.nodejs.org Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a3ad331 commit 27b113d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

doc/api/globals.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,46 @@ changes:
561561
562562
A browser-compatible implementation of the [`fetch()`][] function.
563563

564+
```mjs
565+
const res = await fetch('https://nodejs.org/api/documentation.json');
566+
if (res.ok) {
567+
const data = await res.json();
568+
console.log(data);
569+
}
570+
```
571+
572+
The implementation is based upon [undici](https://undici.nodejs.org), an HTTP/1.1 client
573+
written from scratch for Node.js. You can figure out which version of `undici` is bundled
574+
in your Node.js process reading the `process.versions.undici` property.
575+
576+
## Custom dispatcher
577+
578+
You can use a custom dispatcher to dispatch requests passing it in fetch's options object.
579+
The dispatcher must be compatible with `undici`'s
580+
[`Dispatcher` class](https://undici.nodejs.org/#/docs/api/Dispatcher.md).
581+
582+
```js
583+
fetch(url, { dispatcher: new MyAgent() });
584+
```
585+
586+
It is possible to change the global dispatcher in Node.js installing `undici` and using
587+
the `setGlobalDispatcher()` method. Calling this method will affect both `undici` and
588+
Node.js.
589+
590+
```mjs
591+
import { setGlobalDispatcher } from 'undici';
592+
setGlobalDispatcher(new MyAgent());
593+
```
594+
595+
## Related classes
596+
597+
The following globals are available to use with `fetch`:
598+
599+
* [`FormData`](https://nodejs.org/api/globals.html#class-formdata)
600+
* [`Headers`](https://nodejs.org/api/globals.html#class-headers)
601+
* [`Request`](https://nodejs.org/api/globals.html#request)
602+
* [`Response`](https://nodejs.org/api/globals.html#response).
603+
564604
## Class: `File`
565605

566606
<!-- YAML

0 commit comments

Comments
 (0)