创建swagger.在项目根目录下为NestJS应用程序创建一个yaml文件



我使用NestJS创建一个BE应用程序,我使用下面的语法使用swagger模块启用了swagger

const document = SwaggerModule.createDocument(app, config);  
SwaggerModule.setup('api', app, document);

我的问题是是否有一种方法将文件转换/保存为根文件夹中的.yaml文件?因此,GitLab repo上的任何成员都可以在不运行应用程序的情况下查看应用程序的api和dto。

我找到了一个使用json-to-pretty-yaml包的解决方案,所以现在我可以使用YAML.stringify(document)将文档转换为字符串yaml,然后将输出保存到。yaml文件。

import * as fs from 'fs';
import * as YAML from 'json-to-pretty-yaml';
....
....
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);

const data = YAML.stringify(document);
fs.writeFile("swagger.yaml", data, (err) => {
if (err)
console.log(err);
else {
console.log("swagger.yaml file has been updated successfullyn");
}
});

最新更新