update
This commit is contained in:
@@ -1,5 +1,21 @@
|
||||
import { Query } from '@kevisual/query/query';
|
||||
import { QueryRouterServer, Route } from '@kevisual/router/src/route.ts';
|
||||
import { filter } from '@kevisual/js-filter'
|
||||
export type ProxyItem = {
|
||||
title?: string;
|
||||
type?: 'api' | 'context' | 'page';
|
||||
description?: string;
|
||||
api?: {
|
||||
url: string;
|
||||
},
|
||||
context?: {
|
||||
key: string;
|
||||
},
|
||||
page?: {},
|
||||
where?: string;
|
||||
whereList?: Array<{ title: string; where: string }>;
|
||||
}
|
||||
|
||||
export class QueryProxy {
|
||||
query: Query;
|
||||
router: QueryRouterServer;
|
||||
@@ -7,7 +23,16 @@ export class QueryProxy {
|
||||
constructor(opts?: { query: Query, router?: QueryRouterServer, token?: string }) {
|
||||
this.query = opts?.query || new Query();
|
||||
this.router = opts?.router || new QueryRouterServer();
|
||||
this.token = opts?.token;
|
||||
this.token = opts?.token || this.getDefulatToken();
|
||||
}
|
||||
getDefulatToken() {
|
||||
try {
|
||||
if (localStorage) {
|
||||
return localStorage.getItem('token') || undefined;
|
||||
}
|
||||
} catch (e) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 初始化路由
|
||||
@@ -22,11 +47,12 @@ export class QueryProxy {
|
||||
}
|
||||
const _list = res.data?.list || []
|
||||
for (const item of _list) {
|
||||
if (item.path && item.key) {
|
||||
console.log(`Register route: [${item.path}] ${item.key}`);
|
||||
if (item.path || item.id) {
|
||||
console.log(`Register route: [${item.path}] ${item?.key}`);
|
||||
this.router.route({
|
||||
path: item.path,
|
||||
key: item.key,
|
||||
key: item.key || '',
|
||||
id: item.id,
|
||||
description: item.description,
|
||||
metadata: item.metadata,
|
||||
}).define(async (ctx) => {
|
||||
@@ -43,10 +69,15 @@ export class QueryProxy {
|
||||
/**
|
||||
* 列出路由
|
||||
* @param filter
|
||||
* @param query WHERE metadata.tags CONTAINS 'premium'
|
||||
* @returns
|
||||
*/
|
||||
async listRoutes(filter?: (item: Route) => boolean) {
|
||||
return this.router.routes.filter(filter || (() => true));
|
||||
async listRoutes(filterFn?: (item: Route) => boolean, query?: string) {
|
||||
const routes = this.router.routes.filter(filterFn || (() => true));
|
||||
if (query) {
|
||||
return filter(routes, query);
|
||||
}
|
||||
return routes;
|
||||
}
|
||||
/**
|
||||
* 运行路由
|
||||
|
||||
Reference in New Issue
Block a user