@dazhicheng/utils 1.3.14 → 1.3.16

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 +1 -1
  2. package/src/openapi.ts +54 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dazhicheng/utils",
3
- "version": "1.3.14",
3
+ "version": "1.3.16",
4
4
  "description": "工具库",
5
5
  "main": "./dist/index.esm.js",
6
6
  "type": "module",
package/src/openapi.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { GenerateServiceProps } from "@dazhicheng/openapi";
2
+ import type { APIDataType } from "@dazhicheng/openapi/serviceGenerator";
2
3
  import { ttOpenAPI } from "@dazhicheng/openapi";
3
4
  import process from "node:process";
4
5
 
@@ -55,35 +56,60 @@ export async function runOpenAPI(
55
56
 
56
57
  const services = getServices(env as any);
57
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
+
58
77
  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!;
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);
87
113
  },
88
114
  customFileNames(operationObject: any, apiPath: string) {
89
115
  const segments = apiPath.split("/").filter(Boolean);