mocks-server / main

Node.js mock server running live, interactive mocks in place of real APIs
https://www.mocks-server.org
Apache License 2.0
281 stars 14 forks source link

quicktype for automated mocks? #479

Open andrewluetgers opened 1 year ago

andrewluetgers commented 1 year ago

Is your feature request related to a problem? Please describe. I have been working on creating mock APIS (full CRUD support) in my own implementations with MSW for a few years and have been wondering how much can I automate this and I would like to share a couple thoughts and hear if any of this makes sense for mocks-server.

Describe the solution you'd like I have been following Miragejs and MSW/Data for a while and have recently stumbled upon quicktype and Zod-Mock. Quicktype is amazing. Whereas Miragejs and MSW/Data core value IMHO is in data modeling such that you can produce an interactive mocked API I believe this should be automated. Ideally we should be able to just look at network activity and infer the data model. This is exactly what quicktype enables. Further its does codegen, so we can generate zod code and leverage zod-mock to dynamically respond with faker-js mocked data that passes the type validation of the inferred type.

Additional context Beyond just automatically mocking the data with a type-valid shape I can foresee this enabling monitoring network traffic on the fly in (certainly in dev but perhaps in other contexts) to gen types on the fly and watch for type changes as more routes are seen. Quicktype can make a single type that incorporates multiple examples.

There is probably a path towards automating the transformation/mutation bits as well but thats a deeper discussion and I just want to introduce this idea in its simplest form first.

javierbrea commented 1 year ago

Hi @andrewluetgers , very interesting. Thank you!

Maybe this could be implemented in a plugin somehow. A separated process could previously look at the network activity and generate the routes for the Mocks Server, or even the plugin could create a "proxy", so all of the traffic to the original API goes through it in a previous iteration, which would make easier to it to collect the needed information to generate the routes. So, the sequence would be as simpler as:

Sounds great! 😃