Local Build and Deploy / deploy (push) Successful in 58s
- Make @/lib/safe-action re-export from foundation layer so all 13+ existing server actions instantly get request tracing, rate limiting, and structured error handling without any code changes - Add HSTS, CSP, X-Frame-Options, X-Content-Type-Options to edge proxy (src/proxy.ts) — ran at Cloudflare/Vercel edge for all non-asset routes - Fix rate-limit.ts race condition: compute newCount before assignment to shrink the read-modify-write window; add memory-key prefix to avoid collisions with Redis keys - Add request body size limit (10 MB default) to api-handler.ts with per-route override via maxBodyBytes option - Remove unused imports and clean up backward-compat types
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { ZodError } 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: error.flatten().fieldErrors as Record<string, string[]>,
|
|
};
|
|
}
|
|
if (error instanceof Error && error.name === "ActionError") {
|
|
return { ok: false, error: error.message };
|
|
}
|
|
return foundationHandle(error) as ActionResult<never>;
|
|
}
|