ecyrbe / zodios

typescript http client and server with zod validation
https://www.zodios.org/
MIT License
1.7k stars 46 forks source link

Generate enum and union with zodios #530

Closed palaniichukdmytro closed 1 year ago

palaniichukdmytro commented 1 year ago

I think it's common issue for generate type from open api spec. I am using with zodios https://github.com/astahmer/openapi-zod-client/ openapi-zod-client. But did not found any possible solution to generate enum , instead I see that only union types is possible . Any idea to generate TS enum to use this enum in a code durin compile time .

Example schema json

 {
            "name": "format",
            "in": "query",
            "required": false,
            "schema": {
              "enum": [
                "flat",
                "grouped"
              ],
              "type": "string",
              "default": "flat",
              "title": "Format"
            }

By default it's generated to union with type flag enabled or via handlebars templates you can do your self. But it is not possible to generate TS enum.

Output:

export const Format= z.enum([
  'Flat',
  'Grouped',
])
export type FormatType = z.infer<typeof Format> // union one

Exacted/Desire output

export enum FormatEnum {
Flat = 'Flat', 
Grouped = 'Grouped'
}
palaniichukdmytro commented 1 year ago

not zodios issue