talira

A better fetch API. Works on node, browser, and workers.

open sourceMITself-hostablefree

ofetch is a thin layer over the Fetch API from the UnJS project that runs in Node.js, browsers and worker runtimes. It parses JSON responses on its own (and returns a Blob for binary content types), stringifies object bodies and sets JSON headers, throws a FetchError with the parsed error body when a response isn't ok, and retries idempotent requests on status codes such as 429, 500 and 503. It adds timeouts, a baseURL, query parameters, typed responses and async interceptors (onRequest, onResponse and their error variants), with ofetch.create for shared defaults and ofetch.raw for the full response. The stable release is 1.x; a 2.0 alpha is being developed on the main branch.

Problems it solves

  • one fetch helper that works the same in Node.js, the browser and edge workers
  • stop writing response.json() and JSON.stringify for every API call
  • get the server's error response body when a request fails, not just the status code
  • retry GET requests on 429 and 5xx responses without retrying POSTs and creating duplicates
  • log or add auth headers to every request with interceptors around fetch
  • set a base URL and query parameters for API calls without building URLs by hand
  • abort an API request after a few seconds if the server doesn't answer

Features

  • automatic JSON parsing, Blob for binary content types
  • automatic JSON body stringifying and headers
  • FetchError with the parsed error body
  • retries on configurable status codes, not by default for POST, PUT, PATCH and DELETE
  • timeout option
  • baseURL and query options
  • typed responses
  • onRequest, onRequestError, onResponse and onResponseError interceptors
  • ofetch.create for shared defaults
  • ofetch.raw for headers and the full response
View 2 more
  • streaming bodies and SSE
  • proxies through undici's ProxyAgent in Node.js

Capabilities

Has an APIplan not known

Alternatives

The same job

These do what ofetch does.

  1. node-fetch A light-weight module that brings the Fetch API to Node.js
  2. axios Promise based HTTP client for the browser and node.js
  3. SuperAgent Ajax for Node.js and browsers (JS HTTP client)
  4. Ky Tiny & elegant JavaScript HTTP client based on the Fetch API
  5. undici An HTTP/1.1 client, written from scratch for Node.js
  6. Got Human-friendly and powerful HTTP request library for Node.js

Compare ofetch with the top 3