@kevisual/router 0.2.5 → 0.2.7
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 +13 -9
- package/dist/commander.d.ts +43 -14
- package/dist/commander.js +44 -33
- package/dist/opencode.d.ts +36 -12
- package/dist/opencode.js +1 -1
- package/dist/router-browser.d.ts +38 -33
- package/dist/router-browser.js +11 -8
- package/dist/router-define.d.ts +34 -13
- package/dist/router-simple.d.ts +3 -2
- package/dist/router-simple.js +118 -143
- package/dist/router.d.ts +46 -35
- package/dist/router.js +11 -8
- package/dist/ws.d.ts +36 -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 +21 -13
- 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/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,21 @@ declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Q
|
|
|
488
492
|
path?: string;
|
|
489
493
|
key?: string;
|
|
490
494
|
payload?: any;
|
|
495
|
+
args?: any;
|
|
496
|
+
token?: string;
|
|
497
|
+
data?: any;
|
|
498
|
+
}, ctx?: Partial<RouteContext<C>>): Promise<any>;
|
|
499
|
+
/**
|
|
500
|
+
* 调用了handle
|
|
501
|
+
* @param param0
|
|
502
|
+
* @returns
|
|
503
|
+
*/
|
|
504
|
+
runLocal(msg: {
|
|
505
|
+
rid?: string;
|
|
506
|
+
path?: string;
|
|
507
|
+
key?: string;
|
|
508
|
+
payload?: any;
|
|
509
|
+
args?: any;
|
|
491
510
|
token?: string;
|
|
492
511
|
data?: any;
|
|
493
512
|
}, ctx?: Partial<RouteContext<C>>): Promise<any>;
|
|
@@ -503,7 +522,9 @@ declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Q
|
|
|
503
522
|
* 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件
|
|
504
523
|
* @param fun 认证函数,接收 RouteContext 和认证类型
|
|
505
524
|
*/
|
|
506
|
-
createAuth(fun
|
|
525
|
+
createAuth(fun?: (ctx: RouteContext<C>, type?: 'auth' | 'auth-admin' | 'auth-can') => any, opts?: {
|
|
526
|
+
overwrite?: boolean;
|
|
527
|
+
}): Promise<void>;
|
|
507
528
|
}
|
|
508
529
|
/** JSON Schema 基本类型映射到 TypeScript 类型 */
|
|
509
530
|
type JsonSchemaTypeToTS<T> = T extends {
|
|
@@ -838,7 +859,10 @@ declare class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
|
|
|
838
859
|
listen(handle: any, backlog?: number, listeningListener?: () => void): void;
|
|
839
860
|
listen(handle: any, listeningListener?: () => void): void;
|
|
840
861
|
Route: typeof Route;
|
|
841
|
-
static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<
|
|
862
|
+
static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
|
|
863
|
+
cookies: Record<string, string>;
|
|
864
|
+
token: string;
|
|
865
|
+
}>;
|
|
842
866
|
onServerRequest(fn: (req: IncomingMessage$1, res: ServerResponse$1) => void): void;
|
|
843
867
|
}
|
|
844
868
|
|
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.7",
|
|
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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CustomError, throwError
|
|
1
|
+
import { CustomError, throwError } from './result/error.ts';
|
|
2
2
|
import { pick } from './utils/pick.ts';
|
|
3
3
|
import { listenProcess, MockProcess } from './utils/listen-process.ts';
|
|
4
4
|
import { z } from 'zod';
|
|
@@ -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,14 +766,21 @@ 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);
|
|
773
773
|
}
|
|
774
774
|
return super.run(msg, ctx as RouteContext<C>);
|
|
775
775
|
}
|
|
776
|
-
|
|
776
|
+
/**
|
|
777
|
+
* 调用了handle
|
|
778
|
+
* @param param0
|
|
779
|
+
* @returns
|
|
780
|
+
*/
|
|
781
|
+
async runLocal(msg: { rid?: string; path?: string; key?: string; payload?: any, args?: any, token?: string, data?: any }, ctx?: Partial<RouteContext<C>>) {
|
|
782
|
+
return this.run(msg, { ...ctx, appId: this.appId } as RouteContext<C>);
|
|
783
|
+
}
|
|
777
784
|
async runAction<T extends { rid?: string; path?: string; key?: string; metadata?: { args?: any } } = {}>(
|
|
778
785
|
api: T,
|
|
779
786
|
payload: RunActionPayload<T>,
|
|
@@ -786,7 +793,8 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
|
|
|
786
793
|
* 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件
|
|
787
794
|
* @param fun 认证函数,接收 RouteContext 和认证类型
|
|
788
795
|
*/
|
|
789
|
-
async createAuth(fun
|
|
796
|
+
async createAuth(fun?: (ctx: RouteContext<C>, type?: 'auth' | 'auth-admin' | 'auth-can') => any, opts?: { overwrite?: boolean }) {
|
|
797
|
+
const overwrite = opts?.overwrite ?? false;
|
|
790
798
|
this.route({
|
|
791
799
|
path: 'auth',
|
|
792
800
|
key: 'auth',
|
|
@@ -796,7 +804,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
|
|
|
796
804
|
if (fun) {
|
|
797
805
|
await fun(ctx, 'auth');
|
|
798
806
|
}
|
|
799
|
-
}).addTo(this, { overwrite:
|
|
807
|
+
}).addTo(this, { overwrite: overwrite });
|
|
800
808
|
|
|
801
809
|
this.route({
|
|
802
810
|
path: 'auth-admin',
|
|
@@ -808,7 +816,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
|
|
|
808
816
|
if (fun) {
|
|
809
817
|
await fun(ctx, 'auth-admin');
|
|
810
818
|
}
|
|
811
|
-
}).addTo(this, { overwrite:
|
|
819
|
+
}).addTo(this, { overwrite: overwrite });
|
|
812
820
|
|
|
813
821
|
this.route({
|
|
814
822
|
path: 'auth-can',
|
|
@@ -819,7 +827,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
|
|
|
819
827
|
if (fun) {
|
|
820
828
|
await fun(ctx, 'auth-can');
|
|
821
829
|
}
|
|
822
|
-
}).addTo(this, { overwrite:
|
|
830
|
+
}).addTo(this, { overwrite: overwrite });
|
|
823
831
|
}
|
|
824
832
|
}
|
|
825
833
|
|
package/src/router-simple.ts
CHANGED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export declare const api: {
|
|
2
|
+
"app_domain_manager": {
|
|
3
|
+
/**
|
|
4
|
+
* 获取域名信息,可以通过id或者domain进行查询
|
|
5
|
+
*
|
|
6
|
+
* @param data - Request parameters
|
|
7
|
+
* @param data.data - {object}
|
|
8
|
+
*/
|
|
9
|
+
"get": {
|
|
10
|
+
"path": "app_domain_manager",
|
|
11
|
+
"key": "get",
|
|
12
|
+
"description": "获取域名信息,可以通过id或者domain进行查询",
|
|
13
|
+
"metadata": {
|
|
14
|
+
"args": {
|
|
15
|
+
"data": {
|
|
16
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
17
|
+
"type": "object",
|
|
18
|
+
"properties": {
|
|
19
|
+
"id": {
|
|
20
|
+
"type": "string"
|
|
21
|
+
},
|
|
22
|
+
"domain": {
|
|
23
|
+
"type": "string"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"additionalProperties": false,
|
|
27
|
+
"required": ["id",]
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"viewItem": {
|
|
31
|
+
"api": {
|
|
32
|
+
"url": "/api/router"
|
|
33
|
+
},
|
|
34
|
+
"type": "api",
|
|
35
|
+
"title": "路由"
|
|
36
|
+
},
|
|
37
|
+
"url": "/api/router",
|
|
38
|
+
"source": "query-proxy-api"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"delete": {
|
|
42
|
+
"path": "app_domain_manager",
|
|
43
|
+
"key": "delete",
|
|
44
|
+
"description": "删除域名",
|
|
45
|
+
"metadata": {
|
|
46
|
+
"args": {
|
|
47
|
+
"domainId": {
|
|
48
|
+
"type": "string",
|
|
49
|
+
"optional": true
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"user_manager": {
|
|
56
|
+
"getUser": {
|
|
57
|
+
"path": "user_manager",
|
|
58
|
+
"key": "getUser",
|
|
59
|
+
"description": "获取用户信息",
|
|
60
|
+
"metadata": {
|
|
61
|
+
"args": {
|
|
62
|
+
"userId": {
|
|
63
|
+
"type": "string"
|
|
64
|
+
},
|
|
65
|
+
"includeProfile": {
|
|
66
|
+
"type": "boolean"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/test/api.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const api = {}
|
package/src/test/cli.ts
ADDED
|
File without changes
|
package/src/test/mini.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Mini } from "../route.ts";
|
|
2
|
-
|
|
2
|
+
import { parse, } from '../commander.ts'
|
|
3
3
|
const app = new Mini();
|
|
4
4
|
|
|
5
5
|
app.route({
|
|
6
6
|
path: 'main',
|
|
7
|
-
|
|
7
|
+
rid: 'abc',
|
|
8
8
|
description: '这是一个测试的 main 路由'
|
|
9
9
|
}).define(async (ctx) => {
|
|
10
10
|
ctx.body = {
|
|
@@ -12,5 +12,13 @@ app.route({
|
|
|
12
12
|
}
|
|
13
13
|
}).addTo(app)
|
|
14
14
|
|
|
15
|
+
app.route({
|
|
16
|
+
path: 'good',
|
|
17
|
+
description: '这是一个测试的 good 路由'
|
|
18
|
+
}).define(async (ctx) => {
|
|
19
|
+
ctx.body = { content: 'good' }
|
|
20
|
+
console.log('good')
|
|
21
|
+
}).addTo(app)
|
|
22
|
+
// app.wait()
|
|
15
23
|
|
|
16
|
-
app
|
|
24
|
+
await parse({ app })
|
package/src/test/route-ts.ts
CHANGED
package/src/test/run-schema.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import z from "zod";
|
|
2
2
|
import { App } from "../index.ts";
|
|
3
|
+
import { api as a2 } from './api.js';
|
|
3
4
|
|
|
4
5
|
const app = new App();
|
|
5
6
|
const api = {
|
|
@@ -78,7 +79,7 @@ type API = typeof api;
|
|
|
78
79
|
|
|
79
80
|
// 类型推断生效:payload 根据 metadata.args 自动推断
|
|
80
81
|
// get 的 args.data 是 type:"object",所以 payload 需要 { data: object }
|
|
81
|
-
app.runAction(
|
|
82
|
+
app.runAction(a2.app_domain_manager.get, { data: { idd: "1" }, })
|
|
82
83
|
|
|
83
84
|
// delete 的 args 是 { domainId: { type: "string" } },所以 payload 需要 { domainId: string }
|
|
84
85
|
app.runAction(api.app_domain_manager.delete, { domainId: "d1" })
|
package/src/test/static.ts
CHANGED
package/src/test/ws.ts
CHANGED
|
@@ -99,7 +99,7 @@ export const listenProcess = async ({ app, mockProcess, params = {}, timeout = 1
|
|
|
99
99
|
*/
|
|
100
100
|
if (!msg.path && !msg.id) {
|
|
101
101
|
const route = app.routes.find(r => r.path !== 'router')
|
|
102
|
-
msg.id = route?.
|
|
102
|
+
msg.id = route?.rid
|
|
103
103
|
}
|
|
104
104
|
// 执行主要逻辑
|
|
105
105
|
const result = await app.run(msg, ctx);
|
package/src/test/define.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { App } from '@/app.ts';
|
|
2
|
-
import { QueryUtil } from '@/router-define.ts';
|
|
3
|
-
const v = QueryUtil.create({
|
|
4
|
-
a: {
|
|
5
|
-
path: 'a',
|
|
6
|
-
key: 'b',
|
|
7
|
-
},
|
|
8
|
-
});
|
|
9
|
-
const app = new App();
|
|
10
|
-
app.route(v.get('a'));
|
|
11
|
-
|
|
12
|
-
v.chain('a').define<{ f: () => {} }>(async (ctx) => {
|
|
13
|
-
// ctx.f = 'sdf';
|
|
14
|
-
});
|
package/src/test/listen-ip.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
// import { Server } from 'node:http';
|
|
2
|
-
import { ServerNode } from '../server/server.ts'
|
|
3
|
-
|
|
4
|
-
const server = new ServerNode({
|
|
5
|
-
path: '/',
|
|
6
|
-
handle: async (data, ctx) => {
|
|
7
|
-
console.log('ctx', ctx.req.url)
|
|
8
|
-
console.log('Received data:', data);
|
|
9
|
-
|
|
10
|
-
ctx.res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
11
|
-
return JSON.stringify({ code: 200, message: 'Success', data });
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
server.listen(51015, '0.0.0.0', () => {
|
|
16
|
-
console.log('Server is listening on http://localhost:3000');
|
|
17
|
-
});
|
|
18
|
-
|
package/src/test/schema.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { toJSONSchema, fromJSONSchema } from "@/route.ts";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
const schema = z.object({
|
|
4
|
-
name: z.string(),
|
|
5
|
-
age: z.number(),
|
|
6
|
-
|
|
7
|
-
});
|
|
8
|
-
// console.log("schema", schema);
|
|
9
|
-
const jsonSchema = toJSONSchema(schema);
|
|
10
|
-
console.log("jsonSchema", jsonSchema);
|
|
11
|
-
|
|
12
|
-
const newSchema = fromJSONSchema<true>(jsonSchema, { mergeObject: true });
|
|
13
|
-
console.log("newSchema shape", Object.keys(newSchema.shape));
|
|
14
|
-
console.log('check', newSchema.safeParse({ name: "Alice", age: "30" })?.success);
|