Skip to content

Commit a11e09d

Browse files
lifeisfooaduh95
authored andcommitted
doc: improved fetch docs
PR-URL: #57296 Refs: https://undici.nodejs.org Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent a5d68ba commit a11e09d

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
@@ -516,6 +516,46 @@ changes:
516516
517517
A browser-compatible implementation of the [`fetch()`][] function.
518518

519+
```mjs
520+
const res = await fetch('https://nodejs.org/api/documentation.json');
521+
if (res.ok) {
522+
const data = await res.json();
523+
console.log(data);
524+
}
525+
```
526+
527+
The implementation is based upon [undici](https://undici.nodejs.org), an HTTP/1.1 client
528+
written from scratch for Node.js. You can figure out which version of `undici` is bundled
529+
in your Node.js process reading the `process.versions.undici` property.
530+
531+
## Custom dispatcher
532+
533+
You can use a custom dispatcher to dispatch requests passing it in fetch's options object.
534+
The dispatcher must be compatible with `undici`'s
535+
[`Dispatcher` class](https://undici.nodejs.org/#/docs/api/Dispatcher.md).
536+
537+
```js
538+
fetch(url, { dispatcher: new MyAgent() });
539+
```
540+
541+
It is possible to change the global dispatcher in Node.js installing `undici` and using
542+
the `setGlobalDispatcher()` method. Calling this method will affect both `undici` and
543+
Node.js.
544+
545+
```mjs
546+
import { setGlobalDispatcher } from 'undici';
547+
setGlobalDispatcher(new MyAgent());
548+
```
549+
550+
## Related classes
551+
552+
The following globals are available to use with `fetch`:
553+
554+
* [`FormData`](https://nodejs.org/api/globals.html#class-formdata)
555+
* [`Headers`](https://nodejs.org/api/globals.html#class-headers)
556+
* [`Request`](https://nodejs.org/api/globals.html#request)
557+
* [`Response`](https://nodejs.org/api/globals.html#response).
558+
519559
## Class: `File`
520560

521561
<!-- YAML

0 commit comments

Comments
 (0)