chore: 更新 QueryRouterServer 和 App 类以支持自定义 RouteContext 类型

This commit is contained in:
2026-02-23 23:43:12 +08:00
parent 9859c2f673
commit ad95dc0081
2 changed files with 17 additions and 27 deletions

View File

@@ -25,12 +25,12 @@ export type AppRouteContext<T = {}> = HandleCtx & RouteContext<T> & { app: App<T
* 封装了 Router 和 Server 的 App 模块处理http的请求和响应内置了 Cookie 和 Token 和 res 的处理 * 封装了 Router 和 Server 的 App 模块处理http的请求和响应内置了 Cookie 和 Token 和 res 的处理
* U - Route Context的扩展类型 * U - Route Context的扩展类型
*/ */
export class App<U = {}> extends QueryRouterServer { export class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
declare appId: string; declare appId: string;
router: QueryRouterServer; router: QueryRouterServer;
server: ServerType; server: ServerType;
constructor(opts?: AppOptions<U>) { constructor(opts?: AppOptions<U>) {
super({ initHandle: false, context: { needSerialize: true, ...opts?.routerContext } }); super({ initHandle: false, context: { needSerialize: true, ...opts?.routerContext } as any });
const router = this; const router = this;
let server = opts?.server; let server = opts?.server;
if (!server) { if (!server) {
@@ -64,17 +64,6 @@ export class App<U = {}> extends QueryRouterServer {
this.server.listen(...args); this.server.listen(...args);
} }
Route = Route; Route = Route;
route(opts: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
route(path: string, key?: string): Route<AppRouteContext<U>>;
route(path: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
route(path: string, key?: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
route(...args: any[]) {
return super.route(...args as any[]);
}
async run(msg: { id?: string, path?: string; key?: string; payload?: any }, ctx?: Partial<AppRouteContext<U>> & { [key: string]: any }) {
return await super.run(msg, ctx);
}
static handleRequest(req: IncomingMessage, res: ServerResponse) { static handleRequest(req: IncomingMessage, res: ServerResponse) {
return handleServer(req, res); return handleServer(req, res);
} }

View File

@@ -709,9 +709,9 @@ export class QueryRouter implements throwError {
fromJSONSchema = fromJSONSchema; fromJSONSchema = fromJSONSchema;
} }
type QueryRouterServerOpts = { type QueryRouterServerOpts<C extends SimpleObject = SimpleObject> = {
handleFn?: HandleFn; handleFn?: HandleFn;
context?: RouteContext; context?: RouteContext<C>;
appId?: string; appId?: string;
initHandle?: boolean; initHandle?: boolean;
}; };
@@ -722,11 +722,12 @@ interface HandleFn<T = any> {
/** /**
* QueryRouterServer * QueryRouterServer
* @description 移除server相关的功能只保留router相关的功能和http.createServer不相关独立 * @description 移除server相关的功能只保留router相关的功能和http.createServer不相关独立
* @template C 自定义 RouteContext 类型
*/ */
export class QueryRouterServer extends QueryRouter { export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends QueryRouter {
declare appId: string; declare appId: string;
handle: any; handle: any;
constructor(opts?: QueryRouterServerOpts) { constructor(opts?: QueryRouterServerOpts<C>) {
super(); super();
const initHandle = opts?.initHandle ?? true; const initHandle = opts?.initHandle ?? true;
if (initHandle || opts?.handleFn) { if (initHandle || opts?.handleFn) {
@@ -746,25 +747,25 @@ export class QueryRouterServer extends QueryRouter {
this.add(route, opts); this.add(route, opts);
} }
Route = Route; Route = Route;
route<M extends SimpleObject = SimpleObject>(opts: RouteOpts & { metadata?: M }): Route<M, Required<RouteContext>>; route<M extends SimpleObject = SimpleObject>(opts: RouteOpts & { metadata?: M }): Route<M, Required<RouteContext<C>>>;
route<M extends SimpleObject = SimpleObject>(path: string, opts?: RouteOpts & { metadata?: M }): Route<M, Required<RouteContext>>; route<M extends SimpleObject = SimpleObject>(path: string, opts?: RouteOpts & { metadata?: M }): Route<M, Required<RouteContext<C>>>;
route<M extends SimpleObject = SimpleObject>(path: string, key?: string): Route<M, Required<RouteContext>>; route<M extends SimpleObject = SimpleObject>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
route<M extends SimpleObject = SimpleObject>(path: string, key?: string, opts?: RouteOpts & { metadata?: M }): Route<M, Required<RouteContext>>; route<M extends SimpleObject = SimpleObject>(path: string, key?: string, opts?: RouteOpts & { metadata?: M }): Route<M, Required<RouteContext<C>>>;
route<M extends SimpleObject = SimpleObject>(...args: any[]) { route<M extends SimpleObject = SimpleObject>(...args: any[]) {
const [path, key, opts] = args; const [path, key, opts] = args;
if (typeof path === 'object') { if (typeof path === 'object') {
return new Route<M, Required<RouteContext>>(path.path, path.key, path); return new Route<M, Required<RouteContext<C>>>(path.path, path.key, path);
} }
if (typeof path === 'string') { if (typeof path === 'string') {
if (opts) { if (opts) {
return new Route<M, Required<RouteContext>>(path, key, opts); return new Route<M, Required<RouteContext<C>>>(path, key, opts);
} }
if (key && typeof key === 'object') { if (key && typeof key === 'object') {
return new Route<M, Required<RouteContext>>(path, key?.key || '', key); return new Route<M, Required<RouteContext<C>>>(path, key?.key || '', key);
} }
return new Route<M, Required<RouteContext>>(path, key); return new Route<M, Required<RouteContext<C>>>(path, key);
} }
return new Route<M, Required<RouteContext>>(path, key, opts); return new Route<M, Required<RouteContext<C>>>(path, key, opts);
} }
/** /**
@@ -772,7 +773,7 @@ export class QueryRouterServer extends QueryRouter {
* @param param0 * @param param0
* @returns * @returns
*/ */
async run(msg: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) { async run(msg: { id?: string; path?: string; key?: string; payload?: any }, ctx?: Partial<RouteContext<C>> & { [key: string]: any }) {
const handle = this.handle; const handle = this.handle;
if (handle) { if (handle) {
return handle(msg, ctx); return handle(msg, ctx);