nestia icon indicating copy to clipboard operation
nestia copied to clipboard

Watch for @tag and @security on the Controller class

Open loucass003 opened this issue 5 months ago • 3 comments

It is pretty common for a Controller to have the same security rules on all its endpoints Please allow us to add those annotations on the controller so we do not have to repeat it on all the endpoints.

Here is how a controller would look like

/**
 * Controller for the Libraries
 *
 * @security bearer
 * @tag library
 */
@Controller('library')
@Authenticated() < --- having @security on the controller would be usefull for this use case where a guard is there for all the endpoints
export class LibraryController {
  constructor(public libraryService: LibraryService) {}

  /**
   * List all libraries
   *
   * @security something-else   <---- allow for overide just in case, but still add @tag from the controller
   * 
   */
  @TypedRoute.Get('/')
  public async libraries(): Promise<Library[]> {
    return this.libraryService.getLibraries();
  }
  
  /**
   * Create a new library and start scanning it  <--- here both @security and @tag from the controller would be added as default
   */
  @TypedRoute.Post('new')
  public async newLibrary(
    @Body() createDto: CreateLibraryDTO,
  ): Promise<boolean> {
    return this.libraryService.createLibrary(createDto);
  }


}

loucass003 avatar Jan 16 '24 22:01 loucass003

Use @ApiTag and @ApiSecurity decorator of @nestjs/swagger to the controller.

Nestia can identify them.

samchon avatar Jan 17 '24 04:01 samchon

Ah, I've missed documentation on the https://nestia.io/docs/sdk/swagger article.

I'll do it someday. Thanks for reporting.

samchon avatar Jan 17 '24 04:01 samchon

/**
 * Controller for the Libraries
 */
@ApiTags('library')
@ApiSecurity('bearer')
@Controller('library')
@Authenticated()
export class LibraryController {}

Works as expected thanks for support. Nestia is amazing! keep up the good work

loucass003 avatar Jan 18 '24 19:01 loucass003