'Is there a way to hide all the end-point in the controller.ts using a single decorator?

Currently, I am using @ApiExcludeEndpoint() ### on top of all methods to hide the end-point in the swagger-ui, like this:

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 {
    ......
  }

}

I Need to know is there a way to hide all the end-point in the controller using a single decorator, I checked some documents it says to use @ApiIgnore() and @Hidden() but I can't find those in nestjs-swagger. Please comment on this



Solution 1:[1]

One possibility is to explicitly include the modules that you'd like to include in the swagger docs instead of just "including all modules" by default. Example:

  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);

Without the explicit include:[] property, LionsModule, TigersModule, and BearsModule would be automatically included.

Solution 2:[2]

To hide all the end-point in the controller.ts, you must use ApiExcludeController instead of ApiExcludeEndpoint as in the example.

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 {
    ......
  }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TPoschel
Solution 2