Skip to content

Commit 24ec992

Browse files
committed
docs: add chinese-to-pinyin-plugin doc
1 parent 0c22853 commit 24ec992

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# ChineseToPinyinPlugin
2+
3+
在实际项目中,OpenAPI 文档可能包含中文的接口名称、参数名称或 Schema 名称。`ChineseToPinyinPlugin` 会自动将这些中文标识符转换为拼音,确保生成的 TypeScript 代码符合命名规范。
4+
5+
:::warning
6+
如果有能力修改 OpenAPI 文档,建议直接使用英文标识符。`ChineseToPinyinPlugin` 主要用于无法修改文档的场景下,确保代码生成的顺利进行。
7+
:::
8+
9+
## 使用场景
10+
11+
假设你的 OpenAPI 文档中定义了这样的接口:
12+
13+
```json
14+
{
15+
"paths": {
16+
"/users": {
17+
"get": {
18+
"operationId": "获取用户列表",
19+
"parameters": [
20+
{
21+
"name": "用户名",
22+
"in": "query",
23+
"schema": {
24+
"type": "string"
25+
}
26+
}
27+
]
28+
}
29+
}
30+
},
31+
"components": {
32+
"schemas": {
33+
"用户信息": {
34+
"type": "object",
35+
"properties": {
36+
"姓名": { "type": "string" },
37+
"年龄": { "type": "number" }
38+
}
39+
}
40+
}
41+
}
42+
}
43+
```
44+
45+
如果不使用 `ChineseToPinyinPlugin`,会导致部分接口/数据结构无法生成或生成的代码包含非法标识符。
46+
47+
## 配置方法
48+
49+
```typescript title=".keqrc.ts"
50+
import { ChineseToPinyinPlugin } from '@keq-request/cli/plugins'
51+
52+
export default defineKeqConfig({
53+
outdir: "./src/apis",
54+
modules: {
55+
userService: "./user-service-swagger.json",
56+
},
57+
plugins: [new ChineseToPinyinPlugin()],
58+
})
59+
```
60+
61+
配置后,`ChineseToPinyinPlugin` 会自动转换所有中文标识符:
62+
63+
- `operationId: "获取用户列表"` → 函数名: `huoQuYongHuLieBiao`
64+
- 参数名: `用户名``yongHuMing`
65+
- Schema 名称: `用户信息``YongHuXinXi`
66+
- 属性名: `姓名``xingMing``年龄``nianLing`
67+
68+
生成的代码将完全使用拼音标识符,确保代码的可编译性和规范性。
69+

0 commit comments

Comments
 (0)