ofetch downloads · last 12 months
ofetch is an HTTP client that provides a higher-level API around fetch for making requests in Node.js, browsers, and workers. It addresses the repeated work of issuing requests and handling responses with a consistent interface across those environments.
Its design centers on making fetch easier to use without limiting it to a single JavaScript runtime. The package is suited to developers who want a shared request pattern across client-side code, server-side code, and worker workflows, while retaining fetch as the underlying mental model.
The primary entry point is $fetch, which can be used to make requests and return parsed response data. The ofetch API also supports creating a configured client with create, so shared request options can be applied across calls; hooks provide points to customize request and response handling, and retry and timeout options cover common request behavior.
Because ofetch works in Node.js, browsers, and workers, the same client pattern can fit frontend applications, server routes, and background tasks. It can be integrated wherever those runtimes can make HTTP requests, including code that needs shared defaults or centralized request and response handling rather than repeated per-call setup.
The package is MIT-licensed and is listed at version 1.5.1, with 10.7M weekly downloads and 5.4K GitHub stars. Its reported unpacked size is 63.9 kB and gzip bundle size is 37.3 kB; these figures are useful when evaluating whether its API conveniences fit a bundle budget, but do not by themselves describe application-level performance.
ofetch remains an HTTP client, not a complete data-fetching or caching layer, so applications may need to supply their own higher-level policies. Its cross-runtime behavior does not remove the need to account for differences in available runtime APIs, and teams should verify the request, retry, and error-handling behavior they require before adopting it.
- Use $fetch when you want a concise call that returns parsed response data instead of repeating fetch response parsing at each call site.
- Use create to define a client with shared base options for requests to the same service, keeping common configuration out of individual calls.
- Use ofetch when the same HTTP request pattern needs to run in Node.js, browsers, and workers.
- Use request and response hooks to centralize behavior that should apply consistently across calls, such as handling request or response events.
- Use the retry and timeout options when individual request calls need explicit retry or time-limit behavior.
- Use ofetch in frontend, server-route, or worker code where a fetch-based API is preferable to maintaining separate client patterns for each runtime.
- Avoid ofetch if you require a complete application data-fetching layer with caching or coordinated background updates; use fetch directly and implement those policies separately.
- Avoid it if your code must expose the native fetch Response object unchanged; use fetch directly so response handling remains under your control.
- Avoid it if your target runtime does not provide the APIs required by the package; use a client compatible with that runtime or provide an appropriate runtime implementation.
- Avoid it if the reported 37.3 kB gzip bundle size exceeds your bundle budget; use the native fetch API to avoid adding this package to the bundle.
- Avoid it if your project requires request behavior that cannot be expressed with ofetch's options, hooks, retry, or timeout patterns; use fetch directly and implement the required behavior explicitly.
CORRECTIONS
Spot wrong data here?Spot wrong data on this page?
A short note helps us fix it.A short note helps us fix it. We read every one; confirmed fixes ship in the next nightly build.
Anonymous · No account · No email back