@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/agent/routes/index.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import { app } from '../app.ts'
|
|
2
2
|
import './route-create.ts'
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}).addTo(app);
|
|
13
|
-
}
|
|
4
|
+
app.route({
|
|
5
|
+
path: 'auth',
|
|
6
|
+
key: '',
|
|
7
|
+
id: 'auth',
|
|
8
|
+
description: '身份验证路由',
|
|
9
|
+
}).define(async (ctx) => {
|
|
10
|
+
//
|
|
11
|
+
}).addTo(app, { overwrite: false });
|
|
14
12
|
|
|
13
|
+
app.route({
|
|
14
|
+
path: 'auth-admin',
|
|
15
|
+
key: '',
|
|
16
|
+
id: 'auth-admin',
|
|
17
|
+
description: '管理员身份验证路由',
|
|
18
|
+
}).define(async (ctx) => {
|
|
19
|
+
//
|
|
20
|
+
}).addTo(app, { overwrite: false });
|
|
@@ -6,7 +6,7 @@ app.route({
|
|
|
6
6
|
path: 'router-skill',
|
|
7
7
|
key: 'create-route',
|
|
8
8
|
description: '创建路由技能',
|
|
9
|
-
middleware: ['auth'],
|
|
9
|
+
middleware: ['auth-admin'],
|
|
10
10
|
metadata: {
|
|
11
11
|
tags: ['opencode'],
|
|
12
12
|
...createSkill({
|
|
@@ -38,7 +38,7 @@ app.route({
|
|
|
38
38
|
path: 'router-skill',
|
|
39
39
|
key: 'version',
|
|
40
40
|
description: '获取最新router版本号',
|
|
41
|
-
middleware: ['auth'],
|
|
41
|
+
middleware: ['auth-admin'],
|
|
42
42
|
metadata: {
|
|
43
43
|
tags: ['opencode'],
|
|
44
44
|
...createSkill({
|
|
@@ -59,7 +59,7 @@ app.route({
|
|
|
59
59
|
path: 'route-skill',
|
|
60
60
|
key: 'test',
|
|
61
61
|
description: '测试路由技能',
|
|
62
|
-
middleware: ['auth'],
|
|
62
|
+
middleware: ['auth-admin'],
|
|
63
63
|
metadata: {
|
|
64
64
|
tags: ['opencode'],
|
|
65
65
|
...createSkill({
|
package/dist/app.js
CHANGED
|
@@ -17055,6 +17055,7 @@ class QueryRouter {
|
|
|
17055
17055
|
const maxNextRoute = this.maxNextRoute;
|
|
17056
17056
|
ctx = ctx || {};
|
|
17057
17057
|
ctx.currentPath = path;
|
|
17058
|
+
ctx.currentId = route?.id;
|
|
17058
17059
|
ctx.currentKey = key;
|
|
17059
17060
|
ctx.currentRoute = route;
|
|
17060
17061
|
ctx.index = (ctx.index || 0) + 1;
|
|
@@ -17068,7 +17069,7 @@ class QueryRouter {
|
|
|
17068
17069
|
ctx.code = 500;
|
|
17069
17070
|
ctx.message = "Too many nextRoute";
|
|
17070
17071
|
ctx.body = null;
|
|
17071
|
-
return;
|
|
17072
|
+
return ctx;
|
|
17072
17073
|
}
|
|
17073
17074
|
if (route && route.middleware && route.middleware.length > 0) {
|
|
17074
17075
|
const errorMiddleware = [];
|
|
@@ -17143,7 +17144,9 @@ class QueryRouter {
|
|
|
17143
17144
|
}
|
|
17144
17145
|
return ctx;
|
|
17145
17146
|
}
|
|
17146
|
-
if (ctx.end) {
|
|
17147
|
+
if (ctx.end) {
|
|
17148
|
+
return ctx;
|
|
17149
|
+
}
|
|
17147
17150
|
}
|
|
17148
17151
|
}
|
|
17149
17152
|
}
|
|
@@ -17337,7 +17340,7 @@ class QueryRouter {
|
|
|
17337
17340
|
description: "列出当前应用下的所有的路由信息",
|
|
17338
17341
|
middleware: opts?.middleware || [],
|
|
17339
17342
|
run: async (ctx) => {
|
|
17340
|
-
const tokenUser = ctx.state
|
|
17343
|
+
const tokenUser = ctx.state;
|
|
17341
17344
|
let isUser = !!tokenUser;
|
|
17342
17345
|
const list = this.getList(opts?.filter).filter((item) => {
|
|
17343
17346
|
if (item.id === "auth" || item.id === "auth-can" || item.id === "check-auth-admin" || item.id === "auth-admin") {
|
|
@@ -17368,6 +17371,56 @@ class QueryRouter {
|
|
|
17368
17371
|
fromJSONSchema = fromJSONSchema3;
|
|
17369
17372
|
}
|
|
17370
17373
|
|
|
17374
|
+
class QueryRouterServer extends QueryRouter {
|
|
17375
|
+
handle;
|
|
17376
|
+
constructor(opts) {
|
|
17377
|
+
super();
|
|
17378
|
+
const initHandle = opts?.initHandle ?? true;
|
|
17379
|
+
if (initHandle || opts?.handleFn) {
|
|
17380
|
+
this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
|
|
17381
|
+
}
|
|
17382
|
+
this.setContext({ needSerialize: false, ...opts?.context });
|
|
17383
|
+
if (opts?.appId) {
|
|
17384
|
+
this.appId = opts.appId;
|
|
17385
|
+
} else {
|
|
17386
|
+
this.appId = randomId(16);
|
|
17387
|
+
}
|
|
17388
|
+
}
|
|
17389
|
+
setHandle(wrapperFn, ctx) {
|
|
17390
|
+
this.handle = this.getHandle(this, wrapperFn, ctx);
|
|
17391
|
+
}
|
|
17392
|
+
addRoute(route, opts) {
|
|
17393
|
+
this.add(route, opts);
|
|
17394
|
+
}
|
|
17395
|
+
Route = Route;
|
|
17396
|
+
route(...args) {
|
|
17397
|
+
const [path, key, opts] = args;
|
|
17398
|
+
if (typeof path === "object") {
|
|
17399
|
+
return new Route(path.path, path.key, path);
|
|
17400
|
+
}
|
|
17401
|
+
if (typeof path === "string") {
|
|
17402
|
+
if (opts) {
|
|
17403
|
+
return new Route(path, key, opts);
|
|
17404
|
+
}
|
|
17405
|
+
if (key && typeof key === "object") {
|
|
17406
|
+
return new Route(path, key?.key || "", key);
|
|
17407
|
+
}
|
|
17408
|
+
return new Route(path, key);
|
|
17409
|
+
}
|
|
17410
|
+
return new Route(path, key, opts);
|
|
17411
|
+
}
|
|
17412
|
+
prompt(description) {
|
|
17413
|
+
return new Route(undefined, undefined, { description });
|
|
17414
|
+
}
|
|
17415
|
+
async run(msg, ctx) {
|
|
17416
|
+
const handle = this.handle;
|
|
17417
|
+
if (handle) {
|
|
17418
|
+
return handle(msg, ctx);
|
|
17419
|
+
}
|
|
17420
|
+
return super.run(msg, ctx);
|
|
17421
|
+
}
|
|
17422
|
+
}
|
|
17423
|
+
|
|
17371
17424
|
// src/server/server.ts
|
|
17372
17425
|
import http from "node:http";
|
|
17373
17426
|
import https from "node:https";
|
|
@@ -18257,11 +18310,11 @@ class BunServer extends ServerBase {
|
|
|
18257
18310
|
}
|
|
18258
18311
|
}
|
|
18259
18312
|
// src/app.ts
|
|
18260
|
-
class App extends
|
|
18313
|
+
class App extends QueryRouterServer {
|
|
18261
18314
|
router;
|
|
18262
18315
|
server;
|
|
18263
18316
|
constructor(opts) {
|
|
18264
|
-
super();
|
|
18317
|
+
super({ initHandle: false, context: { needSerialize: true, ...opts?.routerContext } });
|
|
18265
18318
|
const router = this;
|
|
18266
18319
|
let server = opts?.server;
|
|
18267
18320
|
if (!server) {
|
|
@@ -18273,7 +18326,6 @@ class App extends QueryRouter {
|
|
|
18273
18326
|
}
|
|
18274
18327
|
}
|
|
18275
18328
|
server.setHandle(router.getHandle(router, opts?.routerHandle, opts?.routerContext));
|
|
18276
|
-
router.setContext({ needSerialize: true, ...opts?.routerContext });
|
|
18277
18329
|
this.router = router;
|
|
18278
18330
|
this.server = server;
|
|
18279
18331
|
if (opts?.appId) {
|
|
@@ -18286,42 +18338,7 @@ class App extends QueryRouter {
|
|
|
18286
18338
|
listen(...args) {
|
|
18287
18339
|
this.server.listen(...args);
|
|
18288
18340
|
}
|
|
18289
|
-
addRoute(route, opts) {
|
|
18290
|
-
super.add(route, opts);
|
|
18291
|
-
}
|
|
18292
18341
|
Route = Route;
|
|
18293
|
-
route(...args) {
|
|
18294
|
-
const [path, key, opts] = args;
|
|
18295
|
-
if (typeof path === "object") {
|
|
18296
|
-
return new Route(path.path, path.key, path);
|
|
18297
|
-
}
|
|
18298
|
-
if (typeof path === "string") {
|
|
18299
|
-
if (opts) {
|
|
18300
|
-
return new Route(path, key, opts);
|
|
18301
|
-
}
|
|
18302
|
-
if (key && typeof key === "object") {
|
|
18303
|
-
return new Route(path, key?.key || "", key);
|
|
18304
|
-
}
|
|
18305
|
-
return new Route(path, key);
|
|
18306
|
-
}
|
|
18307
|
-
return new Route(path, key, opts);
|
|
18308
|
-
}
|
|
18309
|
-
prompt(...args) {
|
|
18310
|
-
const [desc] = args;
|
|
18311
|
-
let description = "";
|
|
18312
|
-
if (typeof desc === "string") {
|
|
18313
|
-
description = desc;
|
|
18314
|
-
} else if (typeof desc === "function") {
|
|
18315
|
-
description = desc() || "";
|
|
18316
|
-
}
|
|
18317
|
-
return new Route("", "", { description });
|
|
18318
|
-
}
|
|
18319
|
-
async call(message, ctx) {
|
|
18320
|
-
return await super.call(message, ctx);
|
|
18321
|
-
}
|
|
18322
|
-
async run(msg, ctx) {
|
|
18323
|
-
return await super.run(msg, ctx);
|
|
18324
|
-
}
|
|
18325
18342
|
static handleRequest(req, res) {
|
|
18326
18343
|
return handleServer(req, res);
|
|
18327
18344
|
}
|
|
@@ -19242,7 +19259,7 @@ var addCallFn = (app2) => {
|
|
|
19242
19259
|
path: "call",
|
|
19243
19260
|
key: "",
|
|
19244
19261
|
description: "调用",
|
|
19245
|
-
middleware: ["auth"],
|
|
19262
|
+
middleware: ["auth-admin"],
|
|
19246
19263
|
metadata: {
|
|
19247
19264
|
tags: ["opencode"],
|
|
19248
19265
|
...createSkill({
|
|
@@ -19256,7 +19273,7 @@ var addCallFn = (app2) => {
|
|
|
19256
19273
|
args: {
|
|
19257
19274
|
path: tool.schema.string().describe("应用路径,例如 cnb"),
|
|
19258
19275
|
key: tool.schema.string().optional().describe("应用key,例如 list-repos"),
|
|
19259
|
-
payload: tool.schema.object({}).optional().describe("
|
|
19276
|
+
payload: tool.schema.object({}).optional().describe('调用参数, 为对象, 例如 { "query": "javascript" }')
|
|
19260
19277
|
}
|
|
19261
19278
|
})
|
|
19262
19279
|
}
|
|
@@ -19284,8 +19301,13 @@ var createRouterAgentPluginFn = (opts) => {
|
|
|
19284
19301
|
if (!router.hasRoute("call", "")) {
|
|
19285
19302
|
addCallFn(router);
|
|
19286
19303
|
}
|
|
19287
|
-
if (
|
|
19288
|
-
router.route({ path: "auth", key: "", id: "auth", description: "认证" }).define(async (ctx) => {}).addTo(router
|
|
19304
|
+
if (router) {
|
|
19305
|
+
router.route({ path: "auth", key: "", id: "auth", description: "认证" }).define(async (ctx) => {}).addTo(router, {
|
|
19306
|
+
overwrite: false
|
|
19307
|
+
});
|
|
19308
|
+
router.route({ path: "auth-admin", key: "", id: "auth-admin", description: "认证" }).define(async (ctx) => {}).addTo(router, {
|
|
19309
|
+
overwrite: false
|
|
19310
|
+
});
|
|
19289
19311
|
}
|
|
19290
19312
|
const _routes = filter(router.routes, opts?.query || "");
|
|
19291
19313
|
const routes = _routes.filter((r) => {
|
|
@@ -19299,7 +19321,7 @@ var createRouterAgentPluginFn = (opts) => {
|
|
|
19299
19321
|
return false;
|
|
19300
19322
|
});
|
|
19301
19323
|
const AgentPlugin = async (pluginInput) => {
|
|
19302
|
-
useContextKey("plugin-input", () => pluginInput, true);
|
|
19324
|
+
useContextKey("plugin-input", () => pluginInput, { isNew: true });
|
|
19303
19325
|
const hooks = opts?.hooks ? await opts.hooks(pluginInput) : {};
|
|
19304
19326
|
return {
|
|
19305
19327
|
...hooks,
|
|
@@ -19540,14 +19562,14 @@ app
|
|
|
19540
19562
|
10. **中间件找不到会返回 404**,错误信息中会包含找不到的中间件列表。
|
|
19541
19563
|
`;
|
|
19542
19564
|
// package.json
|
|
19543
|
-
var version2 = "0.0.
|
|
19565
|
+
var version2 = "0.0.84";
|
|
19544
19566
|
|
|
19545
19567
|
// agent/routes/route-create.ts
|
|
19546
19568
|
app.route({
|
|
19547
19569
|
path: "router-skill",
|
|
19548
19570
|
key: "create-route",
|
|
19549
19571
|
description: "创建路由技能",
|
|
19550
|
-
middleware: ["auth"],
|
|
19572
|
+
middleware: ["auth-admin"],
|
|
19551
19573
|
metadata: {
|
|
19552
19574
|
tags: ["opencode"],
|
|
19553
19575
|
...createSkill({
|
|
@@ -19581,7 +19603,7 @@ app.route({
|
|
|
19581
19603
|
path: "router-skill",
|
|
19582
19604
|
key: "version",
|
|
19583
19605
|
description: "获取最新router版本号",
|
|
19584
|
-
middleware: ["auth"],
|
|
19606
|
+
middleware: ["auth-admin"],
|
|
19585
19607
|
metadata: {
|
|
19586
19608
|
tags: ["opencode"],
|
|
19587
19609
|
...createSkill({
|
|
@@ -19600,7 +19622,7 @@ app.route({
|
|
|
19600
19622
|
path: "route-skill",
|
|
19601
19623
|
key: "test",
|
|
19602
19624
|
description: "测试路由技能",
|
|
19603
|
-
middleware: ["auth"],
|
|
19625
|
+
middleware: ["auth-admin"],
|
|
19604
19626
|
metadata: {
|
|
19605
19627
|
tags: ["opencode"],
|
|
19606
19628
|
...createSkill({
|
|
@@ -19620,14 +19642,18 @@ app.route({
|
|
|
19620
19642
|
}).addTo(app);
|
|
19621
19643
|
|
|
19622
19644
|
// agent/routes/index.ts
|
|
19623
|
-
|
|
19624
|
-
|
|
19625
|
-
|
|
19626
|
-
|
|
19627
|
-
|
|
19628
|
-
|
|
19629
|
-
|
|
19630
|
-
|
|
19645
|
+
app.route({
|
|
19646
|
+
path: "auth",
|
|
19647
|
+
key: "",
|
|
19648
|
+
id: "auth",
|
|
19649
|
+
description: "身份验证路由"
|
|
19650
|
+
}).define(async (ctx) => {}).addTo(app, { overwrite: false });
|
|
19651
|
+
app.route({
|
|
19652
|
+
path: "auth-admin",
|
|
19653
|
+
key: "",
|
|
19654
|
+
id: "auth-admin",
|
|
19655
|
+
description: "管理员身份验证路由"
|
|
19656
|
+
}).define(async (ctx) => {}).addTo(app, { overwrite: false });
|
|
19631
19657
|
|
|
19632
19658
|
// agent/main.ts
|
|
19633
19659
|
var routerAgentPlugin = createRouterAgentPluginFn({ router: app });
|
package/dist/opencode.d.ts
CHANGED
|
@@ -33,9 +33,20 @@ type RouterContextT = {
|
|
|
33
33
|
code?: number;
|
|
34
34
|
[key: string]: any;
|
|
35
35
|
};
|
|
36
|
+
type BuildRouteContext<M, U> = M extends {
|
|
37
|
+
args?: infer A;
|
|
38
|
+
} ? A extends z.ZodObject<any> ? RouteContext<{
|
|
39
|
+
args?: z.infer<A>;
|
|
40
|
+
}, U> : A extends Record<string, z.ZodTypeAny> ? RouteContext<{
|
|
41
|
+
args?: {
|
|
42
|
+
[K in keyof A]: z.infer<A[K]>;
|
|
43
|
+
};
|
|
44
|
+
}, U> : RouteContext<U> : RouteContext<U>;
|
|
36
45
|
type RouteContext<T = {
|
|
37
46
|
code?: number;
|
|
38
|
-
},
|
|
47
|
+
}, U extends SimpleObject = {}, S = {
|
|
48
|
+
[key: string]: any;
|
|
49
|
+
}> = {
|
|
39
50
|
/**
|
|
40
51
|
* 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
|
|
41
52
|
* 或者不需要登录的,直接调用
|
|
@@ -58,7 +69,14 @@ type RouteContext<T = {
|
|
|
58
69
|
code?: number;
|
|
59
70
|
/** return msg */
|
|
60
71
|
message?: string;
|
|
72
|
+
/**
|
|
73
|
+
* 传递状态
|
|
74
|
+
*/
|
|
61
75
|
state?: S;
|
|
76
|
+
/**
|
|
77
|
+
* 当前routerId
|
|
78
|
+
*/
|
|
79
|
+
currentId?: string;
|
|
62
80
|
/**
|
|
63
81
|
* 当前路径
|
|
64
82
|
*/
|
|
@@ -99,14 +117,12 @@ type RouteContext<T = {
|
|
|
99
117
|
path: string;
|
|
100
118
|
key?: string;
|
|
101
119
|
payload?: any;
|
|
102
|
-
}, ctx?: RouteContext
|
|
103
|
-
[key: string]: any;
|
|
104
|
-
}) => Promise<any>;
|
|
120
|
+
}, ctx?: RouteContext) => Promise<any>;
|
|
105
121
|
index?: number;
|
|
106
122
|
throw?: throwError['throw'];
|
|
107
123
|
/** 是否需要序列化, 使用JSON.stringify和JSON.parse */
|
|
108
124
|
needSerialize?: boolean;
|
|
109
|
-
} & T;
|
|
125
|
+
} & T & U;
|
|
110
126
|
type SimpleObject = Record<string, any>;
|
|
111
127
|
type Run<T extends SimpleObject = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
|
|
112
128
|
type RunMessage = {
|
|
@@ -117,7 +133,7 @@ type RunMessage = {
|
|
|
117
133
|
};
|
|
118
134
|
type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
|
|
119
135
|
type RouteMiddleware = {
|
|
120
|
-
path
|
|
136
|
+
path?: string;
|
|
121
137
|
key?: string;
|
|
122
138
|
id?: string;
|
|
123
139
|
} | string;
|
|
@@ -144,9 +160,11 @@ type RouteOpts<U = {}, T = SimpleObject> = {
|
|
|
144
160
|
type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
|
|
145
161
|
declare const pickValue: readonly ["path", "key", "id", "description", "type", "middleware", "metadata"];
|
|
146
162
|
type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
163
|
+
/**
|
|
164
|
+
* @M 是 route的 metadate的类型,默认是 SimpleObject
|
|
165
|
+
* @U 是 RouteContext 里 state的类型
|
|
166
|
+
*/
|
|
167
|
+
declare class Route<M extends SimpleObject = SimpleObject, U extends SimpleObject = SimpleObject> implements throwError {
|
|
150
168
|
/**
|
|
151
169
|
* 一级路径
|
|
152
170
|
*/
|
|
@@ -156,10 +174,10 @@ declare class Route<U = {
|
|
|
156
174
|
*/
|
|
157
175
|
key?: string;
|
|
158
176
|
id?: string;
|
|
159
|
-
run?: Run
|
|
177
|
+
run?: Run<BuildRouteContext<M, U>>;
|
|
160
178
|
nextRoute?: NextRoute;
|
|
161
179
|
description?: string;
|
|
162
|
-
metadata?:
|
|
180
|
+
metadata?: M;
|
|
163
181
|
middleware?: RouteMiddleware[];
|
|
164
182
|
type?: string;
|
|
165
183
|
/**
|
|
@@ -174,13 +192,13 @@ declare class Route<U = {
|
|
|
174
192
|
} = RouterContextT>(opts: DefineRouteOpts): this;
|
|
175
193
|
define<T extends {
|
|
176
194
|
[key: string]: any;
|
|
177
|
-
} = RouterContextT>(fn: Run<T & U
|
|
195
|
+
} = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
178
196
|
define<T extends {
|
|
179
197
|
[key: string]: any;
|
|
180
|
-
} = RouterContextT>(key: string, fn: Run<T & U
|
|
198
|
+
} = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
181
199
|
define<T extends {
|
|
182
200
|
[key: string]: any;
|
|
183
|
-
} = RouterContextT>(path: string, key: string, fn: Run<T & U
|
|
201
|
+
} = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
184
202
|
update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
|
|
185
203
|
addTo(router: QueryRouter | {
|
|
186
204
|
add: (route: Route) => void;
|
|
@@ -194,11 +212,11 @@ declare class Route<U = {
|
|
|
194
212
|
type AddOpts = {
|
|
195
213
|
overwrite?: boolean;
|
|
196
214
|
};
|
|
197
|
-
declare class QueryRouter implements throwError {
|
|
215
|
+
declare class QueryRouter<T extends SimpleObject = SimpleObject> implements throwError {
|
|
198
216
|
appId: string;
|
|
199
217
|
routes: Route[];
|
|
200
218
|
maxNextRoute: number;
|
|
201
|
-
context?: RouteContext
|
|
219
|
+
context?: RouteContext<T>;
|
|
202
220
|
constructor();
|
|
203
221
|
/**
|
|
204
222
|
* add route
|
|
@@ -226,7 +244,7 @@ declare class QueryRouter implements throwError {
|
|
|
226
244
|
* @param ctx
|
|
227
245
|
* @returns
|
|
228
246
|
*/
|
|
229
|
-
runRoute(path: string, key: string, ctx?: RouteContext):
|
|
247
|
+
runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
|
|
230
248
|
/**
|
|
231
249
|
* 第一次执行
|
|
232
250
|
* @param message
|
|
@@ -237,9 +255,11 @@ declare class QueryRouter implements throwError {
|
|
|
237
255
|
path: string;
|
|
238
256
|
key?: string;
|
|
239
257
|
payload?: any;
|
|
240
|
-
}, ctx?: RouteContext & {
|
|
258
|
+
}, ctx?: RouteContext<T> & {
|
|
241
259
|
[key: string]: any;
|
|
242
|
-
}): Promise<
|
|
260
|
+
}): Promise<RouteContext<T, {}, {
|
|
261
|
+
[key: string]: any;
|
|
262
|
+
}>>;
|
|
243
263
|
/**
|
|
244
264
|
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
245
265
|
* @param message
|
|
@@ -251,9 +271,15 @@ declare class QueryRouter implements throwError {
|
|
|
251
271
|
path?: string;
|
|
252
272
|
key?: string;
|
|
253
273
|
payload?: any;
|
|
254
|
-
}, ctx?: RouteContext & {
|
|
274
|
+
}, ctx?: RouteContext<T> & {
|
|
275
|
+
[key: string]: any;
|
|
276
|
+
}): Promise<RouteContext<T, {}, {
|
|
255
277
|
[key: string]: any;
|
|
256
|
-
}
|
|
278
|
+
}> | {
|
|
279
|
+
code: number;
|
|
280
|
+
body: any;
|
|
281
|
+
message: string;
|
|
282
|
+
}>;
|
|
257
283
|
/**
|
|
258
284
|
* 请求 result 的数据
|
|
259
285
|
* @param message
|
|
@@ -269,9 +295,9 @@ declare class QueryRouter implements throwError {
|
|
|
269
295
|
}, ctx?: RouteContext & {
|
|
270
296
|
[key: string]: any;
|
|
271
297
|
}): Promise<{
|
|
272
|
-
code:
|
|
298
|
+
code: number;
|
|
273
299
|
data: any;
|
|
274
|
-
message:
|
|
300
|
+
message: string;
|
|
275
301
|
}>;
|
|
276
302
|
/**
|
|
277
303
|
* Router Run获取数据
|
|
@@ -284,12 +310,12 @@ declare class QueryRouter implements throwError {
|
|
|
284
310
|
path?: string;
|
|
285
311
|
key?: string;
|
|
286
312
|
payload?: any;
|
|
287
|
-
}, ctx?: RouteContext & {
|
|
313
|
+
}, ctx?: RouteContext<T> & {
|
|
288
314
|
[key: string]: any;
|
|
289
315
|
}): Promise<{
|
|
290
|
-
code:
|
|
316
|
+
code: number;
|
|
291
317
|
data: any;
|
|
292
|
-
message:
|
|
318
|
+
message: string;
|
|
293
319
|
}>;
|
|
294
320
|
/**
|
|
295
321
|
* 设置上下文
|
|
@@ -301,12 +327,12 @@ declare class QueryRouter implements throwError {
|
|
|
301
327
|
/**
|
|
302
328
|
* 获取handle函数, 这里会去执行parse函数
|
|
303
329
|
*/
|
|
304
|
-
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn
|
|
330
|
+
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
|
|
305
331
|
id?: string;
|
|
306
332
|
path?: string;
|
|
307
333
|
key?: string;
|
|
308
334
|
[key: string]: any;
|
|
309
|
-
}, handleContext?: RouteContext) => Promise<{
|
|
335
|
+
}, handleContext?: RouteContext<T>) => Promise<{
|
|
310
336
|
[key: string]: any;
|
|
311
337
|
code: string;
|
|
312
338
|
data?: any;
|
|
@@ -320,22 +346,16 @@ declare class QueryRouter implements throwError {
|
|
|
320
346
|
message: any;
|
|
321
347
|
data?: undefined;
|
|
322
348
|
}>;
|
|
323
|
-
exportRoutes(): Route<
|
|
324
|
-
[key: string]: any;
|
|
325
|
-
}, SimpleObject>[];
|
|
349
|
+
exportRoutes(): Route<SimpleObject, SimpleObject>[];
|
|
326
350
|
importRoutes(routes: Route[]): void;
|
|
327
351
|
importRouter(router: QueryRouter): void;
|
|
328
352
|
throw(...args: any[]): void;
|
|
329
|
-
hasRoute(path: string, key?: string): Route<
|
|
330
|
-
[key: string]: any;
|
|
331
|
-
}, SimpleObject>;
|
|
353
|
+
hasRoute(path: string, key?: string): Route<SimpleObject, SimpleObject>;
|
|
332
354
|
findRoute(opts?: {
|
|
333
355
|
path?: string;
|
|
334
356
|
key?: string;
|
|
335
357
|
id?: string;
|
|
336
|
-
}): Route<
|
|
337
|
-
[key: string]: any;
|
|
338
|
-
}, SimpleObject>;
|
|
358
|
+
}): Route<SimpleObject, SimpleObject>;
|
|
339
359
|
createRouteList(opts?: {
|
|
340
360
|
force?: boolean;
|
|
341
361
|
filter?: (route: Route) => boolean;
|
|
@@ -377,10 +397,11 @@ declare class QueryRouter implements throwError {
|
|
|
377
397
|
[key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
|
|
378
398
|
};
|
|
379
399
|
}
|
|
380
|
-
type QueryRouterServerOpts = {
|
|
400
|
+
type QueryRouterServerOpts<C extends SimpleObject = SimpleObject> = {
|
|
381
401
|
handleFn?: HandleFn;
|
|
382
|
-
context?: RouteContext
|
|
402
|
+
context?: RouteContext<C>;
|
|
383
403
|
appId?: string;
|
|
404
|
+
initHandle?: boolean;
|
|
384
405
|
};
|
|
385
406
|
interface HandleFn<T = any> {
|
|
386
407
|
(msg: {
|
|
@@ -397,20 +418,27 @@ interface HandleFn<T = any> {
|
|
|
397
418
|
/**
|
|
398
419
|
* QueryRouterServer
|
|
399
420
|
* @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
|
|
421
|
+
* @template C 自定义 RouteContext 类型
|
|
400
422
|
*/
|
|
401
|
-
declare class QueryRouterServer extends QueryRouter {
|
|
423
|
+
declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends QueryRouter<C> {
|
|
402
424
|
appId: string;
|
|
403
425
|
handle: any;
|
|
404
|
-
|
|
426
|
+
context: RouteContext<C>;
|
|
427
|
+
constructor(opts?: QueryRouterServerOpts<C>);
|
|
405
428
|
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
|
|
406
429
|
addRoute(route: Route, opts?: AddOpts): void;
|
|
407
430
|
Route: typeof Route;
|
|
408
|
-
route(opts: RouteOpts
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
route(path: string,
|
|
412
|
-
|
|
413
|
-
|
|
431
|
+
route<M extends SimpleObject = SimpleObject>(opts: RouteOpts & {
|
|
432
|
+
metadata?: M;
|
|
433
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
434
|
+
route<M extends SimpleObject = SimpleObject>(path: string, opts?: RouteOpts & {
|
|
435
|
+
metadata?: M;
|
|
436
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
437
|
+
route<M extends SimpleObject = SimpleObject>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
|
|
438
|
+
route<M extends SimpleObject = SimpleObject>(path: string, key?: string, opts?: RouteOpts & {
|
|
439
|
+
metadata?: M;
|
|
440
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
441
|
+
prompt(description: string): Route<SimpleObject, SimpleObject>;
|
|
414
442
|
/**
|
|
415
443
|
* 调用了handle
|
|
416
444
|
* @param param0
|
|
@@ -421,9 +449,7 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
421
449
|
path?: string;
|
|
422
450
|
key?: string;
|
|
423
451
|
payload?: any;
|
|
424
|
-
}, ctx?: RouteContext
|
|
425
|
-
[key: string]: any;
|
|
426
|
-
}): Promise<any>;
|
|
452
|
+
}, ctx?: Partial<RouteContext<C>>): Promise<any>;
|
|
427
453
|
}
|
|
428
454
|
|
|
429
455
|
type Cors = {
|
|
@@ -674,7 +700,7 @@ type RouterHandle = (msg: {
|
|
|
674
700
|
[key: string]: any;
|
|
675
701
|
};
|
|
676
702
|
type AppOptions<T = {}> = {
|
|
677
|
-
router?:
|
|
703
|
+
router?: QueryRouterServer;
|
|
678
704
|
server?: ServerType;
|
|
679
705
|
/** handle msg 关联 */
|
|
680
706
|
routerHandle?: RouterHandle;
|
|
@@ -682,17 +708,18 @@ type AppOptions<T = {}> = {
|
|
|
682
708
|
serverOptions?: ServerNodeOpts;
|
|
683
709
|
appId?: string;
|
|
684
710
|
};
|
|
685
|
-
type AppRouteContext<T
|
|
711
|
+
type AppRouteContext<T> = HandleCtx & RouteContext<T> & {
|
|
686
712
|
app: App<T>;
|
|
687
713
|
};
|
|
688
714
|
/**
|
|
689
715
|
* 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
|
|
690
716
|
* U - Route Context的扩展类型
|
|
691
717
|
*/
|
|
692
|
-
declare class App<U = {}> extends
|
|
718
|
+
declare class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
|
|
693
719
|
appId: string;
|
|
694
|
-
router:
|
|
720
|
+
router: QueryRouterServer;
|
|
695
721
|
server: ServerType;
|
|
722
|
+
context: AppRouteContext<U>;
|
|
696
723
|
constructor(opts?: AppOptions<U>);
|
|
697
724
|
listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
|
|
698
725
|
listen(port: number, hostname?: string, listeningListener?: () => void): void;
|
|
@@ -702,34 +729,7 @@ declare class App<U = {}> extends QueryRouter {
|
|
|
702
729
|
listen(path: string, listeningListener?: () => void): void;
|
|
703
730
|
listen(handle: any, backlog?: number, listeningListener?: () => void): void;
|
|
704
731
|
listen(handle: any, listeningListener?: () => void): void;
|
|
705
|
-
addRoute(route: Route, opts?: AddOpts): void;
|
|
706
732
|
Route: typeof Route;
|
|
707
|
-
route(opts: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
|
|
708
|
-
route(path: string, key?: string): Route<AppRouteContext<U>>;
|
|
709
|
-
route(path: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
|
|
710
|
-
route(path: string, key?: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
|
|
711
|
-
prompt(description: string): Route<AppRouteContext<U>>;
|
|
712
|
-
prompt(description: Function): Route<AppRouteContext<U>>;
|
|
713
|
-
call(message: {
|
|
714
|
-
id?: string;
|
|
715
|
-
path?: string;
|
|
716
|
-
key?: string;
|
|
717
|
-
payload?: any;
|
|
718
|
-
}, ctx?: AppRouteContext<U> & {
|
|
719
|
-
[key: string]: any;
|
|
720
|
-
}): Promise<any>;
|
|
721
|
-
run(msg: {
|
|
722
|
-
id?: string;
|
|
723
|
-
path?: string;
|
|
724
|
-
key?: string;
|
|
725
|
-
payload?: any;
|
|
726
|
-
}, ctx?: Partial<AppRouteContext<U>> & {
|
|
727
|
-
[key: string]: any;
|
|
728
|
-
}): Promise<{
|
|
729
|
-
code: any;
|
|
730
|
-
data: any;
|
|
731
|
-
message: any;
|
|
732
|
-
}>;
|
|
733
733
|
static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
|
|
734
734
|
cookies: Record<string, string>;
|
|
735
735
|
token: string;
|