Skip to main content

Overview

The FetchOptions interface extends the standard RequestInit and provides additional configuration for ofetch requests including base URL, query parameters, hooks, retry logic, and response parsing.

Type Signature

Options

baseURL

string
Base URL to prepend to all requests. The request URL will be resolved against this base.

body

RequestInit['body'] | Record<string, any>
Request body. Accepts standard fetch body types or plain objects. Objects are automatically serialized to JSON and appropriate headers are set.

ignoreResponseError

boolean
default:"false"
When true, prevents ofetch from throwing errors for 4xx and 5xx status codes. The response will be returned normally instead.

query

Record<string, any>
Query parameters to append to the URL. Values are automatically URL-encoded.

params

Record<string, any>
Deprecated: Use query instead. Query parameters to append to the URL.

parseResponse

(responseText: string) => any
Custom function to parse response text. Defaults to JSON.parse for JSON responses.

responseType

ResponseType
Expected response type. One of: "json", "text", "blob", "arrayBuffer", or "stream". Defaults to "json". See ResponseType.

duplex

'half' | undefined
Experimental: Set to "half" to enable duplex streaming. Automatically set when using a ReadableStream as body.

dispatcher

InstanceType<typeof import('undici').Dispatcher>
Only supported in Node.js >= 18 using undici. Custom dispatcher for advanced request handling.
See undici documentation for details.

agent

unknown
Only supported in older Node.js versions using node-fetch-native polyfill. Custom agent for HTTP(S) requests.

timeout

number
Request timeout in milliseconds. The request will be aborted if it takes longer than this value.

retry

number | false
Number of times to retry failed requests. Set to false to disable retries. Defaults to 1 for GET requests and 0 for other methods.

retryDelay

number | ((context: FetchContext<T, R>) => number)
Delay between retries in milliseconds. Can be a number or a function that returns the delay based on the FetchContext.

retryStatusCodes

number[]
HTTP status codes that should trigger a retry. Defaults to [408, 409, 425, 429, 500, 502, 503, 504].

Hooks

FetchOptions extends FetchHooks, providing lifecycle hooks:
FetchHook | FetchHook[]
Called before the request is sent. See onRequest.
FetchHook | FetchHook[]
Called when the request fails. See onRequestError.
FetchHook | FetchHook[]
Called after a successful response. See onResponse.
FetchHook | FetchHook[]
Called when the response has an error status (4xx, 5xx). See onResponseError.

Standard RequestInit Options

FetchOptions also includes all standard RequestInit options except body (which is redefined):
  • method - HTTP method (GET, POST, PUT, DELETE, etc.)
  • headers - Request headers
  • signal - AbortSignal for request cancellation
  • credentials - Credentials mode (omit, same-origin, include)
  • mode - Request mode (cors, no-cors, same-origin)
  • redirect - Redirect mode (follow, error, manual)
  • referrer - Referrer URL
  • referrerPolicy - Referrer policy
  • integrity - Subresource integrity value
  • keepalive - Keep connection alive
  • cache - Cache mode
See MDN RequestInit documentation for details.

Type Parameters

ResponseType
default:"ResponseType"
The expected response type. See ResponseType.
any
default:"any"
The expected type of the response data