doc: improved fetch docs
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
d940b15843
commit
2917f09876
@ -498,6 +498,46 @@ changes:
|
||||
|
||||
A browser-compatible implementation of the [`fetch()`][] function.
|
||||
|
||||
```mjs
|
||||
const res = await fetch('https://nodejs.org/api/documentation.json');
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
console.log(data);
|
||||
}
|
||||
```
|
||||
|
||||
The implementation is based upon [undici](https://undici.nodejs.org), an HTTP/1.1 client
|
||||
written from scratch for Node.js. You can figure out which version of `undici` is bundled
|
||||
in your Node.js process reading the `process.versions.undici` property.
|
||||
|
||||
## Custom dispatcher
|
||||
|
||||
You can use a custom dispatcher to dispatch requests passing it in fetch's options object.
|
||||
The dispatcher must be compatible with `undici`'s
|
||||
[`Dispatcher` class](https://undici.nodejs.org/#/docs/api/Dispatcher.md).
|
||||
|
||||
```js
|
||||
fetch(url, { dispatcher: new MyAgent() });
|
||||
```
|
||||
|
||||
It is possible to change the global dispatcher in Node.js installing `undici` and using
|
||||
the `setGlobalDispatcher()` method. Calling this method will affect both `undici` and
|
||||
Node.js.
|
||||
|
||||
```mjs
|
||||
import { setGlobalDispatcher } from 'undici';
|
||||
setGlobalDispatcher(new MyAgent());
|
||||
```
|
||||
|
||||
## Related classes
|
||||
|
||||
The following globals are available to use with `fetch`:
|
||||
|
||||
* [`FormData`](https://nodejs.org/api/globals.html#class-formdata)
|
||||
* [`Headers`](https://nodejs.org/api/globals.html#class-headers)
|
||||
* [`Request`](https://nodejs.org/api/globals.html#request)
|
||||
* [`Response`](https://nodejs.org/api/globals.html#response).
|
||||
|
||||
## Class: `File`
|
||||
|
||||
<!-- YAML
|
||||
|
Loading…
x
Reference in New Issue
Block a user