Function default

  • Creates a new wretch instance with a base url and base fetch options.

    import wretch from "wretch"

    // Reusable instance
    const w = wretch("https://domain.com", { mode: "cors" })

    Parameters

    • _url: string = ""

      The base url

    • _options: WretchOptions = {}

      The base fetch options

    Returns Wretch

    A fresh wretch instance

Properties

WretchError: typeof WretchError
default: typeof default
errorType: ((errorType: ErrorType) => void)

Type declaration

    • (errorType): void
    • Sets the default method (text, json, …) used to parse the data contained in the response body in case of an HTTP error. As with other static methods, it will affect wretch instances created after calling this function.

      Note: if the response Content-Type header is set to "application/json", the body will be parsed as json regardless of the errorType.

      import wretch from "wretch"

      wretch.errorType("json")

      wretch("http://server/which/returns/an/error/with/a/json/body")
      .get()
      .res()
      .catch(error => {
      // error[errorType] (here, json) contains the parsed body
      console.log(error.json)
      })

      If null, defaults to "text".

      Parameters

      • errorType: ErrorType

      Returns void

options: ((options: WretchOptions, replace?: boolean) => void)

Type declaration

    • (options, replace?): void
    • Sets the default fetch options that will be stored internally when instantiating wretch objects.

      import wretch from "wretch"

      wretch.options({ headers: { "Accept": "application/json" } });

      // The fetch request is sent with both headers.
      wretch("...", { headers: { "X-Custom": "Header" } }).get().res();

      Parameters

      • options: WretchOptions

        Default options

      • replace: boolean = false

        If true, completely replaces the existing options instead of mixing in

      Returns void

polyfills: ((polyfills: object, replace?: boolean) => void)

Type declaration

    • (polyfills, replace?): void
    • Sets the default polyfills that will be stored internally when instantiating wretch objects. Useful for browserless environments like node.js.

      Needed for libraries like fetch-ponyfill.

      import wretch from "wretch"

      wretch.polyfills({
      fetch: require("node-fetch"),
      FormData: require("form-data"),
      URLSearchParams: require("url").URLSearchParams,
      });

      // Uses the above polyfills.
      wretch("...").get().res();

      Parameters

      • polyfills: object

        An object containing the polyfills

      • replace: boolean = false

        If true, replaces the current polyfills instead of mixing in

      Returns void