Skip to content

Commit 59ef35f

Browse files
authored
docs(readme): add example for using a custom Agent (#614)
1 parent 724699d commit 59ef35f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Diff for: README.md

+35
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ the passed options and sends the request using [fetch](https://developer.mozilla
2929
- [Special cases](#special-cases)
3030
- [The `data` parameter – set request body directly](#the-data-parameter--set-request-body-directly)
3131
- [Set parameters for both the URL/query and the request body](#set-parameters-for-both-the-urlquery-and-the-request-body)
32+
- [Set a custom Agent to your requests](#set-a-custom-agent-to-your-requests)
3233
- [LICENSE](#license)
3334

3435
<!-- tocstop -->
@@ -528,6 +529,40 @@ request(
528529
);
529530
```
530531

532+
### Set a custom Agent to your requests
533+
534+
The way to pass a custom `Agent` to requests is by creating a custom `fetch` function and pass it as `options.request.fetch`. A good example can be [undici's `fetch` implementation](https://undici.nodejs.org/#/?id=undicifetchinput-init-promise).
535+
536+
Example ([See example in CodeSandbox](https://codesandbox.io/p/sandbox/nifty-stitch-wdlwlf))
537+
538+
```js
539+
import { request } from "@octokit/request";
540+
import { fetch as undiciFetch, Agent } from "undici";
541+
542+
/** @type {typeof import("undici").fetch} */
543+
const myFetch = (url, options) => {
544+
return undiciFetch(url, {
545+
...options,
546+
dispatcher: new Agent({
547+
keepAliveTimeout: 10,
548+
keepAliveMaxTimeout: 10,
549+
}),
550+
});
551+
};
552+
553+
const { data } = await request("GET /users/{username}", {
554+
username: "octocat",
555+
headers: {
556+
"X-GitHub-Api-Version": "2022-11-28",
557+
},
558+
options: {
559+
request: {
560+
fetch: myFetch,
561+
},
562+
},
563+
});
564+
```
565+
531566
## LICENSE
532567

533568
[MIT](LICENSE)

0 commit comments

Comments
 (0)