ofetch is a modern, cross-platform HTTP client that provides a simplified, "better fetch API" for Node.js, browsers, and web workers. It intelligently handles request and response parsing, automatically stringifying JSON bodies and parsing responses using `destr` for robustness. Key features include automatic error throwing with detailed `FetchError` objects, built-in retry mechanisms for transient network issues and specific status codes, and seamless handling of binary and stream responses. The current stable version is 1.5.1, with a major v2.0.0 release in alpha that will transition to an ESM-only architecture, remove external dependencies, and further reduce bundle size. ofetch differentiates itself by offering a consistent, feature-rich `fetch` experience across diverse JavaScript environments, abstracting away common boilerplate and error handling patterns.
npm install ofetchVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic GET and POST requests, handling JSON bodies, custom headers, error catching with `FetchError`, and fetching binary data as a Blob, highlighting `ofetch`'s versatility.
Migrate your project to use ES module `import` syntax. For Node.js, ensure your `package.json` has `"type": "module"` or use `.mjs` file extensions.
Ensure that any logic interacting with `ctx.options.headers` in `onRequest` or `onResponse` hooks explicitly handles or expects a `Headers` instance, using methods like `headers.get()` or `headers.set()`.
If automatic retries are undesirable or require fine-tuning, you can configure the `retry` and `retryStatusCodes` options (e.g., `retry: 0` to disable) or use the `onRetry` hook for custom logic.
Always wrap `ofetch` calls in a `try...catch` block to handle HTTP errors. Access the error details via `error.data` (if `error` is a `FetchError`) or bypass error throwing with `ignoreResponseError: true` in options.
Change `const { ofetch } = require('ofetch');` to `import { ofetch } from 'ofetch';` and ensure your project is configured for ES modules.Ensure you type-check the error: `if (error instanceof FetchError) { console.error(error.data); }` or explicitly cast the error type if confident.If you need to read the response body multiple times within interceptors, clone the response first: `const clonedResponse = response.clone();`.
Ensure your interceptor logic is robust to `Headers` objects and uses methods like `headers.get()`. For pre-1.4.0, manually convert `HeadersInit` to `Headers` if needed for consistent API access.
No dependency data recorded yet.