orval
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOrval OpenAPI Best Practices
Orval OpenAPI 最佳实践
Comprehensive guide for generating type-safe TypeScript clients from OpenAPI specifications using Orval. Contains 42 rules across 8 categories, prioritized by impact to guide automated configuration, client generation, and testing setup.
本指南全面介绍了如何使用Orval从OpenAPI规范生成类型安全的TypeScript客户端。包含8个类别共42条规则,按影响优先级排序,可指导自动化配置、客户端生成和测试环境搭建。
When to Apply
适用场景
Reference these guidelines when:
- Configuring Orval for a new project
- Setting up OpenAPI-based TypeScript client generation
- Integrating React Query, SWR, or Vue Query with generated hooks
- Creating custom mutators for authentication and error handling
- Generating MSW mocks for testing
在以下场景中可参考本指南:
- 配置新项目的Orval时
- 搭建基于OpenAPI的TypeScript客户端生成流程时
- 将React Query、SWR或Vue Query与生成的钩子集成时
- 创建用于身份验证和错误处理的自定义mutator时
- 生成用于测试的MSW模拟时
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | OpenAPI Specification Quality | CRITICAL | |
| 2 | Configuration Architecture | CRITICAL | |
| 3 | Output Structure & Organization | HIGH | |
| 4 | Custom Client & Mutators | HIGH | |
| 5 | Query Library Integration | MEDIUM-HIGH | |
| 6 | Type Safety & Validation | MEDIUM | |
| 7 | Mock Generation & Testing | MEDIUM | |
| 8 | Advanced Patterns | LOW | |
| 优先级 | 类别 | 影响级别 | 前缀 |
|---|---|---|---|
| 1 | OpenAPI规范质量 | CRITICAL | |
| 2 | 配置架构 | CRITICAL | |
| 3 | 输出结构与组织 | HIGH | |
| 4 | 自定义客户端与Mutator | HIGH | |
| 5 | 查询库集成 | MEDIUM-HIGH | |
| 6 | 类型安全与验证 | MEDIUM | |
| 7 | 模拟生成与测试 | MEDIUM | |
| 8 | 高级模式 | LOW | |
Quick Reference
快速参考
1. OpenAPI Specification Quality (CRITICAL)
1. OpenAPI规范质量(CRITICAL)
- - Use unique and descriptive operationIds
spec-operationid-unique - - Define reusable schemas in components
spec-schemas-reusable - - Organize operations with tags
spec-tags-organization - - Define all response types explicitly
spec-response-types - - Mark required fields explicitly
spec-required-fields
- - 使用唯一且具有描述性的operationId
spec-operationid-unique - - 在components中定义可复用的schema
spec-schemas-reusable - - 使用标签对操作进行分类
spec-tags-organization - - 显式定义所有响应类型
spec-response-types - - 显式标记必填字段
spec-required-fields
2. Configuration Architecture (CRITICAL)
2. 配置架构(CRITICAL)
- - Choose output mode based on API size
orvalcfg-mode-selection - - Select client based on framework requirements
orvalcfg-client-selection - - Separate schemas into dedicated directory
orvalcfg-separate-schemas - - Validate OpenAPI spec before generation
orvalcfg-input-validation - - Configure base URL properly
orvalcfg-baseurl-setup - - Enable automatic code formatting
orvalcfg-prettier-format
- - 根据API规模选择输出模式
orvalcfg-mode-selection - - 根据框架需求选择客户端类型
orvalcfg-client-selection - - 将schema分离到专用目录
orvalcfg-separate-schemas - - 生成前验证OpenAPI规范
orvalcfg-input-validation - - 正确配置基础URL
orvalcfg-baseurl-setup - - 启用自动代码格式化
orvalcfg-prettier-format
3. Output Structure & Organization (HIGH)
3. 输出结构与组织(HIGH)
- - Use distinct file extensions for generated code
output-file-extension - - Generate index files for clean imports
output-index-files - - Configure consistent naming conventions
output-naming-convention - - Enable clean mode for consistent regeneration
output-clean-target - - Enable headers in generated functions
output-headers-enabled
- - 为生成的代码使用不同的文件扩展名
output-file-extension - - 生成索引文件以优化导入体验
output-index-files - - 配置统一的命名规范
output-naming-convention - - 启用清理模式以确保一致的重新生成
output-clean-target - - 在生成的函数中启用头部支持
output-headers-enabled
4. Custom Client & Mutators (HIGH)
4. 自定义客户端与Mutator(HIGH)
- - Use custom mutator for HTTP client configuration
mutator-custom-instance - - Export custom error types from mutator
mutator-error-types - - Export body type wrapper for request transformation
mutator-body-wrapper - - Use interceptors for cross-cutting concerns
mutator-interceptors - - Handle token refresh in mutator
mutator-token-refresh - - Use fetch mutator for smaller bundle size
mutator-fetch-client
- - 使用自定义mutator配置HTTP客户端
mutator-custom-instance - - 从mutator导出自定义错误类型
mutator-error-types - - 导出请求体类型包装器以实现请求转换
mutator-body-wrapper - - 使用拦截器处理横切关注点
mutator-interceptors - - 在mutator中处理令牌刷新逻辑
mutator-token-refresh - - 使用fetch mutator以减小包体积
mutator-fetch-client
5. Query Library Integration (MEDIUM-HIGH)
5. 查询库集成(MEDIUM-HIGH)
- - Configure default query options globally
oquery-hook-options - - Export query keys for cache invalidation
oquery-key-export - - Enable infinite queries for paginated endpoints
oquery-infinite-queries - - Enable suspense mode for streaming UX
oquery-suspense-support - - Pass AbortSignal for request cancellation
oquery-signal-cancellation - - Use generated mutation options types
oquery-mutation-callbacks
- - 全局配置默认查询选项
oquery-hook-options - - 导出查询键以实现缓存失效
oquery-key-export - - 为分页端点启用无限查询
oquery-infinite-queries - - 启用suspense模式以实现流式用户体验
oquery-suspense-support - - 传递AbortSignal以支持请求取消
oquery-signal-cancellation - - 使用生成的突变选项类型
oquery-mutation-callbacks
6. Type Safety & Validation (MEDIUM)
6. 类型安全与验证(MEDIUM)
- - Generate Zod schemas for runtime validation
types-zod-validation - - Enable Zod strict mode for safer validation
types-zod-strict - - Use Zod coercion for type transformations
types-zod-coerce - - Enable useDates for Date type generation
types-use-dates - - Enable useBigInt for large integer support
types-bigint-support
- - 生成Zod schema以实现运行时验证
types-zod-validation - - 启用Zod严格模式以提升验证安全性
types-zod-strict - - 使用Zod强制转换实现类型转换
types-zod-coerce - - 启用useDates以生成Date类型
types-use-dates - - 启用useBigInt以支持大整数
types-bigint-support
7. Mock Generation & Testing (MEDIUM)
7. 模拟生成与测试(MEDIUM)
- - Generate MSW handlers for testing
mock-msw-generation - - Use OpenAPI examples for realistic mocks
mock-use-examples - - Configure mock response delays
mock-delay-config - - Generate mocks for all HTTP status codes
mock-http-status - - Generate mock index files for easy setup
mock-index-files
- - 生成MSW处理程序用于测试
mock-msw-generation - - 使用OpenAPI示例生成真实感模拟数据
mock-use-examples - - 配置模拟响应延迟
mock-delay-config - - 为所有HTTP状态码生成模拟
mock-http-status - - 生成模拟索引文件以简化配置
mock-index-files
8. Advanced Patterns (LOW)
8. 高级模式(LOW)
- - Use input transformer for spec preprocessing
adv-input-transformer - - Override settings per operation
adv-operation-override - - Use output transformer for generated code modification
adv-output-transformer - - Configure form data serialization
adv-form-data-handling
- - 使用输入转换器预处理规范
adv-input-transformer - - 按操作覆盖配置
adv-operation-override - - 使用输出转换器修改生成的代码
adv-output-transformer - - 配置表单数据序列化
adv-form-data-handling
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
- Example: spec-operationid-unique
阅读单个参考文件以获取详细说明和代码示例:
- 章节定义 - 类别结构和影响级别说明
- 规则模板 - 添加新规则的模板
- 示例:spec-operationid-unique
Related Skills
相关技能
- For consuming generated hooks, see skill
tanstack-query - For mocking generated API clients, see skill
test-msw - For schema validation, see skill
zod
- 若要使用生成的钩子,请参考技能
tanstack-query - 若要为生成的API客户端创建模拟,请参考技能
test-msw - 若要进行schema验证,请参考技能
zod
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md包含所有规则详细说明的完整指南:
AGENTS.md