</>
⌕
Junior #swagger#openapi#documentation

Tài liệu hoá API NestJS bằng Swagger/OpenAPI như thế nào?

⚡ Trả lời nhanh

Dùng @nestjs/swagger: DocumentBuilder + SwaggerModule.createDocument/setup tạo OpenAPI spec và UI. Gắn @ApiProperty, @ApiTags, @ApiResponse cho DTO/controller, hoặc bật CLI plugin để tự suy ra metadata từ type TypeScript.

// main.ts
const config = new DocumentBuilder()
  .setTitle('Shop API').setVersion('1.0').addBearerAuth().build();
const doc = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('docs', app, doc);   // UI tại /docs, JSON tại /docs-json

// dto
export class CreateUserDto {
  @ApiProperty({ example: 'a@b.com' }) @IsEmail() email: string;
  @ApiPropertyOptional() @IsString() name?: string;
}

@ApiTags('users') @ApiBearerAuth()
@Controller('users')
export class UsersController {
  @Post() @ApiCreatedResponse({ type: UserDto }) create(@Body() dto: CreateUserDto) {}
}
  • Swagger CLI plugin (nest-cli.json) tự thêm @ApiProperty dựa vào type, đỡ viết tay.
  • Spec OpenAPI có thể sinh client SDK (openapi-generator) hoặc dùng cho contract test.
  • Kế thừa DTO dùng PartialType, PickType, OmitType từ @nestjs/swagger (không phải @nestjs/mapped-types) để metadata được giữ lại.
🃏 Ôn bằng flashcard