Skip to content

docs(readme): add example for using a custom Agent #614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ the passed options and sends the request using [fetch](https://developer.mozilla
- [Special cases](#special-cases)
- [The `data` parameter – set request body directly](#the-data-parameter--set-request-body-directly)
- [Set parameters for both the URL/query and the request body](#set-parameters-for-both-the-urlquery-and-the-request-body)
- [Set a custom Agent to your requests](#set-a-custom-agent-to-your-requests)
- [LICENSE](#license)

<!-- tocstop -->
Expand Down Expand Up @@ -528,6 +529,40 @@ request(
);
```

### Set a custom Agent to your requests

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).

Example ([See example in CodeSandbox](https://codesandbox.io/p/sandbox/nifty-stitch-wdlwlf))

```js
import { request } from "@octokit/request";
import { fetch as undiciFetch, Agent } from "undici";

/** @type {typeof import("undici").fetch} */
const myFetch = (url, options) => {
return undiciFetch(url, {
...options,
dispatcher: new Agent({
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10,
}),
});
};

const { data } = await request("GET /users/{username}", {
username: "octocat",
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
options: {
request: {
fetch: myFetch,
},
},
});
```

## LICENSE

[MIT](LICENSE)