UNPKG

@kellanjs/actioncraft

Version:

Fluent, type-safe builder for Next.js server actions.

43 lines 1.72 kB
import { INTERNAL_ERROR_TYPES } from "../types/errors.js"; import { UNHANDLED_ERROR } from "./errors.js"; /** * Converts input to a serializable format for the `values` field in action results. * * If the input is `FormData`, it is flattened into a plain object so that it can * be safely JSON-serialized. Otherwise, the input is returned as-is. */ export function serializeRawInput(input) { if (input instanceof FormData) { const valueMap = new Map(); for (const [key, value] of input.entries()) { // Ignore React server-action meta-fields if (key.startsWith("$ACTION")) continue; const stringValue = typeof value === "string" ? value : value.name || "[File]"; if (!valueMap.has(key)) valueMap.set(key, []); valueMap.get(key).push(stringValue); } // Collapse single-item arrays const serialized = {}; for (const [key, values] of valueMap.entries()) { serialized[key] = values.length === 1 ? values[0] : values; } return serialized; } // Non-FormData inputs are assumed to already be serialisable return input; } /** * Converts internal error objects into client-facing errors, hiding * implementation details that should not leak outside the server. */ export function convertToClientError(internalError) { if (internalError.type === INTERNAL_ERROR_TYPES.IMPLICIT_RETURN || internalError.type === INTERNAL_ERROR_TYPES.OUTPUT_VALIDATION || internalError.type === INTERNAL_ERROR_TYPES.INTERNAL_LOGIC) { return UNHANDLED_ERROR; } return internalError; } //# sourceMappingURL=transformation.js.map