UNPKG

nuxt-procedures

Version:

Nuxt module to define and easily consume your backend API in a validated and type-safe way using procedures and Zod schemas.

22 lines (21 loc) 660 B
import { defineEventHandler, readBody } from "h3"; import z from "zod"; import superjson from "superjson"; export function defineProcedure({ input: inputSchema, output: outputSchema = z.void(), handler }) { const h = defineEventHandler(async (event) => { const input = inputSchema ? inputSchema.parse(superjson.deserialize(await readBody(event))) : void 0; const outputRaw = await handler(inputSchema ? { input, event } : { event }); const output = outputSchema.parse(outputRaw); return superjson.serialize(output); }); const procedure = h; procedure.__procedureMetadata = { inputSchema, outputSchema }; return procedure; }