News for hackers. Stuff that matters.
Re:Mock Service Worker(msw) releases v2.0 by ushakov on Monday October 23, 2023 @02:00PM (#37989240)
Amazing. At Step CI we’re currently working on a tool that will do the conversion automatically (OpenAPI > MSW)

Feel free to email me at [email protected] if you want to hear more!

Re:Mock Service Worker(msw) releases v2.0 by dcre on Monday October 23, 2023 @02:26PM (#37989595)
We more or less did that too. We generate typed wrappers for every handler, and then we manually implement the internal logic for each endpoint. I doubt reliably generating 100% of the logic is possible, but even if it is, I would guess it's more work to do that than to do it manually.

Generator: https://github.com/oxidecomputer/oxide.ts/blob/64401fa2/gene...

Generated output: https://github.com/oxidecomputer/console/blob/8e74accf/libs/...

Manually implemented endpoint behavior: https://github.com/oxidecomputer/console/blob/8e74accf/libs/...

Re:Mock Service Worker(msw) releases v2.0 by zellyn on Monday October 23, 2023 @03:49PM (#37990628)
I'm so glad to see all of your comments here. I've been fascinated by your setup ever since hearing about it on the Oxide and Friends podcast. The part that I'm most curious about is the part where you weave together auto-generated interface code with hand-implemented mock implementations… I'll have to go read your code and see how that happens.

[Edit, after reading a bit of the code]

Am I correct in understanding that the generated code maps API calls to a typed interface, and then the hand-built implementation simply implements that interface?

So rather than complex weaving where you want hand-coded function bodies but generated signatures, you have generated signatures, where each function body calls a method on a generated interface.

Re:Mock Service Worker(msw) releases v2.0 by dcre on Monday October 23, 2023 @04:06PM (#37990842)
Yeah, basically. We do it with a function call where the argument to the function is that interface representing all the API endpoints. `makeHandlers` handles parsing path params, query params, and request body and passes them to each endpoint handler. So the runtime validation of request bodies is also generated — we generate a zod schema for each request body in the OpenAPI definition and use it to parse the actual request body that comes in.

big function call https://github.com/oxidecomputer/console/blob/bd65b9da7019ad...

automatic body parsing and argument passing: https://github.com/oxidecomputer/console/blob/bd65b9da7019ad...

When an endpoint gets added to the spec, we can rerun the generator and get type errors in the `makeHandlers` telling us endpdoints are missing.

Re:Mock Service Worker(msw) releases v2.0 by wdb on Monday October 23, 2023 @05:24PM (#37991791)
Why not use @mswjs/source?
Re:Mock Service Worker(msw) releases v2.0 by adam_beck on Monday October 23, 2023 @10:52PM (#37994373)
I've seen you mention this a few times but can't seem to find any information on it. I'm very interested in being able to use HAR files with MSW.
Re:Mock Service Worker(msw) releases v2.0 by c01nd01r on Tuesday October 24, 2023 @04:01PM (#38004600)
Could you provide a link to this package? I can't seem to find anything.
Re:Mock Service Worker(msw) releases v2.0 by wdb on Monday October 30, 2023 @03:22PM (#38074390)
Looks like the repo is private. My apologies.