Overview
ofetch provides several type definitions for handling different response formats and type-safe response parsing.ResponseType
The available response types that ofetch can parse."json"- Parse response as JSON (default)"text"- Return response as text string"blob"- Return response as Blob"arrayBuffer"- Return response as ArrayBuffer"stream"- Return response as ReadableStream
ResponseMap
Maps response type strings to their corresponding TypeScript types.responseType.
MappedResponseType
Utility type that maps a response type string to its corresponding TypeScript type.ResponseType
required
The response type string
any
default:"any"
The TypeScript type to use for JSON responses
FetchResponse
Extends the standardResponse interface with an additional _data property containing the parsed response data.
number
HTTP status code (e.g., 200, 404)
string
HTTP status message (e.g., “OK”, “Not Found”)
boolean
True if status is in the range 200-299
Headers
Response headers
string
Final URL of the response (after redirects)
boolean
True if the response is the result of a redirect
ResponseType
Type of response (basic, cors, error, etc.)
ReadableStream | null
Response body stream
T | undefined
Parsed response data. The type depends on the
responseType option.FetchContext
Context object passed to lifecycle hooks containing request, options, response, and error information.FetchRequest
required
The request URL string or Request object
ResolvedFetchOptions<R>
required
Resolved fetch options with headers as a Headers object. See FetchOptions.
FetchResponse<T> | undefined
The response object if available. Present in
onResponse and onResponseError hooks.Error | undefined
Error object if an error occurred. Present in
onRequestError hook.ResolvedFetchOptions
FetchOptions with headers guaranteed to be a Headers object.options property, ensuring headers are always in a consistent format.
Type Parameters
any
default:"any"
The expected type of the response data
ResponseType
default:"ResponseType"
The response type:
"json", "text", "blob", "arrayBuffer", or "stream"Related
- FetchOptions - Request configuration options
- FetchHooks - Lifecycle hooks that use FetchContext
- ofetch.raw() - Returns FetchResponse