API Reference

wretch

Chainable fetch wrapper with a fluent API for base URLs, headers, error handling and response decoding.

Official Documentation ↗
CompatibilityThis documentation covers version v3.0.9. Check the official docs for the latest API surface.

Installation

npmnpm install wretch
pnpmpnpm add wretch
yarnyarn add wretch

Methods

Overview

wretch turns fetch into a readable chain. Each builder step configures part of the request, and the chain ends with a verb plus a decoder such as .json(), .text() or .blob(). Error handling can be declared inline with .catcher().

Core Methods

Start with wretch(baseUrl), add options with .headers(), .query(), .body(), then call .get(), .post(), .put() or .delete() followed by a response decoder. The same builder can be cloned and reused.

Usage Notes

Define a base builder per service and derive request-specific chains from it. Prefer .errorType(“json”) when the backend returns structured errors so catch blocks receive parsed payloads.

Common Pitfalls

Chains are lazy; nothing is sent until a decoder or a .fetch() is called. Reusing a single builder across concurrent calls is safe only if you do not mutate shared middlewares between requests.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/elbywan/wretch@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:53:30.678Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

How do I handle errors in wretch?
Use .errorType() to choose how error bodies are decoded and .catcher() or .catch() to branch on status codes. Without a catcher, non-ok responses reject the promise with an error carrying the status.
How are query parameters added?
Pass a plain object to .query({ page: 1 }) and wretch serializes it into the URL, handling encoding for you.
Can wretch work with a base URL and headers?
wretch(baseUrl) stores the prefix and .headers() or .auth() merge headers into every request from that builder, which is convenient for API clients.