更新 @kevisual/api 版本至 0.0.15,并在 RouterViewItem 中添加 RouteViewPage 类型,重构 QueryProxy 类以支持页面路由初始化

This commit is contained in:
2026-01-02 19:51:11 +08:00
parent 14a80013e1
commit 31e8c0346f
2 changed files with 56 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/api",
"version": "0.0.14",
"version": "0.0.15",
"description": "",
"main": "mod.ts",
"scripts": {

View File

@@ -3,7 +3,9 @@ import { QueryRouterServer, Route } from '@kevisual/router/src/route.ts';
import { filter } from '@kevisual/js-filter'
import { EventEmitter } from 'eventemitter3';
export type RouterViewItem = RouterViewApi | RouterViewContext | RouterViewWorker;
export const RouteTypeList = ['api', 'context', 'worker', 'page'] as const;
export type RouterViewItem = RouterViewApi | RouterViewContext | RouterViewWorker | RouteViewPage;
type RouteViewBase = {
id: string;
title: string;
@@ -44,6 +46,16 @@ export type RouterViewWorker = {
}
} & RouteViewBase;
/**
* 注入 js 的url地址使用importScripts加载
*/
export type RouteViewPage = {
type: 'page',
page: {
url: string,
}
} & RouteViewBase;
export type RouterViewQuery = {
id: string,
query: string,
@@ -138,21 +150,44 @@ export class QueryProxy {
async init() {
const routerViewItems = this.routerViewItems || [];
if (routerViewItems.length === 0) {
// 默认初始化api类型路由
await this.initApi();
return;
}
for (const item of routerViewItems) {
switch (item.type) {
case 'api':
this.initApi(item);
await this.initApi(item);
break;
case 'context':
await this.initContext(item);
break;
case 'worker':
this.initWorker(item);
await this.initWorker(item);
break;
case 'page':
await this.initPage(item);
break;
}
}
this.emitter.emit('initComplete');
}
/**
* 监听初始化完成
* @returns
*/
async listenInitComplete(): Promise<boolean> {
return new Promise((resolve) => {
const timer = setTimeout(() => {
this.emitter.removeAllListeners('initComplete');
resolve(false);
}, 3 * 60000); // 3分钟超时
const func = () => {
clearTimeout(timer);
resolve(true);
}
this.emitter.once('initComplete', func);
});
}
async initApi(item?: RouterViewApi) {
const that = this;
@@ -304,6 +339,23 @@ export class QueryProxy {
}
}
}
async initPage(item?: RouteViewPage) {
if (!item?.page?.url) {
console.warn('Page地址未提供');
return;
}
const url = item.page.url;
try {
if (typeof window !== 'undefined') {
await import(url).then((module) => { }).catch((err) => {
console.error('引入Page脚本失败:', url, err);
});
}
} catch (e) {
console.warn('引入Page脚本失败:', url, e);
return;
}
}
/**
* 列出路由
* @param filter