openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

transformer for dateTime

Open CorneThoes opened this issue 9 months ago • 5 comments

Description

Hey there! I did not find any documentation about custom transformers, is there a wat for me to write one that generates dateTime(luxon) types?

Thanks in avances!

CorneThoes avatar Apr 13 '25 13:04 CorneThoes

Not at the moment @CorneThoes :( I'm pretty sure someone asked about this before though and it's a good feature request

mrlubos avatar Apr 13 '25 20:04 mrlubos

@mrlubos I think it would be great idea to provide an interface in the plugin settings to transform certain types on the fly:

Something like that: (pseudocode)

 {
      name: '@hey-api/transformers',
      dates: true,
      transformations: [
       {
         // matching part
          type: 'string',
          format: 'date-time',

        // implementation part
          transformer: (value: unknown) {
               return new Date();
          },
          transformedTypeName: 'Date',
        }
      ]
 },

In that case, you only would need to bother only with generating the basic transformation template and make it correct for different nesting configurations of the fields, while consumers will create the transformations on their own.

It's easier said than done, since it will require to think through how to set resulting types (which will be required even for global types and if i understand correctly is the responsibility of the typescript plugin) and on top of that figure out how to add imports for types that are custom in the project.

But still, if that is achieved you won't need to bother with the implementations of transformations.

Freddis avatar Apr 15 '25 19:04 Freddis

for the reference purpose: I think, that's what I was asking for here: https://github.com/orgs/hey-api/discussions/1039#discussioncomment-11194255

mrclrchtr avatar Apr 24 '25 23:04 mrclrchtr

@mrclrchtr sorry for being so slow fixing this!

mrlubos avatar Apr 25 '25 00:04 mrlubos

No, no problem. I have also not created an issue because it was out of my focus. I stumbled across it again today by chance.

Thanks for active maintaining!

mrclrchtr avatar Apr 25 '25 00:04 mrclrchtr

I have a related feature request: transformers for date don't modify type: string + format: date fields, or if they do, serialize them properly.

turning on date transformers means when you have a date string schema like this:

        dateOfBirth:
          type: string
          format: date
          description: The patient's date of birth in the format YYYY-MM-DD.
          example: "1980-01-01"

and a server that only accepts RFC 3339 short dates, that server would reject what this library serializes, which appears to default to the JSON serializer for Date.

thyming avatar Sep 26 '25 15:09 thyming