wretch
Version:
A tiny wrapper built around fetch with an intuitive syntax.
1 lines • 3.91 kB
Source Map (JSON)
{"version":3,"file":"formData.min.mjs","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":["convertFormData","formObject","recursive","config","formData","polyfill","ancestors","Object","entries","forEach","key","value","formKey","reduce","acc","ancestor","Array","item","append","includes","wretch","this","body","_config"],"mappings":"AAEA,SAASA,EACPC,EACAC,EAAgC,EAChCC,EACAC,EAAWD,EAAOE,SAAS,WAAY,EAAM,GAC7CC,EAAY,IA0BZ,OAxBAC,OAAOC,QAAQP,GAAYQ,SAAQ,EAAEC,EAAKC,MACxC,IAAIC,EAAUN,EAAUO,QAAO,CAACC,EAAKC,IACnCD,EAAM,GAAGA,KAAOC,KAAcA,GAC7B,MAEH,GADAH,EAAUA,EAAU,GAAGA,KAAWF,KAASA,EACvCC,aAAiBK,MACnB,IAAK,MAAMC,KAAQN,EACjBP,EAASc,OAAON,EAASK,QAE3Bf,GACiB,iBAAVS,GAEHT,aAAqBc,OACtBd,EAAUiB,SAAST,GAOtBN,EAASc,OAAON,EAASD,GAJX,OAAVA,GACFX,EAAgBW,EAAOT,EAAWC,EAAQC,EAAU,IAAIE,EAAWI,GAItE,IAGIN,CACT,CAyDA,MAAMA,EAAuC,CAC3CgB,OAAQ,CACNhB,SAASH,EAAYC,EAAY,GAC/B,OAAOmB,KAAKC,KAAKtB,EAAgBC,EAAYC,EAAWmB,KAAKE,SAC9D"}