safeFetch
Wraps fetch with built-in error handling: returns null when the
request fails (network error, non-OK status, or parse error) instead
of throwing.
Available since v2.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
input | URL | RequestInfo | URL or `Request` object passed to `fetch` |
init | RequestInit | Optional `RequestInit` options passed to `fetch` (optional) |
options | SafeFetchOptions | Parsing options (default: `{ parse: ‘json’ }`) |
Returns
Section titled “Returns”Promise<T | null> — The parsed response body, or null on any failure
Examples
Section titled “Examples”Fetch JSON safely
Section titled “Fetch JSON safely”Returns null on network error or non-OK status instead of throwing.
Fetch plain text
Section titled “Fetch plain text”Pass { parse: “text” } to get the raw response body as a string.
Related Types
Section titled “Related Types”SafeFetchOptions
Section titled “SafeFetchOptions”Options for safeFetch.
