@kevisual/router 0.0.82 → 0.0.84
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/agent/routes/index.ts +16 -10
- package/agent/routes/route-create.ts +3 -3
- package/dist/app.js +98 -69
- package/dist/opencode.d.ts +89 -86
- package/dist/opencode.js +10 -5
- package/dist/router-browser.d.ts +87 -55
- package/dist/router-browser.js +26 -24
- package/dist/router-define.d.ts +84 -55
- package/dist/router.d.ts +92 -86
- package/dist/router.js +28 -62
- package/dist/ws.d.ts +112 -73
- package/package.json +2 -2
- package/readme.md +78 -63
- package/src/app.ts +7 -50
- package/src/opencode.ts +12 -5
- package/src/result/error.ts +22 -0
- package/src/route.ts +92 -78
- package/src/test/app-type.ts +66 -10
- package/src/test/chat.ts +1 -1
- package/src/test/route-ts.ts +15 -0
package/dist/router-define.d.ts
CHANGED
|
@@ -7,6 +7,11 @@ type CustomErrorOptions = {
|
|
|
7
7
|
code?: number;
|
|
8
8
|
message?: string;
|
|
9
9
|
};
|
|
10
|
+
interface throwError {
|
|
11
|
+
throw(code?: number | string, message?: string): void;
|
|
12
|
+
throw(code?: number | string, opts?: CustomErrorOptions): void;
|
|
13
|
+
throw(opts?: CustomErrorOptions): void;
|
|
14
|
+
}
|
|
10
15
|
|
|
11
16
|
declare class MockProcess {
|
|
12
17
|
emitter?: EventEmitter;
|
|
@@ -25,9 +30,20 @@ type RouterContextT = {
|
|
|
25
30
|
code?: number;
|
|
26
31
|
[key: string]: any;
|
|
27
32
|
};
|
|
33
|
+
type BuildRouteContext<M, U> = M extends {
|
|
34
|
+
args?: infer A;
|
|
35
|
+
} ? A extends z.ZodObject<any> ? RouteContext<{
|
|
36
|
+
args?: z.infer<A>;
|
|
37
|
+
}, U> : A extends Record<string, z.ZodTypeAny> ? RouteContext<{
|
|
38
|
+
args?: {
|
|
39
|
+
[K in keyof A]: z.infer<A[K]>;
|
|
40
|
+
};
|
|
41
|
+
}, U> : RouteContext<U> : RouteContext<U>;
|
|
28
42
|
type RouteContext<T = {
|
|
29
43
|
code?: number;
|
|
30
|
-
},
|
|
44
|
+
}, U extends SimpleObject$1 = {}, S = {
|
|
45
|
+
[key: string]: any;
|
|
46
|
+
}> = {
|
|
31
47
|
/**
|
|
32
48
|
* 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
|
|
33
49
|
* 或者不需要登录的,直接调用
|
|
@@ -50,7 +66,14 @@ type RouteContext<T = {
|
|
|
50
66
|
code?: number;
|
|
51
67
|
/** return msg */
|
|
52
68
|
message?: string;
|
|
69
|
+
/**
|
|
70
|
+
* 传递状态
|
|
71
|
+
*/
|
|
53
72
|
state?: S;
|
|
73
|
+
/**
|
|
74
|
+
* 当前routerId
|
|
75
|
+
*/
|
|
76
|
+
currentId?: string;
|
|
54
77
|
/**
|
|
55
78
|
* 当前路径
|
|
56
79
|
*/
|
|
@@ -91,14 +114,12 @@ type RouteContext<T = {
|
|
|
91
114
|
path: string;
|
|
92
115
|
key?: string;
|
|
93
116
|
payload?: any;
|
|
94
|
-
}, ctx?: RouteContext
|
|
95
|
-
[key: string]: any;
|
|
96
|
-
}) => Promise<any>;
|
|
117
|
+
}, ctx?: RouteContext) => Promise<any>;
|
|
97
118
|
index?: number;
|
|
98
|
-
throw?:
|
|
119
|
+
throw?: throwError['throw'];
|
|
99
120
|
/** 是否需要序列化, 使用JSON.stringify和JSON.parse */
|
|
100
121
|
needSerialize?: boolean;
|
|
101
|
-
} & T;
|
|
122
|
+
} & T & U;
|
|
102
123
|
type SimpleObject$1 = Record<string, any>;
|
|
103
124
|
type Run<T extends SimpleObject$1 = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
|
|
104
125
|
type RunMessage = {
|
|
@@ -109,7 +130,7 @@ type RunMessage = {
|
|
|
109
130
|
};
|
|
110
131
|
type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
|
|
111
132
|
type RouteMiddleware = {
|
|
112
|
-
path
|
|
133
|
+
path?: string;
|
|
113
134
|
key?: string;
|
|
114
135
|
id?: string;
|
|
115
136
|
} | string;
|
|
@@ -136,9 +157,11 @@ type RouteOpts<U = {}, T = SimpleObject$1> = {
|
|
|
136
157
|
type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
|
|
137
158
|
declare const pickValue: readonly ["path", "key", "id", "description", "type", "middleware", "metadata"];
|
|
138
159
|
type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
160
|
+
/**
|
|
161
|
+
* @M 是 route的 metadate的类型,默认是 SimpleObject
|
|
162
|
+
* @U 是 RouteContext 里 state的类型
|
|
163
|
+
*/
|
|
164
|
+
declare class Route<M extends SimpleObject$1 = SimpleObject$1, U extends SimpleObject$1 = SimpleObject$1> implements throwError {
|
|
142
165
|
/**
|
|
143
166
|
* 一级路径
|
|
144
167
|
*/
|
|
@@ -148,10 +171,10 @@ declare class Route<U = {
|
|
|
148
171
|
*/
|
|
149
172
|
key?: string;
|
|
150
173
|
id?: string;
|
|
151
|
-
run?: Run
|
|
174
|
+
run?: Run<BuildRouteContext<M, U>>;
|
|
152
175
|
nextRoute?: NextRoute;
|
|
153
176
|
description?: string;
|
|
154
|
-
metadata?:
|
|
177
|
+
metadata?: M;
|
|
155
178
|
middleware?: RouteMiddleware[];
|
|
156
179
|
type?: string;
|
|
157
180
|
/**
|
|
@@ -166,19 +189,19 @@ declare class Route<U = {
|
|
|
166
189
|
} = RouterContextT>(opts: DefineRouteOpts): this;
|
|
167
190
|
define<T extends {
|
|
168
191
|
[key: string]: any;
|
|
169
|
-
} = RouterContextT>(fn: Run<T & U
|
|
192
|
+
} = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
170
193
|
define<T extends {
|
|
171
194
|
[key: string]: any;
|
|
172
|
-
} = RouterContextT>(key: string, fn: Run<T & U
|
|
195
|
+
} = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
173
196
|
define<T extends {
|
|
174
197
|
[key: string]: any;
|
|
175
|
-
} = RouterContextT>(path: string, key: string, fn: Run<T & U
|
|
198
|
+
} = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
176
199
|
update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
|
|
177
200
|
addTo(router: QueryRouter | {
|
|
178
201
|
add: (route: Route) => void;
|
|
179
202
|
[key: string]: any;
|
|
180
203
|
}, opts?: AddOpts): void;
|
|
181
|
-
throw(
|
|
204
|
+
throw(...args: any[]): void;
|
|
182
205
|
}
|
|
183
206
|
/**
|
|
184
207
|
* @parmas overwrite 是否覆盖已存在的route,默认true
|
|
@@ -186,11 +209,11 @@ declare class Route<U = {
|
|
|
186
209
|
type AddOpts = {
|
|
187
210
|
overwrite?: boolean;
|
|
188
211
|
};
|
|
189
|
-
declare class QueryRouter {
|
|
212
|
+
declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements throwError {
|
|
190
213
|
appId: string;
|
|
191
214
|
routes: Route[];
|
|
192
215
|
maxNextRoute: number;
|
|
193
|
-
context?: RouteContext
|
|
216
|
+
context?: RouteContext<T>;
|
|
194
217
|
constructor();
|
|
195
218
|
/**
|
|
196
219
|
* add route
|
|
@@ -218,7 +241,7 @@ declare class QueryRouter {
|
|
|
218
241
|
* @param ctx
|
|
219
242
|
* @returns
|
|
220
243
|
*/
|
|
221
|
-
runRoute(path: string, key: string, ctx?: RouteContext):
|
|
244
|
+
runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
|
|
222
245
|
/**
|
|
223
246
|
* 第一次执行
|
|
224
247
|
* @param message
|
|
@@ -229,9 +252,11 @@ declare class QueryRouter {
|
|
|
229
252
|
path: string;
|
|
230
253
|
key?: string;
|
|
231
254
|
payload?: any;
|
|
232
|
-
}, ctx?: RouteContext & {
|
|
255
|
+
}, ctx?: RouteContext<T> & {
|
|
233
256
|
[key: string]: any;
|
|
234
|
-
}): Promise<
|
|
257
|
+
}): Promise<RouteContext<T, {}, {
|
|
258
|
+
[key: string]: any;
|
|
259
|
+
}>>;
|
|
235
260
|
/**
|
|
236
261
|
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
237
262
|
* @param message
|
|
@@ -243,9 +268,15 @@ declare class QueryRouter {
|
|
|
243
268
|
path?: string;
|
|
244
269
|
key?: string;
|
|
245
270
|
payload?: any;
|
|
246
|
-
}, ctx?: RouteContext & {
|
|
271
|
+
}, ctx?: RouteContext<T> & {
|
|
272
|
+
[key: string]: any;
|
|
273
|
+
}): Promise<RouteContext<T, {}, {
|
|
247
274
|
[key: string]: any;
|
|
248
|
-
}
|
|
275
|
+
}> | {
|
|
276
|
+
code: number;
|
|
277
|
+
body: any;
|
|
278
|
+
message: string;
|
|
279
|
+
}>;
|
|
249
280
|
/**
|
|
250
281
|
* 请求 result 的数据
|
|
251
282
|
* @param message
|
|
@@ -261,9 +292,9 @@ declare class QueryRouter {
|
|
|
261
292
|
}, ctx?: RouteContext & {
|
|
262
293
|
[key: string]: any;
|
|
263
294
|
}): Promise<{
|
|
264
|
-
code:
|
|
295
|
+
code: number;
|
|
265
296
|
data: any;
|
|
266
|
-
message:
|
|
297
|
+
message: string;
|
|
267
298
|
}>;
|
|
268
299
|
/**
|
|
269
300
|
* Router Run获取数据
|
|
@@ -276,12 +307,12 @@ declare class QueryRouter {
|
|
|
276
307
|
path?: string;
|
|
277
308
|
key?: string;
|
|
278
309
|
payload?: any;
|
|
279
|
-
}, ctx?: RouteContext & {
|
|
310
|
+
}, ctx?: RouteContext<T> & {
|
|
280
311
|
[key: string]: any;
|
|
281
312
|
}): Promise<{
|
|
282
|
-
code:
|
|
313
|
+
code: number;
|
|
283
314
|
data: any;
|
|
284
|
-
message:
|
|
315
|
+
message: string;
|
|
285
316
|
}>;
|
|
286
317
|
/**
|
|
287
318
|
* 设置上下文
|
|
@@ -293,12 +324,12 @@ declare class QueryRouter {
|
|
|
293
324
|
/**
|
|
294
325
|
* 获取handle函数, 这里会去执行parse函数
|
|
295
326
|
*/
|
|
296
|
-
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn
|
|
327
|
+
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
|
|
297
328
|
id?: string;
|
|
298
329
|
path?: string;
|
|
299
330
|
key?: string;
|
|
300
331
|
[key: string]: any;
|
|
301
|
-
}, handleContext?: RouteContext) => Promise<{
|
|
332
|
+
}, handleContext?: RouteContext<T>) => Promise<{
|
|
302
333
|
[key: string]: any;
|
|
303
334
|
code: string;
|
|
304
335
|
data?: any;
|
|
@@ -312,24 +343,16 @@ declare class QueryRouter {
|
|
|
312
343
|
message: any;
|
|
313
344
|
data?: undefined;
|
|
314
345
|
}>;
|
|
315
|
-
exportRoutes(): Route<
|
|
316
|
-
[key: string]: any;
|
|
317
|
-
}, SimpleObject$1>[];
|
|
346
|
+
exportRoutes(): Route<SimpleObject$1, SimpleObject$1>[];
|
|
318
347
|
importRoutes(routes: Route[]): void;
|
|
319
348
|
importRouter(router: QueryRouter): void;
|
|
320
|
-
throw(
|
|
321
|
-
|
|
322
|
-
throw(opts?: CustomErrorOptions): void;
|
|
323
|
-
hasRoute(path: string, key?: string): Route<{
|
|
324
|
-
[key: string]: any;
|
|
325
|
-
}, SimpleObject$1>;
|
|
349
|
+
throw(...args: any[]): void;
|
|
350
|
+
hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
326
351
|
findRoute(opts?: {
|
|
327
352
|
path?: string;
|
|
328
353
|
key?: string;
|
|
329
354
|
id?: string;
|
|
330
|
-
}): Route<
|
|
331
|
-
[key: string]: any;
|
|
332
|
-
}, SimpleObject$1>;
|
|
355
|
+
}): Route<SimpleObject$1, SimpleObject$1>;
|
|
333
356
|
createRouteList(opts?: {
|
|
334
357
|
force?: boolean;
|
|
335
358
|
filter?: (route: Route) => boolean;
|
|
@@ -371,10 +394,11 @@ declare class QueryRouter {
|
|
|
371
394
|
[key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
|
|
372
395
|
};
|
|
373
396
|
}
|
|
374
|
-
type QueryRouterServerOpts = {
|
|
397
|
+
type QueryRouterServerOpts<C extends SimpleObject$1 = SimpleObject$1> = {
|
|
375
398
|
handleFn?: HandleFn;
|
|
376
|
-
context?: RouteContext
|
|
399
|
+
context?: RouteContext<C>;
|
|
377
400
|
appId?: string;
|
|
401
|
+
initHandle?: boolean;
|
|
378
402
|
};
|
|
379
403
|
interface HandleFn<T = any> {
|
|
380
404
|
(msg: {
|
|
@@ -391,20 +415,27 @@ interface HandleFn<T = any> {
|
|
|
391
415
|
/**
|
|
392
416
|
* QueryRouterServer
|
|
393
417
|
* @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
|
|
418
|
+
* @template C 自定义 RouteContext 类型
|
|
394
419
|
*/
|
|
395
|
-
declare class QueryRouterServer extends QueryRouter {
|
|
420
|
+
declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> extends QueryRouter<C> {
|
|
396
421
|
appId: string;
|
|
397
422
|
handle: any;
|
|
398
|
-
|
|
423
|
+
context: RouteContext<C>;
|
|
424
|
+
constructor(opts?: QueryRouterServerOpts<C>);
|
|
399
425
|
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
|
|
400
426
|
addRoute(route: Route, opts?: AddOpts): void;
|
|
401
427
|
Route: typeof Route;
|
|
402
|
-
route(opts: RouteOpts
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
route(path: string,
|
|
406
|
-
|
|
407
|
-
|
|
428
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(opts: RouteOpts & {
|
|
429
|
+
metadata?: M;
|
|
430
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
431
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, opts?: RouteOpts & {
|
|
432
|
+
metadata?: M;
|
|
433
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
434
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
|
|
435
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string, opts?: RouteOpts & {
|
|
436
|
+
metadata?: M;
|
|
437
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
438
|
+
prompt(description: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
408
439
|
/**
|
|
409
440
|
* 调用了handle
|
|
410
441
|
* @param param0
|
|
@@ -415,9 +446,7 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
415
446
|
path?: string;
|
|
416
447
|
key?: string;
|
|
417
448
|
payload?: any;
|
|
418
|
-
}, ctx?: RouteContext
|
|
419
|
-
[key: string]: any;
|
|
420
|
-
}): Promise<any>;
|
|
449
|
+
}, ctx?: Partial<RouteContext<C>>): Promise<any>;
|
|
421
450
|
}
|
|
422
451
|
|
|
423
452
|
type RouteObject = {
|