@kevisual/router 0.2.5 → 0.2.6
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/README.md +13 -12
- package/dist/app.js +9 -8
- package/dist/commander.d.ts +29 -14
- package/dist/commander.js +44 -33
- package/dist/opencode.d.ts +22 -12
- package/dist/router-browser.d.ts +24 -33
- package/dist/router-browser.js +8 -8
- package/dist/router-define.d.ts +20 -13
- package/dist/router-simple.d.ts +3 -2
- package/dist/router-simple.js +118 -143
- package/dist/router.d.ts +32 -35
- package/dist/router.js +8 -8
- package/dist/ws.d.ts +22 -12
- package/package.json +7 -7
- package/src/auto/listen/server-time.ts +1 -1
- package/src/browser.ts +0 -2
- package/src/commander.ts +46 -37
- package/src/index.ts +0 -2
- package/src/route.ts +12 -11
- package/src/router-simple.ts +1 -1
- package/src/test/api.d.ts +72 -0
- package/src/test/api.js +1 -0
- package/src/test/cli.ts +0 -0
- package/src/test/mini.ts +11 -3
- package/src/test/route-ts.ts +1 -1
- package/src/test/run-schema.ts +2 -1
- package/src/test/static.ts +1 -1
- package/src/test/ws.ts +1 -0
- package/src/utils/listen-process.ts +1 -1
- package/src/test/define.ts +0 -14
- package/src/test/listen-ip.ts +0 -18
- package/src/test/schema.ts +0 -14
package/dist/router.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ declare class CustomError extends Error {
|
|
|
22
22
|
static fromCode(code?: number): CustomError;
|
|
23
23
|
static fromErrorData(code?: number, data?: any): CustomError;
|
|
24
24
|
static parseError(e: CustomError): {
|
|
25
|
-
code: number
|
|
25
|
+
code: number;
|
|
26
26
|
data: any;
|
|
27
27
|
message: string;
|
|
28
28
|
};
|
|
@@ -36,7 +36,7 @@ declare class CustomError extends Error {
|
|
|
36
36
|
static throw(code?: number | string, opts?: CustomErrorOptions): void;
|
|
37
37
|
static throw(opts?: CustomErrorOptions): void;
|
|
38
38
|
parse(e?: CustomError): {
|
|
39
|
-
code: number
|
|
39
|
+
code: number;
|
|
40
40
|
data: any;
|
|
41
41
|
message: string;
|
|
42
42
|
};
|
|
@@ -59,22 +59,6 @@ declare class MockProcess {
|
|
|
59
59
|
on(fn: (msg?: any) => any): void;
|
|
60
60
|
desctroy(): void;
|
|
61
61
|
}
|
|
62
|
-
type ListenProcessParams = {
|
|
63
|
-
message?: RunMessage;
|
|
64
|
-
context?: any;
|
|
65
|
-
};
|
|
66
|
-
type ListenProcessResponse = {
|
|
67
|
-
success?: boolean;
|
|
68
|
-
data?: {
|
|
69
|
-
code?: number;
|
|
70
|
-
data?: any;
|
|
71
|
-
message?: string;
|
|
72
|
-
[key: string]: any;
|
|
73
|
-
};
|
|
74
|
-
error?: any;
|
|
75
|
-
timestamp?: string;
|
|
76
|
-
[key: string]: any;
|
|
77
|
-
};
|
|
78
62
|
|
|
79
63
|
type RouterContextT = {
|
|
80
64
|
code?: number;
|
|
@@ -232,7 +216,7 @@ declare class Route<M extends SimpleObject$1 = SimpleObject$1, U extends SimpleO
|
|
|
232
216
|
description?: string;
|
|
233
217
|
metadata?: M;
|
|
234
218
|
middleware?: RouteMiddleware[];
|
|
235
|
-
type?: string
|
|
219
|
+
type?: string;
|
|
236
220
|
/**
|
|
237
221
|
* 是否开启debug,开启后会打印错误信息
|
|
238
222
|
*/
|
|
@@ -325,6 +309,7 @@ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements
|
|
|
325
309
|
path: string;
|
|
326
310
|
key?: string;
|
|
327
311
|
payload?: any;
|
|
312
|
+
args?: any;
|
|
328
313
|
}, ctx?: RouteContext<T> & {
|
|
329
314
|
[key: string]: any;
|
|
330
315
|
}): Promise<RouteContext<T, {}, {
|
|
@@ -341,13 +326,14 @@ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements
|
|
|
341
326
|
path?: string;
|
|
342
327
|
key?: string;
|
|
343
328
|
payload?: any;
|
|
329
|
+
args?: any;
|
|
344
330
|
}, ctx?: RouteContext<T> & {
|
|
345
331
|
[key: string]: any;
|
|
346
332
|
}): Promise<RouteContext<T, {}, {
|
|
347
333
|
[key: string]: any;
|
|
348
334
|
}> | {
|
|
349
335
|
code: number;
|
|
350
|
-
body:
|
|
336
|
+
body: any;
|
|
351
337
|
message: string;
|
|
352
338
|
}>;
|
|
353
339
|
/**
|
|
@@ -362,12 +348,13 @@ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements
|
|
|
362
348
|
path: string;
|
|
363
349
|
key?: string;
|
|
364
350
|
payload?: any;
|
|
351
|
+
args?: any;
|
|
365
352
|
}, ctx?: RouteContext & {
|
|
366
353
|
[key: string]: any;
|
|
367
354
|
}): Promise<{
|
|
368
|
-
code: number
|
|
369
|
-
data:
|
|
370
|
-
message: string
|
|
355
|
+
code: number;
|
|
356
|
+
data: any;
|
|
357
|
+
message: string;
|
|
371
358
|
}>;
|
|
372
359
|
/**
|
|
373
360
|
* Router Run获取数据
|
|
@@ -380,12 +367,13 @@ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements
|
|
|
380
367
|
path?: string;
|
|
381
368
|
key?: string;
|
|
382
369
|
payload?: any;
|
|
370
|
+
args?: any;
|
|
383
371
|
}, ctx?: RouteContext<T> & {
|
|
384
372
|
[key: string]: any;
|
|
385
373
|
}): Promise<{
|
|
386
|
-
code: number
|
|
387
|
-
data:
|
|
388
|
-
message: string
|
|
374
|
+
code: number;
|
|
375
|
+
data: any;
|
|
376
|
+
message: string;
|
|
389
377
|
}>;
|
|
390
378
|
/**
|
|
391
379
|
* 设置上下文
|
|
@@ -420,12 +408,12 @@ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements
|
|
|
420
408
|
importRoutes(routes: Route[]): void;
|
|
421
409
|
importRouter(router: QueryRouter): void;
|
|
422
410
|
throw(...args: any[]): void;
|
|
423
|
-
hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1
|
|
411
|
+
hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
424
412
|
findRoute(opts?: {
|
|
425
413
|
path?: string;
|
|
426
414
|
key?: string;
|
|
427
415
|
rid?: string;
|
|
428
|
-
}): Route<SimpleObject$1, SimpleObject$1
|
|
416
|
+
}): Route<SimpleObject$1, SimpleObject$1>;
|
|
429
417
|
createRouteList(opts?: {
|
|
430
418
|
force?: boolean;
|
|
431
419
|
filter?: (route: Route) => boolean;
|
|
@@ -519,6 +507,7 @@ declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> exten
|
|
|
519
507
|
path?: string;
|
|
520
508
|
key?: string;
|
|
521
509
|
payload?: any;
|
|
510
|
+
args?: any;
|
|
522
511
|
token?: string;
|
|
523
512
|
data?: any;
|
|
524
513
|
}, ctx?: Partial<RouteContext<C>>): Promise<any>;
|
|
@@ -534,7 +523,9 @@ declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> exten
|
|
|
534
523
|
* 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件
|
|
535
524
|
* @param fun 认证函数,接收 RouteContext 和认证类型
|
|
536
525
|
*/
|
|
537
|
-
createAuth(fun
|
|
526
|
+
createAuth(fun?: (ctx: RouteContext<C>, type?: 'auth' | 'auth-admin' | 'auth-can') => any, opts?: {
|
|
527
|
+
overwrite?: boolean;
|
|
528
|
+
}): Promise<void>;
|
|
538
529
|
}
|
|
539
530
|
declare class Mini extends QueryRouterServer {
|
|
540
531
|
}
|
|
@@ -628,8 +619,8 @@ declare class Chain {
|
|
|
628
619
|
object: RouteOpts;
|
|
629
620
|
app?: QueryRouterServer;
|
|
630
621
|
constructor(object: RouteOpts, opts?: ChainOptions);
|
|
631
|
-
get key(): string
|
|
632
|
-
get path(): string
|
|
622
|
+
get key(): string;
|
|
623
|
+
get path(): string;
|
|
633
624
|
setDescription(desc: string): this;
|
|
634
625
|
setMeta(metadata: {
|
|
635
626
|
[key: string]: any;
|
|
@@ -1031,7 +1022,10 @@ declare class ServerNode extends ServerBase implements ServerType {
|
|
|
1031
1022
|
* @param res
|
|
1032
1023
|
* @returns
|
|
1033
1024
|
*/
|
|
1034
|
-
declare const handleServer: (req: IncomingMessage, res: ServerResponse) => Promise<
|
|
1025
|
+
declare const handleServer: (req: IncomingMessage, res: ServerResponse) => Promise<{
|
|
1026
|
+
cookies: Record<string, string>;
|
|
1027
|
+
token: string;
|
|
1028
|
+
}>;
|
|
1035
1029
|
|
|
1036
1030
|
type RouterHandle = (msg: {
|
|
1037
1031
|
path: string;
|
|
@@ -1073,9 +1067,12 @@ declare class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
|
|
|
1073
1067
|
listen(handle: any, backlog?: number, listeningListener?: () => void): void;
|
|
1074
1068
|
listen(handle: any, listeningListener?: () => void): void;
|
|
1075
1069
|
Route: typeof Route;
|
|
1076
|
-
static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<
|
|
1070
|
+
static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
|
|
1071
|
+
cookies: Record<string, string>;
|
|
1072
|
+
token: string;
|
|
1073
|
+
}>;
|
|
1077
1074
|
onServerRequest(fn: (req: IncomingMessage$1, res: ServerResponse$1) => void): void;
|
|
1078
1075
|
}
|
|
1079
1076
|
|
|
1080
|
-
export { App, CustomError, Mini,
|
|
1081
|
-
export type { HttpListenerFun,
|
|
1077
|
+
export { App, CustomError, Mini, QueryRouter, QueryRouterServer, QueryUtil, Route, ServerNode, createSchema, createSkill, define, fromJSONSchema, handleServer, toJSONSchema, tool, util };
|
|
1078
|
+
export type { HttpListenerFun, Listener, OnListener, OnWebSocketFn, RouteArray, RouteContext, RouteInfo, RouteMiddleware, RouteObject, RouteOpts, RouterReq, RouterRes, Rule, Run, Schema, Skill, WS, WebSocketListenerFun, WebSocketReq, WebSocketRes };
|
package/dist/router.js
CHANGED
|
@@ -3691,7 +3691,7 @@ var listenProcess = async ({ app, mockProcess, params = {}, timeout: timeout2 =
|
|
|
3691
3691
|
const ctx = mergeParams?.context || {};
|
|
3692
3692
|
if (!msg.path && !msg.id) {
|
|
3693
3693
|
const route = app.routes.find((r) => r.path !== "router");
|
|
3694
|
-
msg.id = route?.
|
|
3694
|
+
msg.id = route?.rid;
|
|
3695
3695
|
}
|
|
3696
3696
|
const result = await app.run(msg, ctx);
|
|
3697
3697
|
const response = {
|
|
@@ -17704,9 +17704,9 @@ class QueryRouter {
|
|
|
17704
17704
|
if (!message?.path) {
|
|
17705
17705
|
return Promise.resolve({ code: 404, body: null, message: "Not found path" });
|
|
17706
17706
|
}
|
|
17707
|
-
const { path, key = "", payload = {}, ...query } = message;
|
|
17707
|
+
const { path, key = "", payload = {}, args = {}, ...query } = message;
|
|
17708
17708
|
ctx = ctx || {};
|
|
17709
|
-
ctx.query = { ...ctx.query, ...query, ...payload };
|
|
17709
|
+
ctx.query = { ...ctx.query, ...query, ...payload, ...args };
|
|
17710
17710
|
ctx.args = ctx.query;
|
|
17711
17711
|
ctx.state = { ...ctx?.state };
|
|
17712
17712
|
ctx.throw = this.throw;
|
|
@@ -17916,7 +17916,8 @@ class QueryRouterServer extends QueryRouter {
|
|
|
17916
17916
|
const { path, key, rid } = api2;
|
|
17917
17917
|
return this.run({ path, key, rid, payload }, ctx);
|
|
17918
17918
|
}
|
|
17919
|
-
async createAuth(fun) {
|
|
17919
|
+
async createAuth(fun, opts) {
|
|
17920
|
+
const overwrite = opts?.overwrite ?? false;
|
|
17920
17921
|
this.route({
|
|
17921
17922
|
path: "auth",
|
|
17922
17923
|
key: "auth",
|
|
@@ -17926,7 +17927,7 @@ class QueryRouterServer extends QueryRouter {
|
|
|
17926
17927
|
if (fun) {
|
|
17927
17928
|
await fun(ctx, "auth");
|
|
17928
17929
|
}
|
|
17929
|
-
}).addTo(this, { overwrite
|
|
17930
|
+
}).addTo(this, { overwrite });
|
|
17930
17931
|
this.route({
|
|
17931
17932
|
path: "auth-admin",
|
|
17932
17933
|
key: "auth-admin",
|
|
@@ -17937,7 +17938,7 @@ class QueryRouterServer extends QueryRouter {
|
|
|
17937
17938
|
if (fun) {
|
|
17938
17939
|
await fun(ctx, "auth-admin");
|
|
17939
17940
|
}
|
|
17940
|
-
}).addTo(this, { overwrite
|
|
17941
|
+
}).addTo(this, { overwrite });
|
|
17941
17942
|
this.route({
|
|
17942
17943
|
path: "auth-can",
|
|
17943
17944
|
key: "auth-can",
|
|
@@ -17947,7 +17948,7 @@ class QueryRouterServer extends QueryRouter {
|
|
|
17947
17948
|
if (fun) {
|
|
17948
17949
|
await fun(ctx, "auth-can");
|
|
17949
17950
|
}
|
|
17950
|
-
}).addTo(this, { overwrite
|
|
17951
|
+
}).addTo(this, { overwrite });
|
|
17951
17952
|
}
|
|
17952
17953
|
}
|
|
17953
17954
|
|
|
@@ -19085,7 +19086,6 @@ export {
|
|
|
19085
19086
|
QueryUtil,
|
|
19086
19087
|
QueryRouterServer,
|
|
19087
19088
|
QueryRouter,
|
|
19088
|
-
MockProcess,
|
|
19089
19089
|
Mini,
|
|
19090
19090
|
CustomError,
|
|
19091
19091
|
App
|
package/dist/ws.d.ts
CHANGED
|
@@ -218,7 +218,7 @@ declare class Route<M extends SimpleObject = SimpleObject, U extends SimpleObjec
|
|
|
218
218
|
description?: string;
|
|
219
219
|
metadata?: M;
|
|
220
220
|
middleware?: RouteMiddleware[];
|
|
221
|
-
type?: string
|
|
221
|
+
type?: string;
|
|
222
222
|
/**
|
|
223
223
|
* 是否开启debug,开启后会打印错误信息
|
|
224
224
|
*/
|
|
@@ -294,6 +294,7 @@ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements thro
|
|
|
294
294
|
path: string;
|
|
295
295
|
key?: string;
|
|
296
296
|
payload?: any;
|
|
297
|
+
args?: any;
|
|
297
298
|
}, ctx?: RouteContext<T> & {
|
|
298
299
|
[key: string]: any;
|
|
299
300
|
}): Promise<RouteContext<T, {}, {
|
|
@@ -310,13 +311,14 @@ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements thro
|
|
|
310
311
|
path?: string;
|
|
311
312
|
key?: string;
|
|
312
313
|
payload?: any;
|
|
314
|
+
args?: any;
|
|
313
315
|
}, ctx?: RouteContext<T> & {
|
|
314
316
|
[key: string]: any;
|
|
315
317
|
}): Promise<RouteContext<T, {}, {
|
|
316
318
|
[key: string]: any;
|
|
317
319
|
}> | {
|
|
318
320
|
code: number;
|
|
319
|
-
body:
|
|
321
|
+
body: any;
|
|
320
322
|
message: string;
|
|
321
323
|
}>;
|
|
322
324
|
/**
|
|
@@ -331,12 +333,13 @@ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements thro
|
|
|
331
333
|
path: string;
|
|
332
334
|
key?: string;
|
|
333
335
|
payload?: any;
|
|
336
|
+
args?: any;
|
|
334
337
|
}, ctx?: RouteContext & {
|
|
335
338
|
[key: string]: any;
|
|
336
339
|
}): Promise<{
|
|
337
|
-
code: number
|
|
338
|
-
data:
|
|
339
|
-
message: string
|
|
340
|
+
code: number;
|
|
341
|
+
data: any;
|
|
342
|
+
message: string;
|
|
340
343
|
}>;
|
|
341
344
|
/**
|
|
342
345
|
* Router Run获取数据
|
|
@@ -349,12 +352,13 @@ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements thro
|
|
|
349
352
|
path?: string;
|
|
350
353
|
key?: string;
|
|
351
354
|
payload?: any;
|
|
355
|
+
args?: any;
|
|
352
356
|
}, ctx?: RouteContext<T> & {
|
|
353
357
|
[key: string]: any;
|
|
354
358
|
}): Promise<{
|
|
355
|
-
code: number
|
|
356
|
-
data:
|
|
357
|
-
message: string
|
|
359
|
+
code: number;
|
|
360
|
+
data: any;
|
|
361
|
+
message: string;
|
|
358
362
|
}>;
|
|
359
363
|
/**
|
|
360
364
|
* 设置上下文
|
|
@@ -389,12 +393,12 @@ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements thro
|
|
|
389
393
|
importRoutes(routes: Route[]): void;
|
|
390
394
|
importRouter(router: QueryRouter): void;
|
|
391
395
|
throw(...args: any[]): void;
|
|
392
|
-
hasRoute(path: string, key?: string): Route<SimpleObject, SimpleObject
|
|
396
|
+
hasRoute(path: string, key?: string): Route<SimpleObject, SimpleObject>;
|
|
393
397
|
findRoute(opts?: {
|
|
394
398
|
path?: string;
|
|
395
399
|
key?: string;
|
|
396
400
|
rid?: string;
|
|
397
|
-
}): Route<SimpleObject, SimpleObject
|
|
401
|
+
}): Route<SimpleObject, SimpleObject>;
|
|
398
402
|
createRouteList(opts?: {
|
|
399
403
|
force?: boolean;
|
|
400
404
|
filter?: (route: Route) => boolean;
|
|
@@ -488,6 +492,7 @@ declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Q
|
|
|
488
492
|
path?: string;
|
|
489
493
|
key?: string;
|
|
490
494
|
payload?: any;
|
|
495
|
+
args?: any;
|
|
491
496
|
token?: string;
|
|
492
497
|
data?: any;
|
|
493
498
|
}, ctx?: Partial<RouteContext<C>>): Promise<any>;
|
|
@@ -503,7 +508,9 @@ declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Q
|
|
|
503
508
|
* 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件
|
|
504
509
|
* @param fun 认证函数,接收 RouteContext 和认证类型
|
|
505
510
|
*/
|
|
506
|
-
createAuth(fun
|
|
511
|
+
createAuth(fun?: (ctx: RouteContext<C>, type?: 'auth' | 'auth-admin' | 'auth-can') => any, opts?: {
|
|
512
|
+
overwrite?: boolean;
|
|
513
|
+
}): Promise<void>;
|
|
507
514
|
}
|
|
508
515
|
/** JSON Schema 基本类型映射到 TypeScript 类型 */
|
|
509
516
|
type JsonSchemaTypeToTS<T> = T extends {
|
|
@@ -838,7 +845,10 @@ declare class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
|
|
|
838
845
|
listen(handle: any, backlog?: number, listeningListener?: () => void): void;
|
|
839
846
|
listen(handle: any, listeningListener?: () => void): void;
|
|
840
847
|
Route: typeof Route;
|
|
841
|
-
static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<
|
|
848
|
+
static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
|
|
849
|
+
cookies: Record<string, string>;
|
|
850
|
+
token: string;
|
|
851
|
+
}>;
|
|
842
852
|
onServerRequest(fn: (req: IncomingMessage$1, res: ServerResponse$1) => void): void;
|
|
843
853
|
}
|
|
844
854
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@kevisual/router",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.6",
|
|
5
5
|
"description": "",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/router.js",
|
|
@@ -22,18 +22,18 @@
|
|
|
22
22
|
"author": "abearxiong",
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@kevisual/code-builder": "^0.0.
|
|
26
|
-
"@kevisual/context": "^0.0.
|
|
25
|
+
"@kevisual/code-builder": "^0.0.7",
|
|
26
|
+
"@kevisual/context": "^0.0.10",
|
|
27
27
|
"@kevisual/dts": "^0.0.4",
|
|
28
28
|
"@kevisual/js-filter": "^0.0.6",
|
|
29
29
|
"@kevisual/local-proxy": "^0.0.8",
|
|
30
30
|
"@kevisual/query": "^0.0.55",
|
|
31
31
|
"@kevisual/remote-app": "^0.0.7",
|
|
32
32
|
"@kevisual/use-config": "^1.0.30",
|
|
33
|
-
"@opencode-ai/plugin": "^1.
|
|
34
|
-
"@types/bun": "^1.3.
|
|
33
|
+
"@opencode-ai/plugin": "^1.4.6",
|
|
34
|
+
"@types/bun": "^1.3.12",
|
|
35
35
|
"@types/crypto-js": "^4.2.2",
|
|
36
|
-
"@types/node": "^25.
|
|
36
|
+
"@types/node": "^25.6.0",
|
|
37
37
|
"@types/send": "^1.2.1",
|
|
38
38
|
"@types/ws": "^8.18.1",
|
|
39
39
|
"@types/xml2js": "^0.4.14",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"eventemitter3": "^5.0.4",
|
|
44
44
|
"fast-glob": "^3.3.3",
|
|
45
45
|
"nanoid": "^5.1.7",
|
|
46
|
-
"path-to-regexp": "^8.
|
|
46
|
+
"path-to-regexp": "^8.4.2",
|
|
47
47
|
"send": "^1.2.1",
|
|
48
48
|
"typescript": "^6.0.2",
|
|
49
49
|
"ws": "npm:@kevisual/ws",
|
package/src/browser.ts
CHANGED
|
@@ -13,6 +13,4 @@ export { createSkill, tool, fromJSONSchema, toJSONSchema } from './route.ts';
|
|
|
13
13
|
export { CustomError } from './result/error.ts';
|
|
14
14
|
|
|
15
15
|
export * from './router-define.ts';
|
|
16
|
-
|
|
17
|
-
export { MockProcess, type ListenProcessParams, type ListenProcessResponse } from './utils/listen-process.ts'
|
|
18
16
|
// --- 以上同步更新至 browser.ts ---
|
package/src/commander.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Command, program } from 'commander';
|
|
|
2
2
|
import { App } from './app.ts';
|
|
3
3
|
import { RemoteApp } from '@kevisual/remote-app'
|
|
4
4
|
import z from 'zod';
|
|
5
|
+
import { create } from 'node:domain';
|
|
5
6
|
export const groupByPath = (routes: App['routes']) => {
|
|
6
7
|
return routes.reduce((acc, route) => {
|
|
7
8
|
const path = route.path || 'default';
|
|
@@ -68,7 +69,7 @@ export const parseDescription = (route: App['routes'][number]) => {
|
|
|
68
69
|
}
|
|
69
70
|
return desc;
|
|
70
71
|
}
|
|
71
|
-
export const
|
|
72
|
+
export const createCommandList = (opts: { app: any, program: Command }) => {
|
|
72
73
|
const { program } = opts;
|
|
73
74
|
const app = opts.app as App;
|
|
74
75
|
const routes = app.routes;
|
|
@@ -76,45 +77,53 @@ export const createCommand = (opts: { app: any, program: Command }) => {
|
|
|
76
77
|
const groupRoutes = groupByPath(routes);
|
|
77
78
|
for (const path in groupRoutes) {
|
|
78
79
|
const routeList = groupRoutes[path];
|
|
79
|
-
|
|
80
|
+
if (!routeList) continue;
|
|
81
|
+
const keys = routeList.map(route => route.key)
|
|
80
82
|
const subProgram = program.command(path).description(`路由[${path}] ${keys.length > 0 ? ': ' + keys.join(', ') : ''}`);
|
|
81
83
|
routeList.forEach(route => {
|
|
82
|
-
if (!route.key) return;
|
|
83
84
|
const description = parseDescription(route);
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (typeof data === 'object') {
|
|
91
|
-
process.stdout.write(JSON.stringify(data, null, 2) + '\n');
|
|
92
|
-
} else {
|
|
93
|
-
process.stdout.write(String(data) + '\n');
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
try {
|
|
97
|
-
let args: Record<string, any> = {};
|
|
98
|
-
if (options.args) {
|
|
99
|
-
args = parseArgs(options.args);
|
|
100
|
-
} else if (passedArgs.length > 0) {
|
|
101
|
-
args = parseArgs(passedArgs.join(' '));
|
|
102
|
-
}
|
|
103
|
-
// 这里可以添加实际的命令执行逻辑,例如调用对应的路由处理函数
|
|
104
|
-
const res = await app.run({ path, key: route.key, payload: args }, { appId: app.appId });
|
|
105
|
-
if (res.code === 200) {
|
|
106
|
-
output(res.data);
|
|
107
|
-
} else {
|
|
108
|
-
output(`Error: ${res.message}`);
|
|
109
|
-
}
|
|
110
|
-
} catch (error) {
|
|
111
|
-
output(`Execution error: ${error instanceof Error ? error.message : String(error)}`);
|
|
112
|
-
}
|
|
113
|
-
});
|
|
85
|
+
if (!route.key) {
|
|
86
|
+
createCommand(subProgram, { description, app, route });
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const _sumCommandy = subProgram.command(route.key)
|
|
90
|
+
createCommand(_sumCommandy, { description, app, route });
|
|
114
91
|
});
|
|
115
92
|
}
|
|
116
93
|
}
|
|
117
|
-
|
|
94
|
+
export const createCommand = (command: Command, opts: { description?: string, app: App, route: any }) => {
|
|
95
|
+
const { description, app, route } = opts;
|
|
96
|
+
const path = route.path;
|
|
97
|
+
command.description(description || '')
|
|
98
|
+
.option('--args <args>', '命令参数,支持 JSON 格式或 key=value 形式,例如: --args \'{"a":1}\' 或 --args \'a=1 b=2\'')
|
|
99
|
+
.argument('[args...]', '位置参数(推荐通过 -- 分隔传入),支持 JSON 或 key=value 格式,例如: -- a=1 b=2 或 -- \'{"a":1}\'')
|
|
100
|
+
.action(async (passedArgs: string[], options, _command) => {
|
|
101
|
+
const output = (data: any) => {
|
|
102
|
+
if (typeof data === 'object') {
|
|
103
|
+
process.stdout.write(JSON.stringify(data, null, 2) + '\n');
|
|
104
|
+
} else {
|
|
105
|
+
process.stdout.write(String(data) + '\n');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
let args: Record<string, any> = {};
|
|
110
|
+
if (options.args) {
|
|
111
|
+
args = parseArgs(options.args);
|
|
112
|
+
} else if (passedArgs.length > 0) {
|
|
113
|
+
args = parseArgs(passedArgs.join(' '));
|
|
114
|
+
}
|
|
115
|
+
// 这里可以添加实际的命令执行逻辑,例如调用对应的路由处理函数
|
|
116
|
+
const res = await app.run({ path, key: route.key, payload: args }, { appId: app.appId });
|
|
117
|
+
if (res.code === 200) {
|
|
118
|
+
output(res.data);
|
|
119
|
+
} else {
|
|
120
|
+
output(`Error: ${res.message}`);
|
|
121
|
+
}
|
|
122
|
+
} catch (error) {
|
|
123
|
+
output(`Execution error: ${error instanceof Error ? error.message : String(error)}`);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
118
127
|
export const parse = async (opts: {
|
|
119
128
|
app: any,
|
|
120
129
|
description?: string,
|
|
@@ -130,7 +139,7 @@ export const parse = async (opts: {
|
|
|
130
139
|
},
|
|
131
140
|
exitOnEnd?: boolean,
|
|
132
141
|
}) => {
|
|
133
|
-
const { description, parse = true, version, exitOnEnd =
|
|
142
|
+
const { description, parse = true, version, exitOnEnd = false } = opts;
|
|
134
143
|
const app = opts.app as App;
|
|
135
144
|
const _program = opts.program || program;
|
|
136
145
|
_program.description(description || 'Router 命令行工具');
|
|
@@ -140,10 +149,10 @@ export const parse = async (opts: {
|
|
|
140
149
|
app.createRouteList();
|
|
141
150
|
|
|
142
151
|
createCliList(app);
|
|
143
|
-
|
|
152
|
+
createCommandList({ app: app as App, program: _program });
|
|
144
153
|
|
|
145
154
|
if (opts.remote) {
|
|
146
|
-
const { token, username, id
|
|
155
|
+
const { token, username, id, url } = opts.remote;
|
|
147
156
|
const remoteApp = new RemoteApp({
|
|
148
157
|
app,
|
|
149
158
|
token,
|
package/src/index.ts
CHANGED
|
@@ -13,8 +13,6 @@ export { createSkill, tool, fromJSONSchema, toJSONSchema } from './route.ts';
|
|
|
13
13
|
export { CustomError } from './result/error.ts';
|
|
14
14
|
|
|
15
15
|
export * from './router-define.ts';
|
|
16
|
-
|
|
17
|
-
export { MockProcess, type ListenProcessParams, type ListenProcessResponse } from './utils/listen-process.ts'
|
|
18
16
|
// --- 以上同步更新至 browser.ts ---
|
|
19
17
|
|
|
20
18
|
export { ServerNode, handleServer } from './server/index.ts';
|
package/src/route.ts
CHANGED
|
@@ -490,13 +490,13 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
|
|
|
490
490
|
* @param ctx
|
|
491
491
|
* @returns
|
|
492
492
|
*/
|
|
493
|
-
async parse(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
|
|
493
|
+
async parse(message: { path: string; key?: string; payload?: any, args?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
|
|
494
494
|
if (!message?.path) {
|
|
495
495
|
return Promise.resolve({ code: 404, body: null, message: 'Not found path' } as RouteContext<T>);
|
|
496
496
|
}
|
|
497
|
-
const { path, key = '', payload = {}, ...query } = message;
|
|
497
|
+
const { path, key = '', payload = {}, args = {}, ...query } = message;
|
|
498
498
|
ctx = ctx || {} as RouteContext<T>;
|
|
499
|
-
ctx.query = { ...ctx.query, ...query, ...payload };
|
|
499
|
+
ctx.query = { ...ctx.query, ...query, ...payload, ...args };
|
|
500
500
|
ctx.args = ctx.query;
|
|
501
501
|
ctx.state = { ...ctx?.state };
|
|
502
502
|
ctx.throw = this.throw;
|
|
@@ -529,7 +529,7 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
|
|
|
529
529
|
* @param ctx
|
|
530
530
|
* @returns
|
|
531
531
|
*/
|
|
532
|
-
async call(message: { rid?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
|
|
532
|
+
async call(message: { rid?: string; path?: string; key?: string; payload?: any, args?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
|
|
533
533
|
let path = message.path;
|
|
534
534
|
let key = message.key;
|
|
535
535
|
// 优先 path + key
|
|
@@ -556,7 +556,7 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
|
|
|
556
556
|
* @deprecated use run or call instead
|
|
557
557
|
* @returns
|
|
558
558
|
*/
|
|
559
|
-
async queryRoute(message: { id?: string; path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
|
559
|
+
async queryRoute(message: { id?: string; path: string; key?: string; payload?: any, args?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
|
560
560
|
const res = await this.call(message, { ...this.context, ...ctx });
|
|
561
561
|
return {
|
|
562
562
|
code: res.code,
|
|
@@ -570,7 +570,7 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
|
|
|
570
570
|
* @param ctx
|
|
571
571
|
* @returns
|
|
572
572
|
*/
|
|
573
|
-
async run(message: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
|
|
573
|
+
async run(message: { id?: string; path?: string; key?: string; payload?: any, args?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
|
|
574
574
|
const res = await this.call(message, { ...this.context, ...ctx });
|
|
575
575
|
return {
|
|
576
576
|
code: res.code,
|
|
@@ -766,7 +766,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
|
|
|
766
766
|
* @param param0
|
|
767
767
|
* @returns
|
|
768
768
|
*/
|
|
769
|
-
async run(msg: { rid?: string; path?: string; key?: string; payload?: any, token?: string, data?: any }, ctx?: Partial<RouteContext<C>>) {
|
|
769
|
+
async run(msg: { rid?: string; path?: string; key?: string; payload?: any, args?: any, token?: string, data?: any }, ctx?: Partial<RouteContext<C>>) {
|
|
770
770
|
const handle = this.handle;
|
|
771
771
|
if (handle) {
|
|
772
772
|
return handle(msg, ctx);
|
|
@@ -786,7 +786,8 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
|
|
|
786
786
|
* 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件
|
|
787
787
|
* @param fun 认证函数,接收 RouteContext 和认证类型
|
|
788
788
|
*/
|
|
789
|
-
async createAuth(fun
|
|
789
|
+
async createAuth(fun?: (ctx: RouteContext<C>, type?: 'auth' | 'auth-admin' | 'auth-can') => any, opts?: { overwrite?: boolean }) {
|
|
790
|
+
const overwrite = opts?.overwrite ?? false;
|
|
790
791
|
this.route({
|
|
791
792
|
path: 'auth',
|
|
792
793
|
key: 'auth',
|
|
@@ -796,7 +797,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
|
|
|
796
797
|
if (fun) {
|
|
797
798
|
await fun(ctx, 'auth');
|
|
798
799
|
}
|
|
799
|
-
}).addTo(this, { overwrite:
|
|
800
|
+
}).addTo(this, { overwrite: overwrite });
|
|
800
801
|
|
|
801
802
|
this.route({
|
|
802
803
|
path: 'auth-admin',
|
|
@@ -808,7 +809,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
|
|
|
808
809
|
if (fun) {
|
|
809
810
|
await fun(ctx, 'auth-admin');
|
|
810
811
|
}
|
|
811
|
-
}).addTo(this, { overwrite:
|
|
812
|
+
}).addTo(this, { overwrite: overwrite });
|
|
812
813
|
|
|
813
814
|
this.route({
|
|
814
815
|
path: 'auth-can',
|
|
@@ -819,7 +820,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
|
|
|
819
820
|
if (fun) {
|
|
820
821
|
await fun(ctx, 'auth-can');
|
|
821
822
|
}
|
|
822
|
-
}).addTo(this, { overwrite:
|
|
823
|
+
}).addTo(this, { overwrite: overwrite });
|
|
823
824
|
}
|
|
824
825
|
}
|
|
825
826
|
|
package/src/router-simple.ts
CHANGED