首页 文章

在NestJS中生成Swagger文档作为JSON / YAML

提问于
浏览
2

我已经关注了instructions to create a Swagger documentation,现在可以使用Swagger UI获取我的文档 . 我'd like to also generate the documentation as JSON or YAML so it'易于导入,例如邮差,但我在 SwaggerModule 中找不到任何合适的方法,Swagger UI也没有任何导出按钮 .

1 回答

  • 3

    根据这个github issue,您可以将创建的Swagger document 字符串化,例如将它写入文件系统,如下所示:

    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);
    

相关问题