{"version":3,"file":"formData.min.cjs","sources":["../../../src/addons/formData.ts"],"sourcesContent":["import type { Wretch, Config, WretchAddon } from \"../types.js\"\n\nfunction convertFormData(\n  formObject: object,\n  recursive: string[] | boolean = false,\n  config: Config,\n  formData = config.polyfill(\"FormData\", true, true),\n  ancestors = [],\n) {\n  Object.entries(formObject).forEach(([key, value]) => {\n    let formKey = ancestors.reduce((acc, ancestor) => (\n      acc ? `${acc}[${ancestor}]` : ancestor\n    ), null)\n    formKey = formKey ? `${formKey}[${key}]` : key\n    if (value instanceof Array) {\n      for (const item of value)\n        formData.append(formKey, item)\n    } else if (\n      recursive &&\n      typeof value === \"object\" &&\n      (\n        !(recursive instanceof Array) ||\n        !recursive.includes(key)\n      )\n    ) {\n      if (value !== null) {\n        convertFormData(value, recursive, config, formData, [...ancestors, key])\n      }\n    } else {\n      formData.append(formKey, value)\n    }\n  })\n\n  return formData\n}\n\nexport interface FormDataAddon {\n  /**\n   * Converts the javascript object to a FormData and sets the request body.\n   *\n   * ```js\n   * const form = {\n   *   hello: \"world\",\n   *   duck: \"Muscovy\",\n   * };\n   *\n   * wretch(\"...\").addons(FormDataAddon).formData(form).post();\n   * ```\n   *\n   * The `recursive` argument when set to `true` will enable recursion through all\n   * nested objects and produce `object[key]` keys. It can be set to an array of\n   * string to exclude specific keys.\n   *\n   * > Warning: Be careful to exclude `Blob` instances in the Browser, and\n   * > `ReadableStream` and `Buffer` instances when using the node.js compatible\n   * > `form-data` package.\n   *\n   * ```js\n   * const form = {\n   *   duck: \"Muscovy\",\n   *   duckProperties: {\n   *     beak: {\n   *       color: \"yellow\",\n   *     },\n   *     legs: 2,\n   *   },\n   *   ignored: {\n   *     key: 0,\n   *   },\n   * };\n   *\n   * // Will append the following keys to the FormData payload:\n   * // \"duck\", \"duckProperties[beak][color]\", \"duckProperties[legs]\"\n   * wretch(\"...\").addons(FormDataAddon).formData(form, [\"ignored\"]).post();\n   * ```\n   *\n   * @param formObject - An object which will be converted to a FormData\n   * @param recursive - If `true`, will recurse through all nested objects. Can be set as an array of string to exclude specific keys.\n   */\n  formData<T extends FormDataAddon, C, R>(this: T & Wretch<T, C, R>, formObject: object, recursive?: string[] | boolean): this\n}\n\n/**\n * Adds the ability to convert a an object to a FormData and use it as a request body.\n *\n * ```js\n * import FormDataAddon from \"wretch/addons/formData\"\n *\n * wretch().addon(FormDataAddon)\n * ```\n */\nconst formData: WretchAddon<FormDataAddon> = {\n  wretch: {\n    formData(formObject, recursive = false) {\n      return this.body(convertFormData(formObject, recursive, this._config))\n    }\n  }\n}\n\nexport default formData\n"],"names":["formKey","ancestors","reduce","acc","ancestor","key","value","Array","item","formData","append","recursive","includes","convertFormData","config","wretch","formObject","this","body","_config"],"mappings":"sDA8BK,4CA5BI,IAAAA,EAAAC,EACPC,QACA,CAAAC,EAAAC,IACAD,EACA,GAAAA,KAAWC,KAAeA,GAAW,MAInC,GADIJ,EAAQA,KAAaA,KAAUK,KAAUA,EACzCC,aAAUC,UAGJ,MAAAC,KAAaF,EACnBG,EAAiBC,OAAAV,EAAOQ,QAExBG,GACH,iBAAAL,gBACUC,OACTI,EAAYC,SAAaP,GAS1BI,EAAAC,OAAAV,EAAAM,GAP0B,OAArBA,GACFO,EAAWP,EAAYK,EAEzBG,EAAAL,EAAA,IAAAR,EAAAI,GAIK,IAENI,CACH,UA2DCM,OAAA,UAC0CC,EAAAL,EAAA,GACnC,OAAAM,KAAAC,KAAAL,EAAAG,EAAAL,EAAAM,KAAAE,SACN"}