Files
jamoke/src/lib/server/getRequestBody.ts

17 lines
354 B
TypeScript

export async function getRequestBody<T = unknown>(
req: Request,
validation?: (target: unknown) => target is T,
): Promise<T> {
if (req.body === null) {
throw new Error("no body is present on the request");
}
const body = await req.json();
if (validation && !validation(body)) {
throw new Error("body validation failed");
}
return body;
}