API Reference

ofetch

Better fetch API for Node, browsers and workers with parsed responses, automatic retries and interceptors.

Official Documentation ↗
CompatibilityThis documentation covers version ofetch v1.x / 任意支持 global fetch 的运行时. Check the official docs for the latest API surface.

Installation

npmnpm install ofetch
pnpmpnpm add ofetch
yarnyarn add ofetch

Methods

Overview

ofetch is a fetch superset that removes the repetitive parts of calling the platform API: it parses responses, maps error statuses to thrown errors, adds retries and base URL handling, and works across runtimes without polyfilling.

Core Methods

Call ofetch directly for one-off requests, or ofetch.create to build a client with shared baseURL, headers, retry policy and interceptors. ofetch.raw gives you the full response for cases where the body alone is not enough.

Usage Notes

Use a typed generic to describe the response shape, and centralize auth and error handling in interceptors so every call behaves consistently. Prefer create over ad-hoc options on each call.

Common Pitfalls

Automatic parsing throws on non-2xx by default, so wrap calls in try/catch and inspect FetchError.response. Large binary downloads should not use the parsed path because the whole body is buffered into memory.

FAQ

How does ofetch parse the response?
ofetch inspects the content type and parses JSON, text, blob or array buffer automatically. For statuses that indicate failure it throws a FetchError containing the parsed body.
Can I add request and response interceptors?
Yes. onRequest, onRequestError, onResponse and onResponseError run around each request, letting you set headers, log, or transform errors centrally.
Does ofetch support streaming?
The parsed convenience methods read the whole body. For streaming you should use the platform fetch directly or options that expose the raw stream, since automatic parsing buffers the response.