astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)
openapi-zod-client.vercel.app
788 stars 84 forks source link

Is there any way to make all output properties readonly? #214

Open craigmiller160 opened 1 year ago

craigmiller160 commented 1 year ago

I'm a strong proponent of immutable data structures. I would love if there was some way that the schemas generated would have all properties set to readonly. I'm currently exploring the API of this library, and I don't see a way to do this yet. Hoping there is a solution, or that a new argument can be added to the CLI.

Thanks.

astahmer commented 1 year ago

maybe you could do it with a custom handlebars template ?

another simple way would be to traverse your OpenAPI schemas and add readonly on each properties before passing that OpenAPI document to openapi-zod-client

craigmiller160 commented 1 year ago

The documentation on how to use the custom handlebars template is far from ideal. I've been trying to figure it out for a minute, I haven't used handlebars very much but it seems simple enough. The big issue for me is understanding all the different context variables that are available and how to use them. Again, I don't see good documentation about this.

As for marking things as readonly in the OpenAPI schema, that is something I hadn't considered. I will investigate this option.

craigmiller160 commented 1 year ago

I'm not seeing any easy ways with handlebars to customize things to the point of making all types readonly. Customizing the openapi schema isn't always an option as well. I'm really hoping that this feature could be added in the future.

astahmer commented 1 year ago

Customizing the openapi schema isn't always an option as well.

what's the issue with your case ?

I'm really hoping that this feature could be added in the future.

feel free to send a PR 🙏

craigmiller160 commented 1 year ago

feel free to send a PR 🙏

Thanks for the invitation. Here is the PR adding the requested functionality: https://github.com/astahmer/openapi-zod-client/pull/221