@vafast/cli 0.1.6 → 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-LIyNoB1P.js → sync-yT8P8ZRu.js} +4 -23
- 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
|
@@ -113,19 +113,9 @@ function generateTypeDefinition(contract, stripPrefix) {
|
|
|
113
113
|
lines.push(" * ⚠️ 请勿手动修改此文件,使用 `vafast sync` 重新生成");
|
|
114
114
|
lines.push(" */");
|
|
115
115
|
lines.push("");
|
|
116
|
-
lines.push("import type {
|
|
116
|
+
lines.push("import type { RequestConfig, Client, EdenClient, RequestBuilder } from '@vafast/api-client'");
|
|
117
117
|
lines.push("import { eden } from '@vafast/api-client'");
|
|
118
118
|
lines.push("");
|
|
119
|
-
lines.push("/** SSE 回调接口 */");
|
|
120
|
-
lines.push("interface SSECallbacks<T> {");
|
|
121
|
-
lines.push(" onMessage: (data: T) => void");
|
|
122
|
-
lines.push(" onError?: (error: { code: number; message: string }) => void");
|
|
123
|
-
lines.push(" onOpen?: () => void");
|
|
124
|
-
lines.push(" onClose?: () => void");
|
|
125
|
-
lines.push(" onReconnect?: (attempt: number, maxAttempts: number) => void");
|
|
126
|
-
lines.push(" onMaxReconnects?: () => void");
|
|
127
|
-
lines.push("}");
|
|
128
|
-
lines.push("");
|
|
129
119
|
const routeTree = buildRouteTree(contract.routes, stripPrefix);
|
|
130
120
|
lines.push("/** API 契约类型 */");
|
|
131
121
|
lines.push("export type Api = {");
|
|
@@ -192,15 +182,6 @@ function generateClientType(tree, indent) {
|
|
|
192
182
|
function generateMethodSignature(route, method) {
|
|
193
183
|
const params = [];
|
|
194
184
|
const returnType = route.schema?.response ? schemaToType(route.schema.response) : "any";
|
|
195
|
-
if (route.sse) {
|
|
196
|
-
if (route.schema?.query) {
|
|
197
|
-
const queryType = schemaToType(route.schema.query);
|
|
198
|
-
params.push(`query: ${queryType}`);
|
|
199
|
-
}
|
|
200
|
-
params.push(`callbacks: SSECallbacks<${returnType}>`);
|
|
201
|
-
params.push("options?: SSESubscribeOptions");
|
|
202
|
-
return `(${params.join(", ")}) => SSESubscription<${returnType}>`;
|
|
203
|
-
}
|
|
204
185
|
if (route.schema?.body) {
|
|
205
186
|
const bodyType = schemaToType(route.schema.body);
|
|
206
187
|
params.push(`body: ${bodyType}`);
|
|
@@ -210,7 +191,7 @@ function generateMethodSignature(route, method) {
|
|
|
210
191
|
params.push(`query?: ${queryType}`);
|
|
211
192
|
}
|
|
212
193
|
params.push("config?: RequestConfig");
|
|
213
|
-
return `(${params.join(", ")}) =>
|
|
194
|
+
return `(${params.join(", ")}) => RequestBuilder<${returnType}>`;
|
|
214
195
|
}
|
|
215
196
|
/**
|
|
216
197
|
* 构建路由树
|
|
@@ -235,7 +216,7 @@ function buildRouteTree(routes, stripPrefix) {
|
|
|
235
216
|
});
|
|
236
217
|
const node = current.get(key);
|
|
237
218
|
if (i === segments.length - 1) {
|
|
238
|
-
const methodName = route.
|
|
219
|
+
const methodName = route.method.toLowerCase();
|
|
239
220
|
node.methods.set(methodName, route);
|
|
240
221
|
}
|
|
241
222
|
current = node.children;
|
|
@@ -275,7 +256,7 @@ function generateMethodType(route) {
|
|
|
275
256
|
const queryType = schemaToType(route.schema.query);
|
|
276
257
|
parts.push(`query: ${queryType}`);
|
|
277
258
|
}
|
|
278
|
-
if (route.schema?.body
|
|
259
|
+
if (route.schema?.body) {
|
|
279
260
|
const bodyType = schemaToType(route.schema.body);
|
|
280
261
|
parts.push(`body: ${bodyType}`);
|
|
281
262
|
}
|