@vafast/cli 0.1.3 → 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
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
|
@@ -74,7 +74,7 @@ function objectToType(schema) {
|
|
|
74
74
|
* 同步 API 类型
|
|
75
75
|
*/
|
|
76
76
|
async function syncTypes(options) {
|
|
77
|
-
const { url, output, endpoint } = options;
|
|
77
|
+
const { url, output, endpoint, stripPrefix } = options;
|
|
78
78
|
console.log(`🔄 正在从 ${url}${endpoint} 获取契约...`);
|
|
79
79
|
const contractUrl = new URL(endpoint, url).toString();
|
|
80
80
|
let contract;
|
|
@@ -88,7 +88,8 @@ async function syncTypes(options) {
|
|
|
88
88
|
process.exit(1);
|
|
89
89
|
}
|
|
90
90
|
console.log(`✅ 获取到 ${contract.routes.length} 个路由`);
|
|
91
|
-
|
|
91
|
+
if (stripPrefix) console.log(`🔧 去掉路径前缀: ${stripPrefix}`);
|
|
92
|
+
const typeContent = generateTypeDefinition(contract, stripPrefix);
|
|
92
93
|
const outputDir = dirname(output);
|
|
93
94
|
mkdirSync(outputDir, { recursive: true });
|
|
94
95
|
writeFileSync(output, typeContent, "utf-8");
|
|
@@ -102,7 +103,7 @@ async function syncTypes(options) {
|
|
|
102
103
|
/**
|
|
103
104
|
* 生成类型定义文件内容
|
|
104
105
|
*/
|
|
105
|
-
function generateTypeDefinition(contract) {
|
|
106
|
+
function generateTypeDefinition(contract, stripPrefix) {
|
|
106
107
|
const lines = [];
|
|
107
108
|
lines.push("/**");
|
|
108
109
|
lines.push(" * 自动生成的 API 类型定义");
|
|
@@ -112,7 +113,7 @@ function generateTypeDefinition(contract) {
|
|
|
112
113
|
lines.push(" * ⚠️ 请勿手动修改此文件,使用 `vafast sync` 重新生成");
|
|
113
114
|
lines.push(" */");
|
|
114
115
|
lines.push("");
|
|
115
|
-
const routeTree = buildRouteTree(contract.routes);
|
|
116
|
+
const routeTree = buildRouteTree(contract.routes, stripPrefix);
|
|
116
117
|
lines.push("export type Api = {");
|
|
117
118
|
lines.push(generateRouteTreeType(routeTree, 1));
|
|
118
119
|
lines.push("}");
|
|
@@ -122,10 +123,14 @@ function generateTypeDefinition(contract) {
|
|
|
122
123
|
/**
|
|
123
124
|
* 构建路由树
|
|
124
125
|
*/
|
|
125
|
-
function buildRouteTree(routes) {
|
|
126
|
+
function buildRouteTree(routes, stripPrefix) {
|
|
126
127
|
const root = /* @__PURE__ */ new Map();
|
|
128
|
+
const normalizedPrefix = stripPrefix ? "/" + stripPrefix.replace(/^\/+|\/+$/g, "") : void 0;
|
|
127
129
|
for (const route of routes) {
|
|
128
|
-
|
|
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;
|
|
129
134
|
let current = root;
|
|
130
135
|
for (let i = 0; i < segments.length; i++) {
|
|
131
136
|
const segment = segments[i];
|