diff --git a/client/msw-handlers.ts b/client/msw-handlers.ts index f380fa0..be337b7 100644 --- a/client/msw-handlers.ts +++ b/client/msw-handlers.ts @@ -1025,13 +1025,6 @@ export interface MSWHandlers { }) => Promisable; } -function validateBody(schema: S, body: unknown) { - const result = schema.transform(snakeify).safeParse(body); - if (result.success) { - return { body: result.data as Json> }; - } - return { bodyErr: json(result.error.issues, { status: 400 }) }; -} function validateParams( schema: S, req: Request, @@ -1083,10 +1076,13 @@ const handler = const { path, query } = params; - const { body, bodyErr } = bodySchema - ? validateBody(bodySchema, await req.json()) - : { body: undefined, bodyErr: undefined }; - if (bodyErr) return json(bodyErr, { status: 400 }); + let body = undefined; + if (bodySchema) { + const rawBody = await req.json(); + const result = bodySchema.transform(snakeify).safeParse(body); + if (!result.success) return json(result.error.issues, { status: 400 }); + body = result.data; + } try { // TypeScript can't narrow the handler down because there's not an explicit relationship between the schema diff --git a/generator/client/msw-handlers.ts b/generator/client/msw-handlers.ts index db1db45..4822724 100644 --- a/generator/client/msw-handlers.ts +++ b/generator/client/msw-handlers.ts @@ -119,13 +119,6 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document) { w("}"); w(` - function validateBody(schema: S, body: unknown) { - const result = schema.transform(snakeify).safeParse(body); - if (result.success) { - return { body: result.data as Json> } - } - return { bodyErr: json(result.error.issues, { status: 400 }) } - } function validateParams(schema: S, req: Request, pathParams: PathParams) { const rawParams = new URLSearchParams(new URL(req.url).search) const params: [string, unknown][] = [] @@ -168,10 +161,13 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document) { const { path, query } = params - const { body, bodyErr } = bodySchema - ? validateBody(bodySchema, await req.json()) - : { body: undefined, bodyErr: undefined }; - if (bodyErr) return json(bodyErr, { status: 400 }); + let body = undefined + if (bodySchema) { + const rawBody = await req.json() + const result = bodySchema.transform(snakeify).safeParse(body); + if (!result.success) return json(result.error.issues, { status: 400 }) + body = result.data + } try { // TypeScript can't narrow the handler down because there's not an explicit relationship between the schema