wretch
Version:
A tiny wrapper built around fetch with an intuitive syntax.
1 lines • 4.42 kB
Source Map (JSON)
{"version":3,"file":"formData.min.mjs","names":[],"sources":["../../../src/addons/formData.ts"],"sourcesContent":["import type { Wretch, WretchAddon } from \"../types.js\"\n\n/**\n * Options for the formData method.\n */\nexport type FormDataOptions = {\n /**\n * Enable recursion through nested objects to produce `object[key]` keys.\n * When set to `true`, all nested objects will be recursively converted.\n * When set to an array of strings, the specified keys will be excluded from recursion.\n */\n recursive?: string[] | boolean\n}\n\nfunction convertFormData(\n formObject: object,\n recursive: string[] | boolean = false,\n formData = new FormData(),\n ancestors = [] as string[],\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 || (globalThis.FileList && value instanceof FileList)) {\n for (const item of value as File[])\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, 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` option 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, { recursive: [\"ignored\"] }).post();\n * ```\n *\n * > Note: This addon does not support specifying a custom `filename`.\n * > If you need to do so, you can use the `body` method directly:\n * > ```js\n * > const form = new FormData();\n * > form.append(\"hello\", \"world\", \"hello.txt\");\n * > wretch(\"...\").body(form).post();\n * > ```\n * > See: https://developer.mozilla.org/en-US/docs/Web/API/FormData/append#example\n *\n * @param formObject - An object which will be converted to a FormData\n * @param options - Optional configuration object\n */\n formData<T extends FormDataAddon, C, R, E>(this: T & Wretch<T, C, R, E>, formObject: object, options?: FormDataOptions): 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, options = {}) {\n return this.body(convertFormData(formObject, options.recursive ?? false))\n }\n }\n}\n\nexport default formData\n"],"mappings":"AAcA,SAAS,EACP,EACA,EAAgC,GAChC,EAAW,IAAI,SACf,EAAY,EAAE,CACd,CAyBA,OAxBA,OAAO,QAAQ,EAAW,CAAC,SAAS,CAAC,EAAK,KAAW,CACnD,IAAI,EAAU,EAAU,QAAQ,EAAK,IACnC,EAAM,GAAG,EAAI,GAAG,EAAS,GAAK,EAC7B,KAAK,CAER,GADA,EAAU,EAAU,GAAG,EAAQ,GAAG,EAAI,GAAK,EACvC,aAAiB,OAAU,WAAW,UAAY,aAAiB,SACrE,IAAK,IAAM,KAAQ,EACjB,EAAS,OAAO,EAAS,EAAK,MAEhC,GACA,OAAO,GAAU,WAEf,EAAE,aAAqB,QACvB,CAAC,EAAU,SAAS,EAAI,EAGtB,IAAU,MACZ,EAAgB,EAAO,EAAW,EAAU,CAAC,GAAG,EAAW,EAAI,CAAC,CAGlE,EAAS,OAAO,EAAS,EAAM,EAEjC,CAEK,EAmET,MAAM,EAAuC,CAC3C,OAAQ,CACN,SAAS,EAAY,EAAU,EAAE,CAAE,CACjC,OAAO,KAAK,KAAK,EAAgB,EAAY,EAAQ,WAAa,GAAM,CAAC,EAE5E,CACF"}