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

Allow excluding inlined `enum` from type generation #1307

Open KiwiKilian opened 3 days ago

KiwiKilian commented 3 days ago

Description

Consider this schema:

{
  "components": {
    "schemas": {
      "SegmentFeature": {
        "required": ["type"],
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": ["Feature"]
          }
        }
      }
    }
  }
}

Which Generates this code intypes.gen.ts:

export type type = 'Feature';

export const type = {
  FEATURE: 'Feature',
} as const;

export type SegmentFeature = {
  type: 'Feature';
};

While the name type is problematic (see #1281), I would also like disable generation of the first two types of such inline enums (enums, which are not themself schema).

It is currently not possible to exclude this generation. input.include isn't even called on the inline enum by itself.

So either have some configuration for such inline enums or this could be also part of a generalized approach?

Preferred output in this case would be to have the two first type declarations removed:

- export type type = 'Feature';

- export const type = {
-   FEATURE: 'Feature',
- } as const;

export type SegmentFeature = {
  type: 'Feature';
};
mrlubos commented 3 days ago

Will add – suggestions for config naming are welcome!

KiwiKilian commented 3 days ago

TBH naming this is is quite hard... I guess it should live within @hey-api/types? One option could be to extend enums to an object?

{
  name: '@hey-api/types';
  enums: {
    emit: 'javascript' | 'typescript';
    include: 'ref' | 'inline' | 'all';
  },
}

But I actually don't see a use case for include: 'inline'. Also include is not perfect, because input.include is a Regex. Alternatives could be select or generateFrom.

Other options would be:

{
  name: '@hey-api/types';
  enums: {
    emit: 'javascript' | 'typescript';
    includeInline: boolean;
  },
}

or

{
  name: '@hey-api/types';
  enums: 'javascript' | 'typescript';
  includeInlineEnums: boolean;
}