Files
router/src/test/app-type.ts
2026-02-23 23:47:59 +08:00

62 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { App } from "@/app.ts";
import { QueryRouterServer } from "@/route.ts";
import z from "zod";
// 示例 1: 使用 App它会自动使用 AppRouteContext<U> 作为 ctx 类型
const app = new App<{
customField: string;
}>();
app.route({
path: 'test1',
metadata: {
args: {
name: z.string(),
}
},
}).define(async (ctx) => {
// 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 };