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
Fromsrc/fetch.ts:268-277:
create method:
- Merges global options with custom global options
- Combines all default options into a single defaults object
- Returns a new
$Fetchinstance with the merged configuration
Options Inheritance
Defaults will be cloned at one level and inherited. Be careful about nested options like
headers.Advanced Example: Retry Configuration
Custom Global Options
You can pass custom global options as the second parameter:- Provide a custom fetch implementation
- Override the global fetch function
- Configure fetch-level defaults