ofetch
A better fetch API with smart defaults — works universally in browsers, Node.js, and edge runtimes with automatic JSON parsing and error handling.
ofetch
General JSA better fetch API with smart defaults — works universally in browsers, Node.js, and edge runtimes with automatic JSON parsing and error handling.
Fit
Bundle (gzip)
3.9 kB
9.5 kB raw
Install size includes transitive dependencies. Bundle size is gzipped browser payload.
Freshness
The Problem
The native fetch API requires manual response.json() calls, doesn't throw on 4xx/5xx responses, provides no retry support, and requires different polyfills for different environments. Building a consistent HTTP client layer takes boilerplate.
What It Does
ofetch auto-parses JSON responses, throws an FetchError (with the response body) on non-2xx status codes, supports automatic retries with backoff, and uses the native fetch in browsers/edge while polyfilling for Node.js. The $fetch export creates instances with base URL and shared headers.
Installation
npm install ofetchUsage Example
import { ofetch, $fetch } from 'ofetch';
// Automatic JSON parsing, throws on 4xx/5xx
const user = await ofetch<User>('/api/users/123');
// Pre-configured instance
const api = $fetch.create({
baseURL: 'https://api.example.com',
headers: { Authorization: `Bearer ${token}` },
retry: 2,
});
const users = await api<User[]>('/users');
// Error handling includes response body
try {
await api('/missing');
} catch (err) {
console.log(err.response.status); // 404
console.log(err.data); // { message: 'Not found' }
}Related packages
Unified TypeScript SDK for building AI-powered apps — switch between 20+ LLM providers without rewriting code.
animejsGeneral JSLightweight JavaScript animation library with a tiny footprint and a powerful, expressive API.
AOSGeneral JSAn easy-to-use library that makes elements fade, slide, or zoom into view as the user scrolls down your page.
archiverGeneral JSStreaming interface for generating ZIP and TAR archives in Node.js — create compressed archives programmatically from files, directories, and buffers.
@formkit/auto-animateGeneral JS> Add smooth transitions to any element with a single line — zero-config animations for adds, removes, and moves.