openapi-ts-v0
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSkill: openapi-ts
Skill: openapi-ts
Scope
范围
- Applies to: Generating TypeScript clients from OpenAPI 3.0 specifications using
@hey-api/openapi-ts - Does NOT cover: Writing OpenAPI specs, API server implementation (see fastify)
- 适用于:使用 从 OpenAPI 3.0 规范生成 TypeScript 客户端
@hey-api/openapi-ts - 不包括:编写 OpenAPI 规范、API 服务器实现(参见 fastify)
Assumptions
前提条件
- v0+
@hey-api/openapi-ts - OpenAPI 3.0 specification format
- TypeScript v5+ with strict mode
- Default output is types and client only; React Query hooks are handwritten in consuming apps
- v0+
@hey-api/openapi-ts - OpenAPI 3.0 规范格式
- TypeScript v5+,启用 strict mode
- 默认输出仅包含类型和客户端;在消费应用中手写 React Query hooks
Principles
原则
- Generate clients from OpenAPI specs (single source of truth)
- Client methods are fully typed from OpenAPI spec
- Error responses are typed from OpenAPI error schemas
- Use factory for client instantiation
createClient - Configure output format and schema type in config file
- Zod schemas and generated React Query plugins are optional add-ons
- 从 OpenAPI 规范生成客户端(单一事实来源)
- 客户端方法完全根据 OpenAPI 规范进行类型化
- 错误响应根据 OpenAPI 错误 schema 进行类型化
- 使用 工厂实例化客户端
createClient - 在配置文件中配置输出格式和 schema 类型
- Zod schemas 和生成的 React Query 插件是可选附加项
Constraints
约束
MUST
必须
- Use for configuration
openapi-ts.config.ts - Generate clients before using in code
- Handle for typed error responses
response.error
- 使用 进行配置
openapi-ts.config.ts - 在代码中使用客户端前先生成客户端
- 处理 ,以支持类型化错误响应
response.error
SHOULD
应该
- Use Prettier formatting ()
output.format: 'prettier' - Generate TypeScript enums ()
types.enums: 'typescript' - Write TanStack Query hooks by hand against the generated client
- Add Zod schemas () only when runtime validation is required
schemas.type: 'zod' - Add generated React Query plugin only when the project opts into it
- 使用 Prettier 格式化()
output.format: 'prettier' - 生成 TypeScript enums()
types.enums: 'typescript' - 基于生成的客户端手写 TanStack Query hooks
- 仅在需要运行时验证时添加 Zod schemas()
schemas.type: 'zod' - 仅当项目选择启用时添加生成的 React Query 插件
AVOID
避免
- Manually editing generated code
- Using generated clients without error handling
- Mixing generated and manual client code
- 手动编辑生成的代码
- 使用未进行错误处理的生成客户端
- 混用生成的客户端代码和手写的客户端代码
Interactions
交互
- Consumes OpenAPI specs generated by fastify
- Works with next for API client usage
- Integrates with TanStack Query (see React Query Integration)
- 消费由 fastify 生成的 OpenAPI 规范
- 与 next 配合用于 API 客户端调用
- 与 TanStack Query 集成(参见 React Query Integration)
Patterns
模式
Configuration Pattern
配置模式
typescript
// openapi-ts.config.ts
import { defineConfig } from '@hey-api/openapi-ts'
export default defineConfig({
input: './openapi.json',
output: {
path: './src/gen',
format: 'prettier',
},
types: {
enums: 'typescript',
},
})typescript
// openapi-ts.config.ts
import { defineConfig } from '@hey-api/openapi-ts'
export default defineConfig({
input: './openapi.json',
output: {
path: './src/gen',
format: 'prettier',
},
types: {
enums: 'typescript',
},
})Client Usage Pattern
客户端使用模式
typescript
import { createClient } from './gen/client'
const client = createClient({
baseUrl: 'https://api.example.com',
})
const response = await client.GET('/users/{id}', {
params: { path: { id: '123' } },
})
if (response.error) {
// Handle typed error
return
}
// response.data is typed from OpenAPI specSee Config Template for complete example.
typescript
import { createClient } from './gen/client'
const client = createClient({
baseUrl: 'https://api.example.com',
})
const response = await client.GET('/users/{id}', {
params: { path: { id: '123' } },
})
if (response.error) {
// Handle typed error
return
}
// response.data is typed from OpenAPI spec请参阅 Config Template 获取完整示例。
References
参考资料
- React Query Integration - Using generated clients with TanStack Query
- React Query Integration - 使用生成的客户端配合 TanStack Query