Catches a bad request (http code 400) and performs a callback.
Syntactic sugar for error(400, cb)
.
Catches an http response with a specific error code or name and performs a callback.
The original request is passed along the error and can be used in order to perform an additional request.
wretch("/resource")
.get()
.unauthorized(async (error, req) => {
// Renew credentials
const token = await wretch("/renewtoken").get().text();
storeToken(token);
// Replay the original request with new credentials
return req.auth(token).get().unauthorized((err) => {
throw err;
}).json();
})
.json()
// The promise chain is preserved as expected
// ".then" will be performed on the result of the original request
// or the replayed one (if a 401 error was thrown)
.then(callback);
Catches any error thrown by the fetch function and perform the callback.
Catches a forbidden request (http code 403) and performs a callback.
Syntactic sugar for error(403, cb)
.
Catches an internal server error (http code 500) and performs a callback.
Syntactic sugar for error(500, cb)
.
Catches a "not found" request (http code 404) and performs a callback.
Syntactic sugar for error(404, cb)
.
Catches a timeout (http code 408) and performs a callback.
Syntactic sugar for error(408, cb)
.
Catches an unauthorized request (http code 401) and performs a callback.
Syntactic sugar for error(401, cb)
.
Read the payload and deserialize it as an ArrayBuffer object.
wretch("...").get().arrayBuffer(arrayBuffer => …)
Read the payload and deserialize it as a Blob.
wretch("...").get().blob(blob => …)
Read the payload and deserialize it as a FormData object.
wretch("...").get().formData(formData => …)
Read the payload and deserialize it as JSON.
wretch("...").get().json((json) => console.log(Object.keys(json)));
The handler for the raw fetch Response. Check the MDN documentation for more details on the Response class.
wretch("...").get().res((response) => console.log(response.url));
Retrieves the payload as a string.
wretch("...").get().text((txt) => console.log(txt));
The resolver interface to chaining catchers and extra methods after the request has been sent. Ultimately returns a Promise.