honojs / middleware

monorepo for Hono third-party middleware/helpers/wrappers
https://hono.dev
451 stars 161 forks source link

[Suggestion] Parse OpenAPI specs to YAML on {route}.yml (zod-to-openapi) #163

Open rafaell-lycan opened 1 year ago

rafaell-lycan commented 1 year ago

Hi,

I've been playing with Hono (with Node) for a few days at work and we've seen some good performance from it.

After testing zod-to-openapi I realized that we could add an additional route with the extension .yml to output the specs as YAML when needed.

e.g.: The example below generates both /docs and /docs.yml routes

const app = new OpenAPIHono()
app.doc('/docs', swaggerOptions)

Reason The scenario would be using it with an Online Swagger Editor or simply connecting to your editor of choice.

For performance reasons, it only parses it when the route is triggered:

Example

handler.openapi(routeSchama, async (c) => {
  const YAML = await import('js-yaml');
  const result = YAML.dump(json)

  return c.text(result)
})

Also, YAML parsing could be a new Hono helper such as hono/html if it becomes useful for more devs.

yusukebe commented 1 year ago

Hi @rafaell-lycan

Thanks the suggestion.

I think you are right that we often create endpoints that output YAML. But in that case, I don't think it's necessary to go to the effort of inclusion in the Hono ecosystem, since we can simply use js-yaml in that case, in my opinion.