wretch
    Preparing search index...

    Interface WretchResponseChain<T, Self, R, ErrorType>

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

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

    Type Parameters

    • T
    • Self = unknown
    • R = undefined
    • ErrorType = undefined
    Index

    Catchers

    badRequest: (
        this: Self & WretchResponseChain<T, Self, R, ErrorType>,
        cb: WretchErrorCallback<T, Self, R, ErrorType>,
    ) => 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, ErrorType>,
        code: string | number | symbol,
        cb: WretchErrorCallback<T, Self, R, ErrorType>,
    ) => 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, ErrorType>,
        cb: WretchErrorCallback<T, Self, R, ErrorType>,
    ) => this

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

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

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

    Syntactic sugar for error(408, cb).

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

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

    Syntactic sugar for error(401, cb).

    Response Type

    arrayBuffer: <Result = ArrayBuffer>(
        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 = Blob>(
        cb?: (type: Blob) => Result | Promise<Result>,
    ) => Promise<Awaited<Result>>

    Read the payload and deserialize it as a Blob.

    wretch("...").get().blob(blob => …)
    
    formData: <Result = FormData>(
        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 = unknown>(
        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 = WretchResponse>(
        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 = string>(
        cb?: (type: string) => Result | Promise<Result>,
    ) => Promise<Awaited<Result>>

    Retrieves the payload as a string.

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