From ad95dc0081fe69b0f66a19c08c58ccb19bc0c1c8 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Mon, 23 Feb 2026 23:43:12 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=20QueryRouterServer?= =?UTF-8?q?=20=E5=92=8C=20App=20=E7=B1=BB=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=20RouteContext=20=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 15 ++------------- src/route.ts | 29 +++++++++++++++-------------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/app.ts b/src/app.ts index f271c0c..173e49e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -25,12 +25,12 @@ export type AppRouteContext = HandleCtx & RouteContext & { app: App extends QueryRouterServer { +export class App extends QueryRouterServer> { declare appId: string; router: QueryRouterServer; server: ServerType; constructor(opts?: AppOptions) { - super({ initHandle: false, context: { needSerialize: true, ...opts?.routerContext } }); + super({ initHandle: false, context: { needSerialize: true, ...opts?.routerContext } as any }); const router = this; let server = opts?.server; if (!server) { @@ -64,17 +64,6 @@ export class App extends QueryRouterServer { this.server.listen(...args); } Route = Route; - route(opts: RouteOpts>): Route>; - route(path: string, key?: string): Route>; - route(path: string, opts?: RouteOpts>): Route>; - route(path: string, key?: string, opts?: RouteOpts>): Route>; - route(...args: any[]) { - return super.route(...args as any[]); - } - - async run(msg: { id?: string, path?: string; key?: string; payload?: any }, ctx?: Partial> & { [key: string]: any }) { - return await super.run(msg, ctx); - } static handleRequest(req: IncomingMessage, res: ServerResponse) { return handleServer(req, res); } diff --git a/src/route.ts b/src/route.ts index c8f609d..1d2e8f1 100644 --- a/src/route.ts +++ b/src/route.ts @@ -709,9 +709,9 @@ export class QueryRouter implements throwError { fromJSONSchema = fromJSONSchema; } -type QueryRouterServerOpts = { +type QueryRouterServerOpts = { handleFn?: HandleFn; - context?: RouteContext; + context?: RouteContext; appId?: string; initHandle?: boolean; }; @@ -722,11 +722,12 @@ interface HandleFn { /** * QueryRouterServer * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立 + * @template C 自定义 RouteContext 类型 */ -export class QueryRouterServer extends QueryRouter { +export class QueryRouterServer extends QueryRouter { declare appId: string; handle: any; - constructor(opts?: QueryRouterServerOpts) { + constructor(opts?: QueryRouterServerOpts) { super(); const initHandle = opts?.initHandle ?? true; if (initHandle || opts?.handleFn) { @@ -746,25 +747,25 @@ export class QueryRouterServer extends QueryRouter { this.add(route, opts); } Route = Route; - route(opts: RouteOpts & { metadata?: M }): Route>; - route(path: string, opts?: RouteOpts & { metadata?: M }): Route>; - route(path: string, key?: string): Route>; - route(path: string, key?: string, opts?: RouteOpts & { metadata?: M }): Route>; + route(opts: RouteOpts & { metadata?: M }): Route>>; + route(path: string, opts?: RouteOpts & { metadata?: M }): Route>>; + route(path: string, key?: string): Route>>; + route(path: string, key?: string, opts?: RouteOpts & { metadata?: M }): Route>>; route(...args: any[]) { const [path, key, opts] = args; if (typeof path === 'object') { - return new Route>(path.path, path.key, path); + return new Route>>(path.path, path.key, path); } if (typeof path === 'string') { if (opts) { - return new Route>(path, key, opts); + return new Route>>(path, key, opts); } if (key && typeof key === 'object') { - return new Route>(path, key?.key || '', key); + return new Route>>(path, key?.key || '', key); } - return new Route>(path, key); + return new Route>>(path, key); } - return new Route>(path, key, opts); + return new Route>>(path, key, opts); } /** @@ -772,7 +773,7 @@ export class QueryRouterServer extends QueryRouter { * @param param0 * @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> & { [key: string]: any }) { const handle = this.handle; if (handle) { return handle(msg, ctx);