add queryRoute for only return code data message

This commit is contained in:
xion 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",
"name": "@kevisual/router",
"version": "0.0.5",
"version": "0.0.6-alpha-1",
"description": "",
"main": "dist/index.js",
"module": "dist/index.js",

View File

@ -82,10 +82,13 @@ export class App<T = {}> {
}
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;
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() {
return this.router.exportRoutes();
}

View File

@ -30,7 +30,10 @@ export type RouteContext<T = { code?: number }, S = any> = {
// TODO:
queryRouter?: QueryRouter;
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;
throw?: (code?: number | string, message?: string, tips?: string) => void;
} & T;
@ -477,12 +480,33 @@ export class QueryRouter {
// TODO: 是否需要queryRouter函数内部处理router路由执行这应该是避免去内部去包含的功能过
ctx.queryRouter = this;
ctx.call = this.call.bind(this);
ctx.queryRoute = this.queryRoute.bind(this);
ctx.index = 0;
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 });
}
/**
* 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) {
this.context = ctx;
}
@ -574,10 +598,13 @@ export class QueryRouterServer extends QueryRouter {
}
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);
}
async run({ path, key, payload }: { path: string; key: string; payload?: any }) {
/**
* queryRoutehandle
* @param param0
* @returns
*/
async run({ path, key, payload }: { path: string; key?: string; payload?: any }) {
const handle = this.handle;
const resultError = (error: string, code = 500) => {
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);
}
}