Skip to content

Commit

Permalink
fix error response bug due to Response inside another Response (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo authored Dec 2, 2023
1 parent 4f25be7 commit e9fd215
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
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 });
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

0 comments on commit e9fd215

Please sign in to comment.