czh-api 1.0.1 → 1.0.2
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/CHANGELOG.md +6 -0
- package/README.md +5 -0
- package/dist/commands/build.js +2 -2
- package/dist/commands/init.js +3 -2
- package/dist/core/parser.js +8 -1
- package/package.json +1 -1
- package/src/commands/build.ts +3 -2
- package/src/commands/init.ts +3 -2
- package/src/core/parser.ts +8 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -49,6 +49,10 @@ npm install -g czh-api
|
|
|
49
49
|
"excludePaths": [
|
|
50
50
|
"/sys/log",
|
|
51
51
|
"/tool/gen"
|
|
52
|
+
],
|
|
53
|
+
"includePaths": [
|
|
54
|
+
"/sys/user",
|
|
55
|
+
"/sys/role"
|
|
52
56
|
]
|
|
53
57
|
}
|
|
54
58
|
```
|
|
@@ -62,6 +66,7 @@ npm install -g czh-api
|
|
|
62
66
|
| `templates` | `string` | 否 | 指向您的自定义模板目录的路径。默认为 `init` 命令生成的目录。 |
|
|
63
67
|
| `customImports` | `string[]` | 否 | 一个自定义导入语句的数组,会被添加到每个生成的 API 文件的顶部。 |
|
|
64
68
|
| `excludePaths` | `string[]` | 否 | 一个 URL 路径前缀的数组。任何以此数组中前缀开头的 API 都将被忽略。 |
|
|
69
|
+
| `includePaths` | `string[]` | 否 | 一个 URL 路径前缀的数组。如果配置了此项,则只同步以这些前缀开头的 API。 |
|
|
65
70
|
|
|
66
71
|
## 📝 模板配置
|
|
67
72
|
|
package/dist/commands/build.js
CHANGED
|
@@ -18,7 +18,7 @@ exports.handleBuild = void 0;
|
|
|
18
18
|
* @Author: czh
|
|
19
19
|
* @Date: 2025-07-02 10:39:30
|
|
20
20
|
* @LastEditors: Czh
|
|
21
|
-
* @LastEditTime: 2025-
|
|
21
|
+
* @LastEditTime: 2025-12-25 12:40:34
|
|
22
22
|
*/
|
|
23
23
|
const chalk_1 = __importDefault(require("chalk"));
|
|
24
24
|
const path_1 = __importDefault(require("path"));
|
|
@@ -45,7 +45,7 @@ const handleBuild = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
45
45
|
const api = yield SwaggerParser.bundle(response.data);
|
|
46
46
|
console.log(chalk_1.default.green('Swagger document fetched and bundled successfully.'));
|
|
47
47
|
// Process the API document
|
|
48
|
-
const modules = (0, parser_1.processApi)(api, config.excludePaths || []);
|
|
48
|
+
const modules = (0, parser_1.processApi)(api, config.excludePaths || [], config.includePaths || []);
|
|
49
49
|
console.log(chalk_1.default.blue('API processed into modules.'));
|
|
50
50
|
// Clean output directory
|
|
51
51
|
const outputDir = path_1.default.join(process.cwd(), config.outputDir);
|
package/dist/commands/init.js
CHANGED
|
@@ -18,7 +18,7 @@ exports.handleInit = void 0;
|
|
|
18
18
|
* @Author: czh
|
|
19
19
|
* @Date: 2025-07-02 10:39:17
|
|
20
20
|
* @LastEditors: Czh
|
|
21
|
-
* @LastEditTime: 2025-
|
|
21
|
+
* @LastEditTime: 2025-12-25 12:51:48
|
|
22
22
|
*/
|
|
23
23
|
const chalk_1 = __importDefault(require("chalk"));
|
|
24
24
|
const promises_1 = __importDefault(require("fs/promises"));
|
|
@@ -28,7 +28,8 @@ const defaultConfig = {
|
|
|
28
28
|
outputDir: './src/api',
|
|
29
29
|
templates: './czh-api-template',
|
|
30
30
|
customImports: ['import http from "@/utils/http";'],
|
|
31
|
-
excludePaths: []
|
|
31
|
+
excludePaths: [],
|
|
32
|
+
includePaths: []
|
|
32
33
|
};
|
|
33
34
|
const templates = ['api.hbs', 'index.hbs', 'types.hbs'];
|
|
34
35
|
const handleInit = () => __awaiter(void 0, void 0, void 0, function* () {
|
package/dist/core/parser.js
CHANGED
|
@@ -73,14 +73,21 @@ function getModuleNameFromPackage(operation) {
|
|
|
73
73
|
/**
|
|
74
74
|
* Processes the validated OpenAPI document into a structured format for code generation.
|
|
75
75
|
* @param api The bundled OpenAPI document.
|
|
76
|
+
* @param excludePaths Paths to exclude (by prefix).
|
|
77
|
+
* @param includePaths Paths to include (by prefix). If provided, only these paths will be processed.
|
|
76
78
|
* @returns A structured representation of modules and their endpoints.
|
|
77
79
|
*/
|
|
78
|
-
const processApi = (api, excludePaths = []) => {
|
|
80
|
+
const processApi = (api, excludePaths = [], includePaths = []) => {
|
|
79
81
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
80
82
|
const modules = {};
|
|
81
83
|
const allSchemas = ((_a = api.components) === null || _a === void 0 ? void 0 : _a.schemas) || api.definitions || {};
|
|
82
84
|
const moduleFunctionNames = {};
|
|
83
85
|
for (const path in api.paths) {
|
|
86
|
+
// 如果配置了 includePaths,只处理匹配的路径
|
|
87
|
+
if (includePaths.length > 0 && !includePaths.some(include => path.startsWith(include))) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
// 排除 excludePaths 中的路径
|
|
84
91
|
if (excludePaths.some(exclude => path.startsWith(exclude))) {
|
|
85
92
|
continue;
|
|
86
93
|
}
|
package/package.json
CHANGED
package/src/commands/build.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @Author: czh
|
|
4
4
|
* @Date: 2025-07-02 10:39:30
|
|
5
5
|
* @LastEditors: Czh
|
|
6
|
-
* @LastEditTime: 2025-
|
|
6
|
+
* @LastEditTime: 2025-12-25 12:40:34
|
|
7
7
|
*/
|
|
8
8
|
import chalk from 'chalk';
|
|
9
9
|
import path from 'path';
|
|
@@ -23,6 +23,7 @@ interface IConfig {
|
|
|
23
23
|
templates?: string;
|
|
24
24
|
customImports?: string[];
|
|
25
25
|
excludePaths?: string[];
|
|
26
|
+
includePaths?: string[];
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
export const handleBuild = async () => {
|
|
@@ -44,7 +45,7 @@ export const handleBuild = async () => {
|
|
|
44
45
|
console.log(chalk.green('Swagger document fetched and bundled successfully.'));
|
|
45
46
|
|
|
46
47
|
// Process the API document
|
|
47
|
-
const modules = processApi(api, config.excludePaths || []);
|
|
48
|
+
const modules = processApi(api, config.excludePaths || [], config.includePaths || []);
|
|
48
49
|
console.log(chalk.blue('API processed into modules.'));
|
|
49
50
|
|
|
50
51
|
// Clean output directory
|
package/src/commands/init.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @Author: czh
|
|
4
4
|
* @Date: 2025-07-02 10:39:17
|
|
5
5
|
* @LastEditors: Czh
|
|
6
|
-
* @LastEditTime: 2025-
|
|
6
|
+
* @LastEditTime: 2025-12-25 12:51:48
|
|
7
7
|
*/
|
|
8
8
|
import chalk from 'chalk';
|
|
9
9
|
import fs from 'fs/promises';
|
|
@@ -14,7 +14,8 @@ const defaultConfig = {
|
|
|
14
14
|
outputDir: './src/api',
|
|
15
15
|
templates: './czh-api-template',
|
|
16
16
|
customImports: ['import http from "@/utils/http";'],
|
|
17
|
-
excludePaths: []
|
|
17
|
+
excludePaths: [],
|
|
18
|
+
includePaths: []
|
|
18
19
|
};
|
|
19
20
|
|
|
20
21
|
const templates = ['api.hbs', 'index.hbs', 'types.hbs'];
|
package/src/core/parser.ts
CHANGED
|
@@ -109,15 +109,22 @@ function getModuleNameFromPackage(operation: OpenAPIV3.OperationObject): string
|
|
|
109
109
|
/**
|
|
110
110
|
* Processes the validated OpenAPI document into a structured format for code generation.
|
|
111
111
|
* @param api The bundled OpenAPI document.
|
|
112
|
+
* @param excludePaths Paths to exclude (by prefix).
|
|
113
|
+
* @param includePaths Paths to include (by prefix). If provided, only these paths will be processed.
|
|
112
114
|
* @returns A structured representation of modules and their endpoints.
|
|
113
115
|
*/
|
|
114
|
-
export const processApi = (api: OpenAPI.Document, excludePaths: string[] = []): Modules => {
|
|
116
|
+
export const processApi = (api: OpenAPI.Document, excludePaths: string[] = [], includePaths: string[] = []): Modules => {
|
|
115
117
|
const modules: Modules = {};
|
|
116
118
|
const allSchemas = (api as OpenAPIV3.Document).components?.schemas || (api as any).definitions ||{};
|
|
117
119
|
|
|
118
120
|
const moduleFunctionNames: { [moduleName: string]: Set<string> } = {};
|
|
119
121
|
|
|
120
122
|
for (const path in api.paths) {
|
|
123
|
+
// 如果配置了 includePaths,只处理匹配的路径
|
|
124
|
+
if (includePaths.length > 0 && !includePaths.some(include => path.startsWith(include))) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
// 排除 excludePaths 中的路径
|
|
121
128
|
if (excludePaths.some(exclude => path.startsWith(exclude))) {
|
|
122
129
|
continue;
|
|
123
130
|
}
|