@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/src/test/app-type.ts
CHANGED
|
@@ -1,13 +1,69 @@
|
|
|
1
|
-
import { App } from
|
|
1
|
+
import { App, AppRouteContext } from "@/app.ts";
|
|
2
|
+
import { QueryRouterServer, RouteContext } from "@/app.ts";
|
|
3
|
+
import z from "zod";
|
|
4
|
+
const route: RouteContext<{ customField: string }> = {} as any;
|
|
5
|
+
route.customField
|
|
6
|
+
const appRoute: AppRouteContext<{ customField: string }> = {} as any;
|
|
7
|
+
appRoute.customField
|
|
8
|
+
// 示例 1: 使用 App,它会自动使用 AppRouteContext<U> 作为 ctx 类型
|
|
9
|
+
const app = new App<{
|
|
10
|
+
customField: string;
|
|
11
|
+
}>();
|
|
12
|
+
app.context.customField = "customValue"; // 可以在 app.context 中添加自定义字段,这些字段会在 ctx 中可用
|
|
13
|
+
app.route({
|
|
14
|
+
path: 'test1',
|
|
15
|
+
metadata: {
|
|
16
|
+
args: {
|
|
17
|
+
name: z.string(),
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
}).define(async (ctx) => {
|
|
21
|
+
// ctx.app 是 App 类型
|
|
22
|
+
const appName = ctx.app.appId;
|
|
23
|
+
// ctx.customField 来自自定义泛型参数
|
|
24
|
+
const customField: string | undefined = ctx.customField;
|
|
2
25
|
|
|
3
|
-
|
|
26
|
+
// ctx.req 和 ctx.res 来自 HandleCtx
|
|
27
|
+
const req = ctx.req;
|
|
28
|
+
const res = ctx.res;
|
|
4
29
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
30
|
+
// ctx.args 从 metadata.args 推断
|
|
31
|
+
const name: string = ctx.args.name;
|
|
32
|
+
const name2: string = ctx.query.name;
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
ctx.body = `Hello ${name}!`;
|
|
36
|
+
});
|
|
37
|
+
// 示例 2: 使用 QueryRouterServer,它可以传递自定义的 Context 类型
|
|
38
|
+
const router = new QueryRouterServer<{
|
|
39
|
+
routerContextField: number;
|
|
40
|
+
}>();
|
|
41
|
+
router.context.routerContextField
|
|
42
|
+
router.route({
|
|
43
|
+
path: 'router-test',
|
|
44
|
+
metadata: {
|
|
45
|
+
args: {
|
|
46
|
+
value: z.number(),
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
}).define(async (ctx) => {
|
|
50
|
+
const value: number = ctx.args.value;
|
|
51
|
+
const field: number | undefined = ctx.routerContextField;
|
|
52
|
+
|
|
53
|
+
ctx.body = value;
|
|
54
|
+
});
|
|
55
|
+
// 示例 3: 不带泛型参数的 QueryRouterServer,使用默认的 RouteContext
|
|
56
|
+
const defaultRouter = new QueryRouterServer();
|
|
57
|
+
defaultRouter.route({
|
|
58
|
+
path: 'default-test',
|
|
59
|
+
metadata: {
|
|
60
|
+
args: {
|
|
61
|
+
id: z.string(),
|
|
62
|
+
}
|
|
63
|
+
},
|
|
11
64
|
}).define(async (ctx) => {
|
|
12
|
-
|
|
13
|
-
|
|
65
|
+
const id: string = ctx.args.id;
|
|
66
|
+
|
|
67
|
+
ctx.body = id;
|
|
68
|
+
});
|
|
69
|
+
export { app, router, defaultRouter };
|
package/src/test/chat.ts
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { QueryRouterServer } from "@/route.ts";
|
|
2
|
+
import z from "zod";
|
|
3
|
+
|
|
4
|
+
const router = new QueryRouterServer()
|
|
5
|
+
|
|
6
|
+
router.route({
|
|
7
|
+
metadata: {
|
|
8
|
+
args: {
|
|
9
|
+
a: z.string(),
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
}).define(async (ctx) => {
|
|
13
|
+
const argA: string = ctx.args.a;
|
|
14
|
+
ctx.body = '1';
|
|
15
|
+
})
|