@dazhicheng/utils 1.3.14 → 1.3.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dazhicheng/utils",
3
- "version": "1.3.14",
3
+ "version": "1.3.17",
4
4
  "description": "工具库",
5
5
  "main": "./dist/index.esm.js",
6
6
  "type": "module",
package/src/openapi.ts CHANGED
@@ -1,107 +1,133 @@
1
- import type { GenerateServiceProps } from "@dazhicheng/openapi";
2
- import { ttOpenAPI } from "@dazhicheng/openapi";
3
- import process from "node:process";
4
-
5
- /**
6
- * 将 kebab-case 或 snake_case 转为 PascalCase,如 user-center → UserCenter
7
- */
8
- function toPascalCase(str: string): string {
9
- return str
10
- .split(/[-_]/)
11
- .map(s => s.charAt(0).toUpperCase() + s.slice(1))
12
- .join("");
13
- }
14
-
15
- export async function runOpenAPI(
16
- envConfig: Record<string, string> = {
17
- dev: "192.168.128.215:11000",
18
- sit: "192.168.129.178:9768",
19
- main: "192.168.129.178:9768",
20
- },
21
- servicesData: Record<string, string[]> = {
22
- iam: [],
23
- rule: ["/procurementTicketConfig/delete"],
24
- // basic: [],
25
- },
26
- ) {
27
- const _envConfig = envConfig;
28
-
29
- const envs = Object.keys(_envConfig) as Env[];
30
-
31
- const env = process.argv[2];
32
-
33
- if (!env || !envs.includes(env as any)) {
34
- console.error(`❌ 请传入环境参数: pnpm openapi ${envs.join(" | ")}`);
35
- process.exit(1);
36
- }
37
-
38
- type Env = keyof typeof _envConfig;
39
-
40
- const microServices = servicesData;
41
-
42
- function getServices(env: Env): GenerateServiceProps[] {
43
- const host = _envConfig[env];
44
- return Object.entries(microServices).map(([name, paths]) => ({
45
- schemaPath: `http://${host}/${name}/v3/api-docs`,
46
- serversPath: "./src/api",
47
- projectName: name,
48
- namespace: `${toPascalCase(name)}API`,
49
- requestLibPath: "import request from '@/utils/http'",
50
- // 指定生成某些接口,不传则全量生成
51
- specifiedPaths: paths,
52
- apiPrefix: name,
53
- }));
54
- }
55
-
56
- const services = getServices(env as any);
57
-
58
- const commonHook = {
59
- customFunctionName(data: any) {
60
- const name = data.operationId!.split("_")[0];
61
- const reserved = [
62
- "export",
63
- "import",
64
- "delete",
65
- "default",
66
- "class",
67
- "new",
68
- "return",
69
- "switch",
70
- "case",
71
- "throw",
72
- "try",
73
- "catch",
74
- "finally",
75
- "const",
76
- "let",
77
- "var",
78
- "function",
79
- "page",
80
- "list",
81
- ];
82
- if (reserved.includes(name)) {
83
- const tag = data.path?.split("/").filter(Boolean)[0] || "";
84
- return `${name}${tag.charAt(0).toUpperCase() + tag.slice(1)}`;
85
- }
86
- return name!;
87
- },
88
- customFileNames(operationObject: any, apiPath: string) {
89
- const segments = apiPath.split("/").filter(Boolean);
90
- return segments[0] ? [segments[0]] : [];
91
- },
92
- };
93
-
94
- for (const config of services) {
95
- console.log(`🚀 [${env}] 正在生成: ${config.projectName} -> ${config.schemaPath}`);
96
- try {
97
- await ttOpenAPI({
98
- ...config,
99
- isCamelCase: true,
100
- hook: commonHook,
101
- });
102
- console.log(`✅ ${config.projectName} 生成完成`);
103
- } catch (error: any) {
104
- console.error(`❌ ${config.projectName} 生成失败: ${error.message}`);
105
- }
106
- }
107
- }
1
+ import type { GenerateServiceProps } from "@dazhicheng/openapi";
2
+ import type { APIDataType } from "@dazhicheng/openapi/serviceGenerator";
3
+ import { ttOpenAPI } from "@dazhicheng/openapi";
4
+ import process from "node:process";
5
+
6
+ /**
7
+ * 将 kebab-case 或 snake_case 转为 PascalCase,如 user-center → UserCenter
8
+ */
9
+ function toPascalCase(str: string): string {
10
+ return str
11
+ .split(/[-_]/)
12
+ .map(s => s.charAt(0).toUpperCase() + s.slice(1))
13
+ .join("");
14
+ }
15
+
16
+ export async function runOpenAPI(
17
+ envConfig: Record<string, string> = {
18
+ dev: "192.168.128.215:11000",
19
+ sit: "192.168.129.178:9768",
20
+ main: "192.168.129.178:9768",
21
+ },
22
+ servicesData: Record<string, string[]> = {
23
+ iam: [],
24
+ rule: ["/procurementTicketConfig/delete"],
25
+ // basic: [],
26
+ },
27
+ ) {
28
+ const _envConfig = envConfig;
29
+
30
+ const envs = Object.keys(_envConfig) as Env[];
31
+
32
+ const env = process.argv[2];
33
+
34
+ if (!env || !envs.includes(env as any)) {
35
+ console.error(`❌ 请传入环境参数: pnpm openapi ${envs.join(" | ")}`);
36
+ process.exit(1);
37
+ }
38
+
39
+ type Env = keyof typeof _envConfig;
40
+
41
+ const microServices = servicesData;
42
+
43
+ function getServices(env: Env): GenerateServiceProps[] {
44
+ const host = _envConfig[env];
45
+ return Object.entries(microServices).map(([name, paths]) => ({
46
+ schemaPath: `http://${host}/${name}/v3/api-docs`,
47
+ serversPath: "./src/api",
48
+ projectName: name,
49
+ namespace: `${toPascalCase(name)}API`,
50
+ requestLibPath: "import request from '@/utils/http'",
51
+ // 指定生成某些接口,不传则全量生成
52
+ specifiedPaths: paths,
53
+ apiPrefix: name,
54
+ }));
55
+ }
56
+
57
+ const services = getServices(env as any);
58
+
59
+ /**
60
+ * @description 将路径模板转成名称:按 `/` 分段,去掉路径变量(如 `${id}`、`{id}`),
61
+ * 再把剩余段从右往左拼成驼峰(最后一段保持原样作为基底,向左各段首字母大写后拼到前面)。
62
+ * 例如:`/api/user/${id}/profile` → `profileUserApi`,`/order/{orderId}` → `order`。
63
+ */
64
+ function pathToName(pathTemplate: string): string {
65
+ // 同时过滤 ${var} 与 {var} 形式的路径变量
66
+ const VARIABLE_PATTERN = /^\$?\{[^}]+\}$/;
67
+ const parts = pathTemplate.split("/").filter(part => part && !VARIABLE_PATTERN.test(part));
68
+ return parts
69
+ .slice()
70
+ .reverse()
71
+ .reduce<string>(
72
+ (name, tag, index) => (index === 0 ? tag : name + tag.charAt(0).toUpperCase() + tag.slice(1)),
73
+ "",
74
+ );
75
+ }
76
+
77
+ const commonHook = {
78
+ customFunctionName(data: APIDataType & { _namesWithNumericSuffix: Set<string> }) {
79
+ // const reserved = [
80
+ // "export",
81
+ // "import",
82
+ // "delete",
83
+ // "default",
84
+ // "class",
85
+ // "new",
86
+ // "return",
87
+ // "switch",
88
+ // "case",
89
+ // "throw",
90
+ // "try",
91
+ // "catch",
92
+ // "finally",
93
+ // "const",
94
+ // "let",
95
+ // "var",
96
+ // "function",
97
+ // // 配合后端的特殊字段
98
+ // "page",
99
+ // "list",
100
+ // "matchConfig",
101
+ // "enableDisable",
102
+ // "exportExcel",
103
+ // "selectList",
104
+ // "queryById",
105
+ // ];
106
+ // const name = data.operationId!.split("_")[0]!;
107
+ // if (reserved.includes(name)) {
108
+ // const tag = data.path?.split("/").filter(Boolean)[0] || "";
109
+ // return `${name}${tag.charAt(0).toUpperCase() + tag.slice(1)}`;
110
+ // }
111
+ // return name;
112
+ return pathToName(data.path);
113
+ },
114
+ customFileNames(operationObject: any, apiPath: string) {
115
+ const segments = apiPath.split("/").filter(Boolean);
116
+ return segments[0] ? [segments[0]] : [];
117
+ },
118
+ };
119
+
120
+ for (const config of services) {
121
+ console.log(`🚀 [${env}] 正在生成: ${config.projectName} -> ${config.schemaPath}`);
122
+ try {
123
+ await ttOpenAPI({
124
+ ...config,
125
+ isCamelCase: true,
126
+ hook: commonHook,
127
+ });
128
+ console.log(`✅ ${config.projectName} 生成完成`);
129
+ } catch (error: any) {
130
+ console.error(`❌ ${config.projectName} 生成失败: ${error.message}`);
131
+ }
132
+ }
133
+ }