@kevisual/router 0.0.83 → 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 +84 -58
- package/dist/opencode.d.ts +81 -81
- package/dist/opencode.js +10 -5
- package/dist/router-browser.d.ts +76 -50
- package/dist/router-browser.js +12 -13
- package/dist/router-define.d.ts +76 -50
- package/dist/router.d.ts +81 -81
- package/dist/router.js +14 -51
- package/dist/ws.d.ts +104 -68
- package/package.json +2 -2
- package/readme.md +78 -63
- package/src/app.ts +7 -50
- package/src/opencode.ts +12 -5
- package/src/route.ts +88 -60
- 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/opencode.js
CHANGED
|
@@ -14703,7 +14703,7 @@ var addCallFn = (app) => {
|
|
|
14703
14703
|
path: "call",
|
|
14704
14704
|
key: "",
|
|
14705
14705
|
description: "调用",
|
|
14706
|
-
middleware: ["auth"],
|
|
14706
|
+
middleware: ["auth-admin"],
|
|
14707
14707
|
metadata: {
|
|
14708
14708
|
tags: ["opencode"],
|
|
14709
14709
|
...createSkill({
|
|
@@ -14717,7 +14717,7 @@ var addCallFn = (app) => {
|
|
|
14717
14717
|
args: {
|
|
14718
14718
|
path: tool.schema.string().describe("应用路径,例如 cnb"),
|
|
14719
14719
|
key: tool.schema.string().optional().describe("应用key,例如 list-repos"),
|
|
14720
|
-
payload: tool.schema.object({}).optional().describe("
|
|
14720
|
+
payload: tool.schema.object({}).optional().describe('调用参数, 为对象, 例如 { "query": "javascript" }')
|
|
14721
14721
|
}
|
|
14722
14722
|
})
|
|
14723
14723
|
}
|
|
@@ -14745,8 +14745,13 @@ var createRouterAgentPluginFn = (opts) => {
|
|
|
14745
14745
|
if (!router.hasRoute("call", "")) {
|
|
14746
14746
|
addCallFn(router);
|
|
14747
14747
|
}
|
|
14748
|
-
if (
|
|
14749
|
-
router.route({ path: "auth", key: "", id: "auth", description: "认证" }).define(async (ctx) => {}).addTo(router
|
|
14748
|
+
if (router) {
|
|
14749
|
+
router.route({ path: "auth", key: "", id: "auth", description: "认证" }).define(async (ctx) => {}).addTo(router, {
|
|
14750
|
+
overwrite: false
|
|
14751
|
+
});
|
|
14752
|
+
router.route({ path: "auth-admin", key: "", id: "auth-admin", description: "认证" }).define(async (ctx) => {}).addTo(router, {
|
|
14753
|
+
overwrite: false
|
|
14754
|
+
});
|
|
14750
14755
|
}
|
|
14751
14756
|
const _routes = filter(router.routes, opts?.query || "");
|
|
14752
14757
|
const routes = _routes.filter((r) => {
|
|
@@ -14760,7 +14765,7 @@ var createRouterAgentPluginFn = (opts) => {
|
|
|
14760
14765
|
return false;
|
|
14761
14766
|
});
|
|
14762
14767
|
const AgentPlugin = async (pluginInput) => {
|
|
14763
|
-
useContextKey("plugin-input", () => pluginInput, true);
|
|
14768
|
+
useContextKey("plugin-input", () => pluginInput, { isNew: true });
|
|
14764
14769
|
const hooks = opts?.hooks ? await opts.hooks(pluginInput) : {};
|
|
14765
14770
|
return {
|
|
14766
14771
|
...hooks,
|
package/dist/router-browser.d.ts
CHANGED
|
@@ -74,9 +74,20 @@ type RouterContextT = {
|
|
|
74
74
|
code?: number;
|
|
75
75
|
[key: string]: any;
|
|
76
76
|
};
|
|
77
|
+
type BuildRouteContext<M, U> = M extends {
|
|
78
|
+
args?: infer A;
|
|
79
|
+
} ? A extends z.ZodObject<any> ? RouteContext<{
|
|
80
|
+
args?: z.infer<A>;
|
|
81
|
+
}, U> : A extends Record<string, z.ZodTypeAny> ? RouteContext<{
|
|
82
|
+
args?: {
|
|
83
|
+
[K in keyof A]: z.infer<A[K]>;
|
|
84
|
+
};
|
|
85
|
+
}, U> : RouteContext<U> : RouteContext<U>;
|
|
77
86
|
type RouteContext<T = {
|
|
78
87
|
code?: number;
|
|
79
|
-
},
|
|
88
|
+
}, U extends SimpleObject$1 = {}, S = {
|
|
89
|
+
[key: string]: any;
|
|
90
|
+
}> = {
|
|
80
91
|
/**
|
|
81
92
|
* 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
|
|
82
93
|
* 或者不需要登录的,直接调用
|
|
@@ -99,7 +110,14 @@ type RouteContext<T = {
|
|
|
99
110
|
code?: number;
|
|
100
111
|
/** return msg */
|
|
101
112
|
message?: string;
|
|
113
|
+
/**
|
|
114
|
+
* 传递状态
|
|
115
|
+
*/
|
|
102
116
|
state?: S;
|
|
117
|
+
/**
|
|
118
|
+
* 当前routerId
|
|
119
|
+
*/
|
|
120
|
+
currentId?: string;
|
|
103
121
|
/**
|
|
104
122
|
* 当前路径
|
|
105
123
|
*/
|
|
@@ -140,14 +158,12 @@ type RouteContext<T = {
|
|
|
140
158
|
path: string;
|
|
141
159
|
key?: string;
|
|
142
160
|
payload?: any;
|
|
143
|
-
}, ctx?: RouteContext
|
|
144
|
-
[key: string]: any;
|
|
145
|
-
}) => Promise<any>;
|
|
161
|
+
}, ctx?: RouteContext) => Promise<any>;
|
|
146
162
|
index?: number;
|
|
147
163
|
throw?: throwError['throw'];
|
|
148
164
|
/** 是否需要序列化, 使用JSON.stringify和JSON.parse */
|
|
149
165
|
needSerialize?: boolean;
|
|
150
|
-
} & T;
|
|
166
|
+
} & T & U;
|
|
151
167
|
type SimpleObject$1 = Record<string, any>;
|
|
152
168
|
type Run<T extends SimpleObject$1 = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
|
|
153
169
|
type RunMessage = {
|
|
@@ -158,7 +174,7 @@ type RunMessage = {
|
|
|
158
174
|
};
|
|
159
175
|
type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
|
|
160
176
|
type RouteMiddleware = {
|
|
161
|
-
path
|
|
177
|
+
path?: string;
|
|
162
178
|
key?: string;
|
|
163
179
|
id?: string;
|
|
164
180
|
} | string;
|
|
@@ -199,9 +215,11 @@ declare const tool: {
|
|
|
199
215
|
/** */
|
|
200
216
|
declare const createSkill: <T = SimpleObject$1>(skill: Skill<T>) => Skill<T>;
|
|
201
217
|
type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
218
|
+
/**
|
|
219
|
+
* @M 是 route的 metadate的类型,默认是 SimpleObject
|
|
220
|
+
* @U 是 RouteContext 里 state的类型
|
|
221
|
+
*/
|
|
222
|
+
declare class Route<M extends SimpleObject$1 = SimpleObject$1, U extends SimpleObject$1 = SimpleObject$1> implements throwError {
|
|
205
223
|
/**
|
|
206
224
|
* 一级路径
|
|
207
225
|
*/
|
|
@@ -211,10 +229,10 @@ declare class Route<U = {
|
|
|
211
229
|
*/
|
|
212
230
|
key?: string;
|
|
213
231
|
id?: string;
|
|
214
|
-
run?: Run
|
|
232
|
+
run?: Run<BuildRouteContext<M, U>>;
|
|
215
233
|
nextRoute?: NextRoute;
|
|
216
234
|
description?: string;
|
|
217
|
-
metadata?:
|
|
235
|
+
metadata?: M;
|
|
218
236
|
middleware?: RouteMiddleware[];
|
|
219
237
|
type?: string;
|
|
220
238
|
/**
|
|
@@ -229,13 +247,13 @@ declare class Route<U = {
|
|
|
229
247
|
} = RouterContextT>(opts: DefineRouteOpts): this;
|
|
230
248
|
define<T extends {
|
|
231
249
|
[key: string]: any;
|
|
232
|
-
} = RouterContextT>(fn: Run<T & U
|
|
250
|
+
} = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
233
251
|
define<T extends {
|
|
234
252
|
[key: string]: any;
|
|
235
|
-
} = RouterContextT>(key: string, fn: Run<T & U
|
|
253
|
+
} = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
236
254
|
define<T extends {
|
|
237
255
|
[key: string]: any;
|
|
238
|
-
} = RouterContextT>(path: string, key: string, fn: Run<T & U
|
|
256
|
+
} = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
239
257
|
update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
|
|
240
258
|
addTo(router: QueryRouter | {
|
|
241
259
|
add: (route: Route) => void;
|
|
@@ -266,11 +284,11 @@ declare const fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?:
|
|
|
266
284
|
type AddOpts = {
|
|
267
285
|
overwrite?: boolean;
|
|
268
286
|
};
|
|
269
|
-
declare class QueryRouter implements throwError {
|
|
287
|
+
declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements throwError {
|
|
270
288
|
appId: string;
|
|
271
289
|
routes: Route[];
|
|
272
290
|
maxNextRoute: number;
|
|
273
|
-
context?: RouteContext
|
|
291
|
+
context?: RouteContext<T>;
|
|
274
292
|
constructor();
|
|
275
293
|
/**
|
|
276
294
|
* add route
|
|
@@ -298,7 +316,7 @@ declare class QueryRouter implements throwError {
|
|
|
298
316
|
* @param ctx
|
|
299
317
|
* @returns
|
|
300
318
|
*/
|
|
301
|
-
runRoute(path: string, key: string, ctx?: RouteContext):
|
|
319
|
+
runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
|
|
302
320
|
/**
|
|
303
321
|
* 第一次执行
|
|
304
322
|
* @param message
|
|
@@ -309,9 +327,11 @@ declare class QueryRouter implements throwError {
|
|
|
309
327
|
path: string;
|
|
310
328
|
key?: string;
|
|
311
329
|
payload?: any;
|
|
312
|
-
}, ctx?: RouteContext & {
|
|
330
|
+
}, ctx?: RouteContext<T> & {
|
|
313
331
|
[key: string]: any;
|
|
314
|
-
}): Promise<
|
|
332
|
+
}): Promise<RouteContext<T, {}, {
|
|
333
|
+
[key: string]: any;
|
|
334
|
+
}>>;
|
|
315
335
|
/**
|
|
316
336
|
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
317
337
|
* @param message
|
|
@@ -323,9 +343,15 @@ declare class QueryRouter implements throwError {
|
|
|
323
343
|
path?: string;
|
|
324
344
|
key?: string;
|
|
325
345
|
payload?: any;
|
|
326
|
-
}, ctx?: RouteContext & {
|
|
346
|
+
}, ctx?: RouteContext<T> & {
|
|
327
347
|
[key: string]: any;
|
|
328
|
-
}): Promise<
|
|
348
|
+
}): Promise<RouteContext<T, {}, {
|
|
349
|
+
[key: string]: any;
|
|
350
|
+
}> | {
|
|
351
|
+
code: number;
|
|
352
|
+
body: any;
|
|
353
|
+
message: string;
|
|
354
|
+
}>;
|
|
329
355
|
/**
|
|
330
356
|
* 请求 result 的数据
|
|
331
357
|
* @param message
|
|
@@ -341,9 +367,9 @@ declare class QueryRouter implements throwError {
|
|
|
341
367
|
}, ctx?: RouteContext & {
|
|
342
368
|
[key: string]: any;
|
|
343
369
|
}): Promise<{
|
|
344
|
-
code:
|
|
370
|
+
code: number;
|
|
345
371
|
data: any;
|
|
346
|
-
message:
|
|
372
|
+
message: string;
|
|
347
373
|
}>;
|
|
348
374
|
/**
|
|
349
375
|
* Router Run获取数据
|
|
@@ -356,12 +382,12 @@ declare class QueryRouter implements throwError {
|
|
|
356
382
|
path?: string;
|
|
357
383
|
key?: string;
|
|
358
384
|
payload?: any;
|
|
359
|
-
}, ctx?: RouteContext & {
|
|
385
|
+
}, ctx?: RouteContext<T> & {
|
|
360
386
|
[key: string]: any;
|
|
361
387
|
}): Promise<{
|
|
362
|
-
code:
|
|
388
|
+
code: number;
|
|
363
389
|
data: any;
|
|
364
|
-
message:
|
|
390
|
+
message: string;
|
|
365
391
|
}>;
|
|
366
392
|
/**
|
|
367
393
|
* 设置上下文
|
|
@@ -373,12 +399,12 @@ declare class QueryRouter implements throwError {
|
|
|
373
399
|
/**
|
|
374
400
|
* 获取handle函数, 这里会去执行parse函数
|
|
375
401
|
*/
|
|
376
|
-
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn
|
|
402
|
+
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
|
|
377
403
|
id?: string;
|
|
378
404
|
path?: string;
|
|
379
405
|
key?: string;
|
|
380
406
|
[key: string]: any;
|
|
381
|
-
}, handleContext?: RouteContext) => Promise<{
|
|
407
|
+
}, handleContext?: RouteContext<T>) => Promise<{
|
|
382
408
|
[key: string]: any;
|
|
383
409
|
code: string;
|
|
384
410
|
data?: any;
|
|
@@ -392,22 +418,16 @@ declare class QueryRouter implements throwError {
|
|
|
392
418
|
message: any;
|
|
393
419
|
data?: undefined;
|
|
394
420
|
}>;
|
|
395
|
-
exportRoutes(): Route<
|
|
396
|
-
[key: string]: any;
|
|
397
|
-
}, SimpleObject$1>[];
|
|
421
|
+
exportRoutes(): Route<SimpleObject$1, SimpleObject$1>[];
|
|
398
422
|
importRoutes(routes: Route[]): void;
|
|
399
423
|
importRouter(router: QueryRouter): void;
|
|
400
424
|
throw(...args: any[]): void;
|
|
401
|
-
hasRoute(path: string, key?: string): Route<
|
|
402
|
-
[key: string]: any;
|
|
403
|
-
}, SimpleObject$1>;
|
|
425
|
+
hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
404
426
|
findRoute(opts?: {
|
|
405
427
|
path?: string;
|
|
406
428
|
key?: string;
|
|
407
429
|
id?: string;
|
|
408
|
-
}): Route<
|
|
409
|
-
[key: string]: any;
|
|
410
|
-
}, SimpleObject$1>;
|
|
430
|
+
}): Route<SimpleObject$1, SimpleObject$1>;
|
|
411
431
|
createRouteList(opts?: {
|
|
412
432
|
force?: boolean;
|
|
413
433
|
filter?: (route: Route) => boolean;
|
|
@@ -449,10 +469,11 @@ declare class QueryRouter implements throwError {
|
|
|
449
469
|
[key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
|
|
450
470
|
};
|
|
451
471
|
}
|
|
452
|
-
type QueryRouterServerOpts = {
|
|
472
|
+
type QueryRouterServerOpts<C extends SimpleObject$1 = SimpleObject$1> = {
|
|
453
473
|
handleFn?: HandleFn;
|
|
454
|
-
context?: RouteContext
|
|
474
|
+
context?: RouteContext<C>;
|
|
455
475
|
appId?: string;
|
|
476
|
+
initHandle?: boolean;
|
|
456
477
|
};
|
|
457
478
|
interface HandleFn<T = any> {
|
|
458
479
|
(msg: {
|
|
@@ -469,20 +490,27 @@ interface HandleFn<T = any> {
|
|
|
469
490
|
/**
|
|
470
491
|
* QueryRouterServer
|
|
471
492
|
* @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
|
|
493
|
+
* @template C 自定义 RouteContext 类型
|
|
472
494
|
*/
|
|
473
|
-
declare class QueryRouterServer extends QueryRouter {
|
|
495
|
+
declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> extends QueryRouter<C> {
|
|
474
496
|
appId: string;
|
|
475
497
|
handle: any;
|
|
476
|
-
|
|
498
|
+
context: RouteContext<C>;
|
|
499
|
+
constructor(opts?: QueryRouterServerOpts<C>);
|
|
477
500
|
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
|
|
478
501
|
addRoute(route: Route, opts?: AddOpts): void;
|
|
479
502
|
Route: typeof Route;
|
|
480
|
-
route(opts: RouteOpts
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
route(path: string,
|
|
484
|
-
|
|
485
|
-
|
|
503
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(opts: RouteOpts & {
|
|
504
|
+
metadata?: M;
|
|
505
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
506
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, opts?: RouteOpts & {
|
|
507
|
+
metadata?: M;
|
|
508
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
509
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
|
|
510
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string, opts?: RouteOpts & {
|
|
511
|
+
metadata?: M;
|
|
512
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
513
|
+
prompt(description: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
486
514
|
/**
|
|
487
515
|
* 调用了handle
|
|
488
516
|
* @param param0
|
|
@@ -493,9 +521,7 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
493
521
|
path?: string;
|
|
494
522
|
key?: string;
|
|
495
523
|
payload?: any;
|
|
496
|
-
}, ctx?: RouteContext
|
|
497
|
-
[key: string]: any;
|
|
498
|
-
}): Promise<any>;
|
|
524
|
+
}, ctx?: Partial<RouteContext<C>>): Promise<any>;
|
|
499
525
|
}
|
|
500
526
|
declare class Mini extends QueryRouterServer {
|
|
501
527
|
}
|
package/dist/router-browser.js
CHANGED
|
@@ -14219,6 +14219,7 @@ class QueryRouter {
|
|
|
14219
14219
|
const maxNextRoute = this.maxNextRoute;
|
|
14220
14220
|
ctx = ctx || {};
|
|
14221
14221
|
ctx.currentPath = path;
|
|
14222
|
+
ctx.currentId = route?.id;
|
|
14222
14223
|
ctx.currentKey = key;
|
|
14223
14224
|
ctx.currentRoute = route;
|
|
14224
14225
|
ctx.index = (ctx.index || 0) + 1;
|
|
@@ -14232,7 +14233,7 @@ class QueryRouter {
|
|
|
14232
14233
|
ctx.code = 500;
|
|
14233
14234
|
ctx.message = "Too many nextRoute";
|
|
14234
14235
|
ctx.body = null;
|
|
14235
|
-
return;
|
|
14236
|
+
return ctx;
|
|
14236
14237
|
}
|
|
14237
14238
|
if (route && route.middleware && route.middleware.length > 0) {
|
|
14238
14239
|
const errorMiddleware = [];
|
|
@@ -14307,7 +14308,9 @@ class QueryRouter {
|
|
|
14307
14308
|
}
|
|
14308
14309
|
return ctx;
|
|
14309
14310
|
}
|
|
14310
|
-
if (ctx.end) {
|
|
14311
|
+
if (ctx.end) {
|
|
14312
|
+
return ctx;
|
|
14313
|
+
}
|
|
14311
14314
|
}
|
|
14312
14315
|
}
|
|
14313
14316
|
}
|
|
@@ -14501,7 +14504,7 @@ class QueryRouter {
|
|
|
14501
14504
|
description: "列出当前应用下的所有的路由信息",
|
|
14502
14505
|
middleware: opts?.middleware || [],
|
|
14503
14506
|
run: async (ctx) => {
|
|
14504
|
-
const tokenUser = ctx.state
|
|
14507
|
+
const tokenUser = ctx.state;
|
|
14505
14508
|
let isUser = !!tokenUser;
|
|
14506
14509
|
const list = this.getList(opts?.filter).filter((item) => {
|
|
14507
14510
|
if (item.id === "auth" || item.id === "auth-can" || item.id === "check-auth-admin" || item.id === "auth-admin") {
|
|
@@ -14536,7 +14539,10 @@ class QueryRouterServer extends QueryRouter {
|
|
|
14536
14539
|
handle;
|
|
14537
14540
|
constructor(opts) {
|
|
14538
14541
|
super();
|
|
14539
|
-
|
|
14542
|
+
const initHandle = opts?.initHandle ?? true;
|
|
14543
|
+
if (initHandle || opts?.handleFn) {
|
|
14544
|
+
this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
|
|
14545
|
+
}
|
|
14540
14546
|
this.setContext({ needSerialize: false, ...opts?.context });
|
|
14541
14547
|
if (opts?.appId) {
|
|
14542
14548
|
this.appId = opts.appId;
|
|
@@ -14567,15 +14573,8 @@ class QueryRouterServer extends QueryRouter {
|
|
|
14567
14573
|
}
|
|
14568
14574
|
return new Route(path, key, opts);
|
|
14569
14575
|
}
|
|
14570
|
-
prompt(
|
|
14571
|
-
|
|
14572
|
-
let description = "";
|
|
14573
|
-
if (typeof desc === "string") {
|
|
14574
|
-
description = desc;
|
|
14575
|
-
} else if (typeof desc === "function") {
|
|
14576
|
-
description = desc() || "";
|
|
14577
|
-
}
|
|
14578
|
-
return new Route("", "", { description });
|
|
14576
|
+
prompt(description) {
|
|
14577
|
+
return new Route(undefined, undefined, { description });
|
|
14579
14578
|
}
|
|
14580
14579
|
async run(msg, ctx) {
|
|
14581
14580
|
const handle = this.handle;
|
package/dist/router-define.d.ts
CHANGED
|
@@ -30,9 +30,20 @@ type RouterContextT = {
|
|
|
30
30
|
code?: number;
|
|
31
31
|
[key: string]: any;
|
|
32
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>;
|
|
33
42
|
type RouteContext<T = {
|
|
34
43
|
code?: number;
|
|
35
|
-
},
|
|
44
|
+
}, U extends SimpleObject$1 = {}, S = {
|
|
45
|
+
[key: string]: any;
|
|
46
|
+
}> = {
|
|
36
47
|
/**
|
|
37
48
|
* 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
|
|
38
49
|
* 或者不需要登录的,直接调用
|
|
@@ -55,7 +66,14 @@ type RouteContext<T = {
|
|
|
55
66
|
code?: number;
|
|
56
67
|
/** return msg */
|
|
57
68
|
message?: string;
|
|
69
|
+
/**
|
|
70
|
+
* 传递状态
|
|
71
|
+
*/
|
|
58
72
|
state?: S;
|
|
73
|
+
/**
|
|
74
|
+
* 当前routerId
|
|
75
|
+
*/
|
|
76
|
+
currentId?: string;
|
|
59
77
|
/**
|
|
60
78
|
* 当前路径
|
|
61
79
|
*/
|
|
@@ -96,14 +114,12 @@ type RouteContext<T = {
|
|
|
96
114
|
path: string;
|
|
97
115
|
key?: string;
|
|
98
116
|
payload?: any;
|
|
99
|
-
}, ctx?: RouteContext
|
|
100
|
-
[key: string]: any;
|
|
101
|
-
}) => Promise<any>;
|
|
117
|
+
}, ctx?: RouteContext) => Promise<any>;
|
|
102
118
|
index?: number;
|
|
103
119
|
throw?: throwError['throw'];
|
|
104
120
|
/** 是否需要序列化, 使用JSON.stringify和JSON.parse */
|
|
105
121
|
needSerialize?: boolean;
|
|
106
|
-
} & T;
|
|
122
|
+
} & T & U;
|
|
107
123
|
type SimpleObject$1 = Record<string, any>;
|
|
108
124
|
type Run<T extends SimpleObject$1 = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
|
|
109
125
|
type RunMessage = {
|
|
@@ -114,7 +130,7 @@ type RunMessage = {
|
|
|
114
130
|
};
|
|
115
131
|
type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
|
|
116
132
|
type RouteMiddleware = {
|
|
117
|
-
path
|
|
133
|
+
path?: string;
|
|
118
134
|
key?: string;
|
|
119
135
|
id?: string;
|
|
120
136
|
} | string;
|
|
@@ -141,9 +157,11 @@ type RouteOpts<U = {}, T = SimpleObject$1> = {
|
|
|
141
157
|
type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
|
|
142
158
|
declare const pickValue: readonly ["path", "key", "id", "description", "type", "middleware", "metadata"];
|
|
143
159
|
type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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 {
|
|
147
165
|
/**
|
|
148
166
|
* 一级路径
|
|
149
167
|
*/
|
|
@@ -153,10 +171,10 @@ declare class Route<U = {
|
|
|
153
171
|
*/
|
|
154
172
|
key?: string;
|
|
155
173
|
id?: string;
|
|
156
|
-
run?: Run
|
|
174
|
+
run?: Run<BuildRouteContext<M, U>>;
|
|
157
175
|
nextRoute?: NextRoute;
|
|
158
176
|
description?: string;
|
|
159
|
-
metadata?:
|
|
177
|
+
metadata?: M;
|
|
160
178
|
middleware?: RouteMiddleware[];
|
|
161
179
|
type?: string;
|
|
162
180
|
/**
|
|
@@ -171,13 +189,13 @@ declare class Route<U = {
|
|
|
171
189
|
} = RouterContextT>(opts: DefineRouteOpts): this;
|
|
172
190
|
define<T extends {
|
|
173
191
|
[key: string]: any;
|
|
174
|
-
} = RouterContextT>(fn: Run<T & U
|
|
192
|
+
} = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
175
193
|
define<T extends {
|
|
176
194
|
[key: string]: any;
|
|
177
|
-
} = RouterContextT>(key: string, fn: Run<T & U
|
|
195
|
+
} = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
178
196
|
define<T extends {
|
|
179
197
|
[key: string]: any;
|
|
180
|
-
} = RouterContextT>(path: string, key: string, fn: Run<T & U
|
|
198
|
+
} = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
181
199
|
update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
|
|
182
200
|
addTo(router: QueryRouter | {
|
|
183
201
|
add: (route: Route) => void;
|
|
@@ -191,11 +209,11 @@ declare class Route<U = {
|
|
|
191
209
|
type AddOpts = {
|
|
192
210
|
overwrite?: boolean;
|
|
193
211
|
};
|
|
194
|
-
declare class QueryRouter implements throwError {
|
|
212
|
+
declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements throwError {
|
|
195
213
|
appId: string;
|
|
196
214
|
routes: Route[];
|
|
197
215
|
maxNextRoute: number;
|
|
198
|
-
context?: RouteContext
|
|
216
|
+
context?: RouteContext<T>;
|
|
199
217
|
constructor();
|
|
200
218
|
/**
|
|
201
219
|
* add route
|
|
@@ -223,7 +241,7 @@ declare class QueryRouter implements throwError {
|
|
|
223
241
|
* @param ctx
|
|
224
242
|
* @returns
|
|
225
243
|
*/
|
|
226
|
-
runRoute(path: string, key: string, ctx?: RouteContext):
|
|
244
|
+
runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
|
|
227
245
|
/**
|
|
228
246
|
* 第一次执行
|
|
229
247
|
* @param message
|
|
@@ -234,9 +252,11 @@ declare class QueryRouter implements throwError {
|
|
|
234
252
|
path: string;
|
|
235
253
|
key?: string;
|
|
236
254
|
payload?: any;
|
|
237
|
-
}, ctx?: RouteContext & {
|
|
255
|
+
}, ctx?: RouteContext<T> & {
|
|
256
|
+
[key: string]: any;
|
|
257
|
+
}): Promise<RouteContext<T, {}, {
|
|
238
258
|
[key: string]: any;
|
|
239
|
-
}
|
|
259
|
+
}>>;
|
|
240
260
|
/**
|
|
241
261
|
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
242
262
|
* @param message
|
|
@@ -248,9 +268,15 @@ declare class QueryRouter implements throwError {
|
|
|
248
268
|
path?: string;
|
|
249
269
|
key?: string;
|
|
250
270
|
payload?: any;
|
|
251
|
-
}, ctx?: RouteContext & {
|
|
271
|
+
}, ctx?: RouteContext<T> & {
|
|
272
|
+
[key: string]: any;
|
|
273
|
+
}): Promise<RouteContext<T, {}, {
|
|
252
274
|
[key: string]: any;
|
|
253
|
-
}
|
|
275
|
+
}> | {
|
|
276
|
+
code: number;
|
|
277
|
+
body: any;
|
|
278
|
+
message: string;
|
|
279
|
+
}>;
|
|
254
280
|
/**
|
|
255
281
|
* 请求 result 的数据
|
|
256
282
|
* @param message
|
|
@@ -266,9 +292,9 @@ declare class QueryRouter implements throwError {
|
|
|
266
292
|
}, ctx?: RouteContext & {
|
|
267
293
|
[key: string]: any;
|
|
268
294
|
}): Promise<{
|
|
269
|
-
code:
|
|
295
|
+
code: number;
|
|
270
296
|
data: any;
|
|
271
|
-
message:
|
|
297
|
+
message: string;
|
|
272
298
|
}>;
|
|
273
299
|
/**
|
|
274
300
|
* Router Run获取数据
|
|
@@ -281,12 +307,12 @@ declare class QueryRouter implements throwError {
|
|
|
281
307
|
path?: string;
|
|
282
308
|
key?: string;
|
|
283
309
|
payload?: any;
|
|
284
|
-
}, ctx?: RouteContext & {
|
|
310
|
+
}, ctx?: RouteContext<T> & {
|
|
285
311
|
[key: string]: any;
|
|
286
312
|
}): Promise<{
|
|
287
|
-
code:
|
|
313
|
+
code: number;
|
|
288
314
|
data: any;
|
|
289
|
-
message:
|
|
315
|
+
message: string;
|
|
290
316
|
}>;
|
|
291
317
|
/**
|
|
292
318
|
* 设置上下文
|
|
@@ -298,12 +324,12 @@ declare class QueryRouter implements throwError {
|
|
|
298
324
|
/**
|
|
299
325
|
* 获取handle函数, 这里会去执行parse函数
|
|
300
326
|
*/
|
|
301
|
-
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn
|
|
327
|
+
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
|
|
302
328
|
id?: string;
|
|
303
329
|
path?: string;
|
|
304
330
|
key?: string;
|
|
305
331
|
[key: string]: any;
|
|
306
|
-
}, handleContext?: RouteContext) => Promise<{
|
|
332
|
+
}, handleContext?: RouteContext<T>) => Promise<{
|
|
307
333
|
[key: string]: any;
|
|
308
334
|
code: string;
|
|
309
335
|
data?: any;
|
|
@@ -317,22 +343,16 @@ declare class QueryRouter implements throwError {
|
|
|
317
343
|
message: any;
|
|
318
344
|
data?: undefined;
|
|
319
345
|
}>;
|
|
320
|
-
exportRoutes(): Route<
|
|
321
|
-
[key: string]: any;
|
|
322
|
-
}, SimpleObject$1>[];
|
|
346
|
+
exportRoutes(): Route<SimpleObject$1, SimpleObject$1>[];
|
|
323
347
|
importRoutes(routes: Route[]): void;
|
|
324
348
|
importRouter(router: QueryRouter): void;
|
|
325
349
|
throw(...args: any[]): void;
|
|
326
|
-
hasRoute(path: string, key?: string): Route<
|
|
327
|
-
[key: string]: any;
|
|
328
|
-
}, SimpleObject$1>;
|
|
350
|
+
hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
329
351
|
findRoute(opts?: {
|
|
330
352
|
path?: string;
|
|
331
353
|
key?: string;
|
|
332
354
|
id?: string;
|
|
333
|
-
}): Route<
|
|
334
|
-
[key: string]: any;
|
|
335
|
-
}, SimpleObject$1>;
|
|
355
|
+
}): Route<SimpleObject$1, SimpleObject$1>;
|
|
336
356
|
createRouteList(opts?: {
|
|
337
357
|
force?: boolean;
|
|
338
358
|
filter?: (route: Route) => boolean;
|
|
@@ -374,10 +394,11 @@ declare class QueryRouter implements throwError {
|
|
|
374
394
|
[key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
|
|
375
395
|
};
|
|
376
396
|
}
|
|
377
|
-
type QueryRouterServerOpts = {
|
|
397
|
+
type QueryRouterServerOpts<C extends SimpleObject$1 = SimpleObject$1> = {
|
|
378
398
|
handleFn?: HandleFn;
|
|
379
|
-
context?: RouteContext
|
|
399
|
+
context?: RouteContext<C>;
|
|
380
400
|
appId?: string;
|
|
401
|
+
initHandle?: boolean;
|
|
381
402
|
};
|
|
382
403
|
interface HandleFn<T = any> {
|
|
383
404
|
(msg: {
|
|
@@ -394,20 +415,27 @@ interface HandleFn<T = any> {
|
|
|
394
415
|
/**
|
|
395
416
|
* QueryRouterServer
|
|
396
417
|
* @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
|
|
418
|
+
* @template C 自定义 RouteContext 类型
|
|
397
419
|
*/
|
|
398
|
-
declare class QueryRouterServer extends QueryRouter {
|
|
420
|
+
declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> extends QueryRouter<C> {
|
|
399
421
|
appId: string;
|
|
400
422
|
handle: any;
|
|
401
|
-
|
|
423
|
+
context: RouteContext<C>;
|
|
424
|
+
constructor(opts?: QueryRouterServerOpts<C>);
|
|
402
425
|
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
|
|
403
426
|
addRoute(route: Route, opts?: AddOpts): void;
|
|
404
427
|
Route: typeof Route;
|
|
405
|
-
route(opts: RouteOpts
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
route(path: string,
|
|
409
|
-
|
|
410
|
-
|
|
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>;
|
|
411
439
|
/**
|
|
412
440
|
* 调用了handle
|
|
413
441
|
* @param param0
|
|
@@ -418,9 +446,7 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
418
446
|
path?: string;
|
|
419
447
|
key?: string;
|
|
420
448
|
payload?: any;
|
|
421
|
-
}, ctx?: RouteContext
|
|
422
|
-
[key: string]: any;
|
|
423
|
-
}): Promise<any>;
|
|
449
|
+
}, ctx?: Partial<RouteContext<C>>): Promise<any>;
|
|
424
450
|
}
|
|
425
451
|
|
|
426
452
|
type RouteObject = {
|