next-safe-action
Version:
Type safe and validated Server Actions in your Next.js project.
1 lines • 87.3 kB
Source Map (JSON)
{"version":3,"sources":["../../../node_modules/.pnpm/deepmerge-ts@7.1.5/node_modules/deepmerge-ts/dist/index.mjs","../src/next/errors/bailout-to-csr.ts","../src/next/errors/http-access-fallback.ts","../src/next/errors/redirect.ts","../src/next/errors/router.ts","../src/next/errors/dynamic-usage.ts","../src/next/errors/postpone.ts","../src/next/errors/index.ts","../src/standard-schema.ts","../src/utils.ts","../src/validation-errors.ts","../src/action-builder.ts","../src/safe-action-client.ts","../src/middleware.ts","../src/index.ts"],"sourcesContent":["/**\n * Special values that tell deepmerge to perform a certain action.\n */\nconst actions = {\n defaultMerge: Symbol(\"deepmerge-ts: default merge\"),\n skip: Symbol(\"deepmerge-ts: skip\"),\n};\n/**\n * Special values that tell deepmergeInto to perform a certain action.\n */\nconst actionsInto = {\n defaultMerge: actions.defaultMerge,\n};\n\n/**\n * The default function to update meta data.\n *\n * It doesn't update the meta data.\n */\nfunction defaultMetaDataUpdater(previousMeta, metaMeta) {\n return metaMeta;\n}\n/**\n * The default function to filter values.\n *\n * It filters out undefined values.\n */\nfunction defaultFilterValues(values, meta) {\n return values.filter((value) => value !== undefined);\n}\n\n/**\n * The different types of objects deepmerge-ts support.\n */\nvar ObjectType;\n(function (ObjectType) {\n ObjectType[ObjectType[\"NOT\"] = 0] = \"NOT\";\n ObjectType[ObjectType[\"RECORD\"] = 1] = \"RECORD\";\n ObjectType[ObjectType[\"ARRAY\"] = 2] = \"ARRAY\";\n ObjectType[ObjectType[\"SET\"] = 3] = \"SET\";\n ObjectType[ObjectType[\"MAP\"] = 4] = \"MAP\";\n ObjectType[ObjectType[\"OTHER\"] = 5] = \"OTHER\";\n})(ObjectType || (ObjectType = {}));\n/**\n * Get the type of the given object.\n *\n * @param object - The object to get the type of.\n * @returns The type of the given object.\n */\nfunction getObjectType(object) {\n if (typeof object !== \"object\" || object === null) {\n return 0 /* ObjectType.NOT */;\n }\n if (Array.isArray(object)) {\n return 2 /* ObjectType.ARRAY */;\n }\n if (isRecord(object)) {\n return 1 /* ObjectType.RECORD */;\n }\n if (object instanceof Set) {\n return 3 /* ObjectType.SET */;\n }\n if (object instanceof Map) {\n return 4 /* ObjectType.MAP */;\n }\n return 5 /* ObjectType.OTHER */;\n}\n/**\n * Get the keys of the given objects including symbol keys.\n *\n * Note: Only keys to enumerable properties are returned.\n *\n * @param objects - An array of objects to get the keys of.\n * @returns A set containing all the keys of all the given objects.\n */\nfunction getKeys(objects) {\n const keys = new Set();\n for (const object of objects) {\n for (const key of [...Object.keys(object), ...Object.getOwnPropertySymbols(object)]) {\n keys.add(key);\n }\n }\n return keys;\n}\n/**\n * Does the given object have the given property.\n *\n * @param object - The object to test.\n * @param property - The property to test.\n * @returns Whether the object has the property.\n */\nfunction objectHasProperty(object, property) {\n return typeof object === \"object\" && Object.prototype.propertyIsEnumerable.call(object, property);\n}\n/**\n * Get an iterable object that iterates over the given iterables.\n */\nfunction getIterableOfIterables(iterables) {\n let mut_iterablesIndex = 0;\n let mut_iterator = iterables[0]?.[Symbol.iterator]();\n return {\n [Symbol.iterator]() {\n return {\n next() {\n do {\n if (mut_iterator === undefined) {\n return { done: true, value: undefined };\n }\n const result = mut_iterator.next();\n if (result.done === true) {\n mut_iterablesIndex += 1;\n mut_iterator = iterables[mut_iterablesIndex]?.[Symbol.iterator]();\n continue;\n }\n return {\n done: false,\n value: result.value,\n };\n } while (true);\n },\n };\n },\n };\n}\n// eslint-disable-next-line unicorn/prefer-set-has -- Array is more performant for a low number of elements.\nconst validRecordToStringValues = [\"[object Object]\", \"[object Module]\"];\n/**\n * Does the given object appear to be a record.\n */\nfunction isRecord(value) {\n // All records are objects.\n if (!validRecordToStringValues.includes(Object.prototype.toString.call(value))) {\n return false;\n }\n const { constructor } = value;\n // If has modified constructor.\n // eslint-disable-next-line ts/no-unnecessary-condition\n if (constructor === undefined) {\n return true;\n }\n const prototype = constructor.prototype;\n // If has modified prototype.\n if (prototype === null ||\n typeof prototype !== \"object\" ||\n !validRecordToStringValues.includes(Object.prototype.toString.call(prototype))) {\n return false;\n }\n // If constructor does not have an Object-specific method.\n // eslint-disable-next-line sonar/prefer-single-boolean-return, no-prototype-builtins\n if (!prototype.hasOwnProperty(\"isPrototypeOf\")) {\n return false;\n }\n // Most likely a record.\n return true;\n}\n\n/**\n * The default strategy to merge records.\n *\n * @param values - The records.\n */\nfunction mergeRecords$1(values, utils, meta) {\n const result = {};\n for (const key of getKeys(values)) {\n const propValues = [];\n for (const value of values) {\n if (objectHasProperty(value, key)) {\n propValues.push(value[key]);\n }\n }\n if (propValues.length === 0) {\n continue;\n }\n const updatedMeta = utils.metaDataUpdater(meta, {\n key,\n parents: values,\n });\n const propertyResult = mergeUnknowns(propValues, utils, updatedMeta);\n if (propertyResult === actions.skip) {\n continue;\n }\n if (key === \"__proto__\") {\n Object.defineProperty(result, key, {\n value: propertyResult,\n configurable: true,\n enumerable: true,\n writable: true,\n });\n }\n else {\n result[key] = propertyResult;\n }\n }\n return result;\n}\n/**\n * The default strategy to merge arrays.\n *\n * @param values - The arrays.\n */\nfunction mergeArrays$1(values) {\n return values.flat();\n}\n/**\n * The default strategy to merge sets.\n *\n * @param values - The sets.\n */\nfunction mergeSets$1(values) {\n return new Set(getIterableOfIterables(values));\n}\n/**\n * The default strategy to merge maps.\n *\n * @param values - The maps.\n */\nfunction mergeMaps$1(values) {\n return new Map(getIterableOfIterables(values));\n}\n/**\n * Get the last value in the given array.\n */\nfunction mergeOthers$1(values) {\n return values.at(-1);\n}\n/**\n * The merge functions.\n */\nconst mergeFunctions = {\n mergeRecords: mergeRecords$1,\n mergeArrays: mergeArrays$1,\n mergeSets: mergeSets$1,\n mergeMaps: mergeMaps$1,\n mergeOthers: mergeOthers$1,\n};\n\n/**\n * Deeply merge objects.\n *\n * @param objects - The objects to merge.\n */\nfunction deepmerge(...objects) {\n return deepmergeCustom({})(...objects);\n}\nfunction deepmergeCustom(options, rootMetaData) {\n const utils = getUtils(options, customizedDeepmerge);\n /**\n * The customized deepmerge function.\n */\n function customizedDeepmerge(...objects) {\n return mergeUnknowns(objects, utils, rootMetaData);\n }\n return customizedDeepmerge;\n}\n/**\n * The the utils that are available to the merge functions.\n *\n * @param options - The options the user specified\n */\nfunction getUtils(options, customizedDeepmerge) {\n return {\n defaultMergeFunctions: mergeFunctions,\n mergeFunctions: {\n ...mergeFunctions,\n ...Object.fromEntries(Object.entries(options)\n .filter(([key, option]) => Object.hasOwn(mergeFunctions, key))\n .map(([key, option]) => (option === false ? [key, mergeFunctions.mergeOthers] : [key, option]))),\n },\n metaDataUpdater: (options.metaDataUpdater ?? defaultMetaDataUpdater),\n deepmerge: customizedDeepmerge,\n useImplicitDefaultMerging: options.enableImplicitDefaultMerging ?? false,\n filterValues: options.filterValues === false ? undefined : (options.filterValues ?? defaultFilterValues),\n actions,\n };\n}\n/**\n * Merge unknown things.\n *\n * @param values - The values.\n */\nfunction mergeUnknowns(values, utils, meta) {\n const filteredValues = utils.filterValues?.(values, meta) ?? values;\n if (filteredValues.length === 0) {\n return undefined;\n }\n if (filteredValues.length === 1) {\n return mergeOthers(filteredValues, utils, meta);\n }\n const type = getObjectType(filteredValues[0]);\n if (type !== 0 /* ObjectType.NOT */ && type !== 5 /* ObjectType.OTHER */) {\n for (let mut_index = 1; mut_index < filteredValues.length; mut_index++) {\n if (getObjectType(filteredValues[mut_index]) === type) {\n continue;\n }\n return mergeOthers(filteredValues, utils, meta);\n }\n }\n switch (type) {\n case 1 /* ObjectType.RECORD */: {\n return mergeRecords(filteredValues, utils, meta);\n }\n case 2 /* ObjectType.ARRAY */: {\n return mergeArrays(filteredValues, utils, meta);\n }\n case 3 /* ObjectType.SET */: {\n return mergeSets(filteredValues, utils, meta);\n }\n case 4 /* ObjectType.MAP */: {\n return mergeMaps(filteredValues, utils, meta);\n }\n default: {\n return mergeOthers(filteredValues, utils, meta);\n }\n }\n}\n/**\n * Merge records.\n *\n * @param values - The records.\n */\nfunction mergeRecords(values, utils, meta) {\n const result = utils.mergeFunctions.mergeRecords(values, utils, meta);\n if (result === actions.defaultMerge ||\n (utils.useImplicitDefaultMerging &&\n result === undefined &&\n utils.mergeFunctions.mergeRecords !== utils.defaultMergeFunctions.mergeRecords)) {\n return utils.defaultMergeFunctions.mergeRecords(values, utils, meta);\n }\n return result;\n}\n/**\n * Merge arrays.\n *\n * @param values - The arrays.\n */\nfunction mergeArrays(values, utils, meta) {\n const result = utils.mergeFunctions.mergeArrays(values, utils, meta);\n if (result === actions.defaultMerge ||\n (utils.useImplicitDefaultMerging &&\n result === undefined &&\n utils.mergeFunctions.mergeArrays !== utils.defaultMergeFunctions.mergeArrays)) {\n return utils.defaultMergeFunctions.mergeArrays(values);\n }\n return result;\n}\n/**\n * Merge sets.\n *\n * @param values - The sets.\n */\nfunction mergeSets(values, utils, meta) {\n const result = utils.mergeFunctions.mergeSets(values, utils, meta);\n if (result === actions.defaultMerge ||\n (utils.useImplicitDefaultMerging &&\n result === undefined &&\n utils.mergeFunctions.mergeSets !== utils.defaultMergeFunctions.mergeSets)) {\n return utils.defaultMergeFunctions.mergeSets(values);\n }\n return result;\n}\n/**\n * Merge maps.\n *\n * @param values - The maps.\n */\nfunction mergeMaps(values, utils, meta) {\n const result = utils.mergeFunctions.mergeMaps(values, utils, meta);\n if (result === actions.defaultMerge ||\n (utils.useImplicitDefaultMerging &&\n result === undefined &&\n utils.mergeFunctions.mergeMaps !== utils.defaultMergeFunctions.mergeMaps)) {\n return utils.defaultMergeFunctions.mergeMaps(values);\n }\n return result;\n}\n/**\n * Merge other things.\n *\n * @param values - The other things.\n */\nfunction mergeOthers(values, utils, meta) {\n const result = utils.mergeFunctions.mergeOthers(values, utils, meta);\n if (result === actions.defaultMerge ||\n (utils.useImplicitDefaultMerging &&\n result === undefined &&\n utils.mergeFunctions.mergeOthers !== utils.defaultMergeFunctions.mergeOthers)) {\n return utils.defaultMergeFunctions.mergeOthers(values);\n }\n return result;\n}\n\n/**\n * The default strategy to merge records into a target record.\n *\n * @param mut_target - The result will be mutated into this record\n * @param values - The records (including the target's value if there is one).\n */\nfunction mergeRecordsInto$1(mut_target, values, utils, meta) {\n for (const key of getKeys(values)) {\n const propValues = [];\n for (const value of values) {\n if (objectHasProperty(value, key)) {\n propValues.push(value[key]);\n }\n }\n if (propValues.length === 0) {\n continue;\n }\n const updatedMeta = utils.metaDataUpdater(meta, {\n key,\n parents: values,\n });\n const propertyTarget = { value: propValues[0] };\n mergeUnknownsInto(propertyTarget, propValues, utils, updatedMeta);\n if (key === \"__proto__\") {\n Object.defineProperty(mut_target.value, key, {\n value: propertyTarget.value,\n configurable: true,\n enumerable: true,\n writable: true,\n });\n }\n else {\n mut_target.value[key] = propertyTarget.value;\n }\n }\n}\n/**\n * The default strategy to merge arrays into a target array.\n *\n * @param mut_target - The result will be mutated into this array\n * @param values - The arrays (including the target's value if there is one).\n */\nfunction mergeArraysInto$1(mut_target, values) {\n mut_target.value.push(...values.slice(1).flat());\n}\n/**\n * The default strategy to merge sets into a target set.\n *\n * @param mut_target - The result will be mutated into this set\n * @param values - The sets (including the target's value if there is one).\n */\nfunction mergeSetsInto$1(mut_target, values) {\n for (const value of getIterableOfIterables(values.slice(1))) {\n mut_target.value.add(value);\n }\n}\n/**\n * The default strategy to merge maps into a target map.\n *\n * @param mut_target - The result will be mutated into this map\n * @param values - The maps (including the target's value if there is one).\n */\nfunction mergeMapsInto$1(mut_target, values) {\n for (const [key, value] of getIterableOfIterables(values.slice(1))) {\n mut_target.value.set(key, value);\n }\n}\n/**\n * Set the target to the last value.\n */\nfunction mergeOthersInto$1(mut_target, values) {\n mut_target.value = values.at(-1);\n}\n/**\n * The merge functions.\n */\nconst mergeIntoFunctions = {\n mergeRecords: mergeRecordsInto$1,\n mergeArrays: mergeArraysInto$1,\n mergeSets: mergeSetsInto$1,\n mergeMaps: mergeMapsInto$1,\n mergeOthers: mergeOthersInto$1,\n};\n\nfunction deepmergeInto(target, ...objects) {\n return void deepmergeIntoCustom({})(target, ...objects);\n}\nfunction deepmergeIntoCustom(options, rootMetaData) {\n const utils = getIntoUtils(options, customizedDeepmergeInto);\n /**\n * The customized deepmerge function.\n */\n function customizedDeepmergeInto(target, ...objects) {\n mergeUnknownsInto({ value: target }, [target, ...objects], utils, rootMetaData);\n }\n return customizedDeepmergeInto;\n}\n/**\n * The the utils that are available to the merge functions.\n *\n * @param options - The options the user specified\n */\nfunction getIntoUtils(options, customizedDeepmergeInto) {\n return {\n defaultMergeFunctions: mergeIntoFunctions,\n mergeFunctions: {\n ...mergeIntoFunctions,\n ...Object.fromEntries(Object.entries(options)\n .filter(([key, option]) => Object.hasOwn(mergeIntoFunctions, key))\n .map(([key, option]) => (option === false ? [key, mergeIntoFunctions.mergeOthers] : [key, option]))),\n },\n metaDataUpdater: (options.metaDataUpdater ?? defaultMetaDataUpdater),\n deepmergeInto: customizedDeepmergeInto,\n filterValues: options.filterValues === false ? undefined : (options.filterValues ?? defaultFilterValues),\n actions: actionsInto,\n };\n}\n/**\n * Merge unknown things into a target.\n *\n * @param mut_target - The target to merge into.\n * @param values - The values.\n */\nfunction mergeUnknownsInto(mut_target, values, utils, meta) {\n const filteredValues = utils.filterValues?.(values, meta) ?? values;\n if (filteredValues.length === 0) {\n return;\n }\n if (filteredValues.length === 1) {\n return void mergeOthersInto(mut_target, filteredValues, utils, meta);\n }\n const type = getObjectType(mut_target.value);\n if (type !== 0 /* ObjectType.NOT */ && type !== 5 /* ObjectType.OTHER */) {\n for (let mut_index = 1; mut_index < filteredValues.length; mut_index++) {\n if (getObjectType(filteredValues[mut_index]) === type) {\n continue;\n }\n return void mergeOthersInto(mut_target, filteredValues, utils, meta);\n }\n }\n switch (type) {\n case 1 /* ObjectType.RECORD */: {\n return void mergeRecordsInto(mut_target, filteredValues, utils, meta);\n }\n case 2 /* ObjectType.ARRAY */: {\n return void mergeArraysInto(mut_target, filteredValues, utils, meta);\n }\n case 3 /* ObjectType.SET */: {\n return void mergeSetsInto(mut_target, filteredValues, utils, meta);\n }\n case 4 /* ObjectType.MAP */: {\n return void mergeMapsInto(mut_target, filteredValues, utils, meta);\n }\n default: {\n return void mergeOthersInto(mut_target, filteredValues, utils, meta);\n }\n }\n}\n/**\n * Merge records into a target record.\n *\n * @param mut_target - The target to merge into.\n * @param values - The records.\n */\nfunction mergeRecordsInto(mut_target, values, utils, meta) {\n const action = utils.mergeFunctions.mergeRecords(mut_target, values, utils, meta);\n if (action === actionsInto.defaultMerge) {\n utils.defaultMergeFunctions.mergeRecords(mut_target, values, utils, meta);\n }\n}\n/**\n * Merge arrays into a target array.\n *\n * @param mut_target - The target to merge into.\n * @param values - The arrays.\n */\nfunction mergeArraysInto(mut_target, values, utils, meta) {\n const action = utils.mergeFunctions.mergeArrays(mut_target, values, utils, meta);\n if (action === actionsInto.defaultMerge) {\n utils.defaultMergeFunctions.mergeArrays(mut_target, values);\n }\n}\n/**\n * Merge sets into a target set.\n *\n * @param mut_target - The target to merge into.\n * @param values - The sets.\n */\nfunction mergeSetsInto(mut_target, values, utils, meta) {\n const action = utils.mergeFunctions.mergeSets(mut_target, values, utils, meta);\n if (action === actionsInto.defaultMerge) {\n utils.defaultMergeFunctions.mergeSets(mut_target, values);\n }\n}\n/**\n * Merge maps into a target map.\n *\n * @param mut_target - The target to merge into.\n * @param values - The maps.\n */\nfunction mergeMapsInto(mut_target, values, utils, meta) {\n const action = utils.mergeFunctions.mergeMaps(mut_target, values, utils, meta);\n if (action === actionsInto.defaultMerge) {\n utils.defaultMergeFunctions.mergeMaps(mut_target, values);\n }\n}\n/**\n * Merge other things into a target.\n *\n * @param mut_target - The target to merge into.\n * @param values - The other things.\n */\nfunction mergeOthersInto(mut_target, values, utils, meta) {\n const action = utils.mergeFunctions.mergeOthers(mut_target, values, utils, meta);\n if (action === actionsInto.defaultMerge || mut_target.value === actionsInto.defaultMerge) {\n utils.defaultMergeFunctions.mergeOthers(mut_target, values);\n }\n}\n\nexport { deepmerge, deepmergeCustom, deepmergeInto, deepmergeIntoCustom, getKeys, getObjectType, objectHasProperty };\n","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/shared/lib/lazy-dynamic/bailout-to-csr.ts\n\n// This has to be a shared module which is shared between client component error boundary and dynamic component\nconst BAILOUT_TO_CSR = \"BAILOUT_TO_CLIENT_SIDE_RENDERING\";\n\n/** An error that should be thrown when we want to bail out to client-side rendering. */\nclass BailoutToCSRError extends Error {\n\tpublic readonly digest = BAILOUT_TO_CSR;\n\n\tconstructor(public readonly reason: string) {\n\t\tsuper(`Bail out to client-side rendering: ${reason}`);\n\t}\n}\n\n/** Checks if a passed argument is an error that is thrown if we want to bail out to client-side rendering. */\nexport function isBailoutToCSRError(err: unknown): err is BailoutToCSRError {\n\tif (typeof err !== \"object\" || err === null || !(\"digest\" in err)) {\n\t\treturn false;\n\t}\n\n\treturn err.digest === BAILOUT_TO_CSR;\n}\n","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/http-access-fallback/http-access-fallback.ts\n\nconst HTTPAccessErrorStatus = {\n\tNOT_FOUND: 404,\n\tFORBIDDEN: 403,\n\tUNAUTHORIZED: 401,\n};\n\nconst ALLOWED_CODES = new Set(Object.values(HTTPAccessErrorStatus));\n\nconst HTTP_ERROR_FALLBACK_ERROR_CODE = \"NEXT_HTTP_ERROR_FALLBACK\";\n\nexport type HTTPAccessFallbackError = Error & {\n\tdigest: `${typeof HTTP_ERROR_FALLBACK_ERROR_CODE};${string}`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by\n * the HTTP navigation APIs `notFound()`, `forbidden()` or `unauthorized()`.\n *\n * @param error the error that may reference a HTTP access error\n * @returns true if the error is a HTTP access error\n */\nexport function isHTTPAccessFallbackError(error: unknown): error is HTTPAccessFallbackError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\tconst [prefix, httpStatus] = error.digest.split(\";\");\n\n\treturn prefix === HTTP_ERROR_FALLBACK_ERROR_CODE && ALLOWED_CODES.has(Number(httpStatus));\n}\n\nexport function getAccessFallbackHTTPStatus(error: HTTPAccessFallbackError): number {\n\tconst httpStatus = error.digest.split(\";\")[1];\n\treturn Number(httpStatus);\n}\n","// Comes from: https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect-error.ts\n\nenum RedirectStatusCode {\n\tSeeOther = 303,\n\tTemporaryRedirect = 307,\n\tPermanentRedirect = 308,\n}\n\nconst REDIRECT_ERROR_CODE = \"NEXT_REDIRECT\";\n\nenum RedirectType {\n\tpush = \"push\",\n\treplace = \"replace\",\n}\n\nexport type RedirectError = Error & {\n\tdigest: `${typeof REDIRECT_ERROR_CODE};${RedirectType};${string};${RedirectStatusCode};`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by the\n * `redirect(url)` helper.\n *\n * @param error the error that may reference a redirect error\n * @returns true if the error is a redirect error\n */\nexport function isRedirectError(error: unknown): error is RedirectError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\n\tconst digest = error.digest.split(\";\");\n\tconst [errorCode, type] = digest;\n\tconst destination = digest.slice(2, -2).join(\";\");\n\tconst status = digest.at(-2);\n\n\tconst statusCode = Number(status);\n\n\treturn (\n\t\terrorCode === REDIRECT_ERROR_CODE &&\n\t\t(type === \"replace\" || type === \"push\") &&\n\t\ttypeof destination === \"string\" &&\n\t\t!isNaN(statusCode) &&\n\t\tstatusCode in RedirectStatusCode\n\t);\n}\n","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/is-next-router-error.ts\n\nimport { isHTTPAccessFallbackError, type HTTPAccessFallbackError } from \"./http-access-fallback\";\nimport { isRedirectError, type RedirectError } from \"./redirect\";\n\n/**\n * Returns true if the error is a navigation signal error. These errors are\n * thrown by user code to perform navigation operations and interrupt the React\n * render.\n */\nexport function isNextRouterError(error: unknown): error is RedirectError | HTTPAccessFallbackError {\n\treturn isRedirectError(error) || isHTTPAccessFallbackError(error);\n}\n","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/export/helpers/is-dynamic-usage-error.ts\n\nimport { isBailoutToCSRError } from \"./bailout-to-csr\";\nimport { isNextRouterError } from \"./router\";\n\nconst DYNAMIC_ERROR_CODE = \"DYNAMIC_SERVER_USAGE\";\n\nclass DynamicServerError extends Error {\n\tdigest: typeof DYNAMIC_ERROR_CODE = DYNAMIC_ERROR_CODE;\n\n\tconstructor(public readonly description: string) {\n\t\tsuper(`Dynamic server usage: ${description}`);\n\t}\n}\n\nfunction isDynamicServerError(err: unknown): err is DynamicServerError {\n\tif (typeof err !== \"object\" || err === null || !(\"digest\" in err) || typeof err.digest !== \"string\") {\n\t\treturn false;\n\t}\n\n\treturn err.digest === DYNAMIC_ERROR_CODE;\n}\n\nfunction isDynamicPostponeReason(reason: string) {\n\treturn (\n\t\treason.includes(\"needs to bail out of prerendering at this point because it used\") &&\n\t\treason.includes(\"Learn more: https://nextjs.org/docs/messages/ppr-caught-error\")\n\t);\n}\n\nfunction isDynamicPostpone(err: unknown) {\n\tif (\n\t\ttypeof err === \"object\" &&\n\t\terr !== null &&\n\t\t// eslint-disable-next-line\n\t\ttypeof (err as any).message === \"string\"\n\t) {\n\t\t// eslint-disable-next-line\n\t\treturn isDynamicPostponeReason((err as any).message);\n\t}\n\treturn false;\n}\n\nexport const isDynamicUsageError = (err: unknown) =>\n\tisDynamicServerError(err) || isBailoutToCSRError(err) || isNextRouterError(err) || isDynamicPostpone(err);\n","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/server/lib/router-utils/is-postpone.ts\n\nconst REACT_POSTPONE_TYPE: symbol = Symbol.for(\"react.postpone\");\n\nexport function isPostpone(error: any): boolean {\n\treturn (\n\t\ttypeof error === \"object\" &&\n\t\terror !== null &&\n\t\t// eslint-disable-next-line\n\t\terror.$$typeof === REACT_POSTPONE_TYPE\n\t);\n}\n","import type { NavigationKind } from \"../../index.types\";\nimport { isBailoutToCSRError } from \"./bailout-to-csr\";\nimport { isDynamicUsageError } from \"./dynamic-usage\";\nimport { getAccessFallbackHTTPStatus, isHTTPAccessFallbackError } from \"./http-access-fallback\";\nimport { isPostpone } from \"./postpone\";\nimport { isRedirectError } from \"./redirect\";\nimport { isNextRouterError } from \"./router\";\n\nexport class FrameworkErrorHandler {\n\t#frameworkError: Error | undefined;\n\n\tstatic isNavigationError(error: unknown): error is Error {\n\t\treturn isNextRouterError(error) || isBailoutToCSRError(error) || isDynamicUsageError(error) || isPostpone(error);\n\t}\n\n\tstatic getNavigationKind(error: Error): NavigationKind {\n\t\tif (isRedirectError(error)) {\n\t\t\treturn \"redirect\";\n\t\t} else if (isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error) === 404) {\n\t\t\treturn \"notFound\";\n\t\t} else if (isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error) === 403) {\n\t\t\treturn \"forbidden\";\n\t\t} else if (isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error) === 401) {\n\t\t\treturn \"unauthorized\";\n\t\t} else {\n\t\t\treturn \"other\";\n\t\t}\n\t}\n\n\t// Used in action builder.\n\thandleError(e: unknown) {\n\t\tif (FrameworkErrorHandler.isNavigationError(e)) {\n\t\t\tthis.#frameworkError = e;\n\t\t\treturn;\n\t\t}\n\n\t\t// If it's not a framework error, rethrow it, so it gets returned as a server error.\n\t\tthrow e;\n\t}\n\n\tget error() {\n\t\treturn this.#frameworkError;\n\t}\n}\n","/** The Standard Schema interface. */\nexport interface StandardSchemaV1<Input = unknown, Output = Input> {\n\t/** The Standard Schema properties. */\n\treadonly \"~standard\": StandardSchemaV1.Props<Input, Output>;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-namespace\nexport declare namespace StandardSchemaV1 {\n\t/** The Standard Schema properties interface. */\n\texport interface Props<Input = unknown, Output = Input> {\n\t\t/** The version number of the standard. */\n\t\treadonly version: 1;\n\t\t/** The vendor name of the schema library. */\n\t\treadonly vendor: string;\n\t\t/** Validates unknown input values. */\n\t\treadonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;\n\t\t/** Inferred types associated with the schema. */\n\t\treadonly types?: Types<Input, Output> | undefined;\n\t}\n\n\t/** The result interface of the validate function. */\n\texport type Result<Output> = SuccessResult<Output> | FailureResult;\n\n\t/** The result interface if validation succeeds. */\n\texport interface SuccessResult<Output> {\n\t\t/** The typed output value. */\n\t\treadonly value: Output;\n\t\t/** The non-existent issues. */\n\t\treadonly issues?: undefined;\n\t}\n\n\t/** The result interface if validation fails. */\n\texport interface FailureResult {\n\t\t/** The issues of failed validation. */\n\t\treadonly issues: ReadonlyArray<Issue>;\n\t}\n\n\t/** The issue interface of the failure output. */\n\texport interface Issue {\n\t\t/** The error message of the issue. */\n\t\treadonly message: string;\n\t\t/** The path of the issue, if any. */\n\t\treadonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;\n\t}\n\n\t/** The path segment interface of the issue. */\n\texport interface PathSegment {\n\t\t/** The key representing a path segment. */\n\t\treadonly key: PropertyKey;\n\t}\n\n\t/** The Standard Schema types interface. */\n\texport interface Types<Input = unknown, Output = Input> {\n\t\t/** The input type of the schema. */\n\t\treadonly input: Input;\n\t\t/** The output type of the schema. */\n\t\treadonly output: Output;\n\t}\n\n\t/** Infers the input type of a Standard Schema. */\n\texport type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema[\"~standard\"][\"types\"]>[\"input\"];\n\n\t/** Infers the output type of a Standard Schema. */\n\texport type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema[\"~standard\"][\"types\"]>[\"output\"];\n}\n\n// custom helpers\n\n/** Infer the input type of an array of Standard Schemas. */\nexport type InferInputArray<Schemas extends readonly StandardSchemaV1[]> = {\n\t[K in keyof Schemas]: StandardSchemaV1.InferInput<Schemas[K]>;\n};\n\n/** Infer the output type of an array of Standard Schemas. */\nexport type InferOutputArray<Schemas extends readonly StandardSchemaV1[]> = {\n\t[K in keyof Schemas]: StandardSchemaV1.InferOutput<Schemas[K]>;\n};\n\n/** Infer the input type of a Standard Schema, or a default type if the schema is undefined. */\nexport type InferInputOrDefault<MaybeSchema, Default> = MaybeSchema extends StandardSchemaV1\n\t? StandardSchemaV1.InferInput<MaybeSchema>\n\t: Default;\n\n/** Infer the output type of a Standard Schema, or a default type if the schema is undefined. */\nexport type InferOutputOrDefault<MaybeSchema, Default> = MaybeSchema extends StandardSchemaV1\n\t? StandardSchemaV1.InferOutput<MaybeSchema>\n\t: Default;\n\nexport async function standardParse<Output>(schema: StandardSchemaV1<unknown, Output>, value: unknown) {\n\treturn schema[\"~standard\"].validate(value);\n}\n","export const DEFAULT_SERVER_ERROR_MESSAGE = \"Something went wrong while executing the operation.\";\n\n/**\n * Checks if passed argument is an instance of Error.\n */\nexport const isError = (error: unknown): error is Error => error instanceof Error;\n\n/**\n * Checks what the winning boolean value is from a series of values, from lowest to highest priority.\n * `null` and `undefined` values are skipped.\n */\nexport const winningBoolean = (...args: (boolean | undefined | null)[]) => {\n\treturn args.reduce((acc, v) => (typeof v === \"boolean\" ? v : acc), false) as boolean;\n};\n","/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */\nimport type { StandardSchemaV1 } from \"./standard-schema\";\nimport type { FlattenedValidationErrors, IssueWithUnionErrors, ValidationErrors } from \"./validation-errors.types\";\n\nconst getKey = (segment: PropertyKey | StandardSchemaV1.PathSegment) =>\n\ttypeof segment === \"object\" ? segment.key : segment;\n\nconst getIssueMessage = (issue: IssueWithUnionErrors) => {\n\tif (issue.unionErrors) {\n\t\treturn issue.unionErrors.map((u) => u.issues.map((i) => i.message)).flat();\n\t}\n\treturn issue.message;\n};\n\n// This function is used internally to build the validation errors object from a list of validation issues.\nexport const buildValidationErrors = <S extends StandardSchemaV1 | undefined>(\n\tissues: readonly IssueWithUnionErrors[]\n) => {\n\tconst ve: any = {};\n\n\tfor (const issue of issues) {\n\t\tconst { path, message, unionErrors } = issue;\n\n\t\t// When path is undefined or empty, set root errors.\n\t\tif (!path || path.length === 0) {\n\t\t\tve._errors = ve._errors ? [...ve._errors, message] : [message];\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Reference to errors object.\n\t\tlet ref = ve;\n\n\t\t// Set object for the path, if it doesn't exist.\n\t\tfor (let i = 0; i < path.length - 1; i++) {\n\t\t\tconst k = getKey(path[i]!);\n\n\t\t\tif (!ref[k]) {\n\t\t\t\tref[k] = {};\n\t\t\t}\n\n\t\t\tref = ref[k];\n\t\t}\n\n\t\t// Key is always the last element of the path.\n\t\tconst key = getKey(path[path.length - 1]!);\n\n\t\tconst issueMessage = getIssueMessage(issue);\n\n\t\t// Set error for the current path. If `_errors` array exists, add the message to it.\n\t\tref[key] = ref[key]?._errors\n\t\t\t? {\n\t\t\t\t\t...structuredClone(ref[key]),\n\t\t\t\t\t_errors: [...ref[key]._errors, issueMessage],\n\t\t\t\t}\n\t\t\t: { ...structuredClone(ref[key]), _errors: unionErrors ? issueMessage : [issueMessage] };\n\t}\n\n\treturn ve as ValidationErrors<S>;\n};\n\n// This class is internally used to throw validation errors in action's server code function, using\n// `returnValidationErrors`.\nexport class ActionServerValidationError<S extends StandardSchemaV1> extends Error {\n\tpublic validationErrors: ValidationErrors<S>;\n\tconstructor(validationErrors: ValidationErrors<S>) {\n\t\tsuper(\"Server Action server validation error(s) occurred\");\n\t\tthis.validationErrors = validationErrors;\n\t}\n}\n\n// This class is internally used to throw validation errors in action's server code function, using\n// `returnValidationErrors`.\nexport class ActionValidationError<CVE> extends Error {\n\tpublic validationErrors: CVE;\n\tconstructor(validationErrors: CVE, overriddenErrorMessage?: string) {\n\t\tsuper(overriddenErrorMessage ?? \"Server Action validation error(s) occurred\");\n\t\tthis.validationErrors = validationErrors;\n\t}\n}\n\n// This class is internally used to throw validation errors in action's server code function, using\n// `returnValidationErrors`.\nexport class ActionBindArgsValidationError extends Error {\n\tpublic validationErrors: unknown[];\n\tconstructor(validationErrors: unknown[]) {\n\t\tsuper(\"Server Action bind args validation error(s) occurred\");\n\t\tthis.validationErrors = validationErrors;\n\t}\n}\n\n/**\n * Return custom validation errors to the client from the action's server code function.\n * Code declared after this function invocation will not be executed.\n * @param schema Input schema\n * @param validationErrors Validation errors object\n *\n * {@link https://next-safe-action.dev/docs/define-actions/validation-errors#returnvalidationerrors See docs for more information}\n */\nexport function returnValidationErrors<\n\tS extends StandardSchemaV1 | (() => Promise<StandardSchemaV1>),\n\tAS extends StandardSchemaV1 = S extends () => Promise<StandardSchemaV1> ? Awaited<ReturnType<S>> : S, // actual schema\n>(schema: S, validationErrors: ValidationErrors<AS>): never {\n\tthrow new ActionServerValidationError<AS>(validationErrors);\n}\n\n/**\n * Default validation errors format.\n * Emulation of `zod`'s [`format`](https://zod.dev/ERROR_HANDLING?id=formatting-errors) function.\n */\nexport function formatValidationErrors<VE extends ValidationErrors<any>>(validationErrors: VE) {\n\treturn validationErrors;\n}\n\n/**\n * Transform default formatted validation errors into flattened structure.\n * `formErrors` contains global errors, and `fieldErrors` contains errors for each field,\n * one level deep. It discards errors for nested fields.\n * Emulation of `zod`'s [`flatten`](https://zod.dev/ERROR_HANDLING?id=flattening-errors) function.\n * @param {ValidationErrors} [validationErrors] Validation errors object\n *\n * {@link https://next-safe-action.dev/docs/define-actions/validation-errors#flattenvalidationerrorsutility-function See docs for more information}\n */\nexport function flattenValidationErrors<VE extends ValidationErrors<any>>(validationErrors: VE) {\n\tconst flattened: FlattenedValidationErrors<VE> = {\n\t\tformErrors: [],\n\t\tfieldErrors: {},\n\t};\n\n\tfor (const [key, value] of Object.entries<string[] | { _errors: string[] }>(validationErrors ?? {})) {\n\t\tif (key === \"_errors\" && Array.isArray(value)) {\n\t\t\tflattened.formErrors = [...value];\n\t\t} else {\n\t\t\tif (\"_errors\" in value) {\n\t\t\t\tflattened.fieldErrors[key as keyof Omit<VE, \"_errors\">] = [...value._errors];\n\t\t\t}\n\t\t}\n\t}\n\n\treturn flattened;\n}\n\n/**\n * This error is thrown when an action metadata is invalid, i.e. when there's a mismatch between the\n * type of the metadata schema returned from `defineMetadataSchema` and the actual data passed.\n */\nexport class ActionMetadataValidationError<MDS extends StandardSchemaV1 | undefined> extends Error {\n\tpublic validationErrors: ValidationErrors<MDS>;\n\n\tconstructor(validationErrors: ValidationErrors<MDS>) {\n\t\tsuper(\"Invalid metadata input. Please be sure to pass metadata via `metadata` method before defining the action.\");\n\t\tthis.name = \"ActionMetadataError\";\n\t\tthis.validationErrors = validationErrors;\n\t}\n}\n\n/**\n * This error is thrown when an action's data (output) is invalid, i.e. when there's a mismatch between the\n * type of the data schema passed to `dataSchema` method and the actual return of the action.\n */\nexport class ActionOutputDataValidationError<DS extends StandardSchemaV1 | undefined> extends Error {\n\tpublic validationErrors: ValidationErrors<DS>;\n\n\tconstructor(validationErrors: ValidationErrors<DS>) {\n\t\tsuper(\n\t\t\t\"Invalid action data (output). Please be sure to return data following the shape of the schema passed to `dataSchema` method.\"\n\t\t);\n\t\tthis.name = \"ActionOutputDataError\";\n\t\tthis.validationErrors = validationErrors;\n\t}\n}\n","import { deepmerge } from \"deepmerge-ts\";\nimport type {} from \"zod\";\nimport type {\n\tDVES,\n\tMiddlewareResult,\n\tSafeActionClientArgs,\n\tSafeActionFn,\n\tSafeActionResult,\n\tSafeActionUtils,\n\tSafeStateActionFn,\n\tServerCodeFn,\n\tStateServerCodeFn,\n} from \"./index.types\";\nimport { FrameworkErrorHandler } from \"./next/errors\";\nimport type {\n\tInferInputArray,\n\tInferInputOrDefault,\n\tInferOutputArray,\n\tInferOutputOrDefault,\n\tStandardSchemaV1,\n} from \"./standard-schema\";\nimport { standardParse } from \"./standard-schema\";\nimport { DEFAULT_SERVER_ERROR_MESSAGE, isError, winningBoolean } from \"./utils\";\nimport {\n\tActionBindArgsValidationError,\n\tActionMetadataValidationError,\n\tActionOutputDataValidationError,\n\tActionServerValidationError,\n\tActionValidationError,\n\tbuildValidationErrors,\n} from \"./validation-errors\";\nimport type { ValidationErrors } from \"./validation-errors.types\";\n\nexport function actionBuilder<\n\tServerError,\n\tODVES extends DVES | undefined, // override default validation errors shape\n\tMetadataSchema extends StandardSchemaV1 | undefined = undefined,\n\tMD = InferOutputOrDefault<MetadataSchema, undefined>, // metadata type (inferred from metadata schema)\n\tCtx extends object = {},\n\tISF extends (() => Promise<StandardSchemaV1>) | undefined = undefined, // input schema function\n\tIS extends StandardSchemaV1 | undefined = ISF extends Function ? Awaited<ReturnType<ISF>> : undefined, // input schema\n\tOS extends StandardSchemaV1 | undefined = undefined, // output schema\n\tconst BAS extends readonly StandardSchemaV1[] = [],\n\tCVE = undefined,\n>(args: SafeActionClientArgs<ServerError, ODVES, MetadataSchema, MD, true, Ctx, ISF, IS, OS, BAS, CVE>) {\n\tconst bindArgsSchemas = args.bindArgsSchemas ?? [];\n\n\tfunction buildAction({ withState }: { withState: false }): {\n\t\taction: <Data extends InferOutputOrDefault<OS, any>>(\n\t\t\tserverCodeFn: ServerCodeFn<MD, Ctx, IS, BAS, Data>,\n\t\t\tutils?: SafeActionUtils<ServerError, MD, Ctx, IS, BAS, CVE, Data>\n\t\t) => SafeActionFn<ServerError, IS, BAS, CVE, Data>;\n\t};\n\tfunction buildAction({ withState }: { withState: true }): {\n\t\taction: <Data extends InferOutputOrDefault<OS, any>>(\n\t\t\tserverCodeFn: StateServerCodeFn<ServerError, MD, Ctx, IS, BAS, CVE, Data>,\n\t\t\tutils?: SafeActionUtils<ServerError, MD, Ctx, IS, BAS, CVE, Data>\n\t\t) => SafeStateActionFn<ServerError, IS, BAS, CVE, Data>;\n\t};\n\tfunction buildAction({ withState }: { withState: boolean }) {\n\t\treturn {\n\t\t\taction: <Data extends InferOutputOrDefault<OS, any>>(\n\t\t\t\tserverCodeFn:\n\t\t\t\t\t| ServerCodeFn<MD, Ctx, IS, BAS, Data>\n\t\t\t\t\t| StateServerCodeFn<ServerError, MD, Ctx, IS, BAS, CVE, Data>,\n\t\t\t\tutils?: SafeActionUtils<ServerError, MD, Ctx, IS, BAS, CVE, Data>\n\t\t\t) => {\n\t\t\t\treturn async (...clientInputs: unknown[]) => {\n\t\t\t\t\tlet currentCtx: object = {};\n\t\t\t\t\tconst middlewareResult: MiddlewareResult<ServerError, object> = { success: false };\n\t\t\t\t\ttype PrevResult = SafeActionResult<ServerError, IS, CVE, Data>;\n\t\t\t\t\tlet prevResult: PrevResult = {};\n\t\t\t\t\tconst parsedInputDatas: any[] = [];\n\t\t\t\t\tconst frameworkErrorHandler = new FrameworkErrorHandler();\n\n\t\t\t\t\t// Track if server error has been handled.\n\t\t\t\t\tlet serverErrorHandled = false;\n\n\t\t\t\t\tif (withState) {\n\t\t\t\t\t\t// Previous state is placed between bind args and main arg inputs, so it's always at the index of\n\t\t\t\t\t\t// the bind args schemas + 1. Get it and remove it from the client inputs array.\n\t\t\t\t\t\tprevResult = clientInputs.splice(bindArgsSchemas.length, 1)[0] as PrevResult;\n\t\t\t\t\t}\n\n\t\t\t\t\t// If the number of bind args schemas + 1 (which is the optional main arg schema) is greater\n\t\t\t\t\t// than the number of provided client inputs, it means that the main argument is missing.\n\t\t\t\t\t// This happens when the main schema is missing (since it's optional), or if a void main schema\n\t\t\t\t\t// is provided along with bind args schemas.\n\t\t\t\t\tif (bindArgsSchemas.length + 1 > clientInputs.length) {\n\t\t\t\t\t\tclientInputs.push(undefined);\n\t\t\t\t\t}\n\n\t\t\t\t\t// Execute the middleware stack.\n\t\t\t\t\tconst executeMiddlewareStack = async (idx = 0) => {\n\t\t\t\t\t\tif (frameworkErrorHandler.error) {\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst middlewareFn = args.middlewareFns[idx];\n\t\t\t\t\t\tmiddlewareResult.ctx = currentCtx;\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tif (idx === 0) {\n\t\t\t\t\t\t\t\tif (args.metadataSchema) {\n\t\t\t\t\t\t\t\t\t// Validate metadata input.\n\t\t\t\t\t\t\t\t\tconst parsedMd = await standardParse(args.metadataSchema, args.metadata);\n\n\t\t\t\t\t\t\t\t\tif (parsedMd.issues) {\n\t\t\t\t\t\t\t\t\t\tthrow new ActionMetadataValidationError<MetadataSchema>(buildValidationErrors(parsedMd.issues));\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Middleware function.\n\t\t\t\t\t\t\tif (middlewareFn) {\n\t\t\t\t\t\t\t\tawait middlewareFn({\n\t\t\t\t\t\t\t\t\tclientInput: clientInputs.at(-1), // pass raw client input\n\t\t\t\t\t\t\t\t\tbindArgsClientInputs: bindArgsSchemas.length ? clientInputs.slice(0, -1) : [],\n\t\t\t\t\t\t\t\t\tctx: currentCtx,\n\t\t\t\t\t\t\t\t\tmetadata: args.metadata,\n\t\t\t\t\t\t\t\t\tnext: async (nextOpts) => {\n\t\t\t\t\t\t\t\t\t\tcurrentCtx = deepmerge(currentCtx, nextOpts?.ctx ?? {});\n\t\t\t\t\t\t\t\t\t\tawait executeMiddlewareStack(idx + 1);\n\t\t\t\t\t\t\t\t\t\treturn middlewareResult;\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t}).catch((e) => {\n\t\t\t\t\t\t\t\t\tframeworkErrorHandler.handleError(e);\n\t\t\t\t\t\t\t\t\tif (frameworkErrorHandler.error) {\n\t\t\t\t\t\t\t\t\t\tmiddlewareResult.success = false;\n\t\t\t\t\t\t\t\t\t\tmiddlewareResult.navigationKind = FrameworkErrorHandler.getNavigationKind(\n\t\t\t\t\t\t\t\t\t\t\tframeworkErrorHandler.error\n\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t// Action function.\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// Validate the client inputs in parallel.\n\t\t\t\t\t\t\t\tconst parsedInputs = await Promise.all(\n\t\t\t\t\t\t\t\t\tclientInputs.map(async (input, i) => {\n\t\t\t\t\t\t\t\t\t\t// Last client input in the array, main argument (no bind arg).\n\t\t\t\t\t\t\t\t\t\tif (i === clientInputs.length - 1) {\n\t\t\t\t\t\t\t\t\t\t\t// If schema is undefined, set parsed data to undefined.\n\t\t\t\t\t\t\t\t\t\t\tif (typeof args.inputSchemaFn === \"undefined\") {\n\t\t\t\t\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\t\t\t\t\tvalue: undefined,\n\t\t\t\t\t\t\t\t\t\t\t\t} as const satisfies StandardSchemaV1.Result<undefined>;\n\t\t\t\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t\t\t\t// Otherwise, parse input with the schema.\n\t\t\t\t\t\t\t\t\t\t\treturn standardParse(await args.inputSchemaFn(), input);\n\t\t\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t\t\t// Otherwise, we're processing bind args client inputs.\n\t\t\t\t\t\t\t\t\t\treturn standardParse(bindArgsSchemas[i]!, input);\n\t\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t\tlet hasBindValidationErrors = false;\n\n\t\t\t\t\t\t\t\t// Initialize the bind args validation errors array with null values.\n\t\t\t\t\t\t\t\t// It has the same length as the number of bind arguments (parsedInputs - 1).\n\t\t\t\t\t\t\t\tconst bindArgsValidationErrors = Array(parsedInputs.length - 1).fill({});\n\n\t\t\t\t\t\t\t\tfor (let i = 0; i < parsedInputs.length; i++) {\n\t\t\t\t\t\t\t\t\tconst parsedInput = parsedInputs[i]!;\n\n\t\t\t\t\t\t\t\t\tif (!parsedInput.issues) {\n\t\t\t\t\t\t\t\t\t\tparsedInputDatas.push(parsedInput.value);\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\t// If we're processing a bind argument and there are validation errors for this one,\n\t\t\t\t\t\t\t\t\t\t// we need to store them in the bind args validation errors array at this index.\n\t\t\t\t\t\t\t\t\t\tif (i < parsedInputs.length - 1) {\n\t\t\t\t\t\t\t\t\t\t\tbindArgsValidationErrors[i] = buildValidationErrors<BAS[number]>(parsedInput.issues);\n\t\t\t\t\t\t\t\t\t\t\thasBindValidationErrors = true;\n\t\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\t\t// Otherwise, we're processing the non-bind argument (the last one) in the array.\n\t\t\t\t\t\t\t\t\t\t\tconst validationErrors = buildValidationErrors<IS>(parsedInput.issues);\n\n\t\t\t\t\t\t\t\t\t\t\tmiddlewareResult.validationErrors = await Promise.resolve(\n\t\t\t\t\t\t\t\t\t\t\t\targs.handleValidationErrorsShape(validationErrors, {\n\t\t\t\t\t\t\t\t\t\t\t\t\tclientInput: clientInputs.at(-1) as InferInputOrDefault<IS, undefined>,\n\t\t\t\t\t\t\t\t\t\t\t\t\tbindArgsClientInputs: (bindArgsSchemas.length\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t? clientInputs.slice(0, -1)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t: []) as InferInputArray<BAS>,\n\t\t\t\t\t\t\t\t\t\t\t\t\tctx: currentCtx as Ctx,\n\t\t\t\t\t\t\t\t\t\t\t\t\tmetadata: args.metadata,\n\t\t\t\t\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t// If there are bind args validation errors, throw an error.\n\t\t\t\t\t\t\t\tif (hasBindValidationErrors) {\n\t\t\t\t\t\t\t\t\tthrow new ActionBindArgsValidationError(bindArgsValidationErrors);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tif (middlewareResult.validationErrors) {\n\t\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\t\tconst scfArgs: Parameters<StateServerCodeFn<ServerError, MD, Ctx, IS, BAS, CVE, Data>> = [];\n\n\t\t\t\t\t\t\t\t// Server code function always has this object as the first argument.\n\t\t\t\t\t\t\t\tscfArgs[0] = {\n\t\t\t\t\t\t\t\t\tparsedInput: parsedInputDatas.at(-1) as InferOutputOrDefault<IS, undefined>,\n\t\t\t\t\t\t\t\t\tbindArgsParsedInputs: parsedInputDatas.slice(0, -1) as InferOutputArray<BAS>,\n\t\t\t\t\t\t\t\t\tclientInput: clientInputs.at(-1) as InferInputOrDefault<IS, undefined>,\n\t\t\t\t\t\t\t\t\tbindArgsClientInputs: (bindArgsSchemas.length\n\t\t\t\t\t\t\t\t\t\t? clientInputs.slice(0, -1)\n\t\t\t\t\t\t\t\t\t\t: []) as InferInputArray<BAS>,\n\t\t\t\t\t\t\t\t\tctx: currentCtx as Ctx,\n\t\t\t\t\t\t\t\t\tmetadata: args.metadata,\n\t\t\t\t\t\t\t\t};\n\n\t\t\t\t\t\t\t\t// If this action is stateful, server code function also has a `prevResult` property inside the second\n\t\t\t\t\t\t\t\t// argument object.\n\t\t\t\t\t\t\t\tif (withState) {\n\t\t\t\t\t\t\t\t\tscfArgs[1] = { prevResult: structuredClone(prevResult) };\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tconst data = await serverCodeFn(...scfArgs).catch((e) => framewo