@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.
@@ -1,13 +1,69 @@
1
- import { App } from '../app.ts'
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
- const app = new App<{ f: string }>();
26
+ // ctx.req ctx.res 来自 HandleCtx
27
+ const req = ctx.req;
28
+ const res = ctx.res;
4
29
 
5
- app.route({
6
- path: 't',
7
- run: async (ctx) => {
8
- // ctx.r
9
- ctx.app;
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
- ctx.f = 'hello';
13
- }).addTo(app);
65
+ const id: string = ctx.args.id;
66
+
67
+ ctx.body = id;
68
+ });
69
+ export { app, router, defaultRouter };
package/src/test/chat.ts CHANGED
@@ -14,4 +14,4 @@ app.prompt('获取天气的工具。\n参数是 city 为对应的城市').define
14
14
 
15
15
  export const chat = new RouterChat({ router: app.router });
16
16
 
17
- console.log(chat.chat());
17
+ console.log(chat.getChatPrompt());
@@ -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
+ })