@dazhicheng/utils 1.3.36 → 1.3.38

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.36",
3
+ "version": "1.3.38",
4
4
  "description": "工具库",
5
5
  "main": "./dist/index.esm.js",
6
6
  "type": "module",
@@ -51,7 +51,7 @@
51
51
  "defu": "^6.1.4",
52
52
  "klona": "^2.0.6",
53
53
  "tailwind-merge": "^3.5.0",
54
- "@dazhicheng/openapi": "1.1.9"
54
+ "@dazhicheng/openapi": "1.1.10"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "axios": "^1.0.0",
package/src/openapi.ts CHANGED
@@ -3,6 +3,8 @@ import type { APIDataType } from "@dazhicheng/openapi/serviceGenerator";
3
3
  import { ttOpenAPI } from "@dazhicheng/openapi";
4
4
  import process from "node:process";
5
5
 
6
+ type DevConfig = "dev" | "sit" | "main";
7
+
6
8
  /**
7
9
  * 将 kebab-case 或 snake_case 转为 PascalCase,如 user-center → UserCenter
8
10
  */
@@ -13,6 +15,18 @@ function toPascalCase(str: string): string {
13
15
  .join("");
14
16
  }
15
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
+
16
30
  export async function runOpenAPI(
17
31
  envConfig: Record<string, string> = {
18
32
  dev: "192.168.128.215:11000",
@@ -20,10 +34,18 @@ export async function runOpenAPI(
20
34
  main: "192.168.129.178:9768",
21
35
  },
22
36
  servicesData: Record<string, string[]> = {
23
- iam: [],
24
- rule: ["/procurementTicketConfig/delete"],
37
+ // iam: [],
38
+ // rule: ["/procurementTicketConfig/delete"],
25
39
  // basic: [],
26
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
+ },
27
49
  ) {
28
50
  const _envConfig = envConfig;
29
51
 
@@ -54,7 +76,18 @@ export async function runOpenAPI(
54
76
  }));
55
77
  }
56
78
 
57
- const services = getServices(env as any);
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
+ }
58
91
 
59
92
  /**
60
93
  * @description 将路径模板转成名称:按 `/` 分段,去掉路径变量(如 `${id}`、`{id}`),
@@ -120,8 +153,10 @@ export async function runOpenAPI(
120
153
  for (const config of services) {
121
154
  console.log(`🚀 [${env}] 正在生成: ${config.projectName} -> ${config.schemaPath}`);
122
155
  try {
156
+ const authHeader = await resolveAuthorization();
123
157
  await ttOpenAPI({
124
158
  ...config,
159
+ authorization: authHeader,
125
160
  isCamelCase: true,
126
161
  hook: commonHook,
127
162
  });