@dazhicheng/utils 1.3.12 → 1.3.13

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/openapi.ts +105 -95
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dazhicheng/utils",
3
- "version": "1.3.12",
3
+ "version": "1.3.13",
4
4
  "description": "工具库",
5
5
  "main": "./dist/index.esm.js",
6
6
  "type": "module",
@@ -55,7 +55,7 @@
55
55
  "lodash-es": "^4.17.23",
56
56
  "tailwind-merge": "^3.5.0",
57
57
  "vue": "^3.5.21",
58
- "@dazhicheng/openapi": "1.1.2"
58
+ "@dazhicheng/openapi": "1.1.3"
59
59
  },
60
60
  "scripts": {
61
61
  "build": "rimraf dist && node ../../scripts/build.js utils",
package/src/openapi.ts CHANGED
@@ -1,95 +1,105 @@
1
- import type { GenerateServiceProps } from "@dazhicheng/openapi";
2
- import { ttOpenAPI } from "@dazhicheng/openapi";
3
- import process from "node:process";
4
-
5
- export async function runOpenAPI(
6
- envConfig: Record<string, string> = {
7
- dev: "192.168.128.215:11000",
8
- sit: "192.168.129.178:9768",
9
- main: "192.168.129.178:9768",
10
- },
11
- servicesData: Record<string, string[]> = {
12
- iam: [],
13
- rule: ["/procurementTicketConfig/delete"],
14
- // basic: [],
15
- },
16
- ) {
17
- const _envConfig = envConfig;
18
-
19
- const envs = Object.keys(_envConfig) as Env[];
20
-
21
- const env = process.argv[2];
22
-
23
- if (!env || !envs.includes(env as any)) {
24
- console.error(`❌ 请传入环境参数: pnpm openapi ${envs.join(" | ")}`);
25
- process.exit(1);
26
- }
27
-
28
- type Env = keyof typeof _envConfig;
29
-
30
- const microServices = servicesData;
31
-
32
- function getServices(env: Env): GenerateServiceProps[] {
33
- const host = _envConfig[env];
34
- return Object.entries(microServices).map(([name, paths]) => ({
35
- schemaPath: `http://${host}/${name}/v3/api-docs`,
36
- serversPath: "./src/api",
37
- projectName: name,
38
- namespace: `${name.charAt(0).toUpperCase()}${name.slice(1)}API`,
39
- requestLibPath: "import request from '@/utils/http'",
40
- // 指定生成某些接口,不传则全量生成
41
- specifiedPaths: paths,
42
- apiPrefix: name,
43
- }));
44
- }
45
-
46
- const services = getServices(env as any);
47
-
48
- const commonHook = {
49
- customFunctionName(data: any) {
50
- const name = data.operationId!;
51
- const reserved = [
52
- "export",
53
- "import",
54
- "delete",
55
- "default",
56
- "class",
57
- "new",
58
- "return",
59
- "switch",
60
- "case",
61
- "throw",
62
- "try",
63
- "catch",
64
- "finally",
65
- "const",
66
- "let",
67
- "var",
68
- "function",
69
- ];
70
- if (reserved.includes(name)) {
71
- const tag = data.path?.split("/").filter(Boolean)[0] || "";
72
- return `${name}${tag.charAt(0).toUpperCase() + tag.slice(1)}`;
73
- }
74
- return name!;
75
- },
76
- customFileNames(operationObject: any, apiPath: string) {
77
- const segments = apiPath.split("/").filter(Boolean);
78
- return segments[0] ? [segments[0]] : [];
79
- },
80
- };
81
-
82
- for (const config of services) {
83
- console.log(`🚀 [${env}] 正在生成: ${config.projectName} -> ${config.schemaPath}`);
84
- try {
85
- await ttOpenAPI({
86
- ...config,
87
- isCamelCase: true,
88
- hook: commonHook,
89
- });
90
- console.log(`✅ ${config.projectName} 生成完成`);
91
- } catch (error: any) {
92
- console.error(`❌ ${config.projectName} 生成失败: ${error.message}`);
93
- }
94
- }
95
- }
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!;
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
+ ];
80
+ if (reserved.includes(name)) {
81
+ const tag = data.path?.split("/").filter(Boolean)[0] || "";
82
+ return `${name}${tag.charAt(0).toUpperCase() + tag.slice(1)}`;
83
+ }
84
+ return name!;
85
+ },
86
+ customFileNames(operationObject: any, apiPath: string) {
87
+ const segments = apiPath.split("/").filter(Boolean);
88
+ return segments[0] ? [segments[0]] : [];
89
+ },
90
+ };
91
+
92
+ for (const config of services) {
93
+ console.log(`🚀 [${env}] 正在生成: ${config.projectName} -> ${config.schemaPath}`);
94
+ try {
95
+ await ttOpenAPI({
96
+ ...config,
97
+ isCamelCase: true,
98
+ hook: commonHook,
99
+ });
100
+ console.log(`✅ ${config.projectName} 生成完成`);
101
+ } catch (error: any) {
102
+ console.error(`❌ ${config.projectName} 生成失败: ${error.message}`);
103
+ }
104
+ }
105
+ }