add queryRoute for only return code data message
This commit is contained in:
parent
7f369b7b07
commit
e0c7d40a9c
@ -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",
|
||||
|
@ -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();
|
||||
}
|
||||
|
39
src/route.ts
39
src/route.ts
@ -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 }) {
|
||||
|
||||
/**
|
||||
* 等于queryRoute,但是调用了handle
|
||||
* @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
47
src/utils/route-map.ts
Normal 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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user