// Server actions answer forms with an ActionState: an error to show, or a notice. import "server-only"; import { ApiError } from "./api"; export type ActionState = { error?: string; notice?: string } | null; /** Runs fn, turning the API's errors into a message for the form; fn may return a notice. */ export async function attempt(fn: () => Promise): Promise { try { const notice = await fn(); return notice ? { notice } : {}; } catch (e) { if (e instanceof ApiError) return { error: e.message }; throw e; } } /** A form field as a string ("" when missing). */ export function field(form: FormData, name: string): string { const v = form.get(name); return typeof v === "string" ? v.trim() : ""; }