@vafast/cli 0.1.2 → 0.1.4
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/cli.js +4 -3
- package/dist/commands/sync.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/{sync-DU-mhmR1.js → sync-Bq9q17kE.js} +25 -10
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { syncTypes } from "./sync-
|
|
1
|
+
import { syncTypes } from "./sync-Bq9q17kE.js";
|
|
2
2
|
import { cac } from "cac";
|
|
3
3
|
|
|
4
4
|
//#region src/cli.ts
|
|
5
5
|
const cli = cac("vafast");
|
|
6
|
-
cli.command("sync", "从服务端同步 API 类型定义").option("--url <url>", "服务端地址(必填)").option("--out <path>", "输出文件路径", { default: "src/api.generated.ts" }).option("--endpoint <path>", "API Spec 接口路径", { default: "/api-spec" }).action(async (options) => {
|
|
6
|
+
cli.command("sync", "从服务端同步 API 类型定义").option("--url <url>", "服务端地址(必填)").option("--out <path>", "输出文件路径", { default: "src/api.generated.ts" }).option("--endpoint <path>", "API Spec 接口路径", { default: "/api-spec" }).option("--strip-prefix <prefix>", "从路径中去掉的前缀(如 /billingRestfulApi)").action(async (options) => {
|
|
7
7
|
if (!options.url) {
|
|
8
8
|
console.error("❌ 请指定服务端地址:--url <url>");
|
|
9
9
|
process.exit(1);
|
|
@@ -11,7 +11,8 @@ cli.command("sync", "从服务端同步 API 类型定义").option("--url <url>",
|
|
|
11
11
|
await syncTypes({
|
|
12
12
|
url: options.url,
|
|
13
13
|
output: options.out,
|
|
14
|
-
endpoint: options.endpoint
|
|
14
|
+
endpoint: options.endpoint,
|
|
15
|
+
stripPrefix: options.stripPrefix
|
|
15
16
|
});
|
|
16
17
|
});
|
|
17
18
|
cli.help();
|
package/dist/commands/sync.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -40,13 +40,22 @@ function arrayToType(schema) {
|
|
|
40
40
|
* 对象类型转换
|
|
41
41
|
*/
|
|
42
42
|
function objectToType(schema) {
|
|
43
|
+
const patternProps = schema.patternProperties;
|
|
44
|
+
if (patternProps) {
|
|
45
|
+
const patterns = Object.values(patternProps);
|
|
46
|
+
if (patterns.length > 0 && patterns[0]) {
|
|
47
|
+
const valueType = schemaToType(patterns[0]);
|
|
48
|
+
return `Record<string, ${valueType}>`;
|
|
49
|
+
}
|
|
50
|
+
return "Record<string, unknown>";
|
|
51
|
+
}
|
|
43
52
|
if (!schema.properties) {
|
|
44
53
|
if (schema.additionalProperties === true) return "Record<string, unknown>";
|
|
45
54
|
if (typeof schema.additionalProperties === "object") {
|
|
46
55
|
const valueType = schemaToType(schema.additionalProperties);
|
|
47
56
|
return `Record<string, ${valueType}>`;
|
|
48
57
|
}
|
|
49
|
-
return "
|
|
58
|
+
return "Record<string, unknown>";
|
|
50
59
|
}
|
|
51
60
|
const required = new Set(schema.required || []);
|
|
52
61
|
const props = [];
|
|
@@ -55,7 +64,7 @@ function objectToType(schema) {
|
|
|
55
64
|
const optional = required.has(key) ? "" : "?";
|
|
56
65
|
props.push(`${key}${optional}: ${propType}`);
|
|
57
66
|
}
|
|
58
|
-
if (props.length === 0) return "
|
|
67
|
+
if (props.length === 0) return "Record<string, unknown>";
|
|
59
68
|
return `{ ${props.join("; ")} }`;
|
|
60
69
|
}
|
|
61
70
|
|
|
@@ -65,7 +74,7 @@ function objectToType(schema) {
|
|
|
65
74
|
* 同步 API 类型
|
|
66
75
|
*/
|
|
67
76
|
async function syncTypes(options) {
|
|
68
|
-
const { url, output, endpoint } = options;
|
|
77
|
+
const { url, output, endpoint, stripPrefix } = options;
|
|
69
78
|
console.log(`🔄 正在从 ${url}${endpoint} 获取契约...`);
|
|
70
79
|
const contractUrl = new URL(endpoint, url).toString();
|
|
71
80
|
let contract;
|
|
@@ -79,7 +88,8 @@ async function syncTypes(options) {
|
|
|
79
88
|
process.exit(1);
|
|
80
89
|
}
|
|
81
90
|
console.log(`✅ 获取到 ${contract.routes.length} 个路由`);
|
|
82
|
-
|
|
91
|
+
if (stripPrefix) console.log(`🔧 去掉路径前缀: ${stripPrefix}`);
|
|
92
|
+
const typeContent = generateTypeDefinition(contract, stripPrefix);
|
|
83
93
|
const outputDir = dirname(output);
|
|
84
94
|
mkdirSync(outputDir, { recursive: true });
|
|
85
95
|
writeFileSync(output, typeContent, "utf-8");
|
|
@@ -93,7 +103,7 @@ async function syncTypes(options) {
|
|
|
93
103
|
/**
|
|
94
104
|
* 生成类型定义文件内容
|
|
95
105
|
*/
|
|
96
|
-
function generateTypeDefinition(contract) {
|
|
106
|
+
function generateTypeDefinition(contract, stripPrefix) {
|
|
97
107
|
const lines = [];
|
|
98
108
|
lines.push("/**");
|
|
99
109
|
lines.push(" * 自动生成的 API 类型定义");
|
|
@@ -103,7 +113,7 @@ function generateTypeDefinition(contract) {
|
|
|
103
113
|
lines.push(" * ⚠️ 请勿手动修改此文件,使用 `vafast sync` 重新生成");
|
|
104
114
|
lines.push(" */");
|
|
105
115
|
lines.push("");
|
|
106
|
-
const routeTree = buildRouteTree(contract.routes);
|
|
116
|
+
const routeTree = buildRouteTree(contract.routes, stripPrefix);
|
|
107
117
|
lines.push("export type Api = {");
|
|
108
118
|
lines.push(generateRouteTreeType(routeTree, 1));
|
|
109
119
|
lines.push("}");
|
|
@@ -113,10 +123,14 @@ function generateTypeDefinition(contract) {
|
|
|
113
123
|
/**
|
|
114
124
|
* 构建路由树
|
|
115
125
|
*/
|
|
116
|
-
function buildRouteTree(routes) {
|
|
126
|
+
function buildRouteTree(routes, stripPrefix) {
|
|
117
127
|
const root = /* @__PURE__ */ new Map();
|
|
128
|
+
const normalizedPrefix = stripPrefix ? "/" + stripPrefix.replace(/^\/+|\/+$/g, "") : void 0;
|
|
118
129
|
for (const route of routes) {
|
|
119
|
-
|
|
130
|
+
let path = route.path;
|
|
131
|
+
if (normalizedPrefix && path.startsWith(normalizedPrefix)) path = path.slice(normalizedPrefix.length) || "/";
|
|
132
|
+
const segments = path.split("/").filter(Boolean);
|
|
133
|
+
if (segments.length === 0) continue;
|
|
120
134
|
let current = root;
|
|
121
135
|
for (let i = 0; i < segments.length; i++) {
|
|
122
136
|
const segment = segments[i];
|
|
@@ -141,8 +155,9 @@ function generateRouteTreeType(tree, indent) {
|
|
|
141
155
|
const lines = [];
|
|
142
156
|
const pad = " ".repeat(indent);
|
|
143
157
|
for (const [key, node] of tree) {
|
|
144
|
-
|
|
145
|
-
|
|
158
|
+
const needsQuotes = /[^a-zA-Z0-9_$]/.test(key) || /^\d/.test(key);
|
|
159
|
+
const propName = needsQuotes ? `'${key}'` : key;
|
|
160
|
+
lines.push(`${pad}${propName}: {`);
|
|
146
161
|
for (const [method, route] of node.methods) {
|
|
147
162
|
const methodType = generateMethodType(route);
|
|
148
163
|
if (route.description) lines.push(`${pad} /** ${route.description} */`);
|