Overview
TheFetchOptions 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
Base URL to prepend to all requests. The request URL will be resolved against this base.
body
Request body. Accepts standard fetch body types or plain objects. Objects are automatically serialized to JSON and appropriate headers are set.
ignoreResponseError
When
true, prevents ofetch from throwing errors for 4xx and 5xx status codes. The response will be returned normally instead.query
Query parameters to append to the URL. Values are automatically URL-encoded.
params
Deprecated: Use
query instead. Query parameters to append to the URL.parseResponse
Custom function to parse response text. Defaults to
JSON.parse for JSON responses.responseType
Expected response type. One of:
"json", "text", "blob", "arrayBuffer", or "stream". Defaults to "json". See ResponseType.duplex
Experimental: Set to
"half" to enable duplex streaming. Automatically set when using a ReadableStream as body.dispatcher
Only supported in Node.js >= 18 using undici. Custom dispatcher for advanced request handling.
agent
Only supported in older Node.js versions using node-fetch-native polyfill. Custom agent for HTTP(S) requests.
timeout
Request timeout in milliseconds. The request will be aborted if it takes longer than this value.
retry
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
Delay between retries in milliseconds. Can be a number or a function that returns the delay based on the FetchContext.
retryStatusCodes
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:Called when the request fails. See onRequestError.
Called after a successful response. See onResponse.
Called when the response has an error status (4xx, 5xx). See onResponseError.
Standard RequestInit Options
FetchOptions also includes all standardRequestInit options except body (which is redefined):
method- HTTP method (GET, POST, PUT, DELETE, etc.)headers- Request headerssignal- AbortSignal for request cancellationcredentials- Credentials mode (omit, same-origin, include)mode- Request mode (cors, no-cors, same-origin)redirect- Redirect mode (follow, error, manual)referrer- Referrer URLreferrerPolicy- Referrer policyintegrity- Subresource integrity valuekeepalive- Keep connection alivecache- Cache mode
Type Parameters
The expected response type. See ResponseType.
The expected type of the response data
Related
- FetchHooks - Lifecycle hooks
- ResponseType - Response type options
- FetchContext - Context object passed to hooks