wretch
    Preparing search index...

    Interface FormDataAddon

    interface FormDataAddon {
        formData<T extends FormDataAddon, C, R, E>(
            this: T & Wretch<T, C, R, E>,
            formObject: object,
            options?: FormDataOptions,
        ): this;
    }
    Index

    Methods

    Methods

    • Converts the javascript object to a FormData and sets the request body.

      const form = {
      hello: "world",
      duck: "Muscovy",
      };

      wretch("...").addons(FormDataAddon).formData(form).post();

      The recursive option when set to true will enable recursion through all nested objects and produce object[key] keys. It can be set to an array of string to exclude specific keys.

      Warning: Be careful to exclude Blob instances in the Browser, and ReadableStream and Buffer instances when using the node.js compatible form-data package.

      const form = {
      duck: "Muscovy",
      duckProperties: {
      beak: {
      color: "yellow",
      },
      legs: 2,
      },
      ignored: {
      key: 0,
      },
      };

      // Will append the following keys to the FormData payload:
      // "duck", "duckProperties[beak][color]", "duckProperties[legs]"
      wretch("...").addons(FormDataAddon).formData(form, { recursive: ["ignored"] }).post();

      Note: This addon does not support specifying a custom filename. If you need to do so, you can use the body method directly:

      const form = new FormData();
      form.append("hello", "world", "hello.txt");
      wretch("...").body(form).post();

      See: https://developer.mozilla.org/en-US/docs/Web/API/FormData/append#example

      Type Parameters

      Parameters

      • this: T & Wretch<T, C, R, E>
      • formObject: object

        An object which will be converted to a FormData

      • Optionaloptions: FormDataOptions

        Optional configuration object

      Returns this