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