wretch
Chainable fetch wrapper with a fluent API for base URLs, headers, error handling and response decoding.
Official Documentation ↗v3.0.9. Check the official docs for the latest API surface.Installation
npm install wretchpnpm add wretchyarn add wretchMethods
No methods match this filter.
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.
- Catchers 🔗 Catchers are optional, but if none are provided an error will still be thrown for http error codes and it will be up to you to catch it. (https://cdn.jsdelivr.net/gh/elbywan/wretch@latest/README.md)
- Request Replay The original request is passed along the error and can be used in order to perform an additional request. (https://cdn.jsdelivr.net/gh/elbywan/wretch@latest/README.md)
- Also, allows using URLs with wretch that contain credentials, which would otherwise throw an error. (https://cdn.jsdelivr.net/gh/elbywan/wretch@latest/README.md)
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.