Skip to main content

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.
Available Types:
  • "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
Example:

ResponseMap

Maps response type strings to their corresponding TypeScript types.
This interface is used internally for type inference when you specify a responseType.

MappedResponseType

Utility type that maps a response type string to its corresponding TypeScript type.
Type Parameters:
ResponseType
required
The response type string
any
default:"any"
The TypeScript type to use for JSON responses
Example:

FetchResponse

Extends the standard Response interface with an additional _data property containing the parsed response data.
Properties: Inherits all properties from the standard Response interface:
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
Additional Property:
T | undefined
Parsed response data. The type depends on the responseType option.
Example:

FetchContext

Context object passed to lifecycle hooks containing request, options, response, and error information.
Properties:
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.
Example:

ResolvedFetchOptions

FetchOptions with headers guaranteed to be a Headers object.
This is the type used in the FetchContext’s 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"