Skip to content
Open
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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,36 @@ const apiFetch = ofetch.create({ baseURL: "/api" });
apiFetch("/test"); // Same as ofetch('/test', { baseURL: '/api' })
```

## ✔️ Custom `fetch` implementation

`ofetch` calls the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) under the hood. By default it uses `globalThis.fetch`, but you can provide your own implementation when creating an instance.

This is useful for tests (mocking requests), forcing a specific runtime implementation (for example [undici](https://undici.nodejs.org/) in Node.js), or environments where you need a custom `fetch` wrapper.

Pass `fetch` to `createFetch` or as the second argument of `ofetch.create`:

```ts
import { createFetch } from "ofetch";

const $fetch = createFetch({
fetch: myCustomFetch,
defaults: {
baseURL: "/api",
},
});
```

```ts
import { ofetch } from "ofetch";

const apiFetch = ofetch.create(
{ baseURL: "/api" },
{ fetch: myCustomFetch }
);
```

The custom `fetch` must match the standard `fetch(input, init?)` signature. All `ofetch` features (JSON body handling, retries, hooks, and so on) still run on top of it.

## 💡 Adding headers

By using `headers` option, `ofetch` adds extra headers in addition to the request default headers:
Expand Down