kubb-labs / kubb

OpenAPI to TypeScript, React-Query, Zod, Zodios, Faker.js, MSW and Axios.
https://kubb.dev
MIT License
568 stars 40 forks source link

Override some field in specific type definition that was generated #1041

Open vlanemcev opened 1 month ago

vlanemcev commented 1 month ago

What is the problem this feature would solve?

Sometimes it happens that the swagger / open api schemes that are generated by the backend do not quite correspond to what the backend actually sends as a response. Because of this, sometimes you need to help the typescript plugin correctly define/override types for models/operations, etc. Changing the schema file itself is not an option because it itself is automatically generated on the backend side and is dynamically generated.

For example, after running Kubb generation with pluginTs I take this type:

type Generated = {
 fieldA: number | string;
 fieldB: string;
}

And I want to have override the type definition that will be generated for this type Generated. For example, I want fieldA to be a string and not a number | string.

As I know, the Kubb already has this possibility for Zod plugin by mapping field inside plugin configurator, something like this:

mapper: {
    balance_history: "z.array(z.tuple([z.number(), z.number()]))", // we're overriding the resulted schema
}

According to my feeling and already using generation in projects, I can say that this is the most requested feature.

External documents/projects?

No response

What is the feature you are proposing to solve the problem?

Add some mappers (similar to the Zod plugin) to be able to override generated typescript definitions.

What alternatives have you considered?

I have not found any alternative solutions to solve this other than defining the type on my side and using it.

stijnvanhulle commented 1 week ago

@vlanemcev I added the following pr: https://github.com/kubb-labs/kubb/pull/1075. The big difference with the Zod and Faker plugin is that you need to use the Factory functionality of TypeScript instead of returning string.

vlanemcev commented 1 week ago

@stijnvanhulle Nice, thanks so much for this functionality! I'll try it in my project.

Btw, could we update the doc regarding it? Because using mappers is unspecified there.. Thanks!

stijnvanhulle commented 3 days ago

@vlanemcev Hi, that is on purpose right now because the API is still incomplete. I will probably make some changes before v3 and then make it an official feature.