samchon / nestia

NestJS Helper Libraries + TypeScript OpenAPI generator
https://nestia.io/
MIT License
1.79k stars 92 forks source link

Watch for @tag and @security on the Controller class #754

Open loucass003 opened 8 months ago

loucass003 commented 8 months ago

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

}
samchon commented 8 months ago

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

Nestia can identify them.

samchon commented 8 months ago

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

I'll do it someday. Thanks for reporting.

loucass003 commented 7 months ago
/**
 * 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