-
Notifications
You must be signed in to change notification settings - Fork 2
Generate MSW Handlers #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
w0(`${isQuery ? "?" : ""}: `); | ||
schemaToTypes(param.schema, io); | ||
w(","); | ||
// To generate separate path and query params |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This bit will generate separate query and path params. All we have to do is turn it on and replace the other references.
/** | ||
* Custom transformer: convenience function for setting response \`status\` and/or | ||
* \`delay\`. | ||
* | ||
* @see https://mswjs.io/docs/basics/response-transformer#custom-transformer | ||
*/ | ||
export function json<B>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ripped some of the utilities out of the mock-api
lib and re-exported them back over there. There's probably a better/cleaner way to share these.
const successResponse = | ||
conf.responses["200"] || | ||
conf.responses["201"] || | ||
conf.responses["202"] || | ||
conf.responses["204"]; | ||
|
||
const successTypeRef = contentRef(successResponse); | ||
const successType = successTypeRef | ||
? "Api." + refToSchemaName(successTypeRef) | ||
: "void"; | ||
|
||
const bodyTypeRef = contentRef(conf.requestBody); | ||
const bodyType = bodyTypeRef ? refToSchemaName(bodyTypeRef) : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could probably be dried up, it's basically copied directly from the API generator.
function validateParams<S extends ZodSchema>(schema: S, req: RestRequest) { | ||
const rawParams = new URLSearchParams(req.url.search) | ||
const params: [string, unknown][] = [] | ||
// Ensure numeric params like \`limit\` are parsed as numbers | ||
for (const [name, value] of rawParams) { | ||
params.push([name, isNaN(Number(value)) ? value : Number(value)]) | ||
} | ||
const result = schema.safeParse({ | ||
...req.params, | ||
...Object.fromEntries(params), | ||
}) | ||
if (result.success) { | ||
return { params: result.data } | ||
} | ||
return { paramsErr: json(result.error.issues, { status: 400 }) } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@david-crespo you'd mentioned cleaning this up in the other PR. I did clean up the types though it's still a bit mixed together. Once we separate out the query and the path params this'll become two functions and I think it'll be cleaner.
"dist/Api.d.ts", | ||
"dist/Api.js", | ||
"dist/Api.mjs", | ||
"dist/chunk-XYDDUAPD.mjs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and disabled chunking. It was getting kinda noisy.
eb81e21
to
bc1ac6c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heroic. let's party
This adds generation for MSW's handler scaffolding. Ultimately it generates two things:
makeHandlers
which takes...MSWHandlers
which defines the types for more ergonomic, custom handlers.makeHandlers
generates the array ofrest.[get|put|post|etc]
handlers which internally take care of setting up the route and validating params and the body. It also wraps the provided handler for error handling and output formatting.In this new setup we'll define handlers by their operation name (which should match up with the sdk). Error handling is done via exceptions. So
lookupOrg
will throw if the org isn't found instead of returning a result. This reduces a ton of boilerplate. Likewise instead of returningres(json(blah))
you can just return values directory.A handler can return
ResponseTransformer
(e.g.json()
)A handler can throw
ResponseTransformer
(e.g.json()
)I've laid the groundwork to separate path and query params though I haven't actually done that yet. Hopefully we can tackle that as a follow up. There's a console PR that will be the pair of this that has the real implementation of the MSW setup.