Wretch uses a two-stage chaining pattern: a Request Chain followed by a Response Chain.
1. Request Chain
Starts with wretch(url). You can chain helper methods (like .headers(), .auth(), .query()), body type methods (like .json() or .formData()), and finally an HTTP method (like .get(), .post(), .put(), .delete()). The HTTP method call triggers the actual fetch().
2. Response Chain
Once fetch() is called, the response chain begins. You can chain catchers (like .notFound(), .unauthorized(), .error()) to handle specific status codes, and finally a response type handler (like .json(), .text(), .blob(), or .res()) to parse the data. The response type handler returns a standard Promise.
Example Chain:
await wretch("https://api.example.com") // Base URL
.headers({ "X-Api-Key": "secret" }) // Helper method
.query({ limit: 10 }) // Helper method
.json({ name: "Alice", role: "admin" }) // Body type
.post("/users") // HTTP method (starts request)
.badRequest(err => console.log("Invalid")) // Catcher
.unauthorized(err => console.log("No auth")) // Catcher
.json(user => console.log(user)) // Response type