api-design-expert
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAPI Design Expert Skill
API设计专家技能
Expert in RESTful API design, OpenAPI/Swagger documentation, versioning strategies, error handling, and API best practices for NestJS applications.
精通NestJS应用中的RESTful API设计、OpenAPI/Swagger文档编写、版本控制策略、错误处理及API最佳实践。
When to Use
使用场景
- Designing new API endpoints
- Creating RESTful APIs
- Writing OpenAPI/Swagger documentation
- Implementing API versioning
- Designing error responses
- Creating DTOs and validation
- Implementing pagination, filtering, sorting
- 设计新的API端点
- 创建RESTful API
- 编写OpenAPI/Swagger文档
- 实现API版本控制
- 设计错误响应
- 创建DTO及验证逻辑
- 实现分页、过滤、排序功能
Project Context Discovery
项目上下文调研
Before providing guidance:
- Check for API patterns
.agents/SYSTEM/ARCHITECTURE.md - Review existing controllers and DTOs
- Check for OpenAPI/Swagger setup
- Review versioning strategy
在提供指导前:
- 查看文件了解API模式
.agents/SYSTEM/ARCHITECTURE.md - 审阅现有控制器和DTO
- 检查OpenAPI/Swagger配置情况
- 审阅版本控制策略
Core Principles
核心原则
RESTful Design
RESTful设计
typescript
// Use nouns, plural, hierarchical
GET /api/users
GET /api/users/:id
POST /api/users
PUT /api/users/:id
DELETE /api/users/:id
GET /api/users/:id/poststypescript
// 使用名词、复数形式、层级结构
GET /api/users
GET /api/users/:id
POST /api/users
PUT /api/users/:id
DELETE /api/users/:id
GET /api/users/:id/postsHTTP Status Codes
HTTP状态码
- /
200 OK/201 Created204 No Content - /
400 Bad Request/401 Unauthorized403 Forbidden - /
404 Not Found/409 Conflict500 Internal Server Error
- /
200 OK/201 Created204 No Content - /
400 Bad Request/401 Unauthorized403 Forbidden - /
404 Not Found/409 Conflict500 Internal Server Error
Response Format
响应格式
typescript
// Single resource
{ "data": {...}, "meta": {...} }
// List with pagination
{ "data": [...], "pagination": { "page", "limit", "total" } }typescript
// 单个资源
{ "data": {...}, "meta": {...} }
// 带分页的列表
{ "data": [...], "pagination": { "page", "limit", "total" } }Error Format
错误格式
typescript
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Validation failed",
"details": [...],
"timestamp": "...",
"path": "/api/users"
}
}typescript
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Validation failed",
"details": [...],
"timestamp": "...",
"path": "/api/users"
}
}Best Practices
最佳实践
- Consistent naming conventions
- Validate all inputs with DTOs
- OpenAPI/Swagger documentation
- Authentication on all endpoints
- Pagination for lists
- Version APIs from the start
For complete DTO examples, pagination/filtering/sorting patterns, versioning strategies, OpenAPI setup, CRUD controller patterns, nested resources, bulk operations, and anti-patterns, see:
references/full-guide.md- 统一命名规范
- 使用DTO验证所有输入
- 编写OpenAPI/Swagger文档
- 所有端点添加认证机制
- 列表数据实现分页
- 从项目初期就进行API版本控制
如需完整的DTO示例、分页/过滤/排序模式、版本控制策略、OpenAPI配置、CRUD控制器模式、嵌套资源、批量操作及反模式相关内容,请参阅:
references/full-guide.md