Files
Epicnabbo-Catalogus-Updated…/src/lib/safe-action-shared.ts
T
SimoandCursor 830d252346 chore: upgrade Zod 4, Vitest 4, and TypeScript 7
Bump major tooling stacks and adapt Zod error APIs (issues/flattenError) plus tsconfig paths for TS 7 baseUrl removal.

Co-authored-by: Cursor <[email protected]>
2026-07-21 20:34:29 +02:00

38 lines
1.0 KiB
TypeScript

import { ZodError, z } from "zod";
import { handleActionError as foundationHandle } from "@/lib/foundation/action";
export type ActionResult<T = Record<string, unknown>> =
| { ok: true; data?: T }
| { ok: false; error: string; fieldErrors?: Record<string, string[]> };
export function actionOk<T = Record<string, unknown>>(
data?: T,
): ActionResult<T> {
return { ok: true, data: data ?? ({} as T) };
}
export function actionError(message: string): ActionResult<never> {
return { ok: false, error: message };
}
export class ActionError extends Error {
constructor(message: string) {
super(message);
this.name = "ActionError";
}
}
export function handleActionError(error: unknown): ActionResult<never> {
if (error instanceof ZodError) {
return {
ok: false,
error: "Validation failed",
fieldErrors: z.flattenError(error).fieldErrors as Record<string, string[]>,
};
}
if (error instanceof Error && error.name === "ActionError") {
return { ok: false, error: error.message };
}
return foundationHandle(error) as ActionResult<never>;
}