SkeLLLa / fastify-oas

Fastify OpenAPI plugin.
https://www.comebackalive.in.ua/
MIT License
72 stars 20 forks source link

Generate spec to a file without running the instance #56

Closed carlosfaria94 closed 3 years ago

carlosfaria94 commented 3 years ago

Hello,

Thank you for your contribution @SkeLLLa

I have a question. It's possible to generate a spec file/documentation/json to file system? I need it to generate a OpenAPI client in a CI pipeline.

Currently, I'm doing like this:

export const exportOpenApiSpec = (): void =>
  app.listen(port, host, (err) => {
    if (err) {
      app.log.error(err)
      process.exit(1)
    }

    const spec = JSON.stringify(app.oas())
    writeFile('api-client/spec.json', spec, (err) => {
      if (err) return app.log.error(err)
      app.log.info('Exported with success')
      process.exit(0)
    })
  })

It is possible to do it without running a fastify instance?

Thank you

SkeLLLa commented 3 years ago

Hi. I don't think that it's possible since in order to construct openapi fastify needs to be init all routes, get schemas and so on. You probably can try to create ci-specific instance with only fastify itself and this plugin, start it get get those files. However will that work or not heavily depends on what you've got in your code.

carlosfaria94 commented 3 years ago

Thanks @SkeLLLa

For now my solution works, it starts, generate the spec to a file and stops the instance, so I will keep it 👍