更新 @kevisual/api 版本至 0.0.21,添加 router-api-proxy.ts 和 proxy.ts 文件,重构 QueryProxy 类以优化 API 查询初始化
This commit is contained in:
48
query/query-proxy/router-api-proxy.ts
Normal file
48
query/query-proxy/router-api-proxy.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Query } from "@kevisual/query";
|
||||
import { RouterViewApi, RouterItem } from ".";
|
||||
import { App, type QueryRouterServer } from "@kevisual/router";
|
||||
import { filter } from "@kevisual/js-filter";
|
||||
export const initApi = async (opts: {
|
||||
item?: RouterViewApi,
|
||||
router: QueryRouterServer | App,
|
||||
token?: string,
|
||||
/**
|
||||
* WHERE path = 'auth' OR path = 'router'
|
||||
*/
|
||||
exclude?: string;
|
||||
}) => {
|
||||
const router = opts?.router! as QueryRouterServer;
|
||||
const item = opts?.item;
|
||||
const token = opts?.token;
|
||||
const query = item?.api?.query || new Query({ url: item?.api?.url || '/api/router' })
|
||||
const res = await query.post<{ list: RouterItem[] }>({ path: "router", key: 'list', token: token });
|
||||
if (res.code !== 200) {
|
||||
console.error('初始化路由失败:', query.url, res.message);
|
||||
return
|
||||
}
|
||||
let _list = res.data?.list || []
|
||||
if (opts?.exclude) {
|
||||
_list = filter(_list, opts.exclude);
|
||||
}
|
||||
for (const r of _list) {
|
||||
if (r.path || r.id) {
|
||||
console.debug(`注册路由: [${r.path}] ${r?.key}`, 'API');
|
||||
let metadata = r.metadata || {};
|
||||
metadata.viewItem = item;
|
||||
router.route({
|
||||
path: r.path,
|
||||
key: r.key || '',
|
||||
id: r.id,
|
||||
description: r.description,
|
||||
metadata: metadata,
|
||||
}).define(async (ctx) => {
|
||||
const msg = { ...ctx.query };
|
||||
if (msg.token === undefined && token !== undefined) {
|
||||
msg.token = token;
|
||||
}
|
||||
const res = await query.post<any>({ path: r.path, key: r.key, ...msg });
|
||||
ctx.forward(res)
|
||||
}).addTo(router);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user