{"version":3,"file":"forms.99ae166d.mjs","sources":["../../../../../../node_modules/@sveltejs/kit/src/runtime/app/forms.js"],"sourcesContent":["import * as devalue from 'devalue';\nimport { BROWSER, DEV } from 'esm-env';\nimport { client } from '../client/singletons.js';\nimport { invalidateAll } from './navigation.js';\n\n/**\n * This action updates the `form` property of the current page with the given data and updates `$page.status`.\n * In case of an error, it redirects to the nearest error page.\n * @template {Record<string, unknown> | undefined} Success\n * @template {Record<string, unknown> | undefined} Failure\n * @param {import('@sveltejs/kit').ActionResult<Success, Failure>} result\n * @returns {Promise<void>}\n */\nexport function applyAction(result) {\n\tif (BROWSER) {\n\t\treturn client.apply_action(result);\n\t} else {\n\t\tthrow new Error('Cannot call applyAction(...) on the server');\n\t}\n}\n\n/**\n * Use this function to deserialize the response from a form submission.\n * Usage:\n *\n * ```js\n * import { deserialize } from '$app/forms';\n *\n * async function handleSubmit(event) {\n *   const response = await fetch('/form?/action', {\n *     method: 'POST',\n *     body: new FormData(event.target)\n *   });\n *\n *   const result = deserialize(await response.text());\n *   // ...\n * }\n * ```\n * @template {Record<string, unknown> | undefined} Success\n * @template {Record<string, unknown> | undefined} Failure\n * @param {string} result\n * @returns {import('@sveltejs/kit').ActionResult<Success, Failure>}\n */\nexport function deserialize(result) {\n\tconst parsed = JSON.parse(result);\n\tif (parsed.data) {\n\t\tparsed.data = devalue.parse(parsed.data);\n\t}\n\treturn parsed;\n}\n\n/**\n * @param {string} old_name\n * @param {string} new_name\n * @param {string} call_location\n * @returns void\n */\nfunction warn_on_access(old_name, new_name, call_location) {\n\tif (!DEV) return;\n\t// TODO 2.0: Remove this code\n\tconsole.warn(\n\t\t`\\`${old_name}\\` has been deprecated in favor of \\`${new_name}\\`. \\`${old_name}\\` will be removed in a future version. (Called from ${call_location})`\n\t);\n}\n\n/**\n * Shallow clone an element, so that we can access e.g. `form.action` without worrying\n * that someone has added an `<input name=\"action\">` (https://github.com/sveltejs/kit/issues/7593)\n * @template {HTMLElement} T\n * @param {T} element\n * @returns {T}\n */\nfunction clone(element) {\n\treturn /** @type {T} */ (HTMLElement.prototype.cloneNode.call(element));\n}\n\n/**\n * This action enhances a `<form>` element that otherwise would work without JavaScript.\n *\n * The `submit` function is called upon submission with the given FormData and the `action` that should be triggered.\n * If `cancel` is called, the form will not be submitted.\n * You can use the abort `controller` to cancel the submission in case another one starts.\n * If a function is returned, that function is called with the response from the server.\n * If nothing is returned, the fallback will be used.\n *\n * If this function or its return value isn't set, it\n * - falls back to updating the `form` prop with the returned data if the action is one same page as the form\n * - updates `$page.status`\n * - resets the `<form>` element and invalidates all data in case of successful submission with no redirect response\n * - redirects in case of a redirect response\n * - redirects to the nearest error page in case of an unexpected error\n *\n * If you provide a custom function with a callback and want to use the default behavior, invoke `update` in your callback.\n * @template {Record<string, unknown> | undefined} Success\n * @template {Record<string, unknown> | undefined} Failure\n * @param {HTMLFormElement} form_element The form element\n * @param {import('@sveltejs/kit').SubmitFunction<Success, Failure>} submit Submit callback\n */\nexport function enhance(form_element, submit = () => {}) {\n\tif (DEV && clone(form_element).method !== 'post') {\n\t\tthrow new Error('use:enhance can only be used on <form> fields with method=\"POST\"');\n\t}\n\n\t/**\n\t * @param {{\n\t *   action: URL;\n\t *   invalidateAll?: boolean;\n\t *   result: import('@sveltejs/kit').ActionResult;\n\t *   reset?: boolean\n\t * }} opts\n\t */\n\tconst fallback_callback = async ({\n\t\taction,\n\t\tresult,\n\t\treset = true,\n\t\tinvalidateAll: shouldInvalidateAll = true\n\t}) => {\n\t\tif (result.type === 'success') {\n\t\t\tif (reset) {\n\t\t\t\t// We call reset from the prototype to avoid DOM clobbering\n\t\t\t\tHTMLFormElement.prototype.reset.call(form_element);\n\t\t\t}\n\t\t\tif (shouldInvalidateAll) {\n\t\t\t\tawait invalidateAll();\n\t\t\t}\n\t\t}\n\n\t\t// For success/failure results, only apply action if it belongs to the\n\t\t// current page, otherwise `form` will be updated erroneously\n\t\tif (\n\t\t\tlocation.origin + location.pathname === action.origin + action.pathname ||\n\t\t\tresult.type === 'redirect' ||\n\t\t\tresult.type === 'error'\n\t\t) {\n\t\t\tapplyAction(result);\n\t\t}\n\t};\n\n\t/** @param {SubmitEvent} event */\n\tasync function handle_submit(event) {\n\t\tconst method = event.submitter?.hasAttribute('formmethod')\n\t\t\t? /** @type {HTMLButtonElement | HTMLInputElement} */ (event.submitter).formMethod\n\t\t\t: clone(form_element).method;\n\t\tif (method !== 'post') return;\n\n\t\tevent.preventDefault();\n\n\t\tconst action = new URL(\n\t\t\t// We can't do submitter.formAction directly because that property is always set\n\t\t\tevent.submitter?.hasAttribute('formaction')\n\t\t\t\t? /** @type {HTMLButtonElement | HTMLInputElement} */ (event.submitter).formAction\n\t\t\t\t: clone(form_element).action\n\t\t);\n\n\t\tconst form_data = new FormData(form_element);\n\n\t\tif (DEV && clone(form_element).enctype !== 'multipart/form-data') {\n\t\t\tfor (const value of form_data.values()) {\n\t\t\t\tif (value instanceof File) {\n\t\t\t\t\t// TODO 2.0: Upgrade to `throw Error`\n\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t'Your form contains <input type=\"file\"> fields, but is missing the `enctype=\"multipart/form-data\"` attribute. This will lead to inconsistent behavior between enhanced and native forms. For more details, see https://github.com/sveltejs/kit/issues/9819. This will be upgraded to an error in v2.0.'\n\t\t\t\t\t);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst submitter_name = event.submitter?.getAttribute('name');\n\t\tif (submitter_name) {\n\t\t\tform_data.append(submitter_name, event.submitter?.getAttribute('value') ?? '');\n\t\t}\n\n\t\tconst controller = new AbortController();\n\n\t\tlet cancelled = false;\n\t\tconst cancel = () => (cancelled = true);\n\n\t\t// TODO 2.0: Remove `data` and `form`\n\t\tconst callback =\n\t\t\t(await submit({\n\t\t\t\taction,\n\t\t\t\tcancel,\n\t\t\t\tcontroller,\n\t\t\t\tget data() {\n\t\t\t\t\twarn_on_access('data', 'formData', 'use:enhance submit function');\n\t\t\t\t\treturn form_data;\n\t\t\t\t},\n\t\t\t\tformData: form_data,\n\t\t\t\tget form() {\n\t\t\t\t\twarn_on_access('form', 'formElement', 'use:enhance submit function');\n\t\t\t\t\treturn form_element;\n\t\t\t\t},\n\t\t\t\tformElement: form_element,\n\t\t\t\tsubmitter: event.submitter\n\t\t\t})) ?? fallback_callback;\n\t\tif (cancelled) return;\n\n\t\t/** @type {import('@sveltejs/kit').ActionResult} */\n\t\tlet result;\n\n\t\ttry {\n\t\t\tconst response = await fetch(action, {\n\t\t\t\tmethod: 'POST',\n\t\t\t\theaders: {\n\t\t\t\t\taccept: 'application/json',\n\t\t\t\t\t'x-sveltekit-action': 'true'\n\t\t\t\t},\n\t\t\t\tcache: 'no-store',\n\t\t\t\tbody: form_data,\n\t\t\t\tsignal: controller.signal\n\t\t\t});\n\n\t\t\tresult = deserialize(await response.text());\n\t\t\tif (result.type === 'error') result.status = response.status;\n\t\t} catch (error) {\n\t\t\tif (/** @type {any} */ (error)?.name === 'AbortError') return;\n\t\t\tresult = { type: 'error', error };\n\t\t}\n\n\t\tcallback({\n\t\t\taction,\n\t\t\tget data() {\n\t\t\t\twarn_on_access('data', 'formData', 'callback returned from use:enhance submit function');\n\t\t\t\treturn form_data;\n\t\t\t},\n\t\t\tformData: form_data,\n\t\t\tget form() {\n\t\t\t\twarn_on_access('form', 'formElement', 'callback returned from use:enhance submit function');\n\t\t\t\treturn form_element;\n\t\t\t},\n\t\t\tformElement: form_element,\n\t\t\tupdate: (opts) =>\n\t\t\t\tfallback_callback({\n\t\t\t\t\taction,\n\t\t\t\t\tresult,\n\t\t\t\t\treset: opts?.reset,\n\t\t\t\t\tinvalidateAll: opts?.invalidateAll\n\t\t\t\t}),\n\t\t\t// @ts-expect-error generic constraints stuff we don't care about\n\t\t\tresult\n\t\t});\n\t}\n\n\t// @ts-expect-error\n\tHTMLFormElement.prototype.addEventListener.call(form_element, 'submit', handle_submit);\n\n\treturn {\n\t\tdestroy() {\n\t\t\t// @ts-expect-error\n\t\t\tHTMLFormElement.prototype.removeEventListener.call(form_element, 'submit', handle_submit);\n\t\t}\n\t};\n}\n"],"names":["applyAction","result","client","deserialize","parsed","devalue.parse","clone","element","enhance","form_element","submit","fallback_callback","action","reset","shouldInvalidateAll","invalidateAll","handle_submit","event","_a","_b","form_data","submitter_name","_c","_d","controller","cancelled","callback","response","error","opts"],"mappings":"qIAaO,SAASA,EAAYC,EAAQ,CAElC,OAAOC,EAAO,aAAaD,CAAM,CAInC,CAwBO,SAASE,EAAYF,EAAQ,CACnC,MAAMG,EAAS,KAAK,MAAMH,CAAM,EAChC,OAAIG,EAAO,OACVA,EAAO,KAAOC,EAAcD,EAAO,IAAI,GAEjCA,CACR,CAuBA,SAASE,EAAMC,EAAS,CACvB,OAAyB,YAAY,UAAU,UAAU,KAAKA,CAAO,CACtE,CAwBO,SAASC,EAAQC,EAAcC,EAAS,IAAM,GAAI,CAaxD,MAAMC,EAAoB,MAAO,CAChC,OAAAC,EACA,OAAAX,EACA,MAAAY,EAAQ,GACR,cAAeC,EAAsB,EACvC,IAAO,CACDb,EAAO,OAAS,YACfY,GAEH,gBAAgB,UAAU,MAAM,KAAKJ,CAAY,EAE9CK,GACH,MAAMC,EAAa,IAOpB,SAAS,OAAS,SAAS,WAAaH,EAAO,OAASA,EAAO,UAC/DX,EAAO,OAAS,YAChBA,EAAO,OAAS,UAEhBD,EAAYC,CAAM,CAErB,EAGC,eAAee,EAAcC,EAAO,aAInC,KAHeC,EAAAD,EAAM,YAAN,MAAAC,EAAiB,aAAa,cACWD,EAAM,UAAW,WACtEX,EAAMG,CAAY,EAAE,UACR,OAAQ,OAEvBQ,EAAM,eAAc,EAEpB,MAAML,EAAS,IAAI,KAElBO,EAAAF,EAAM,YAAN,MAAAE,EAAiB,aAAa,cAC0BF,EAAM,UAAW,WACtEX,EAAMG,CAAY,EAAE,MAC1B,EAEQW,EAAY,IAAI,SAASX,CAAY,EAcrCY,GAAiBC,EAAAL,EAAM,YAAN,YAAAK,EAAiB,aA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