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

Custom plugin support #1236

Closed ha1fstack closed 2 weeks ago

ha1fstack commented 2 weeks ago

Description

Currently the plugins are strongly integrated in the main package. It would be great being able to bring our own plugin as npm package. (The internal packages and plugins are needed to be published individually if so I guess?) For my use case, I am trying to use hey-api in corporate project and I want to generate custom outputs to suit the requirements.

similar issue but client: https://github.com/hey-api/openapi-ts/issues/1213

mrlubos commented 2 weeks ago

@ha1fstack yep! Are you able to use the Node.js runtime? Would something like this work?

import { myPlugin } from './src/my-plugin'

export default {
  client: '@hey-api/client-fetch',
  input: 'path/to/openapi.json',
  output: 'src/client',
  plugins: [
    myPlugin(),
    // ...other plugins
  ]
};

The implementation of myPlugin would use the same API as all other plugins. What are you trying to generate?

ha1fstack commented 2 weeks ago

I'm trying to generate custom fetchers for endpoints. I could use custom clients somehow but in some cases building up from scratch would better suit the needs.

Would something like this work?

That's what I meant 🙏 then it would be possible to create plugins locally or publish plugin packages separately. If so then the internal interfaces, generators, clients (internal or custom if custom client feature is implemented) needed to be exposed so we can create our own plugin implementations like internal ones. Also... might be better to separate core module and internal plugin/client as individual package 🤔 Explict import of each plugin might clarify things and it would be easier to implement flexible plugin interface (ie. passing options to plugin) => nvm I got confused 😅

mrlubos commented 2 weeks ago

Yep!

...separate core module and internal plugin/client as individual package...

This is not currently planned. I am not convinced that explicit imports would provide additional clarity over a list of plugins. What do you mean by implementing flexible plugin interfaces? You can already pass plugin-specific options to each

mrlubos commented 2 weeks ago

@ha1fstack I might get this in if you're interested, with the caveat that it's not going to be a stable API right now. The main thing that might change is the context object and its options. Are you okay with that?

ha1fstack commented 2 weeks ago

Sure! Thanks for prompt response 🙏

mrlubos commented 2 weeks ago

@ha1fstack aside, is there any way you could promote Hey API within your open source projects? I see you've got Slash, a collection of packages used in Toss. Is Hey API one of the packages you use?

Also feel free to ask any other questions about company-specific implementation. Would love to help you make the most of this codegen

mrlubos commented 2 weeks ago

@ha1fstack check it out! https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/test/plugins.spec.ts#L229-L254

I won't be promoting it anywhere and the types aren't fully there yet, but it works 🎉 let me know if you've got any other questions, would love to see what you'll build with it!