Skip to content

Commit 6dd2783

Browse files
committed
docs: add translators description
1 parent 5840e4d commit 6dd2783

6 files changed

Lines changed: 421 additions & 10 deletions

File tree

.vscode/tasks.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Start Docusaurus Dev Server",
6+
"type": "shell",
7+
"command": "pnpm start",
8+
"problemMatcher": [],
9+
"isBackground": true,
10+
"presentation": {
11+
"reveal": "always",
12+
"panel": "new",
13+
"focus": false
14+
},
15+
"runOptions": {
16+
"runOn": "folderOpen"
17+
}
18+
}
19+
]
20+
}

docs/7.libraries/1.cli/1.overview.mdx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ request
281281
| outdir | true | - | 编译结果的输出目录 |
282282
| modules | true | - | Swagger 文件地址和模块名称 |
283283
| fileNamingStyle | false | - | 文件名风格 |
284-
| mode | false | `micro-function` | 生成的代码风格 |
285284
| esm | false |`package.json` 是否设置了 `"type": "module"` 则为 `true` | 是否生成 ESM 风格的代码 |
286-
| plugins | false | `[]` | 插件向第三方开发者提供了完整的自定义 cli 行为的能力 |
285+
| translators | false | `[]` | [生成的代码风格](./4.translators.mdx) |
286+
| plugins | false | `[]` | [插件向第三方开发者提供了完整的自定义 cli 行为的能力](./2.plugin.mdx) |
287287

288288
#### fileNamingStyle
289289

@@ -301,13 +301,6 @@ request
301301
| `FileNamingStyle.sentenceCase` | `"Two words"` |
302302
| `FileNamingStyle.snakeCase` | `"two_words"` |
303303

304-
#### mode
305-
306-
| **枚举** | **描述** |
307-
| :---------------------- | :--------------------------- |
308-
| `micro-function` | 生成函数形式的 API 调用代码 |
309-
| `nestjs-module` | 生成适用于 NestJS 的模块代码 |
310-
311304
### `.keqignore`
312305

313306
还可以通过 `.keqignore` 文件忽略不需要生成代码的 HTTP 接口:

docs/7.libraries/1.cli/3.build-in-plugin/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| [PrettierPlugin](./build-in-plugin/prettier-plugin) | 代码生成后自动运行 Prettier 格式化 (`prettier --write`) |
1616
| [OverwriteOperationIdPlugin](./build-in-plugin/overwrite-operation-id-plugin) | 根据自定义规则覆盖 OpenAPI 文档中的 `operationId` 字段 |
1717
| [OverwriteQueryOptionsPlugin](./build-in-plugin/overwrite-query-options-plugin) | 自定义 URL Query 参数的格式化方式 |
18+
| [UseValibotPlugin](./build-in-plugin/use-valibot-plugin) | 使用 [Valibot](https://valibot.dev/) 生成 `components/schema` |
1819

1920
## 核心插件
2021

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import InstallPackagesCode from '@site/docs/components/_install-packages-code.mdx'
2+
3+
4+
# UseValibotPlugin
5+
6+
`UseValibotPlugin` 使用 [Valibot](https://valibot.dev/) 来生成 OpenAPI 文档中 `components/schemas` 的类型定义和验证模式。Valibot 是一个轻量级的模式验证库,提供了更小的包体积和更好的类型推断。
7+
8+
:::tip
9+
使用 Valibot 相比传统的 TypeScript 类型定义,可以在运行时进行数据验证,确保 API 响应数据的类型安全。
10+
:::
11+
12+
## 安装依赖
13+
14+
使用此插件前,需要先安装 Valibot:
15+
16+
<InstallPackagesCode packages="valibot" />
17+
18+
## 配置方法
19+
20+
```typescript title=".keqrc.ts"
21+
import { UseValibotPlugin } from '@keq-request/cli/plugins'
22+
23+
export default defineKeqConfig({
24+
outdir: "./src/apis",
25+
modules: {
26+
catService: "./cat-service-swagger.json",
27+
},
28+
plugins: [new UseValibotPlugin()],
29+
})
30+
```
31+
32+
## 使用场景
33+
34+
假设你的 OpenAPI 文档中定义了这样的 Schema:
35+
36+
```json
37+
{
38+
"components": {
39+
"schemas": {
40+
"Cat": {
41+
"type": "object",
42+
"required": ["id", "name"],
43+
"properties": {
44+
"id": {
45+
"type": "integer",
46+
"format": "int64"
47+
},
48+
"name": {
49+
"type": "string",
50+
"minLength": 1,
51+
"maxLength": 100
52+
},
53+
"breed": {
54+
"type": "string"
55+
},
56+
"age": {
57+
"type": "integer",
58+
"minimum": 0,
59+
"maximum": 30
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
```
67+
68+
### 不使用 UseValibotPlugin
69+
70+
默认情况下,CLI 会生成纯 TypeScript 类型定义:
71+
72+
```typescript
73+
export interface Cat {
74+
id: number
75+
name: string
76+
breed?: string
77+
age?: number
78+
}
79+
```
80+
81+
这种方式只提供编译时的类型检查,无法在运行时验证数据。
82+
83+
### 使用 UseValibotPlugin
84+
85+
配置插件后,CLI 会生成 Valibot schema:
86+
87+
```typescript
88+
import * as v from 'valibot'
89+
90+
export const CatSchema = v.object({
91+
id: v.pipe(v.number(), v.integer()),
92+
name: v.pipe(v.string(), v.minLength(1), v.maxLength(100)),
93+
breed: v.optional(v.string()),
94+
age: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0), v.maxValue(30))),
95+
})
96+
97+
export type Cat = v.InferOutput<typeof CatSchema>
98+
```
99+
100+
你可以使用生成的 schema 进行运行时验证:
101+
102+
#### 验证 API 响应数据
103+
104+
```typescript
105+
import { CatSchema } from './apis/cat-service'
106+
import * as v from 'valibot'
107+
108+
// 验证 API 响应数据
109+
const response = await fetch('/api/cats/1')
110+
const data = await response.json()
111+
112+
try {
113+
const cat = v.parse(CatSchema, data)
114+
console.log(cat) // 类型安全的猫咪数据
115+
} catch (error) {
116+
console.error('数据验证失败:', error)
117+
}
118+
```
119+
120+
#### 表单校验与请求提交
121+
122+
在实际应用中,可以结合表单校验和请求体验证,确保只有合法数据才会被提交:
123+
124+
```typescript
125+
import { CatSchema, Cat } from './apis/cat-service'
126+
import * as v from 'valibot'
127+
128+
// 表单的数据结构很可能是 Partial 类型
129+
type CatFormData = Partial<Cat>
130+
131+
async function submitCatForm(formData: CatFormData) {
132+
if (!v.is(CatSchema, formData)) {
133+
// Do Something...
134+
return
135+
}
136+
137+
// 校验通过,发送请求
138+
const response = await fetch('/api/cats', {
139+
method: 'POST',
140+
headers: { 'Content-Type': 'application/json' },
141+
body: JSON.stringify(formData),
142+
})
143+
144+
const result = await response.json()
145+
console.log('猫咪创建成功')
146+
}
147+
```

0 commit comments

Comments
 (0)