chore: 删除不再使用的文件并更新路由上下文以支持自定义字段

This commit is contained in:
2026-02-24 01:01:43 +08:00
parent f8337a1216
commit 52b10f2f03
16 changed files with 73 additions and 224 deletions

View File

@@ -19,7 +19,7 @@ type AppOptions<T = {}> = {
appId?: string;
};
export type AppRouteContext<T = {}> = HandleCtx & RouteContext<T> & { app: App<T> };
export type AppRouteContext<T> = HandleCtx & RouteContext<T> & { app: App<T> };
/**
* 封装了 Router 和 Server 的 App 模块处理http的请求和响应内置了 Cookie 和 Token 和 res 的处理
@@ -29,6 +29,7 @@ export class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
declare appId: string;
router: QueryRouterServer;
server: ServerType;
declare context: AppRouteContext<U>;
constructor(opts?: AppOptions<U>) {
super({ initHandle: false, context: { needSerialize: true, ...opts?.routerContext } as any });
const router = this;

View File

@@ -23,7 +23,7 @@ type BuildRouteContext<M, U> = M extends { args?: infer A }
: RouteContext<U>
: RouteContext<U>;
export type RouteContext<T = { code?: number }, S = any> = {
export type RouteContext<T = { code?: number }, U extends SimpleObject = {}, S = { [key: string]: any }> = {
/**
* 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
* 或者不需要登录的,直接调用
@@ -77,19 +77,19 @@ export type RouteContext<T = { code?: number }, S = any> = {
ctx?: RouteContext & { [key: string]: any },
) => Promise<any>;
/** 请求 route的返回结果解析了body为data就类同于 query.post获取的数据*/
run?: (message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>;
run?: (message: { path: string; key?: string; payload?: any }, ctx?: RouteContext) => Promise<any>;
index?: number;
throw?: throwError['throw'];
/** 是否需要序列化, 使用JSON.stringify和JSON.parse */
needSerialize?: boolean;
} & T;
} & T & U;
export type SimpleObject = Record<string, any>;
export type Run<T extends SimpleObject = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
export type RunMessage = { path?: string; key?: string; id?: string; payload?: any; };
export type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
export type RouteMiddleware =
| {
path: string;
path?: string;
key?: string;
id?: string;
}
@@ -148,7 +148,11 @@ export type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
type ExtractMetadata<M> = M extends { metadata?: infer Meta } ? Meta extends SimpleObject ? Meta : SimpleObject : SimpleObject;
export class Route<M extends SimpleObject = SimpleObject, U extends SimpleObject = SimpleObject, T extends SimpleObject = SimpleObject> implements throwError {
/**
* @M 是 route的 metadate的类型默认是 SimpleObject
* @U 是 RouteContext 里 state的类型
*/
export class Route<M extends SimpleObject = SimpleObject, U extends SimpleObject = SimpleObject> implements throwError {
/**
* 一级路径
*/
@@ -287,11 +291,11 @@ export const fromJSONSchema = schema.fromJSONSchema;
* @parmas overwrite 是否覆盖已存在的route默认true
*/
export type AddOpts = { overwrite?: boolean };
export class QueryRouter implements throwError {
export class QueryRouter<T extends SimpleObject = SimpleObject> implements throwError {
appId: string = '';
routes: Route[];
maxNextRoute = 40;
context?: RouteContext = {}; // default context for call
context?: RouteContext<T> = {} as RouteContext<T>; // default context for call
constructor() {
this.routes = [];
}
@@ -334,10 +338,10 @@ export class QueryRouter implements throwError {
* @param ctx
* @returns
*/
async runRoute(path: string, key: string, ctx?: RouteContext) {
async runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>> {
const route = this.routes.find((r) => r.path === path && r.key === key);
const maxNextRoute = this.maxNextRoute;
ctx = (ctx || {}) as RouteContext;
ctx = (ctx || {}) as RouteContext<T>;
ctx.currentPath = path;
ctx.currentId = route?.id;
ctx.currentKey = key;
@@ -353,7 +357,7 @@ export class QueryRouter implements throwError {
ctx.code = 500;
ctx.message = 'Too many nextRoute';
ctx.body = null;
return;
return ctx;
}
// run middleware
if (route && route.middleware && route.middleware.length > 0) {
@@ -408,7 +412,7 @@ export class QueryRouter implements throwError {
const middleware = routeMiddleware[i];
if (middleware) {
try {
await middleware.run(ctx as Required<RouteContext>);
await middleware.run(ctx as Required<RouteContext<T>>);
} catch (e) {
if (route?.isDebug) {
console.error('=====debug====:middlerware error');
@@ -430,6 +434,7 @@ export class QueryRouter implements throwError {
return ctx;
}
if (ctx.end) {
return ctx;
}
}
}
@@ -438,7 +443,7 @@ export class QueryRouter implements throwError {
if (route) {
if (route.run) {
try {
await route.run(ctx as Required<RouteContext>);
await route.run(ctx as Required<RouteContext<T>>);
} catch (e) {
if (route?.isDebug) {
console.error('=====debug====:route error');
@@ -493,7 +498,7 @@ export class QueryRouter implements throwError {
}
}
// 如果没有找到route返回404这是因为出现了错误
return Promise.resolve({ code: 404, body: 'Not found' });
return Promise.resolve({ code: 404, body: 'Not found' } as RouteContext<T>);
}
/**
* 第一次执行
@@ -501,12 +506,12 @@ export class QueryRouter implements throwError {
* @param ctx
* @returns
*/
async parse(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
async parse(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
if (!message?.path) {
return Promise.resolve({ code: 404, body: null, message: 'Not found path' });
return Promise.resolve({ code: 404, body: null, message: 'Not found path' } as RouteContext<T>);
}
const { path, key = '', payload = {}, ...query } = message;
ctx = ctx || {};
ctx = ctx || {} as RouteContext<T>;
ctx.query = { ...ctx.query, ...query, ...payload };
ctx.args = ctx.query;
ctx.state = { ...ctx?.state };
@@ -540,7 +545,7 @@ export class QueryRouter implements throwError {
* @param ctx
* @returns
*/
async call(message: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
async call(message: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
let path = message.path;
let key = message.key;
// 优先 path + key
@@ -581,7 +586,7 @@ export class QueryRouter implements throwError {
* @param ctx
* @returns
*/
async run(message: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
async run(message: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
const res = await this.call(message, { ...this.context, ...ctx });
return {
code: res.code,
@@ -595,7 +600,7 @@ export class QueryRouter implements throwError {
* @param ctx
*/
setContext(ctx: RouteContext) {
this.context = ctx;
this.context = ctx as RouteContext<T>;
}
getList(filter?: (route: Route) => boolean): RouteInfo[] {
return this.routes.filter(filter || (() => true)).map((r) => {
@@ -606,11 +611,11 @@ export class QueryRouter implements throwError {
/**
* 获取handle函数, 这里会去执行parse函数
*/
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext) {
return async (msg: { id?: string; path?: string; key?: string;[key: string]: any }, handleContext?: RouteContext) => {
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext) {
return async (msg: { id?: string; path?: string; key?: string;[key: string]: any }, handleContext?: RouteContext<T>) => {
try {
const context = { ...ctx, ...handleContext };
const res = await router.call(msg, context);
const res = await router.call(msg, context) as any;
if (wrapperFn) {
res.data = res.body;
return wrapperFn(res, context);
@@ -663,7 +668,7 @@ export class QueryRouter implements throwError {
description: '列出当前应用下的所有的路由信息',
middleware: opts?.middleware || [],
run: async (ctx: RouteContext) => {
const tokenUser = ctx.state.tokenUser;
const tokenUser = ctx.state as unknown as { tokenUser?: any };
let isUser = !!tokenUser;
const list = this.getList(opts?.filter).filter((item) => {
if (item.id === 'auth' || item.id === 'auth-can' || item.id === 'check-auth-admin' || item.id === 'auth-admin') {
@@ -724,9 +729,10 @@ interface HandleFn<T = any> {
* @description 移除server相关的功能只保留router相关的功能和http.createServer不相关独立
* @template C 自定义 RouteContext 类型
*/
export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends QueryRouter {
export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends QueryRouter<C> {
declare appId: string;
handle: any;
declare context: RouteContext<C>;
constructor(opts?: QueryRouterServerOpts<C>) {
super();
const initHandle = opts?.initHandle ?? true;
@@ -767,18 +773,21 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
}
return new Route<M, Required<RouteContext<C>>>(path, key, opts);
}
prompt(description: string) {
return new Route(undefined, undefined, { description });
}
/**
* 调用了handle
* @param param0
* @returns
*/
async run(msg: { id?: string; path?: string; key?: string; payload?: any }, ctx?: Partial<RouteContext<C>> & { [key: string]: any }) {
async run(msg: { id?: string; path?: string; key?: string; payload?: any }, ctx?: Partial<RouteContext<C>>) {
const handle = this.handle;
if (handle) {
return handle(msg, ctx);
}
return super.run(msg, ctx);
return super.run(msg, ctx as RouteContext<C>);
}
}

View File

@@ -1,10 +1,15 @@
import { App } from "@/app.ts";
import { QueryRouterServer } from "@/route.ts";
import { App, AppRouteContext } from "@/app.ts";
import { QueryRouterServer, RouteContext } from "@/app.ts";
import z from "zod";
const route: RouteContext<{ customField: string }> = {} as any;
route.customField
const appRoute: AppRouteContext<{ customField: string }> = {} as any;
appRoute.customField
// 示例 1: 使用 App它会自动使用 AppRouteContext<U> 作为 ctx 类型
const app = new App<{
customField: string;
}>();
app.context.customField = "customValue"; // 可以在 app.context 中添加自定义字段,这些字段会在 ctx 中可用
app.route({
path: 'test1',
metadata: {
@@ -15,23 +20,25 @@ app.route({
}).define(async (ctx) => {
// ctx.app 是 App 类型
const appName = ctx.app.appId;
// ctx.customField 来自自定义泛型参数
const customField: string | undefined = ctx.customField;
// 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;
const name2: string = ctx.query.name;
ctx.body = `Hello ${name}!`;
});
// 示例 2: 使用 QueryRouterServer它可以传递自定义的 Context 类型
const router = new QueryRouterServer<{
routerContextField: number;
}>();
router.context.routerContextField
router.route({
path: 'router-test',
metadata: {
@@ -42,7 +49,7 @@ router.route({
}).define(async (ctx) => {
const value: number = ctx.args.value;
const field: number | undefined = ctx.routerContextField;
ctx.body = value;
});
// 示例 3: 不带泛型参数的 QueryRouterServer使用默认的 RouteContext
@@ -56,7 +63,7 @@ defaultRouter.route({
},
}).define(async (ctx) => {
const id: string = ctx.args.id;
ctx.body = id;
});
export { app, router, defaultRouter };