有没有办法使用单个装饰器隐藏controller.ts中的所有端点?

Vig*_*esh 5 nestjs nestjs-swagger

目前,我在所有方法之上使用 @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 {
    ......
  }

}
Run Code Online (Sandbox Code Playgroud)

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

מנח*_*רקי 11

要隐藏controller.ts中的所有端点,您必须使用ApiExcludeController而不是示例中的ApiExcludeEndpoint。

https://docs.nestjs.com/openapi/decorators

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,
  ApiExcludeController
  // ApiExcludeEndpoint
} from '@nestjs/swagger';


@Controller()
@ApiTags('Resources')
@ApiSecurity('apiKey')
@ApiExcludeController()
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 {
    ......
  }

}
Run Code Online (Sandbox Code Playgroud)


TPo*_*hel 10

一种可能性是明确包含您想要包含在 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);
Run Code Online (Sandbox Code Playgroud)

如果没有显式include:[]属性,LionsModuleTigersModule、 和BearsModule将自动包含在内。