0

目前,我在所有方法之上使用 @ApiExcludeEndpoint() ### 来隐藏 swagger-ui 中的端点,如下所示:

import { Controller, Get, Query, Param } from '@nestjs/common';
import { ResourceService } from './resource.service';
import { Auth } from 'src/auth/auth.decorator';
import {
  ApiTags,
  ApiSecurity,
  ApiOkResponse,
  ApiForbiddenResponse,
  ApiCreatedResponse,
  ApiExcludeEndpoint
} from '@nestjs/swagger';


@Controller()
@ApiTags('Resources')
@ApiSecurity('apiKey')
export class ResourceController {
  constructor(private readonly resourceService: ResourceService) {}

  @Get('get_url')
  @ApiExcludeEndpoint()
  @Get()
  @ApiOkResponse({
    description: 'Resources list has succesfully been returned',
  })
  @ApiForbiddenResponse({ description: 'You are not allowed' })
  @Auth(...common_privileges)
  findAll(@Query() query: any): any {
    ......
  }

  
  @Get('get_url/:id')
  @ApiExcludeEndpoint()
  @ApiOkResponse({ description: 'Resource has succesfully been returned' })
  @ApiForbiddenResponse({ description: 'You are not allowed' })
  @Auth(...common_privileges)
  findById(@Param('id') id: string, @Query() query: any): any {
    ......
  }

}

我需要知道有没有办法使用单个装饰器隐藏控制器中的所有端点,我检查了一些文件说要使用 @ApiIgnore() 和 @Hidden() 但我在nestjs中找不到那些-昂首阔步。请对此发表评论

4

1 回答 1

1

一种可能性是在 swagger 文档中明确包含您想要包含的模块,而不是默认情况下仅“包含所有模块”。例子:

  const options = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .build();

  const catDocument = SwaggerModule.createDocument(app, options, {
    include: [LionsModule, TigersModule], // don't include, say, BearsModule
  });
  SwaggerModule.setup('api/cats', app, catDocument);

如果没有显式include:[]属性,LionsModule,TigersModuleBearsModule将自动包含在内。

于 2021-06-14T13:15:11.987 回答