Interface WretchResponseChain<T, Self, R>

The resolver interface to chaining catchers and extra methods after the request has been sent. Ultimately returns a Promise.

interface WretchResponseChain<T, Self, R> {
    arrayBuffer: (<Result>(cb?: ((type: ArrayBuffer) => Result | Promise<Result>)) => Promise<Awaited<Result>>);
    badRequest: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this);
    blob: (<Result>(cb?: ((type: Blob) => Result | Promise<Result>)) => Promise<Awaited<Result>>);
    error: ((this: Self & WretchResponseChain<T, Self, R>, code: string | number | symbol, cb: WretchErrorCallback<T, Self, R>) => this);
    fetchError: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this);
    forbidden: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this);
    formData: (<Result>(cb?: ((type: FormData) => Result | Promise<Result>)) => Promise<Awaited<Result>>);
    internalError: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this);
    json: (<Result>(cb?: ((type: any) => Result | Promise<Result>)) => Promise<Awaited<Result>>);
    notFound: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this);
    res: (<Result>(cb?: ((type: WretchResponse) => Result | Promise<Result>)) => Promise<Awaited<Result>>);
    text: (<Result>(cb?: ((type: string) => Result | Promise<Result>)) => Promise<Awaited<Result>>);
    timeout: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this);
    unauthorized: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this);
}

Type Parameters

  • T
  • Self = unknown
  • R = undefined

Catchers

badRequest: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this)

Catches a bad request (http code 400) and performs a callback.

Syntactic sugar for error(400, cb).

error: ((this: Self & WretchResponseChain<T, Self, R>, code: string | number | symbol, cb: WretchErrorCallback<T, Self, R>) => this)

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);
fetchError: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this)

Catches any error thrown by the fetch function and perform the callback.

forbidden: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this)

Catches a forbidden request (http code 403) and performs a callback.

Syntactic sugar for error(403, cb).

internalError: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this)

Catches an internal server error (http code 500) and performs a callback.

Syntactic sugar for error(500, cb).

notFound: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this)

Catches a "not found" request (http code 404) and performs a callback.

Syntactic sugar for error(404, cb).

timeout: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this)

Catches a timeout (http code 408) and performs a callback.

Syntactic sugar for error(408, cb).

unauthorized: ((this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this)

Catches an unauthorized request (http code 401) and performs a callback.

Syntactic sugar for error(401, cb).

Response Type

arrayBuffer: (<Result>(cb?: ((type: ArrayBuffer) => Result | Promise<Result>)) => Promise<Awaited<Result>>)

Read the payload and deserialize it as an ArrayBuffer object.

wretch("...").get().arrayBuffer(arrayBuffer => …)
blob: (<Result>(cb?: ((type: Blob) => Result | Promise<Result>)) => Promise<Awaited<Result>>)

Read the payload and deserialize it as a Blob.

wretch("...").get().blob(blob => …)
formData: (<Result>(cb?: ((type: FormData) => Result | Promise<Result>)) => Promise<Awaited<Result>>)

Read the payload and deserialize it as a FormData object.

wretch("...").get().formData(formData => …)
json: (<Result>(cb?: ((type: any) => Result | Promise<Result>)) => Promise<Awaited<Result>>)

Read the payload and deserialize it as JSON.

wretch("...").get().json((json) => console.log(Object.keys(json)));
res: (<Result>(cb?: ((type: WretchResponse) => Result | Promise<Result>)) => Promise<Awaited<Result>>)

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));
text: (<Result>(cb?: ((type: string) => Result | Promise<Result>)) => Promise<Awaited<Result>>)

Retrieves the payload as a string.

wretch("...").get().text((txt) => console.log(txt));