@kevisual/router 0.0.83 → 0.0.85
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 +103 -64
- package/dist/opencode.d.ts +99 -82
- package/dist/opencode.js +10 -5
- package/dist/router-browser.d.ts +77 -51
- package/dist/router-browser.js +16 -17
- package/dist/router-define.d.ts +77 -51
- package/dist/router.d.ts +101 -82
- package/dist/router.js +33 -57
- package/dist/ws.d.ts +122 -69
- package/package.json +7 -7
- package/readme.md +78 -63
- package/src/app.ts +7 -50
- package/src/opencode.ts +12 -5
- package/src/route.ts +80 -62
- package/src/server/server-base.ts +4 -1
- package/src/server/server-bun.ts +6 -2
- package/src/server/server-type.ts +17 -0
- package/src/server/ws-server.ts +8 -1
- 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-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;
|
|
@@ -171,7 +187,7 @@ type RouteOpts<U = {}, T = SimpleObject$1> = {
|
|
|
171
187
|
description?: string;
|
|
172
188
|
metadata?: T;
|
|
173
189
|
middleware?: RouteMiddleware[];
|
|
174
|
-
type?: 'route' | 'middleware';
|
|
190
|
+
type?: 'route' | 'middleware' | 'compound';
|
|
175
191
|
/**
|
|
176
192
|
* $#$ will be used to split path and key
|
|
177
193
|
*/
|
|
@@ -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
|
@@ -280,7 +280,7 @@ function pick(obj, keys) {
|
|
|
280
280
|
// node_modules/.pnpm/eventemitter3@5.0.4/node_modules/eventemitter3/index.mjs
|
|
281
281
|
var import__ = __toESM(require_eventemitter3(), 1);
|
|
282
282
|
|
|
283
|
-
// node_modules/.pnpm/es-toolkit@1.
|
|
283
|
+
// node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/predicate/isPlainObject.mjs
|
|
284
284
|
function isPlainObject(value) {
|
|
285
285
|
if (!value || typeof value !== "object") {
|
|
286
286
|
return false;
|
|
@@ -293,12 +293,12 @@ function isPlainObject(value) {
|
|
|
293
293
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
294
294
|
}
|
|
295
295
|
|
|
296
|
-
// node_modules/.pnpm/es-toolkit@1.
|
|
296
|
+
// node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/_internal/isUnsafeProperty.mjs
|
|
297
297
|
function isUnsafeProperty(key) {
|
|
298
298
|
return key === "__proto__";
|
|
299
299
|
}
|
|
300
300
|
|
|
301
|
-
// node_modules/.pnpm/es-toolkit@1.
|
|
301
|
+
// node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/object/merge.mjs
|
|
302
302
|
function merge(target, source) {
|
|
303
303
|
const sourceKeys = Object.keys(source);
|
|
304
304
|
for (let i = 0;i < sourceKeys.length; i++) {
|
|
@@ -14096,7 +14096,7 @@ class Route {
|
|
|
14096
14096
|
if (opts) {
|
|
14097
14097
|
this.id = opts.id || randomId(12, "rand-");
|
|
14098
14098
|
if (!opts.id && opts.idUsePath) {
|
|
14099
|
-
const delimiter = opts.delimiter ?? "
|
|
14099
|
+
const delimiter = opts.delimiter ?? "$$";
|
|
14100
14100
|
this.id = path + delimiter + key;
|
|
14101
14101
|
}
|
|
14102
14102
|
this.run = opts.run;
|
|
@@ -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;
|