SPB Git
1.1 KB · 18 lines typescript
1import { z } from "zod";2import { requireUser, getWorkspace, jsonError, HttpError } from "@/lib/auth/guard";3import type { SessionUser } from "@/lib/auth/session";4/** Standard handler wrapper: auth + workspace + error mapping. */5export function handler(fn: (ctx: { req: Request; user: SessionUser; ws: { id: string; ownerId: string; name: string }; params: Record<string, string> }) => Promise<Response>) {6  return async (req: Request, routeCtx?: { params: Promise<Record<string, string>> }) => {7    try { const user = await requireUser(req); const ws = await getWorkspace(user.id); const params = routeCtx ? await routeCtx.params : {}; return await fn({ req, user, ws, params }); }8    catch (e) { return jsonError(e); }9  };10}11export async function parseBody<T extends z.ZodTypeAny>(req: Request, s: T): Promise<z.infer<T>> {12  const j = await req.json().catch(() => { throw new HttpError(400, "Invalid JSON body"); });13  const r = s.safeParse(j); if (!r.success) throw new HttpError(400, "Validation: " + r.error.issues.map((i) => `${i.path.join(".")} ${i.message}`).join("; "));14  return r.data;15}16export const uuid = z.string().uuid();17export { HttpError };18
Files · main
Menu

Theme