Wretch

A tiny (~1.8KB gzipped) wrapper built around fetch with an intuitive syntax.

Wretch does not mutate its internal state. Each function returns a copy of the original object.

Wretch's syntax is readable and concise. Includes TypeScript definition files for autocompletion.

All methods are 100% chainable. Because a.b().c() is prettier than var z = a; z.b(); z.c().

Wretch is compatible with browsers and Node.js. Use any polyfill you like!

Use Addons to add new methods and capabilities. Use Middlewares to intercept requests and fully customize Wretch behaviour.

npm i wretch
yarn add wretch
pnpm add wretch
bun add wretch
<script src="https://unpkg.com/wretch"></script>

Quick Start

Build beautiful, chainable HTTP requests in seconds

1

Import wretch

import wretch from "wretch"
2

Create an instance

wretch()
3

Configure with helpers

  .url("https://jsonplaceholder.typicode.com/users")
4

Add request body

  .json({ name: "John Doe", email: "john@example.com" }) // .formData, .formUrl…
5

Execute HTTP method

  .post() // .get, .put, .patch, .delete…
6

Handle errors

  .notFound(err => console.log("User not found")) // .unauthorized, .forbidden…
  .error(500, err => console.log("Server error"))
7

Parse response

  .json() // .text, .blob, .arrayBuffer…

Complete Example

See all the pieces come together in one beautiful chain

import wretch from "wretch"

const user = await wretch("https://jsonplaceholder.typicode.com/users")
  .json({ name: "John Doe", email: "john@example.com" })
  .post()
  .notFound(err => console.log("User not found"))
  .error(500, err => console.log("Server error"))
  .json()

console.log(user)