Skip to main content

Overview

ofetch.create() allows you to create new fetch instances with pre-configured options that are shared across all requests. This is useful for setting up API clients with common settings like base URLs, headers, and interceptors.

Basic Usage

Common Use Cases

API Client with Base URL

Create a dedicated fetch instance for your API:

Authenticated Requests

Set up authentication headers globally:

Multiple API Clients

Create separate instances for different services:

How It Works

From src/fetch.ts:268-277:
The create method:
  1. Merges global options with custom global options
  2. Combines all default options into a single defaults object
  3. Returns a new $Fetch instance with the merged configuration

Options Inheritance

Defaults will be cloned at one level and inherited. Be careful about nested options like headers.
Options are merged shallowly. For nested options like headers, the entire object is replaced:
To preserve base headers, use interceptors:

Advanced Example: Retry Configuration

Custom Global Options

You can pass custom global options as the second parameter:
This allows you to:
  • Provide a custom fetch implementation
  • Override the global fetch function
  • Configure fetch-level defaults

TypeScript Support

The created instance maintains full type safety: