add queryRoute for only return code data message

This commit is contained in:
熊潇 2024-11-26 19:25:43 +08:00
parent 7f369b7b07
commit e0c7d40a9c
4 changed files with 85 additions and 8 deletions

View File

@ -1,7 +1,7 @@
{ {
"$schema": "https://json.schemastore.org/package", "$schema": "https://json.schemastore.org/package",
"name": "@kevisual/router", "name": "@kevisual/router",
"version": "0.0.5", "version": "0.0.6-alpha-1",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"module": "dist/index.js", "module": "dist/index.js",

View File

@ -82,10 +82,13 @@ export class App<T = {}> {
} }
return new Route(path, key, opts); return new Route(path, key, opts);
} }
async call(message: { path: string; key: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) { async call(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
const router = this.router; const router = this.router;
return await router.call(message, ctx); return await router.call(message, ctx);
} }
async queryRoute(path: string, key?: string, payload?: any, ctx?: RouteContext & { [key: string]: any }) {
return await this.router.queryRoute({ path, key, payload }, ctx);
}
exportRoutes() { exportRoutes() {
return this.router.exportRoutes(); return this.router.exportRoutes();
} }

View File

@ -30,7 +30,10 @@ export type RouteContext<T = { code?: number }, S = any> = {
// TODO: // TODO:
queryRouter?: QueryRouter; queryRouter?: QueryRouter;
error?: any; error?: any;
call?: (message: { path: string; key: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>; /** 请求 route的返回结果包函ctx */
call?: (message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>;
/** 请求 route的返回结果不包函ctx */
queryRoute?: (message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>;
index?: number; index?: number;
throw?: (code?: number | string, message?: string, tips?: string) => void; throw?: (code?: number | string, message?: string, tips?: string) => void;
} & T; } & T;
@ -477,12 +480,33 @@ export class QueryRouter {
// TODO: 是否需要queryRouter函数内部处理router路由执行这应该是避免去内部去包含的功能过 // TODO: 是否需要queryRouter函数内部处理router路由执行这应该是避免去内部去包含的功能过
ctx.queryRouter = this; ctx.queryRouter = this;
ctx.call = this.call.bind(this); ctx.call = this.call.bind(this);
ctx.queryRoute = this.queryRoute.bind(this);
ctx.index = 0; ctx.index = 0;
return await this.runRoute(path, key, ctx); return await this.runRoute(path, key, ctx);
} }
async call(message: { path: string; key: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) { /**
* context的请求返回的内容
* @param message
* @param ctx
* @returns
*/
async call(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
return await this.parse(message, { ...this.context, ...ctx }); return await this.parse(message, { ...this.context, ...ctx });
} }
/**
* result
* @param message
* @param ctx
* @returns
*/
async queryRoute(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
const res = await this.parse(message, { ...this.context, ...ctx });
return {
code: res.code,
data: res.body,
message: res.message,
};
}
async setContext(ctx: RouteContext) { async setContext(ctx: RouteContext) {
this.context = ctx; this.context = ctx;
} }
@ -574,10 +598,13 @@ export class QueryRouterServer extends QueryRouter {
} }
return new Route(path, key, opts); return new Route(path, key, opts);
} }
async call(message: { path: string; key: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
return await this.parse(message, ctx); /**
} * queryRoutehandle
async run({ path, key, payload }: { path: string; key: string; payload?: any }) { * @param param0
* @returns
*/
async run({ path, key, payload }: { path: string; key?: string; payload?: any }) {
const handle = this.handle; const handle = this.handle;
const resultError = (error: string, code = 500) => { const resultError = (error: string, code = 500) => {
const r = { const r = {

47
src/utils/route-map.ts Normal file
View File

@ -0,0 +1,47 @@
export type RouteMapInfo = {
pathKey?: string;
id: string;
};
export class RouteMap {
private keyMap: Map<string, RouteMapInfo> = new Map(); // 通过 path key 查找
private idMap: Map<string, RouteMapInfo> = new Map(); // 通过 id 查找
// 添加数据
add(info: RouteMapInfo) {
if (!info.pathKey && !info.id) {
console.error('appKey 和 appId 不能同时为空');
return;
}
this.keyMap.set(info.pathKey, info);
if (info.id) {
this.idMap.set(info.id, info);
}
}
// 删除数据
removeByKey(key: string) {
const info = this.keyMap.get(key);
if (info) {
this.keyMap.delete(info.pathKey);
this.idMap.delete(info.id);
return true;
}
return false;
}
removeByAppId(appId: string) {
const info = this.idMap.get(appId);
if (info) {
this.keyMap.delete(info.pathKey);
this.idMap.delete(info.id);
return true;
}
return false;
}
// 查询数据
getByKey(key: string): RouteMapInfo | undefined {
return this.keyMap.get(key);
}
getByAppId(appId: string): RouteMapInfo | undefined {
return this.idMap.get(appId);
}
}