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

`transformer` "transforms" dates unintentionally #1260

Open mrclrchtr opened 1 week ago

mrclrchtr commented 1 week ago

Description

Even with dates switched off, transformers are created for the dates and the dates are “passed” through the transformers.

At the end they are strings again. The types do not have the type Date.

The problem is that I want to have a string like this for date: 2024-10-10. However, I get 2024-10-10T00:00:00.000Z.

This is the server response:

"date": "2024-10-10",

This happens because of:

export const XXXModelResponseTransformer: XXXModelResponseTransformer =
  (data) => {
    if (data?.date) {
      data.date = new Date(data.date);
    }
    return data;
  };

My config:

export default defineConfig({
  input: "../../specifications/service.yaml",
  output: {
    path: "src/generated/client/service",
    format: "prettier",
    lint: "eslint",
  },
  plugins: [
    "@hey-api/schemas",
    {
      asClass: true,
      name: "@hey-api/services",
    },
    {
      enums: "typescript",
      name: "@hey-api/types",
    },
    {
      dates: false,
      name: "@hey-api/transformers",
    },
    "@tanstack/react-query",
  ],
  client: "@hey-api/client-fetch",
});

Reproducible example or configuration

No response

OpenAPI specification (optional)

e.g.:

        orderDate:
          type: string
          format: date

System information (optional)

"@hey-api/client-fetch": "0.4.2", "@hey-api/openapi-ts": "0.54.4",

mrlubos commented 1 week ago

That's a good feedback. I will change it as you describe, though I wonder why you use the plugin at all if you don't want to transform dates?

The problem is that I want to have a string like this for date: 2024-10-10. However, I get 2024-10-10T00:00:00.000Z.

I don't fully understand this. How do you end up with a string if the transformer gives you a date?

mrclrchtr commented 1 week ago

Yes, I turned off the transformer yesterday and realized that this fixed my problem without losing other functions.

I thought the transformer transformed more than just dates. I wanted to be as typed as possible.