在 NestJS 中将 Swagger 文档生成为 JSON/YAML [英] Generate Swagger documentation as JSON/YAML in NestJS

查看:57
本文介绍了在 NestJS 中将 Swagger 文档生成为 JSON/YAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照 创建 Swagger 文档的说明,现在可以使用我的文档使用 Swagger UI.我还想将文档生成为 JSON 或 YAML,因此很容易导入,例如Postman,但我在 SwaggerModule 中找不到任何合适的方法,Swagger UI 也没有任何导出按钮.

I've followed the instructions to create a Swagger documentation, and my documentation is now available using Swagger UI. I'd like to also generate the documentation as JSON or YAML so it's easy to import in e.g. Postman, but I can't find any suitable methods in the SwaggerModule, nor does the Swagger UI have any export button.

推荐答案

根据这个 github 问题 您可以将创建的 Swagger document 字符串化,例如像这样将其写入文件系统:

According to this github issue you can just stringify the created Swagger document and e.g. write it to the file system like this:

const app = await NestFactory.create(ApplicationModule);
const options = new DocumentBuilder()
    .setTitle("Title")
    .setDescription("description")
    .setVersion("1.0")
    .build();
const document = SwaggerModule.createDocument(app, options);

fs.writeFileSync("./swagger-spec.json", JSON.stringify(document));
SwaggerModule.setup("/api", app, document);

await app.listen(80);

这篇关于在 NestJS 中将 Swagger 文档生成为 JSON/YAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆