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

3

6

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

Acree answered 2/3, 2021 at 9:50 Comment(0)
C
11

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.

Cham answered 14/6, 2021 at 13:15 Comment(0)
R
12

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

}
Roslyn answered 27/4, 2022 at 10:31 Comment(0)
C
11

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.

Cham answered 14/6, 2021 at 13:15 Comment(0)
D
2

You can hide all endpoints inside your controller with @ApiExcludeController(true) decorator:

@ApiExcludeController(true)
@Controller('cats')
export class CatsController {}
Diathermy answered 28/8, 2023 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.