@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.d.ts
CHANGED
|
@@ -32,12 +32,20 @@ declare class CustomError extends Error {
|
|
|
32
32
|
* @returns
|
|
33
33
|
*/
|
|
34
34
|
static isError(error: unknown): error is CustomError;
|
|
35
|
+
static throw(code?: number | string, message?: string): void;
|
|
36
|
+
static throw(code?: number | string, opts?: CustomErrorOptions): void;
|
|
37
|
+
static throw(opts?: CustomErrorOptions): void;
|
|
35
38
|
parse(e?: CustomError): {
|
|
36
39
|
code: number;
|
|
37
40
|
data: any;
|
|
38
41
|
message: string;
|
|
39
42
|
};
|
|
40
43
|
}
|
|
44
|
+
interface throwError {
|
|
45
|
+
throw(code?: number | string, message?: string): void;
|
|
46
|
+
throw(code?: number | string, opts?: CustomErrorOptions): void;
|
|
47
|
+
throw(opts?: CustomErrorOptions): void;
|
|
48
|
+
}
|
|
41
49
|
|
|
42
50
|
declare class MockProcess {
|
|
43
51
|
emitter?: EventEmitter;
|
|
@@ -72,9 +80,20 @@ type RouterContextT = {
|
|
|
72
80
|
code?: number;
|
|
73
81
|
[key: string]: any;
|
|
74
82
|
};
|
|
83
|
+
type BuildRouteContext<M, U> = M extends {
|
|
84
|
+
args?: infer A;
|
|
85
|
+
} ? A extends z.ZodObject<any> ? RouteContext<{
|
|
86
|
+
args?: z.infer<A>;
|
|
87
|
+
}, U> : A extends Record<string, z.ZodTypeAny> ? RouteContext<{
|
|
88
|
+
args?: {
|
|
89
|
+
[K in keyof A]: z.infer<A[K]>;
|
|
90
|
+
};
|
|
91
|
+
}, U> : RouteContext<U> : RouteContext<U>;
|
|
75
92
|
type RouteContext<T = {
|
|
76
93
|
code?: number;
|
|
77
|
-
},
|
|
94
|
+
}, U extends SimpleObject$1 = {}, S = {
|
|
95
|
+
[key: string]: any;
|
|
96
|
+
}> = {
|
|
78
97
|
/**
|
|
79
98
|
* 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
|
|
80
99
|
* 或者不需要登录的,直接调用
|
|
@@ -97,7 +116,14 @@ type RouteContext<T = {
|
|
|
97
116
|
code?: number;
|
|
98
117
|
/** return msg */
|
|
99
118
|
message?: string;
|
|
119
|
+
/**
|
|
120
|
+
* 传递状态
|
|
121
|
+
*/
|
|
100
122
|
state?: S;
|
|
123
|
+
/**
|
|
124
|
+
* 当前routerId
|
|
125
|
+
*/
|
|
126
|
+
currentId?: string;
|
|
101
127
|
/**
|
|
102
128
|
* 当前路径
|
|
103
129
|
*/
|
|
@@ -138,14 +164,12 @@ type RouteContext<T = {
|
|
|
138
164
|
path: string;
|
|
139
165
|
key?: string;
|
|
140
166
|
payload?: any;
|
|
141
|
-
}, ctx?: RouteContext
|
|
142
|
-
[key: string]: any;
|
|
143
|
-
}) => Promise<any>;
|
|
167
|
+
}, ctx?: RouteContext) => Promise<any>;
|
|
144
168
|
index?: number;
|
|
145
|
-
throw?:
|
|
169
|
+
throw?: throwError['throw'];
|
|
146
170
|
/** 是否需要序列化, 使用JSON.stringify和JSON.parse */
|
|
147
171
|
needSerialize?: boolean;
|
|
148
|
-
} & T;
|
|
172
|
+
} & T & U;
|
|
149
173
|
type SimpleObject$1 = Record<string, any>;
|
|
150
174
|
type Run<T extends SimpleObject$1 = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
|
|
151
175
|
type RunMessage = {
|
|
@@ -156,7 +180,7 @@ type RunMessage = {
|
|
|
156
180
|
};
|
|
157
181
|
type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
|
|
158
182
|
type RouteMiddleware = {
|
|
159
|
-
path
|
|
183
|
+
path?: string;
|
|
160
184
|
key?: string;
|
|
161
185
|
id?: string;
|
|
162
186
|
} | string;
|
|
@@ -197,9 +221,11 @@ declare const tool: {
|
|
|
197
221
|
/** */
|
|
198
222
|
declare const createSkill: <T = SimpleObject$1>(skill: Skill<T>) => Skill<T>;
|
|
199
223
|
type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
224
|
+
/**
|
|
225
|
+
* @M 是 route的 metadate的类型,默认是 SimpleObject
|
|
226
|
+
* @U 是 RouteContext 里 state的类型
|
|
227
|
+
*/
|
|
228
|
+
declare class Route<M extends SimpleObject$1 = SimpleObject$1, U extends SimpleObject$1 = SimpleObject$1> implements throwError {
|
|
203
229
|
/**
|
|
204
230
|
* 一级路径
|
|
205
231
|
*/
|
|
@@ -209,10 +235,10 @@ declare class Route<U = {
|
|
|
209
235
|
*/
|
|
210
236
|
key?: string;
|
|
211
237
|
id?: string;
|
|
212
|
-
run?: Run
|
|
238
|
+
run?: Run<BuildRouteContext<M, U>>;
|
|
213
239
|
nextRoute?: NextRoute;
|
|
214
240
|
description?: string;
|
|
215
|
-
metadata?:
|
|
241
|
+
metadata?: M;
|
|
216
242
|
middleware?: RouteMiddleware[];
|
|
217
243
|
type?: string;
|
|
218
244
|
/**
|
|
@@ -227,19 +253,19 @@ declare class Route<U = {
|
|
|
227
253
|
} = RouterContextT>(opts: DefineRouteOpts): this;
|
|
228
254
|
define<T extends {
|
|
229
255
|
[key: string]: any;
|
|
230
|
-
} = RouterContextT>(fn: Run<T & U
|
|
256
|
+
} = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
231
257
|
define<T extends {
|
|
232
258
|
[key: string]: any;
|
|
233
|
-
} = RouterContextT>(key: string, fn: Run<T & U
|
|
259
|
+
} = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
234
260
|
define<T extends {
|
|
235
261
|
[key: string]: any;
|
|
236
|
-
} = RouterContextT>(path: string, key: string, fn: Run<T & U
|
|
262
|
+
} = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
237
263
|
update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
|
|
238
264
|
addTo(router: QueryRouter | {
|
|
239
265
|
add: (route: Route) => void;
|
|
240
266
|
[key: string]: any;
|
|
241
267
|
}, opts?: AddOpts): void;
|
|
242
|
-
throw(
|
|
268
|
+
throw(...args: any[]): void;
|
|
243
269
|
}
|
|
244
270
|
declare const toJSONSchema: (args: any, opts?: {
|
|
245
271
|
mergeObject?: boolean;
|
|
@@ -264,11 +290,11 @@ declare const fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?:
|
|
|
264
290
|
type AddOpts = {
|
|
265
291
|
overwrite?: boolean;
|
|
266
292
|
};
|
|
267
|
-
declare class QueryRouter {
|
|
293
|
+
declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements throwError {
|
|
268
294
|
appId: string;
|
|
269
295
|
routes: Route[];
|
|
270
296
|
maxNextRoute: number;
|
|
271
|
-
context?: RouteContext
|
|
297
|
+
context?: RouteContext<T>;
|
|
272
298
|
constructor();
|
|
273
299
|
/**
|
|
274
300
|
* add route
|
|
@@ -296,7 +322,7 @@ declare class QueryRouter {
|
|
|
296
322
|
* @param ctx
|
|
297
323
|
* @returns
|
|
298
324
|
*/
|
|
299
|
-
runRoute(path: string, key: string, ctx?: RouteContext):
|
|
325
|
+
runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
|
|
300
326
|
/**
|
|
301
327
|
* 第一次执行
|
|
302
328
|
* @param message
|
|
@@ -307,9 +333,11 @@ declare class QueryRouter {
|
|
|
307
333
|
path: string;
|
|
308
334
|
key?: string;
|
|
309
335
|
payload?: any;
|
|
310
|
-
}, ctx?: RouteContext & {
|
|
336
|
+
}, ctx?: RouteContext<T> & {
|
|
311
337
|
[key: string]: any;
|
|
312
|
-
}): Promise<
|
|
338
|
+
}): Promise<RouteContext<T, {}, {
|
|
339
|
+
[key: string]: any;
|
|
340
|
+
}>>;
|
|
313
341
|
/**
|
|
314
342
|
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
315
343
|
* @param message
|
|
@@ -321,9 +349,15 @@ declare class QueryRouter {
|
|
|
321
349
|
path?: string;
|
|
322
350
|
key?: string;
|
|
323
351
|
payload?: any;
|
|
324
|
-
}, ctx?: RouteContext & {
|
|
352
|
+
}, ctx?: RouteContext<T> & {
|
|
325
353
|
[key: string]: any;
|
|
326
|
-
}): Promise<
|
|
354
|
+
}): Promise<RouteContext<T, {}, {
|
|
355
|
+
[key: string]: any;
|
|
356
|
+
}> | {
|
|
357
|
+
code: number;
|
|
358
|
+
body: any;
|
|
359
|
+
message: string;
|
|
360
|
+
}>;
|
|
327
361
|
/**
|
|
328
362
|
* 请求 result 的数据
|
|
329
363
|
* @param message
|
|
@@ -339,9 +373,9 @@ declare class QueryRouter {
|
|
|
339
373
|
}, ctx?: RouteContext & {
|
|
340
374
|
[key: string]: any;
|
|
341
375
|
}): Promise<{
|
|
342
|
-
code:
|
|
376
|
+
code: number;
|
|
343
377
|
data: any;
|
|
344
|
-
message:
|
|
378
|
+
message: string;
|
|
345
379
|
}>;
|
|
346
380
|
/**
|
|
347
381
|
* Router Run获取数据
|
|
@@ -354,12 +388,12 @@ declare class QueryRouter {
|
|
|
354
388
|
path?: string;
|
|
355
389
|
key?: string;
|
|
356
390
|
payload?: any;
|
|
357
|
-
}, ctx?: RouteContext & {
|
|
391
|
+
}, ctx?: RouteContext<T> & {
|
|
358
392
|
[key: string]: any;
|
|
359
393
|
}): Promise<{
|
|
360
|
-
code:
|
|
394
|
+
code: number;
|
|
361
395
|
data: any;
|
|
362
|
-
message:
|
|
396
|
+
message: string;
|
|
363
397
|
}>;
|
|
364
398
|
/**
|
|
365
399
|
* 设置上下文
|
|
@@ -371,12 +405,12 @@ declare class QueryRouter {
|
|
|
371
405
|
/**
|
|
372
406
|
* 获取handle函数, 这里会去执行parse函数
|
|
373
407
|
*/
|
|
374
|
-
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn
|
|
408
|
+
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
|
|
375
409
|
id?: string;
|
|
376
410
|
path?: string;
|
|
377
411
|
key?: string;
|
|
378
412
|
[key: string]: any;
|
|
379
|
-
}, handleContext?: RouteContext) => Promise<{
|
|
413
|
+
}, handleContext?: RouteContext<T>) => Promise<{
|
|
380
414
|
[key: string]: any;
|
|
381
415
|
code: string;
|
|
382
416
|
data?: any;
|
|
@@ -390,24 +424,16 @@ declare class QueryRouter {
|
|
|
390
424
|
message: any;
|
|
391
425
|
data?: undefined;
|
|
392
426
|
}>;
|
|
393
|
-
exportRoutes(): Route<
|
|
394
|
-
[key: string]: any;
|
|
395
|
-
}, SimpleObject$1>[];
|
|
427
|
+
exportRoutes(): Route<SimpleObject$1, SimpleObject$1>[];
|
|
396
428
|
importRoutes(routes: Route[]): void;
|
|
397
429
|
importRouter(router: QueryRouter): void;
|
|
398
|
-
throw(
|
|
399
|
-
|
|
400
|
-
throw(opts?: CustomErrorOptions): void;
|
|
401
|
-
hasRoute(path: string, key?: string): Route<{
|
|
402
|
-
[key: string]: any;
|
|
403
|
-
}, SimpleObject$1>;
|
|
430
|
+
throw(...args: any[]): void;
|
|
431
|
+
hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
404
432
|
findRoute(opts?: {
|
|
405
433
|
path?: string;
|
|
406
434
|
key?: string;
|
|
407
435
|
id?: string;
|
|
408
|
-
}): Route<
|
|
409
|
-
[key: string]: any;
|
|
410
|
-
}, SimpleObject$1>;
|
|
436
|
+
}): Route<SimpleObject$1, SimpleObject$1>;
|
|
411
437
|
createRouteList(opts?: {
|
|
412
438
|
force?: boolean;
|
|
413
439
|
filter?: (route: Route) => boolean;
|
|
@@ -449,10 +475,11 @@ declare class QueryRouter {
|
|
|
449
475
|
[key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
|
|
450
476
|
};
|
|
451
477
|
}
|
|
452
|
-
type QueryRouterServerOpts = {
|
|
478
|
+
type QueryRouterServerOpts<C extends SimpleObject$1 = SimpleObject$1> = {
|
|
453
479
|
handleFn?: HandleFn;
|
|
454
|
-
context?: RouteContext
|
|
480
|
+
context?: RouteContext<C>;
|
|
455
481
|
appId?: string;
|
|
482
|
+
initHandle?: boolean;
|
|
456
483
|
};
|
|
457
484
|
interface HandleFn<T = any> {
|
|
458
485
|
(msg: {
|
|
@@ -469,20 +496,27 @@ interface HandleFn<T = any> {
|
|
|
469
496
|
/**
|
|
470
497
|
* QueryRouterServer
|
|
471
498
|
* @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
|
|
499
|
+
* @template C 自定义 RouteContext 类型
|
|
472
500
|
*/
|
|
473
|
-
declare class QueryRouterServer extends QueryRouter {
|
|
501
|
+
declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> extends QueryRouter<C> {
|
|
474
502
|
appId: string;
|
|
475
503
|
handle: any;
|
|
476
|
-
|
|
504
|
+
context: RouteContext<C>;
|
|
505
|
+
constructor(opts?: QueryRouterServerOpts<C>);
|
|
477
506
|
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
|
|
478
507
|
addRoute(route: Route, opts?: AddOpts): void;
|
|
479
508
|
Route: typeof Route;
|
|
480
|
-
route(opts: RouteOpts
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
route(path: string,
|
|
484
|
-
|
|
485
|
-
|
|
509
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(opts: RouteOpts & {
|
|
510
|
+
metadata?: M;
|
|
511
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
512
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, opts?: RouteOpts & {
|
|
513
|
+
metadata?: M;
|
|
514
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
515
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
|
|
516
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string, opts?: RouteOpts & {
|
|
517
|
+
metadata?: M;
|
|
518
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
519
|
+
prompt(description: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
486
520
|
/**
|
|
487
521
|
* 调用了handle
|
|
488
522
|
* @param param0
|
|
@@ -493,9 +527,7 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
493
527
|
path?: string;
|
|
494
528
|
key?: string;
|
|
495
529
|
payload?: any;
|
|
496
|
-
}, ctx?: RouteContext
|
|
497
|
-
[key: string]: any;
|
|
498
|
-
}): Promise<any>;
|
|
530
|
+
}, ctx?: Partial<RouteContext<C>>): Promise<any>;
|
|
499
531
|
}
|
|
500
532
|
declare class Mini extends QueryRouterServer {
|
|
501
533
|
}
|
|
@@ -951,7 +983,7 @@ type RouterHandle = (msg: {
|
|
|
951
983
|
[key: string]: any;
|
|
952
984
|
};
|
|
953
985
|
type AppOptions<T = {}> = {
|
|
954
|
-
router?:
|
|
986
|
+
router?: QueryRouterServer;
|
|
955
987
|
server?: ServerType;
|
|
956
988
|
/** handle msg 关联 */
|
|
957
989
|
routerHandle?: RouterHandle;
|
|
@@ -959,17 +991,18 @@ type AppOptions<T = {}> = {
|
|
|
959
991
|
serverOptions?: ServerNodeOpts;
|
|
960
992
|
appId?: string;
|
|
961
993
|
};
|
|
962
|
-
type AppRouteContext<T
|
|
994
|
+
type AppRouteContext<T> = HandleCtx & RouteContext<T> & {
|
|
963
995
|
app: App<T>;
|
|
964
996
|
};
|
|
965
997
|
/**
|
|
966
998
|
* 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
|
|
967
999
|
* U - Route Context的扩展类型
|
|
968
1000
|
*/
|
|
969
|
-
declare class App<U = {}> extends
|
|
1001
|
+
declare class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
|
|
970
1002
|
appId: string;
|
|
971
|
-
router:
|
|
1003
|
+
router: QueryRouterServer;
|
|
972
1004
|
server: ServerType;
|
|
1005
|
+
context: AppRouteContext<U>;
|
|
973
1006
|
constructor(opts?: AppOptions<U>);
|
|
974
1007
|
listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
|
|
975
1008
|
listen(port: number, hostname?: string, listeningListener?: () => void): void;
|
|
@@ -979,34 +1012,7 @@ declare class App<U = {}> extends QueryRouter {
|
|
|
979
1012
|
listen(path: string, listeningListener?: () => void): void;
|
|
980
1013
|
listen(handle: any, backlog?: number, listeningListener?: () => void): void;
|
|
981
1014
|
listen(handle: any, listeningListener?: () => void): void;
|
|
982
|
-
addRoute(route: Route, opts?: AddOpts): void;
|
|
983
1015
|
Route: typeof Route;
|
|
984
|
-
route(opts: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
|
|
985
|
-
route(path: string, key?: string): Route<AppRouteContext<U>>;
|
|
986
|
-
route(path: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
|
|
987
|
-
route(path: string, key?: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
|
|
988
|
-
prompt(description: string): Route<AppRouteContext<U>>;
|
|
989
|
-
prompt(description: Function): Route<AppRouteContext<U>>;
|
|
990
|
-
call(message: {
|
|
991
|
-
id?: string;
|
|
992
|
-
path?: string;
|
|
993
|
-
key?: string;
|
|
994
|
-
payload?: any;
|
|
995
|
-
}, ctx?: AppRouteContext<U> & {
|
|
996
|
-
[key: string]: any;
|
|
997
|
-
}): Promise<any>;
|
|
998
|
-
run(msg: {
|
|
999
|
-
id?: string;
|
|
1000
|
-
path?: string;
|
|
1001
|
-
key?: string;
|
|
1002
|
-
payload?: any;
|
|
1003
|
-
}, ctx?: Partial<AppRouteContext<U>> & {
|
|
1004
|
-
[key: string]: any;
|
|
1005
|
-
}): Promise<{
|
|
1006
|
-
code: any;
|
|
1007
|
-
data: any;
|
|
1008
|
-
message: any;
|
|
1009
|
-
}>;
|
|
1010
1016
|
static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
|
|
1011
1017
|
cookies: Record<string, string>;
|
|
1012
1018
|
token: string;
|
package/dist/router.js
CHANGED
|
@@ -3051,6 +3051,18 @@ class CustomError extends Error {
|
|
|
3051
3051
|
static isError(error) {
|
|
3052
3052
|
return error instanceof CustomError || typeof error === "object" && error !== null && "code" in error;
|
|
3053
3053
|
}
|
|
3054
|
+
static throw(...args) {
|
|
3055
|
+
const [args0, args1] = args;
|
|
3056
|
+
if (args0 && typeof args0 === "object") {
|
|
3057
|
+
throw new CustomError(args0);
|
|
3058
|
+
}
|
|
3059
|
+
if (args1 && typeof args1 === "object") {
|
|
3060
|
+
throw new CustomError(args0, args1);
|
|
3061
|
+
} else if (args1) {
|
|
3062
|
+
throw new CustomError(args0, { message: args1 });
|
|
3063
|
+
}
|
|
3064
|
+
throw new CustomError(args0);
|
|
3065
|
+
}
|
|
3054
3066
|
parse(e) {
|
|
3055
3067
|
if (e) {
|
|
3056
3068
|
return CustomError.parseError(e);
|
|
@@ -16997,7 +17009,7 @@ class Route {
|
|
|
16997
17009
|
router.add(this, opts);
|
|
16998
17010
|
}
|
|
16999
17011
|
throw(...args) {
|
|
17000
|
-
throw
|
|
17012
|
+
CustomError.throw(...args);
|
|
17001
17013
|
}
|
|
17002
17014
|
}
|
|
17003
17015
|
var toJSONSchemaRoute = (route) => {
|
|
@@ -17040,6 +17052,7 @@ class QueryRouter {
|
|
|
17040
17052
|
const maxNextRoute = this.maxNextRoute;
|
|
17041
17053
|
ctx = ctx || {};
|
|
17042
17054
|
ctx.currentPath = path;
|
|
17055
|
+
ctx.currentId = route?.id;
|
|
17043
17056
|
ctx.currentKey = key;
|
|
17044
17057
|
ctx.currentRoute = route;
|
|
17045
17058
|
ctx.index = (ctx.index || 0) + 1;
|
|
@@ -17053,7 +17066,7 @@ class QueryRouter {
|
|
|
17053
17066
|
ctx.code = 500;
|
|
17054
17067
|
ctx.message = "Too many nextRoute";
|
|
17055
17068
|
ctx.body = null;
|
|
17056
|
-
return;
|
|
17069
|
+
return ctx;
|
|
17057
17070
|
}
|
|
17058
17071
|
if (route && route.middleware && route.middleware.length > 0) {
|
|
17059
17072
|
const errorMiddleware = [];
|
|
@@ -17128,7 +17141,9 @@ class QueryRouter {
|
|
|
17128
17141
|
}
|
|
17129
17142
|
return ctx;
|
|
17130
17143
|
}
|
|
17131
|
-
if (ctx.end) {
|
|
17144
|
+
if (ctx.end) {
|
|
17145
|
+
return ctx;
|
|
17146
|
+
}
|
|
17132
17147
|
}
|
|
17133
17148
|
}
|
|
17134
17149
|
}
|
|
@@ -17295,16 +17310,7 @@ class QueryRouter {
|
|
|
17295
17310
|
this.importRoutes(router.routes);
|
|
17296
17311
|
}
|
|
17297
17312
|
throw(...args) {
|
|
17298
|
-
|
|
17299
|
-
if (args0 && typeof args0 === "object") {
|
|
17300
|
-
throw new CustomError(args0);
|
|
17301
|
-
}
|
|
17302
|
-
if (args1 && typeof args1 === "object") {
|
|
17303
|
-
throw new CustomError(args0, args1);
|
|
17304
|
-
} else if (args1) {
|
|
17305
|
-
throw new CustomError(args0, { message: args1 });
|
|
17306
|
-
}
|
|
17307
|
-
throw new CustomError(args0);
|
|
17313
|
+
CustomError.throw(...args);
|
|
17308
17314
|
}
|
|
17309
17315
|
hasRoute(path, key = "") {
|
|
17310
17316
|
return this.routes.find((r) => r.path === path && r.key === key);
|
|
@@ -17331,7 +17337,7 @@ class QueryRouter {
|
|
|
17331
17337
|
description: "列出当前应用下的所有的路由信息",
|
|
17332
17338
|
middleware: opts?.middleware || [],
|
|
17333
17339
|
run: async (ctx) => {
|
|
17334
|
-
const tokenUser = ctx.state
|
|
17340
|
+
const tokenUser = ctx.state;
|
|
17335
17341
|
let isUser = !!tokenUser;
|
|
17336
17342
|
const list = this.getList(opts?.filter).filter((item) => {
|
|
17337
17343
|
if (item.id === "auth" || item.id === "auth-can" || item.id === "check-auth-admin" || item.id === "auth-admin") {
|
|
@@ -17366,7 +17372,10 @@ class QueryRouterServer extends QueryRouter {
|
|
|
17366
17372
|
handle;
|
|
17367
17373
|
constructor(opts) {
|
|
17368
17374
|
super();
|
|
17369
|
-
|
|
17375
|
+
const initHandle = opts?.initHandle ?? true;
|
|
17376
|
+
if (initHandle || opts?.handleFn) {
|
|
17377
|
+
this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
|
|
17378
|
+
}
|
|
17370
17379
|
this.setContext({ needSerialize: false, ...opts?.context });
|
|
17371
17380
|
if (opts?.appId) {
|
|
17372
17381
|
this.appId = opts.appId;
|
|
@@ -17397,15 +17406,8 @@ class QueryRouterServer extends QueryRouter {
|
|
|
17397
17406
|
}
|
|
17398
17407
|
return new Route(path, key, opts);
|
|
17399
17408
|
}
|
|
17400
|
-
prompt(
|
|
17401
|
-
|
|
17402
|
-
let description = "";
|
|
17403
|
-
if (typeof desc === "string") {
|
|
17404
|
-
description = desc;
|
|
17405
|
-
} else if (typeof desc === "function") {
|
|
17406
|
-
description = desc() || "";
|
|
17407
|
-
}
|
|
17408
|
-
return new Route("", "", { description });
|
|
17409
|
+
prompt(description) {
|
|
17410
|
+
return new Route(undefined, undefined, { description });
|
|
17409
17411
|
}
|
|
17410
17412
|
async run(msg, ctx) {
|
|
17411
17413
|
const handle = this.handle;
|
|
@@ -18481,11 +18483,11 @@ class BunServer extends ServerBase {
|
|
|
18481
18483
|
}
|
|
18482
18484
|
}
|
|
18483
18485
|
// src/app.ts
|
|
18484
|
-
class App extends
|
|
18486
|
+
class App extends QueryRouterServer {
|
|
18485
18487
|
router;
|
|
18486
18488
|
server;
|
|
18487
18489
|
constructor(opts) {
|
|
18488
|
-
super();
|
|
18490
|
+
super({ initHandle: false, context: { needSerialize: true, ...opts?.routerContext } });
|
|
18489
18491
|
const router = this;
|
|
18490
18492
|
let server = opts?.server;
|
|
18491
18493
|
if (!server) {
|
|
@@ -18497,7 +18499,6 @@ class App extends QueryRouter {
|
|
|
18497
18499
|
}
|
|
18498
18500
|
}
|
|
18499
18501
|
server.setHandle(router.getHandle(router, opts?.routerHandle, opts?.routerContext));
|
|
18500
|
-
router.setContext({ needSerialize: true, ...opts?.routerContext });
|
|
18501
18502
|
this.router = router;
|
|
18502
18503
|
this.server = server;
|
|
18503
18504
|
if (opts?.appId) {
|
|
@@ -18510,42 +18511,7 @@ class App extends QueryRouter {
|
|
|
18510
18511
|
listen(...args) {
|
|
18511
18512
|
this.server.listen(...args);
|
|
18512
18513
|
}
|
|
18513
|
-
addRoute(route, opts) {
|
|
18514
|
-
super.add(route, opts);
|
|
18515
|
-
}
|
|
18516
18514
|
Route = Route;
|
|
18517
|
-
route(...args) {
|
|
18518
|
-
const [path, key, opts] = args;
|
|
18519
|
-
if (typeof path === "object") {
|
|
18520
|
-
return new Route(path.path, path.key, path);
|
|
18521
|
-
}
|
|
18522
|
-
if (typeof path === "string") {
|
|
18523
|
-
if (opts) {
|
|
18524
|
-
return new Route(path, key, opts);
|
|
18525
|
-
}
|
|
18526
|
-
if (key && typeof key === "object") {
|
|
18527
|
-
return new Route(path, key?.key || "", key);
|
|
18528
|
-
}
|
|
18529
|
-
return new Route(path, key);
|
|
18530
|
-
}
|
|
18531
|
-
return new Route(path, key, opts);
|
|
18532
|
-
}
|
|
18533
|
-
prompt(...args) {
|
|
18534
|
-
const [desc] = args;
|
|
18535
|
-
let description = "";
|
|
18536
|
-
if (typeof desc === "string") {
|
|
18537
|
-
description = desc;
|
|
18538
|
-
} else if (typeof desc === "function") {
|
|
18539
|
-
description = desc() || "";
|
|
18540
|
-
}
|
|
18541
|
-
return new Route("", "", { description });
|
|
18542
|
-
}
|
|
18543
|
-
async call(message, ctx) {
|
|
18544
|
-
return await super.call(message, ctx);
|
|
18545
|
-
}
|
|
18546
|
-
async run(msg, ctx) {
|
|
18547
|
-
return await super.run(msg, ctx);
|
|
18548
|
-
}
|
|
18549
18515
|
static handleRequest(req, res) {
|
|
18550
18516
|
return handleServer(req, res);
|
|
18551
18517
|
}
|