mantis-app
Version:
M.A.N.T.I.S (MongoDB, Analog, Nx, Tailwind CSS, Ionic, Storybook) is not just a CLI tool; it's your passport to a seamless full-stack project launch.
19 lines (17 loc) • 481 B
text/typescript
import { readBody, H3Event, EventHandlerRequest, createError } from 'h3';
import type { z } from 'zod';
export const parseBody = async <T extends z.ZodSchema>(
event: H3Event<EventHandlerRequest>,
schema: T,
): Promise<z.output<T>> => {
const body = await readBody(event);
const result = schema.safeParse(body);
if (result.success) {
return result.data;
} else {
throw createError({
statusCode: 400,
message: 'Invalid request body',
});
}
};