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

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

Type declaration

error: ((this, code, cb) => WretchResponseChain<T, Self, R>)

Type declaration

    • (this, code, cb): 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, cb) => WretchResponseChain<T, Self, R>)

Type declaration

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

Type declaration

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

Type declaration

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

Type declaration

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

Type declaration

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

Type declaration

Response Type

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

Type declaration

    • <Result>(cb?): 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) => Result | Promise<Result>)
          • (type): Result | Promise<Result>
          • Parameters

            • type: ArrayBuffer

            Returns Result | Promise<Result>

      Returns Promise<Awaited<Result>>

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

Type declaration

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

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

      Type Parameters

      • Result = Blob

      Parameters

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

            • type: Blob

            Returns Result | Promise<Result>

      Returns Promise<Awaited<Result>>

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

Type declaration

    • <Result>(cb?): 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) => Result | Promise<Result>)
          • (type): Result | Promise<Result>
          • Parameters

            • type: FormData

            Returns Result | Promise<Result>

      Returns Promise<Awaited<Result>>

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

Type declaration

    • <Result>(cb?): 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) => Result | Promise<Result>)
          • (type): Result | Promise<Result>
          • Parameters

            • type: any

            Returns Result | Promise<Result>

      Returns Promise<Awaited<Result>>

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

Type declaration

    • <Result>(cb?): 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

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

            Returns Result | Promise<Result>

      Returns Promise<Awaited<Result>>

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

Type declaration

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

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

      Type Parameters

      • Result = string

      Parameters

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

            • type: string

            Returns Result | Promise<Result>

      Returns Promise<Awaited<Result>>

Generated using TypeDoc