NestJs Swagger-是否可以在NestExpressApplication中列出所有Swagger文档?
我正在将NestJs与SwaggerModule一起使用,我希望在应用程序初始化后列出所有文档 比如:NestJs Swagger-是否可以在NestExpressApplication中列出所有Swagger文档?,swagger,nestjs,Swagger,Nestjs,我正在将NestJs与SwaggerModule一起使用,我希望在应用程序初始化后列出所有文档 比如: [ { title: 'Admin', description: 'Admin endpoints', version: '1.2.3', url: 'admin/123/' }, { title: 'Resources', description: 'Resources endpoints', version: '1.2.5
[
{
title: 'Admin',
description: 'Admin endpoints',
version: '1.2.3',
url: 'admin/123/'
},
{
title: 'Resources',
description: 'Resources endpoints',
version: '1.2.5',
url: 'admin/125/'
}
]
是否有一个对象我可以访问,它包含这些文档的信息,还是我必须自己创建它?我刚刚浏览了
NestJS
文档,似乎无法在运行时添加提供程序。但这并非不可能
SwaggerDocumentService
@Injectable()
导出类swagger文档服务{
私有文件:数组;
获取swaggerDocuments():数组{
归还此文件;
}
AddSwiggerDocument(值:忽略):无效{
这。_swaggerDocuments.push(value);//您可能希望使用不变的方式,但只是为了给您一个想法
}
}
SwaggerDocumentModule
,并将其设置为全局。然后在SwaggerDocumentModule
@Global()
@模块({
提供者:[炫耀文档服务],
导出:[炫耀文档服务]
})
导出类swagger文档模块
AppModule
@模块({
...
导入:[炫耀文档模块]
})
导出类AppModule
main.ts
中,抓取SwaggerDocumentService
的实例并设置文档async bootstrap(){
const app=wait NestFactory.create(AppModule);
const-swaggerDocumentService=app.get(swaggerDocumentService);//可能需要检查null
//使用DocumentBuilder设置选项
const options=new DocumentBuilder()。。。;
swaggerDocumentService.addSwaggerDocument(选项);
}
SwaggerDocumentService
@Injectable()
导出类应用程序服务{
构造函数(私有只读swaggerDocumentService:swaggerDocumentService){
Swagger DocumentService.Swagger文档;//将是Swagger文档的数组
}
}
这不是您使用DocumentBuilder
构建的选项吗?是的,但是有没有办法从app.service.ts
中访问此信息?