@vafast/cli 0.1.8 → 0.2.1
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 +1 -1
- package/dist/commands/sync.d.ts +49 -2
- package/dist/index.js +1 -1
- package/dist/{sync-CroyE4Do.js → sync-yT8P8ZRu.js} +4 -27
- package/package.json +1 -1
package/dist/cli.js
CHANGED
package/dist/commands/sync.d.ts
CHANGED
|
@@ -3,14 +3,61 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 从服务端拉取契约并生成 TypeScript 类型定义
|
|
5
5
|
*/
|
|
6
|
-
interface SyncOptions {
|
|
6
|
+
export interface SyncOptions {
|
|
7
7
|
url: string;
|
|
8
8
|
output: string;
|
|
9
9
|
endpoint: string;
|
|
10
10
|
stripPrefix?: string;
|
|
11
11
|
}
|
|
12
|
+
export interface RouteContract {
|
|
13
|
+
method: string;
|
|
14
|
+
path: string;
|
|
15
|
+
name?: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
/** 是否为 SSE 端点 */
|
|
18
|
+
sse?: boolean;
|
|
19
|
+
schema?: {
|
|
20
|
+
body?: unknown;
|
|
21
|
+
query?: unknown;
|
|
22
|
+
params?: unknown;
|
|
23
|
+
response?: unknown;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export interface ApiContract {
|
|
27
|
+
version: string;
|
|
28
|
+
generatedAt: string;
|
|
29
|
+
routes: RouteContract[];
|
|
30
|
+
}
|
|
12
31
|
/**
|
|
13
32
|
* 同步 API 类型
|
|
14
33
|
*/
|
|
15
34
|
export declare function syncTypes(options: SyncOptions): Promise<void>;
|
|
16
|
-
|
|
35
|
+
/**
|
|
36
|
+
* 生成类型定义文件内容
|
|
37
|
+
*/
|
|
38
|
+
export declare function generateTypeDefinition(contract: ApiContract, stripPrefix?: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* 生成客户端接口类型(带完整方法签名,IDE 友好)
|
|
41
|
+
*/
|
|
42
|
+
export declare function generateClientType(tree: Map<string, RouteTreeNode>, indent: number): string;
|
|
43
|
+
/**
|
|
44
|
+
* 生成方法签名(函数类型)
|
|
45
|
+
*/
|
|
46
|
+
export declare function generateMethodSignature(route: RouteContract, method: string): string;
|
|
47
|
+
export interface RouteTreeNode {
|
|
48
|
+
methods: Map<string, RouteContract>;
|
|
49
|
+
children: Map<string, RouteTreeNode>;
|
|
50
|
+
isDynamic: boolean;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 构建路由树
|
|
54
|
+
*/
|
|
55
|
+
export declare function buildRouteTree(routes: RouteContract[], stripPrefix?: string): Map<string, RouteTreeNode>;
|
|
56
|
+
/**
|
|
57
|
+
* 生成路由树的类型定义
|
|
58
|
+
*/
|
|
59
|
+
export declare function generateRouteTreeType(tree: Map<string, RouteTreeNode>, indent: number): string;
|
|
60
|
+
/**
|
|
61
|
+
* 生成方法类型
|
|
62
|
+
*/
|
|
63
|
+
export declare function generateMethodType(route: RouteContract): string;
|
package/dist/index.js
CHANGED
|
@@ -105,7 +105,6 @@ async function syncTypes(options) {
|
|
|
105
105
|
*/
|
|
106
106
|
function generateTypeDefinition(contract, stripPrefix) {
|
|
107
107
|
const lines = [];
|
|
108
|
-
const hasSSE = contract.routes.some((route) => route.sse);
|
|
109
108
|
lines.push("/**");
|
|
110
109
|
lines.push(" * 自动生成的 API 类型定义");
|
|
111
110
|
lines.push(` * 生成时间: ${contract.generatedAt}`);
|
|
@@ -114,22 +113,9 @@ function generateTypeDefinition(contract, stripPrefix) {
|
|
|
114
113
|
lines.push(" * ⚠️ 请勿手动修改此文件,使用 `vafast sync` 重新生成");
|
|
115
114
|
lines.push(" */");
|
|
116
115
|
lines.push("");
|
|
117
|
-
|
|
118
|
-
else lines.push("import type { ApiResponse, RequestConfig, Client, EdenClient } from '@vafast/api-client'");
|
|
116
|
+
lines.push("import type { RequestConfig, Client, EdenClient, RequestBuilder } from '@vafast/api-client'");
|
|
119
117
|
lines.push("import { eden } from '@vafast/api-client'");
|
|
120
118
|
lines.push("");
|
|
121
|
-
if (hasSSE) {
|
|
122
|
-
lines.push("/** SSE 回调接口 */");
|
|
123
|
-
lines.push("interface SSECallbacks<T> {");
|
|
124
|
-
lines.push(" onMessage: (data: T) => void");
|
|
125
|
-
lines.push(" onError?: (error: { code: number; message: string }) => void");
|
|
126
|
-
lines.push(" onOpen?: () => void");
|
|
127
|
-
lines.push(" onClose?: () => void");
|
|
128
|
-
lines.push(" onReconnect?: (attempt: number, maxAttempts: number) => void");
|
|
129
|
-
lines.push(" onMaxReconnects?: () => void");
|
|
130
|
-
lines.push("}");
|
|
131
|
-
lines.push("");
|
|
132
|
-
}
|
|
133
119
|
const routeTree = buildRouteTree(contract.routes, stripPrefix);
|
|
134
120
|
lines.push("/** API 契约类型 */");
|
|
135
121
|
lines.push("export type Api = {");
|
|
@@ -196,15 +182,6 @@ function generateClientType(tree, indent) {
|
|
|
196
182
|
function generateMethodSignature(route, method) {
|
|
197
183
|
const params = [];
|
|
198
184
|
const returnType = route.schema?.response ? schemaToType(route.schema.response) : "any";
|
|
199
|
-
if (route.sse) {
|
|
200
|
-
if (route.schema?.query) {
|
|
201
|
-
const queryType = schemaToType(route.schema.query);
|
|
202
|
-
params.push(`query: ${queryType}`);
|
|
203
|
-
}
|
|
204
|
-
params.push(`callbacks: SSECallbacks<${returnType}>`);
|
|
205
|
-
params.push("options?: SSESubscribeOptions");
|
|
206
|
-
return `(${params.join(", ")}) => SSESubscription<${returnType}>`;
|
|
207
|
-
}
|
|
208
185
|
if (route.schema?.body) {
|
|
209
186
|
const bodyType = schemaToType(route.schema.body);
|
|
210
187
|
params.push(`body: ${bodyType}`);
|
|
@@ -214,7 +191,7 @@ function generateMethodSignature(route, method) {
|
|
|
214
191
|
params.push(`query?: ${queryType}`);
|
|
215
192
|
}
|
|
216
193
|
params.push("config?: RequestConfig");
|
|
217
|
-
return `(${params.join(", ")}) =>
|
|
194
|
+
return `(${params.join(", ")}) => RequestBuilder<${returnType}>`;
|
|
218
195
|
}
|
|
219
196
|
/**
|
|
220
197
|
* 构建路由树
|
|
@@ -239,7 +216,7 @@ function buildRouteTree(routes, stripPrefix) {
|
|
|
239
216
|
});
|
|
240
217
|
const node = current.get(key);
|
|
241
218
|
if (i === segments.length - 1) {
|
|
242
|
-
const methodName = route.
|
|
219
|
+
const methodName = route.method.toLowerCase();
|
|
243
220
|
node.methods.set(methodName, route);
|
|
244
221
|
}
|
|
245
222
|
current = node.children;
|
|
@@ -279,7 +256,7 @@ function generateMethodType(route) {
|
|
|
279
256
|
const queryType = schemaToType(route.schema.query);
|
|
280
257
|
parts.push(`query: ${queryType}`);
|
|
281
258
|
}
|
|
282
|
-
if (route.schema?.body
|
|
259
|
+
if (route.schema?.body) {
|
|
283
260
|
const bodyType = schemaToType(route.schema.body);
|
|
284
261
|
parts.push(`body: ${bodyType}`);
|
|
285
262
|
}
|