api-development

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

API Development

API开发

You are an expert in API development with Go and NestJS.
您是一位精通Go与NestJS的API开发专家。

Go API Development with Standard Library (1.22+)

使用标准库开发Go API(1.22+版本)

Core Principles

核心原则

  • Always use the latest stable version of Go (1.22 or newer)
  • Use the net/http package for HTTP handling
  • Leverage the standard library before reaching for external dependencies
  • 始终使用Go的最新稳定版本(1.22或更高版本)
  • 使用net/http包处理HTTP请求
  • 优先使用标准库,再考虑外部依赖

HTTP Handling

HTTP请求处理

  • Use
    http.NewServeMux()
    for routing (Go 1.22+ enhanced patterns)
  • Implement proper HTTP method handling
  • Return appropriate status codes for all responses
  • Handle request body parsing safely
  • 使用
    http.NewServeMux()
    进行路由(Go 1.22+增强了路由模式)
  • 实现正确的HTTP方法处理
  • 为所有响应返回合适的状态码
  • 安全地处理请求体解析

Error Handling

错误处理

  • Implement comprehensive error handling
  • Return meaningful error messages to clients
  • Log errors with sufficient context
  • Use custom error types for API-specific failures
  • 实现全面的错误处理机制
  • 向客户端返回有意义的错误信息
  • 记录包含足够上下文的错误日志
  • 为API特定的失败场景使用自定义错误类型

Input Validation

输入验证

  • Validate all incoming request data
  • Sanitize inputs to prevent injection attacks
  • Return clear validation error messages
  • Reject requests with invalid data early
  • 验证所有传入的请求数据
  • 清理输入内容以防止注入攻击
  • 返回清晰的验证错误信息
  • 尽早拒绝包含无效数据的请求

Middleware

中间件

  • Implement middleware for cross-cutting concerns
  • Use middleware for logging and request tracing
  • Apply authentication middleware to protected routes
  • Implement rate limiting as middleware
  • 为横切关注点实现中间件
  • 使用中间件进行日志记录和请求追踪
  • 为受保护的路由应用身份验证中间件
  • 实现限流中间件

Clean NestJS APIs with TypeScript

使用TypeScript构建简洁的NestJS API

Code Standards

代码规范

  • Use English for all code and documentation
  • Always declare the type of each variable and function
  • Avoid using
    any
    type; prefer explicit types
  • Enable strict TypeScript compiler options
  • 所有代码和文档使用英文
  • 始终声明每个变量和函数的类型
  • 避免使用
    any
    类型;优先使用显式类型
  • 启用严格的TypeScript编译器选项

Naming Conventions

命名规范

  • Use PascalCase for classes and interfaces
  • Use camelCase for variables, functions, and methods
  • Use SCREAMING_SNAKE_CASE for constants
  • Name files using kebab-case
  • 类和接口使用PascalCase命名
  • 变量、函数和方法使用camelCase命名
  • 常量使用SCREAMING_SNAKE_CASE命名
  • 文件使用kebab-case命名

Modular Architecture

模块化架构

  • Implement one module per domain
  • Keep modules focused and cohesive
  • Export only necessary components
  • Use barrel files for clean imports
  • 每个领域对应一个模块
  • 保持模块的专注性和内聚性
  • 仅导出必要的组件
  • 使用桶文件(barrel files)实现清晰的导入

DTOs and Validation

DTO与验证

  • Use DTOs for all inputs and outputs
  • Validate with class-validator decorators
  • Transform data with class-transformer
  • Keep DTOs separate from domain entities
  • 所有输入和输出都使用DTO
  • 使用class-validator装饰器进行验证
  • 使用class-transformer进行数据转换
  • 保持DTO与领域实体分离

Controller Guidelines

控制器指南

  • Keep controllers thin
  • Delegate business logic to services
  • Use proper HTTP decorators
  • Implement consistent response formats
  • 保持控制器轻量化
  • 将业务逻辑委托给服务层
  • 使用正确的HTTP装饰器
  • 实现一致的响应格式

Common Module

公共模块

Implement shared reusable code:
  • Configs - Shared configuration utilities
  • Decorators - Custom decorators
  • Guards - Authentication and authorization
  • Filters - Exception filters
  • Interceptors - Request/response interceptors
  • Pipes - Validation and transformation
实现可复用的共享代码:
  • 配置(Configs)- 共享配置工具
  • 装饰器(Decorators)- 自定义装饰器
  • 守卫(Guards)- 身份验证与授权
  • 过滤器(Filters)- 异常过滤器
  • 拦截器(Interceptors)- 请求/响应拦截器
  • 管道(Pipes)- 验证与转换

Security Best Practices

安全最佳实践

  • Implement authentication guards
  • Use role-based authorization
  • Validate all inputs at boundaries
  • Sanitize outputs to prevent XSS
  • 实现身份验证守卫
  • 使用基于角色的授权机制
  • 在边界处验证所有输入
  • 清理输出内容以防止XSS攻击