@cnbcool/cnb-api-generate 2.16.7 → 2.16.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -326
- package/package.json +1 -1
- package/quick-commands.json +1 -1
package/README.md
CHANGED
|
@@ -1,23 +1,15 @@
|
|
|
1
1
|
# cnb-api-generate
|
|
2
2
|
|
|
3
|
-
`
|
|
3
|
+
基于 `swagger2.0` 生成 TypeScript 代码与 Agent Skill 的工具。
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
- `cag`:生成基于 `@cnb/request` 和 `@reduxjs/toolkit` 的 TypeScript 代码,`Interface` / `Enum` 与 `swagger.json` 强绑定。
|
|
6
|
+
- `csg`:基于 `swagger` 生成 Agent Skill(`SKILL.md` + CLI)。
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
## 为什么需要
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
- ❎ 手动定义`Interface`和`Enum`,可能存在认为失误写错代码。
|
|
12
|
-
- ❎ 可能存在多个重复定义的`Interface`和`Enum`。
|
|
10
|
+
前后端各维护一套接口定义时,后端 `swagger.json` 变更无法触达前端:编译能过,运行时才报错;`Interface` / `Enum` 靠手写,容易写错或重复定义。
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
- 💯 `swargger.json`与所有的前端网络调用函数代码强绑定,当`swargger.json`变化后,重新生成前端代码,实现`Typescript`代码重新生成,从而在编译时就能发现类型错误的问题。
|
|
17
|
-
- 💯 `Interface`和`Enum`的定义完全自动化,完全基于`swargger.json`,无需人为干预。
|
|
18
|
-
- 💯 生成的代码更规范,抹除因开发人员水平差异导致编写的`Typescript`代码质量参差不齐。
|
|
19
|
-
- 💯 完善的jsDos定义,除了生成`Typescript`代码外,还会根据`swargger.json`定义每个参数的说明,自动生成jsDos的注释。
|
|
20
|
-
- 💯 从生成的代码和注释质量倒推接口的文档规范。
|
|
12
|
+
使用本工具后,`Interface` / `Enum` 与请求函数全部由 `swagger.json` 生成,`swagger.json` 一变重新生成即可在编译期发现类型错误,同时自动生成 jsDoc 注释。
|
|
21
13
|
|
|
22
14
|
## 安装
|
|
23
15
|
|
|
@@ -27,13 +19,15 @@ npm install @cnbcool/cnb-api-generate -D
|
|
|
27
19
|
yarn add @cnbcool/cnb-api-generate -D
|
|
28
20
|
```
|
|
29
21
|
|
|
22
|
+
> 必须搭配 `@cnb/request` 使用。
|
|
23
|
+
|
|
30
24
|
## 快速使用
|
|
31
25
|
|
|
32
|
-
|
|
26
|
+
在运行 `cag` 的目录下创建 `cag.config.js`:
|
|
33
27
|
|
|
34
28
|
```javascript
|
|
35
29
|
module.exports = {
|
|
36
|
-
target: '<
|
|
30
|
+
target: '<swagger.json 文件地址>',
|
|
37
31
|
}
|
|
38
32
|
```
|
|
39
33
|
|
|
@@ -43,26 +37,20 @@ npx cag
|
|
|
43
37
|
yarn cag
|
|
44
38
|
```
|
|
45
39
|
|
|
46
|
-
>cnb-api-generate必须搭配@cnb/request使用!!!
|
|
47
|
-
|
|
48
|
-
|
|
49
40
|
## cag.config.js
|
|
50
|
-
`cnb-api-generate`基本不需要任何配置,为了灵活支持输入输出文件的位置和引用代码的自定义位置,提供一些简单的配置。
|
|
51
41
|
|
|
52
42
|
### target
|
|
53
|
-
|
|
43
|
+
`swagger.json` 的文件位置,相对 `cag.config.js`。
|
|
54
44
|
|
|
55
45
|
### outputDir
|
|
56
|
-
|
|
46
|
+
生成代码的输出目录,相对 `cag.config.js`。
|
|
57
47
|
|
|
58
48
|
### printeReduxAction
|
|
59
|
-
|
|
49
|
+
是否输出 `Redux Action` 代码,默认 `true`。
|
|
60
50
|
|
|
61
51
|
### requestDependencyMap
|
|
62
|
-
|
|
52
|
+
自定义请求模块中依赖的函数与 TypeScript 定义。
|
|
63
53
|
|
|
64
|
-
- path -> 模块所在路径
|
|
65
|
-
- isDefault -> 是否default导出
|
|
66
54
|
```typescript
|
|
67
55
|
[key in string]: {
|
|
68
56
|
path: string;
|
|
@@ -70,309 +58,17 @@ yarn cag
|
|
|
70
58
|
}
|
|
71
59
|
```
|
|
72
60
|
|
|
73
|
-
>
|
|
74
|
-
|
|
75
|
-
## Example
|
|
76
|
-
`cnb-api-generate`基于`swargger`中定义的类型生成三类型代码,`Interface`、`Enum`和`Api`。其中`Api`会基于`swargger`中对接口的分类进行目录级归类输出。
|
|
77
|
-
|
|
78
|
-
### Interface
|
|
79
|
-
对`swagger`中定义的`definitions`处理,生成`Interface`。
|
|
80
|
-
```json
|
|
81
|
-
"dto.AiAutoPrResult": {
|
|
82
|
-
"type": "object",
|
|
83
|
-
"properties": {
|
|
84
|
-
"buildLogUrl": {
|
|
85
|
-
"description": "构建链接",
|
|
86
|
-
"type": "string"
|
|
87
|
-
},
|
|
88
|
-
"message": {
|
|
89
|
-
"description": "message",
|
|
90
|
-
"type": "string"
|
|
91
|
-
},
|
|
92
|
-
"sn": {
|
|
93
|
-
"description": "构建号",
|
|
94
|
-
"type": "string"
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
interfaces/dto.aiautoprresult.ts
|
|
101
|
-
```typescript
|
|
102
|
-
export interface DtoAiAutoPrResult {
|
|
103
|
-
/**
|
|
104
|
-
* 构建链接
|
|
105
|
-
*/
|
|
106
|
-
buildLogUrl?: string;
|
|
107
|
-
/**
|
|
108
|
-
* message
|
|
109
|
-
*/
|
|
110
|
-
message?: string;
|
|
111
|
-
/**
|
|
112
|
-
* 构建号
|
|
113
|
-
*/
|
|
114
|
-
sn?: string;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
#### 依赖其他Interface
|
|
120
|
-
在`swagger`的定义中,`definitions`是可以互相依赖的。`cnb-api-generate`会自动处理好所有依赖的文件引入,确保代码正常使用。
|
|
121
|
-
|
|
122
|
-
```json
|
|
123
|
-
"api.CommitObject": {
|
|
124
|
-
"type": "object",
|
|
125
|
-
"properties": {
|
|
126
|
-
"author": {
|
|
127
|
-
"$ref": "#/definitions/api.Signature"
|
|
128
|
-
},
|
|
129
|
-
"comment_count": {
|
|
130
|
-
"type": "integer"
|
|
131
|
-
},
|
|
132
|
-
"committer": {
|
|
133
|
-
"$ref": "#/definitions/api.Signature"
|
|
134
|
-
},
|
|
135
|
-
"message": {
|
|
136
|
-
"type": "string"
|
|
137
|
-
},
|
|
138
|
-
"tree": {
|
|
139
|
-
"$ref": "#/definitions/api.CommitObjectTree"
|
|
140
|
-
},
|
|
141
|
-
"verification": {
|
|
142
|
-
"$ref": "#/definitions/api.CommitObjectVerification"
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
```
|
|
147
|
-
interfaces/api.commitobject.ts
|
|
148
|
-
```typescript
|
|
149
|
-
import { ApiSignature } from "./api.signature";
|
|
150
|
-
import { ApiCommitObjectTree } from "./api.commitobjecttree";
|
|
151
|
-
import { ApiCommitObjectVerification } from "./api.commitobjectverification";
|
|
152
|
-
export interface ApiCommitObject {
|
|
153
|
-
author?: ApiSignature;
|
|
154
|
-
comment_count?: number;
|
|
155
|
-
committer?: ApiSignature;
|
|
156
|
-
message?: string;
|
|
157
|
-
tree?: ApiCommitObjectTree;
|
|
158
|
-
verification?: ApiCommitObjectVerification;
|
|
159
|
-
}
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
### Enum
|
|
163
|
-
对`swagger`中定义的`definitions`处理,生成`Interface`。
|
|
164
|
-
|
|
165
|
-
```json
|
|
166
|
-
"constant.ActivityType": {
|
|
167
|
-
"type": "string",
|
|
168
|
-
"enum": [
|
|
169
|
-
"mine",
|
|
170
|
-
"fork",
|
|
171
|
-
"follow",
|
|
172
|
-
"star",
|
|
173
|
-
"join_group",
|
|
174
|
-
"create_repo",
|
|
175
|
-
"user_create_release",
|
|
176
|
-
"repo_create_release",
|
|
177
|
-
"user_deploy_success",
|
|
178
|
-
"repo_deploy_success",
|
|
179
|
-
"at_user",
|
|
180
|
-
"comment"
|
|
181
|
-
],
|
|
182
|
-
"x-enum-comments": {
|
|
183
|
-
"AtUser": "AtUser @用户",
|
|
184
|
-
"Comment": "Comment pr,issue 评论",
|
|
185
|
-
"CreateRepo": "CreateRepo 创建仓库",
|
|
186
|
-
"Follow": "Follow 用户 follow",
|
|
187
|
-
"Fork": "Fork 仓库 fork",
|
|
188
|
-
"JoinGroup": "JoinGroup 加入组织",
|
|
189
|
-
"Mine": "Mine 与我相关的动态",
|
|
190
|
-
"RepoCreateRelease": "RepoCreateRelease 仓库发布版本",
|
|
191
|
-
"RepoDeploySuccess": "RepoDeploySuccess 仓库部署版本",
|
|
192
|
-
"Star": "Star 仓库 star",
|
|
193
|
-
"UserCreateRelease": "UserCreateRelease 用户发布版本",
|
|
194
|
-
"UserDeploySuccess": "UserDeploySuccess 用户部署版本"
|
|
195
|
-
},
|
|
196
|
-
"x-enum-descriptions": [
|
|
197
|
-
"Mine 与我相关的动态",
|
|
198
|
-
"Fork 仓库 fork",
|
|
199
|
-
"Follow 用户 follow",
|
|
200
|
-
"Star 仓库 star",
|
|
201
|
-
"JoinGroup 加入组织",
|
|
202
|
-
"CreateRepo 创建仓库",
|
|
203
|
-
"UserCreateRelease 用户发布版本",
|
|
204
|
-
"RepoCreateRelease 仓库发布版本",
|
|
205
|
-
"UserDeploySuccess 用户部署版本",
|
|
206
|
-
"RepoDeploySuccess 仓库部署版本",
|
|
207
|
-
"AtUser @用户",
|
|
208
|
-
"Comment pr,issue 评论"
|
|
209
|
-
],
|
|
210
|
-
"x-enum-varnames": [
|
|
211
|
-
"Mine",
|
|
212
|
-
"Fork",
|
|
213
|
-
"Follow",
|
|
214
|
-
"Star",
|
|
215
|
-
"JoinGroup",
|
|
216
|
-
"CreateRepo",
|
|
217
|
-
"UserCreateRelease",
|
|
218
|
-
"RepoCreateRelease",
|
|
219
|
-
"UserDeploySuccess",
|
|
220
|
-
"RepoDeploySuccess",
|
|
221
|
-
"AtUser",
|
|
222
|
-
"Comment"
|
|
223
|
-
]
|
|
224
|
-
}
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
enums/constant.activitytype.ts
|
|
228
|
-
```typescript
|
|
229
|
-
export enum ConstantActivityType {
|
|
230
|
-
/* Mine 与我相关的动态 */
|
|
231
|
-
Mine = "mine",
|
|
232
|
-
/* Fork 仓库 fork */
|
|
233
|
-
Fork = "fork",
|
|
234
|
-
/* Follow 用户 follow */
|
|
235
|
-
Follow = "follow",
|
|
236
|
-
/* Star 仓库 star */
|
|
237
|
-
Star = "star",
|
|
238
|
-
/* JoinGroup 加入组织 */
|
|
239
|
-
JoinGroup = "join_group",
|
|
240
|
-
/* CreateRepo 创建仓库 */
|
|
241
|
-
CreateRepo = "create_repo",
|
|
242
|
-
/* UserCreateRelease 用户发布版本 */
|
|
243
|
-
UserCreateRelease = "user_create_release",
|
|
244
|
-
/* RepoCreateRelease 仓库发布版本 */
|
|
245
|
-
RepoCreateRelease = "repo_create_release",
|
|
246
|
-
/* UserDeploySuccess 用户部署版本 */
|
|
247
|
-
UserDeploySuccess = "user_deploy_success",
|
|
248
|
-
/* RepoDeploySuccess 仓库部署版本 */
|
|
249
|
-
RepoDeploySuccess = "repo_deploy_success",
|
|
250
|
-
/* AtUser @用户 */
|
|
251
|
-
AtUser = "at_user",
|
|
252
|
-
/* Comment pr,issue 评论 */
|
|
253
|
-
Comment = "comment",
|
|
254
|
-
}
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
### Api
|
|
258
|
-
`cnb-api-generate`是为cnb量身定做的代码生成工具,所以基于`@cnb/request`和`@reduxjs/toolkit`生成调用代码,并且自动完成对生成的`interface`和`enum`的引用依赖。
|
|
259
|
-
|
|
260
|
-
```json
|
|
261
|
-
"/user/gpg-keys/{id}": {
|
|
262
|
-
"delete": {
|
|
263
|
-
"security": [
|
|
264
|
-
{
|
|
265
|
-
"BearerAuth": []
|
|
266
|
-
}
|
|
267
|
-
],
|
|
268
|
-
"consumes": [
|
|
269
|
-
"application/json"
|
|
270
|
-
],
|
|
271
|
-
"produces": [
|
|
272
|
-
"application/json",
|
|
273
|
-
"application/vnd.cnb.web+json"
|
|
274
|
-
],
|
|
275
|
-
"tags": [
|
|
276
|
-
"Users"
|
|
277
|
-
],
|
|
278
|
-
"summary": "删除用户 GPG key",
|
|
279
|
-
"operationId": "DeleteGPGKey",
|
|
280
|
-
"parameters": [
|
|
281
|
-
{
|
|
282
|
-
"type": "string",
|
|
283
|
-
"description": "gpg id",
|
|
284
|
-
"name": "id",
|
|
285
|
-
"in": "path",
|
|
286
|
-
"required": true
|
|
287
|
-
}
|
|
288
|
-
],
|
|
289
|
-
"responses": {
|
|
290
|
-
"200": {
|
|
291
|
-
"description": "OK"
|
|
292
|
-
},
|
|
293
|
-
"404": {
|
|
294
|
-
"description": "Not Found",
|
|
295
|
-
"schema": {
|
|
296
|
-
"$ref": "#/definitions/die.WebError"
|
|
297
|
-
}
|
|
298
|
-
},
|
|
299
|
-
"500": {
|
|
300
|
-
"description": "Internal Server Error",
|
|
301
|
-
"schema": {
|
|
302
|
-
"$ref": "#/definitions/die.WebError"
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
```
|
|
309
|
-
|
|
310
|
-
apis/users/delete-gpg-key.ts
|
|
311
|
-
|
|
312
|
-
```typescript
|
|
313
|
-
import type { IncomingMessage } from "http";
|
|
314
|
-
import { createAsyncThunk } from "@reduxjs/toolkit";
|
|
315
|
-
import fetch from "@/request";
|
|
316
|
-
import { CnbRequestOptions, CnbRequestResult } from "@cnb/request";
|
|
317
|
-
import { AxiosRequestConfig } from "axios";
|
|
318
|
-
import { DieWebError } from "../../interfaces/die.weberror";
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* @description Other reuqest params
|
|
322
|
-
*/
|
|
323
|
-
type RequestConfig<DataType = any> = AxiosRequestConfig<DataType> & {
|
|
324
|
-
options?: CnbRequestOptions;
|
|
325
|
-
req?: IncomingMessage;
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* @description DeleteGPGKeyRes Success Response Type
|
|
330
|
-
*/
|
|
331
|
-
export type DeleteGPGKeyRes = unknown;
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* @description DeleteGPGKeyError Error Response Type
|
|
335
|
-
*/
|
|
336
|
-
export type DeleteGPGKeyError = DieWebError;
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* @description No description
|
|
340
|
-
* @tags Users
|
|
341
|
-
* @name deleteGPGKey
|
|
342
|
-
* @summary 删除用户 GPG key
|
|
343
|
-
* @request delete:/user/gpg-keys/{id}
|
|
344
|
-
|
|
345
|
-
----------------------------------
|
|
346
|
-
* @param {string} arg0
|
|
347
|
-
* @param {RequestConfig} arg1 - Other reuqest params
|
|
348
|
-
*/
|
|
349
|
-
export async function deleteGPGKey(
|
|
350
|
-
id: string,
|
|
351
|
-
{req, options, ...axiosConfig}: RequestConfig = {},
|
|
352
|
-
): Promise<CnbRequestResult<DeleteGPGKeyRes, DeleteGPGKeyError>> {
|
|
353
|
-
return await fetch.request<DeleteGPGKeyRes, DeleteGPGKeyError>({
|
|
354
|
-
...axiosConfig,
|
|
355
|
-
_next_req: req,
|
|
356
|
-
options: options,
|
|
357
|
-
url: `/user/gpg-keys/${id}`,
|
|
358
|
-
_apiTag: "/user/gpg-keys/{id}",
|
|
359
|
-
method: "delete",
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
```
|
|
363
|
-
|
|
364
|
-
## 生成SKILL
|
|
61
|
+
> 基于 `@cnb/request` 已内置,无需配置。
|
|
365
62
|
|
|
366
|
-
|
|
63
|
+
## 生成 SKILL
|
|
367
64
|
|
|
368
65
|
### 文件配置
|
|
369
|
-
|
|
66
|
+
|
|
370
67
|
```javascript
|
|
371
68
|
module.exports = {
|
|
372
69
|
target: './template/swagger.json',
|
|
373
70
|
skillsOutputDir: `./`,
|
|
374
71
|
}
|
|
375
|
-
|
|
376
72
|
```
|
|
377
73
|
|
|
378
74
|
### 执行命令
|
|
@@ -381,7 +77,7 @@ module.exports = {
|
|
|
381
77
|
npx csg
|
|
382
78
|
```
|
|
383
79
|
|
|
384
|
-
|
|
80
|
+
生成目录结构:
|
|
385
81
|
|
|
386
82
|
```
|
|
387
83
|
| SKILL.md
|
|
@@ -392,8 +88,20 @@ npx csg
|
|
|
392
88
|
|
|
393
89
|
### 环境变量依赖
|
|
394
90
|
|
|
395
|
-
|
|
91
|
+
- `CNB_API_ENDPOINT`:可选,自定义 API 地址(默认 `https://api.cnb.cool`)
|
|
92
|
+
- `CNB_TOKEN`:OpenAPI 令牌
|
|
93
|
+
- `CNB_WEB_ENDPOINT`:与 `CNB_API_ENDPOINT` 对应的根域名(默认 `https://cnb.cool`)
|
|
94
|
+
|
|
95
|
+
## skills list 命令
|
|
96
|
+
|
|
97
|
+
生成的 Skill 内置 `cnb skills list`,用于列出本地 skills:
|
|
98
|
+
|
|
99
|
+
```shell
|
|
100
|
+
cnb skills list -p -a codebuddy --json
|
|
101
|
+
```
|
|
396
102
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
103
|
+
| 参数 | 说明 |
|
|
104
|
+
| --- | --- |
|
|
105
|
+
| `--json` | 以 JSON 输出 |
|
|
106
|
+
| `-p` | 查询项目(仓库)级 skills |
|
|
107
|
+
| `-a codebuddy` | 按 agent 过滤(此处为当前 NPC 的 agent 名) |
|
package/package.json
CHANGED
package/quick-commands.json
CHANGED
|
@@ -36,6 +36,6 @@
|
|
|
36
36
|
{ "shortName": "get-files", "realTool": "get-pr-files", "description": "获取 PR 附件", "repoOnly": true, "dataTip": "--file-path 附件路径" }
|
|
37
37
|
],
|
|
38
38
|
"skills": [
|
|
39
|
-
{ "shortName": "list", "realTool": "__skills-list__", "description": "列出本地 skills", "custom": true, "dataTip": "-p -a
|
|
39
|
+
{ "shortName": "list", "realTool": "__skills-list__", "description": "列出本地 skills", "custom": true, "dataTip": "-p -a codebuddy --json" }
|
|
40
40
|
]
|
|
41
41
|
}
|