Cơ BảnNestJS iconNestJS

Swagger / OpenAPI documentation trong NestJS — setup và các annotations phổ biến?

NestJS tích hợp Swagger qua @nestjs/swagger. Setup trong main.ts: DocumentBuilder cấu hình title/description/version/auth, SwaggerModule.createDocument() tạo document, SwaggerModule.setup('api/docs', app, document) serve UI.

Annotations trên DTO: @ApiProperty({ example, description }) cho required fields, @ApiPropertyOptional() cho optional fields. Hỗ trợ enum, type, minLength, maxLength, default.

Annotations trên Controller: @ApiTags('GroupName') nhóm endpoints trong Swagger UI, @ApiBearerAuth() chỉ định cần JWT, @ApiOperation({ summary }) mô tả endpoint, @ApiResponse({ status, type, description }) document các response codes. Param annotations: @ApiParam() cho route params, @ApiQuery() cho query params. PickType, OmitType, PartialType từ @nestjs/swagger để tạo derived DTOs tái sử dụng schema.

Xem toàn bộ NestJS cùng filter theo level & chủ đề con.

Mở danh sách NestJS