From f8337a1216a097c44c5fd91487c8bfa0a57673e4 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Mon, 23 Feb 2026 23:47:59 +0800 Subject: [PATCH] temp --- src/test/app-type.ts | 71 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/src/test/app-type.ts b/src/test/app-type.ts index 384a978..22ccfdb 100644 --- a/src/test/app-type.ts +++ b/src/test/app-type.ts @@ -1,13 +1,62 @@ -import { App } from '../app.ts' - -const app = new App<{ f: string }>(); - +import { App } from "@/app.ts"; +import { QueryRouterServer } from "@/route.ts"; +import z from "zod"; +// 示例 1: 使用 App,它会自动使用 AppRouteContext 作为 ctx 类型 +const app = new App<{ + customField: string; +}>(); app.route({ - path: 't', - run: async (ctx) => { - // ctx.r - ctx.app; - } + path: 'test1', + metadata: { + args: { + name: z.string(), + } + }, }).define(async (ctx) => { - ctx.f = 'hello'; -}).addTo(app); \ No newline at end of file + // ctx.app 是 App 类型 + const appName = ctx.app.appId; + + // ctx.req 和 ctx.res 来自 HandleCtx + const req = ctx.req; + const res = ctx.res; + + // ctx.args 从 metadata.args 推断 + const name: string = ctx.args.name; + + // ctx.customField 来自自定义泛型参数 + const customField: string | undefined = ctx.customField; + + ctx.body = `Hello ${name}!`; +}); +// 示例 2: 使用 QueryRouterServer,它可以传递自定义的 Context 类型 +const router = new QueryRouterServer<{ + routerContextField: number; +}>(); +router.route({ + path: 'router-test', + metadata: { + args: { + value: z.number(), + } + }, +}).define(async (ctx) => { + const value: number = ctx.args.value; + const field: number | undefined = ctx.routerContextField; + + ctx.body = value; +}); +// 示例 3: 不带泛型参数的 QueryRouterServer,使用默认的 RouteContext +const defaultRouter = new QueryRouterServer(); +defaultRouter.route({ + path: 'default-test', + metadata: { + args: { + id: z.string(), + } + }, +}).define(async (ctx) => { + const id: string = ctx.args.id; + + ctx.body = id; +}); +export { app, router, defaultRouter }; \ No newline at end of file