Interface WretchResponseChain<T, Self, R>

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

Type Parameters

  • T

  • Self = unknown

  • R = undefined

Hierarchy

  • WretchResponseChain

Catchers Properties

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

Type declaration

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

Type declaration

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

      Parameters

      Returns WretchResponseChain<T, Self, R>

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

Type declaration

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

Type declaration

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

Type declaration

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

Type declaration

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

Type declaration

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

Type declaration

Response Type Properties

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

Type declaration

    • <Result>(cb?: ((type: ArrayBuffer) => Result | Promise<Result>)): Promise<Awaited<Result>>
    • Read the payload and deserialize it as an ArrayBuffer object.

      wretch("...").get().arrayBuffer(arrayBuffer => …)
      

      Type Parameters

      • Result = ArrayBuffer

      Parameters

      • Optional cb: ((type: ArrayBuffer) => Result | Promise<Result>)
          • (type: ArrayBuffer): Result | Promise<Result>
          • Parameters

            • type: ArrayBuffer

            Returns Result | Promise<Result>

      Returns Promise<Awaited<Result>>

blob: (<Result>(cb?: ((type: Blob) => Result | Promise<Result>)) => Promise<Awaited<Result>>)

Type declaration

    • <Result>(cb?: ((type: Blob) => Result | Promise<Result>)): Promise<Awaited<Result>>
    • Read the payload and deserialize it as a Blob.

      wretch("...").get().blob(blob => …)
      

      Type Parameters

      • Result = Blob

      Parameters

      • Optional cb: ((type: Blob) => Result | Promise<Result>)
          • (type: Blob): Result | Promise<Result>
          • Parameters

            • type: Blob

            Returns Result | Promise<Result>

      Returns Promise<Awaited<Result>>

formData: (<Result>(cb?: ((type: FormData) => Result | Promise<Result>)) => Promise<Awaited<Result>>)

Type declaration

    • <Result>(cb?: ((type: FormData) => Result | Promise<Result>)): Promise<Awaited<Result>>
    • Read the payload and deserialize it as a FormData object.

      wretch("...").get().formData(formData => …)
      

      Type Parameters

      • Result = FormData

      Parameters

      • Optional cb: ((type: FormData) => Result | Promise<Result>)
          • (type: FormData): Result | Promise<Result>
          • Parameters

            • type: FormData

            Returns Result | Promise<Result>

      Returns Promise<Awaited<Result>>

json: (<Result>(cb?: ((type: any) => Result | Promise<Result>)) => Promise<Awaited<Result>>)

Type declaration

    • <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)));
      

      Type Parameters

      • Result = unknown

      Parameters

      • Optional cb: ((type: any) => Result | Promise<Result>)
          • (type: any): Result | Promise<Result>
          • Parameters

            • type: any

            Returns Result | Promise<Result>

      Returns Promise<Awaited<Result>>

res: (<Result>(cb?: ((type: WretchResponse) => Result | Promise<Result>)) => Promise<Awaited<Result>>)

Type declaration

    • <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));
      

      Type Parameters

      Parameters

      Returns Promise<Awaited<Result>>

text: (<Result>(cb?: ((type: string) => Result | Promise<Result>)) => Promise<Awaited<Result>>)

Type declaration

    • <Result>(cb?: ((type: string) => Result | Promise<Result>)): Promise<Awaited<Result>>
    • Retrieves the payload as a string.

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

      Type Parameters

      • Result = string

      Parameters

      • Optional cb: ((type: string) => Result | Promise<Result>)
          • (type: string): Result | Promise<Result>
          • Parameters

            • type: string

            Returns Result | Promise<Result>

      Returns Promise<Awaited<Result>>

Generated using TypeDoc