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

Use date transformer without creating transformer functions #1214

Open georgesmith46 opened 3 weeks ago

georgesmith46 commented 3 weeks ago

Description

When using the new plugins config, I am unable to use the date transformer without also creating the transformer functions. I only need the types for my use case. This was possible in versions <0.54.0

Example OpenAPI document

openapi: 3.0.3
info:
  title: My API
paths:
  /foo:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MyType'
components:
  schemas:
    MyType:
      properties:
        foo:
          type: string
          format: date-time
      type: object

v0.53.11

export default defineConfig({
    client: "@hey-api/client-fetch",
    input: "...",
    output: {
        path: "./generated",
    },
    types: {
        dates: true,
        tree: false,
    },
    services: false,
    schemas: false,
});
export type MyType = {
    foo?: Date;
};

v0.54.1

export default defineConfig({
    client: "@hey-api/client-fetch",
    input: "...",
    output: {
        path: "./generated",
    },
    plugins: [
        {
            tree: false,
            name: "@hey-api/types",
        },
        {
            dates: true,
            name: "@hey-api/transformers",
        },
    ],
});
export type MyType = {
    foo?: Date;
};

export type GetFooResponseTransformer = (data: any) => Promise<GetFooResponse>;

export type MyTypeModelResponseTransformer = (data: any) => MyType;

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

export const GetFooResponseTransformer: GetFooResponseTransformer = async (data) => {
    MyTypeModelResponseTransformer(data);
    return data;
};

Is there a way to get the same output as v0.53.11 in the new plugins config?

mrlubos commented 3 weeks ago

@georgesmith46 Can you describe your use case? The feedback I've gotten previously on this feature was that it's useless without actually transforming the values

georgesmith46 commented 3 weeks ago

I generate zod schemas from these type files which handles the transform. I just found #876 though which may achieve the same result

mrlubos commented 3 weeks ago

@georgesmith46 In the meantime, you could always disable the transformer by overriding it with null/undefined. Are you able to share an example Zod schema where you do this transform?

georgesmith46 commented 3 weeks ago

So this is for a service, the OpenAPI document is the source of truth and then the types/zod schemas are generated from the document.

I'm using ts-to-zod to generate the zod schemas after generating the types. I then use these schemas to parse incoming requests and coerce the ISO dates into date objects.