{"library":"ofetch","title":"ofetch","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install ofetch"],"cli":null},"imports":["import { ofetch } from 'ofetch'","import type { FetchError } from 'ofetch'","import { createFetch } from 'ofetch'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { ofetch, type FetchError } from \"ofetch\";\n\n// Example of a GET request\nasync function getUserData() {\n  try {\n    const users = await ofetch(\"https://jsonplaceholder.typicode.com/users\");\n    console.log(\"Fetched users:\", users.map(u => u.name));\n  } catch (error) {\n    if (error instanceof FetchError) {\n      console.error(\"GET Error data:\", error.data);\n    } else {\n      console.error(\"GET Unknown error:\", error);\n    }\n  }\n}\n\n// Example of a POST request with JSON body and custom headers\nasync function createUser() {\n  const newUser = {\n    name: \"John Doe\",\n    username: \"johndoe\",\n    email: \"john.doe@example.com\",\n  };\n  try {\n    const response = await ofetch(\"https://jsonplaceholder.typicode.com/users\", {\n      method: \"POST\",\n      body: newUser,\n      headers: {\n        \"X-Client-ID\": process.env.CLIENT_ID ?? 'default-client'\n      },\n    });\n    console.log(\"Created user with ID:\", response.id);\n  } catch (error) {\n    if (error instanceof FetchError) {\n      console.error(\"POST Error data:\", error.data);\n    } else {\n      console.error(\"POST Unknown error:\", error);\n    }\n  }\n}\n\n// Example of fetching a binary response (e.g., an image blob)\nasync function fetchImageBlob() {\n  try {\n    const imageBlob = await ofetch(\"https://via.placeholder.com/150\", {\n      responseType: \"blob\",\n      baseURL: \"https://example.com\"\n    });\n    console.log(`Fetched image blob of type: ${imageBlob.type} and size: ${imageBlob.size} bytes`);\n    // In a browser, you could then use URL.createObjectURL(imageBlob) to display it.\n  } catch (error) {\n    console.error(\"Image fetch error:\", error);\n  }\n}\n\nasync function runExamples() {\n  await getUserData();\n  await createUser();\n  await fetchImageBlob();\n}\n\nrunExamples();","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}