Nestjs Swagger - 在不同的路线上发布不同的 API 文档

Posted

技术标签:

【中文标题】Nestjs Swagger - 在不同的路线上发布不同的 API 文档【英文标题】:Nestjs Swagger - Publishing different API docs on separate routes 【发布时间】:2020-05-11 03:18:27 【问题描述】:

我正在构建一个具有公共 API 和内部 API 的应用。我想将这些文档发布到不同的路线。我认为这可以通过仅在文档中添加某些标签 (addTag) 来完成,但在进一步阅读和实验之后,它并没有完成这项工作。

文档总是包含所有内容,来自所有模块的所有记录端点。

这甚至可能吗?如果有,怎么做?

我不认为代码是必要的,但 FWIW:

const pubOptions = new DocumentBuilder()
    .setTitle('Pub API Docs')
    .setDescription('Blah blah API documentation')
    .setVersion(p.version)
    .addBearerAuth()
    .addTag('public-app')
    .build();
const document = SwaggerModule.createDocument(app, pubOptions);
SwaggerModule.setup('public-api', app, document);

const internalOptions = new DocumentBuilder()
    .setTitle('Internal API Docs')
    .setDescription('Blah blah API documentation')
    .setVersion(p.version)
    .addBearerAuth()
    .addTag('internal')
    .build();
const iDocument = SwaggerModule.createDocument(app, internalOptions);
SwaggerModule.setup('internal-api', app, iDocument);

【问题讨论】:

【参考方案1】:

您需要告诉SwaggerModule.createDocument 函数要组合的swagger 中包含哪些模块。 Here are the related docs。作为第三个参数,您可以传入一个带有 include 属性的对象,该属性包含一个模块数组。这些模块就是与即将构建的 swagger 文档相关的模块。

【讨论】:

根据这个 PR ,exclude 选项将作为补充添加

以上是关于Nestjs Swagger - 在不同的路线上发布不同的 API 文档的主要内容,如果未能解决你的问题,请参考以下文章

@nestjs/swagger:是不是可以防止网络钓鱼?

@nestjs/swagger 没有设置授权标头

如何在nestjs中禁用swagger进行生产

NRWL + NestJS 尝试使用 Swagger 插件

NestJS/swagger:ApiExtraModel 期望啥模型作为参数?

@nestjs/swagger:如何添加 API URL?