Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions client/msw-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,13 +1025,6 @@ export interface MSWHandlers {
}) => Promisable<StatusCode>;
}

function validateBody<S extends ZodSchema>(schema: S, body: unknown) {
const result = schema.transform(snakeify).safeParse(body);
if (result.success) {
return { body: result.data as Json<z.infer<S>> };
}
return { bodyErr: json(result.error.issues, { status: 400 }) };
}
function validateParams<S extends ZodSchema>(
schema: S,
req: Request,
Expand Down Expand Up @@ -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
Expand Down
18 changes: 7 additions & 11 deletions generator/client/msw-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,6 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document) {
w("}");

w(`
function validateBody<S extends ZodSchema>(schema: S, body: unknown) {
const result = schema.transform(snakeify).safeParse(body);
if (result.success) {
return { body: result.data as Json<z.infer<S>> }
}
return { bodyErr: json(result.error.issues, { status: 400 }) }
}
function validateParams<S extends ZodSchema>(schema: S, req: Request, pathParams: PathParams) {
const rawParams = new URLSearchParams(new URL(req.url).search)
const params: [string, unknown][] = []
Expand Down Expand Up @@ -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 });

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bodyErr is already a Response, so we were seeing {} as the response body in the browser because JSON.stringify(new Response()) == '{}'.

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
Expand Down