@vafast/cli 0.1.5 → 0.1.8

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,4 +1,4 @@
1
- import { syncTypes } from "./sync-CS1-nDMt.js";
1
+ import { syncTypes } from "./sync-CroyE4Do.js";
2
2
  import { cac } from "cac";
3
3
 
4
4
  //#region src/cli.ts
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import { schemaToType, syncTypes } from "./sync-CS1-nDMt.js";
1
+ import { schemaToType, syncTypes } from "./sync-CroyE4Do.js";
2
2
 
3
3
  export { schemaToType, syncTypes };
@@ -105,6 +105,7 @@ 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);
108
109
  lines.push("/**");
109
110
  lines.push(" * 自动生成的 API 类型定义");
110
111
  lines.push(` * 生成时间: ${contract.generatedAt}`);
@@ -113,9 +114,22 @@ function generateTypeDefinition(contract, stripPrefix) {
113
114
  lines.push(" * ⚠️ 请勿手动修改此文件,使用 `vafast sync` 重新生成");
114
115
  lines.push(" */");
115
116
  lines.push("");
116
- lines.push("import type { ApiResponse, RequestConfig, Client, EdenClient } from '@vafast/api-client'");
117
+ if (hasSSE) lines.push("import type { ApiResponse, RequestConfig, Client, EdenClient, SSESubscription, SSESubscribeOptions } from '@vafast/api-client'");
118
+ else lines.push("import type { ApiResponse, RequestConfig, Client, EdenClient } from '@vafast/api-client'");
117
119
  lines.push("import { eden } from '@vafast/api-client'");
118
120
  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
+ }
119
133
  const routeTree = buildRouteTree(contract.routes, stripPrefix);
120
134
  lines.push("/** API 契约类型 */");
121
135
  lines.push("export type Api = {");
@@ -181,6 +195,16 @@ function generateClientType(tree, indent) {
181
195
  */
182
196
  function generateMethodSignature(route, method) {
183
197
  const params = [];
198
+ 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
+ }
184
208
  if (route.schema?.body) {
185
209
  const bodyType = schemaToType(route.schema.body);
186
210
  params.push(`body: ${bodyType}`);
@@ -190,7 +214,6 @@ function generateMethodSignature(route, method) {
190
214
  params.push(`query?: ${queryType}`);
191
215
  }
192
216
  params.push("config?: RequestConfig");
193
- const returnType = route.schema?.response ? schemaToType(route.schema.response) : "any";
194
217
  return `(${params.join(", ")}) => Promise<ApiResponse<${returnType}>>`;
195
218
  }
196
219
  /**
@@ -215,7 +238,10 @@ function buildRouteTree(routes, stripPrefix) {
215
238
  isDynamic
216
239
  });
217
240
  const node = current.get(key);
218
- if (i === segments.length - 1) node.methods.set(route.method.toLowerCase(), route);
241
+ if (i === segments.length - 1) {
242
+ const methodName = route.sse ? "sse" : route.method.toLowerCase();
243
+ node.methods.set(methodName, route);
244
+ }
219
245
  current = node.children;
220
246
  }
221
247
  }
@@ -253,7 +279,7 @@ function generateMethodType(route) {
253
279
  const queryType = schemaToType(route.schema.query);
254
280
  parts.push(`query: ${queryType}`);
255
281
  }
256
- if (route.schema?.body) {
282
+ if (route.schema?.body && !route.sse) {
257
283
  const bodyType = schemaToType(route.schema.body);
258
284
  parts.push(`body: ${bodyType}`);
259
285
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vafast/cli",
3
- "version": "0.1.5",
3
+ "version": "0.1.8",
4
4
  "description": "Vafast CLI - 类型同步和开发工具",
5
5
  "type": "module",
6
6
  "bin": {
@@ -39,7 +39,6 @@
39
39
  "@types/node": "^22.0.0",
40
40
  "tsdown": "^0.2.0",
41
41
  "typescript": "^5.4.5",
42
- "vafast": "^0.5.1",
43
42
  "vitest": "^2.0.0"
44
43
  }
45
44
  }