wretch
    Preparing search index...

    Interface WretchResponseChain<T, Self, R, ErrorType, CatcherResult>

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

    Type Parameters

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

    Catchers

    badRequest: <Result>(
        this: Self & WretchResponseChain<T, Self, R, ErrorType, CatcherResult>,
        cb: WretchErrorCallback<T, Self, R, ErrorType, CatcherResult, Result>,
    ) => Self & WretchResponseChain<
        T,
        Self,
        R,
        ErrorType,
        CatcherResult
        | WretchCatcherResult<Result>,
    >

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

    Syntactic sugar for error(400, cb).

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

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

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

    forbidden: <Result>(
        this: Self & WretchResponseChain<T, Self, R, ErrorType, CatcherResult>,
        cb: WretchErrorCallback<T, Self, R, ErrorType, CatcherResult, Result>,
    ) => Self & WretchResponseChain<
        T,
        Self,
        R,
        ErrorType,
        CatcherResult
        | WretchCatcherResult<Result>,
    >

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

    Syntactic sugar for error(403, cb).

    internalError: <Result>(
        this: Self & WretchResponseChain<T, Self, R, ErrorType, CatcherResult>,
        cb: WretchErrorCallback<T, Self, R, ErrorType, CatcherResult, Result>,
    ) => Self & WretchResponseChain<
        T,
        Self,
        R,
        ErrorType,
        CatcherResult
        | WretchCatcherResult<Result>,
    >

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

    Syntactic sugar for error(500, cb).

    notFound: <Result>(
        this: Self & WretchResponseChain<T, Self, R, ErrorType, CatcherResult>,
        cb: WretchErrorCallback<T, Self, R, ErrorType, CatcherResult, Result>,
    ) => Self & WretchResponseChain<
        T,
        Self,
        R,
        ErrorType,
        CatcherResult
        | WretchCatcherResult<Result>,
    >

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

    Syntactic sugar for error(404, cb).

    timeout: <Result>(
        this: Self & WretchResponseChain<T, Self, R, ErrorType, CatcherResult>,
        cb: WretchErrorCallback<T, Self, R, ErrorType, CatcherResult, Result>,
    ) => Self & WretchResponseChain<
        T,
        Self,
        R,
        ErrorType,
        CatcherResult
        | WretchCatcherResult<Result>,
    >

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

    Syntactic sugar for error(408, cb).

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

    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<CatcherResult | 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<CatcherResult | 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<CatcherResult | 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<CatcherResult | 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<CatcherResult | 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<CatcherResult | Awaited<Result>>

    Retrieves the payload as a string.

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