hey-api / openapi-ts

🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more. Support: @mrlubos
https://heyapi.dev
Other
1.39k stars 107 forks source link

[FeatureRequest] `transformer` for a custom `format` #1262

Open mrclrchtr opened 1 week ago

mrclrchtr commented 1 week ago

I would like to define a transformer for a custom format and this should be applied when a string has this format:

e.g.

export class LocalTime {
  hour: number;
  minute: number;

  constructor(hour: number, minute: number) {
    this.hour = hour;
    this.minute = minute;
  }
}
      time:
          type: string
          format: time

then I would like to be able to parse this string into LocalTime.

Maybe, I could create my own plugin for this?

If this doesn't fit here, I'll open a separate topic or issue.

Originally posted by @mrclrchtr in https://github.com/hey-api/openapi-ts/discussions/1039#discussioncomment-11194255

mrlubos commented 1 week ago

You'd also need to be able to transform the generated types, right? What would the appropriate type be in your example?

mrclrchtr commented 1 week ago

Unfortunately, I don't quite understand the question.

I'll try to explain my idea again:

proposal:

{
    name: '@hey-api/transformers',
    custom: {
        type: string,
        format: localTime,
        parser: {
            regex: "/^([01]?\d|2[0-3]):([0-5]\d)(?::([0-5]\d))?$/", # For demo only
        },
        target: {
            pathToCass: "/calendar/LocalTime", # to be used for the import
            constructor: ["$1", "$2"]
        }
    }
},

then the parser would know how to parse the string and the transformer would know which elemento to set where in the constructor.

I just thought of that quickly... there is certainly a better API for this.