vafast 0.3.11 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +144 -1
- package/dist/index.js.map +1 -1
- package/dist/monitoring/index.js +18 -1
- package/dist/monitoring/index.js.map +1 -1
- package/dist/monitoring/native-monitor.js +18 -1
- package/dist/monitoring/native-monitor.js.map +1 -1
- package/dist/router/index.js +2 -1
- package/dist/router/index.js.map +1 -1
- package/dist/router.js +2 -1
- package/dist/router.js.map +1 -1
- package/dist/server/index.js +18 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/server-factory.js +18 -1
- package/dist/server/server-factory.js.map +1 -1
- package/dist/server/server.d.ts +15 -1
- package/dist/server/server.js +18 -1
- package/dist/server/server.js.map +1 -1
- package/dist/types/types.d.ts +12 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +126 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/route-registry.d.ts +134 -0
- package/dist/utils/route-registry.js +128 -0
- package/dist/utils/route-registry.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { FlattenedRoute, Method } from '../types/types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 路由注册表
|
|
5
|
+
*
|
|
6
|
+
* 提供路由元信息的收集和查询能力
|
|
7
|
+
* 可用于:API 文档生成、Webhook 事件注册、权限检查、审计日志等
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // 创建注册表
|
|
12
|
+
* const registry = createRouteRegistry(server.getRoutes())
|
|
13
|
+
*
|
|
14
|
+
* // 查询路由
|
|
15
|
+
* const route = registry.get('POST', '/auth/signIn')
|
|
16
|
+
*
|
|
17
|
+
* // 筛选有 webhook 配置的路由
|
|
18
|
+
* const webhookRoutes = registry.filter('webhook')
|
|
19
|
+
*
|
|
20
|
+
* // 按分类获取
|
|
21
|
+
* const authRoutes = registry.getByCategory('auth')
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 路由元信息(不含 handler 和 middleware)
|
|
27
|
+
*/
|
|
28
|
+
interface RouteMeta {
|
|
29
|
+
method: Method;
|
|
30
|
+
path: string;
|
|
31
|
+
fullPath: string;
|
|
32
|
+
name?: string;
|
|
33
|
+
description?: string;
|
|
34
|
+
/** 扩展字段 */
|
|
35
|
+
[key: string]: unknown;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 路由注册表
|
|
39
|
+
*
|
|
40
|
+
* 泛型 T 用于定义扩展字段的类型
|
|
41
|
+
*/
|
|
42
|
+
declare class RouteRegistry<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
43
|
+
/** 所有路由元信息 */
|
|
44
|
+
private routes;
|
|
45
|
+
/** 路由映射表:METHOD:fullPath -> RouteMeta */
|
|
46
|
+
private routeMap;
|
|
47
|
+
/** 分类映射表:category -> RouteMeta[] */
|
|
48
|
+
private categoryMap;
|
|
49
|
+
constructor(routes: FlattenedRoute[]);
|
|
50
|
+
/**
|
|
51
|
+
* 构建注册表
|
|
52
|
+
*/
|
|
53
|
+
private buildRegistry;
|
|
54
|
+
/**
|
|
55
|
+
* 提取分类(第一段路径)
|
|
56
|
+
*/
|
|
57
|
+
private extractCategory;
|
|
58
|
+
/**
|
|
59
|
+
* 获取所有路由元信息
|
|
60
|
+
*/
|
|
61
|
+
getAll(): RouteMeta[];
|
|
62
|
+
/**
|
|
63
|
+
* 按 method + path 查询路由
|
|
64
|
+
*/
|
|
65
|
+
get(method: string, path: string): (RouteMeta & T) | undefined;
|
|
66
|
+
/**
|
|
67
|
+
* 检查路由是否存在
|
|
68
|
+
*/
|
|
69
|
+
has(method: string, path: string): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* 按分类获取路由
|
|
72
|
+
*/
|
|
73
|
+
getByCategory(category: string): RouteMeta[];
|
|
74
|
+
/**
|
|
75
|
+
* 获取所有分类
|
|
76
|
+
*/
|
|
77
|
+
getCategories(): string[];
|
|
78
|
+
/**
|
|
79
|
+
* 筛选有特定字段的路由
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // 获取所有配置了 webhook 的路由
|
|
84
|
+
* const webhookRoutes = registry.filter('webhook')
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
filter<K extends string>(field: K): (RouteMeta & Record<K, unknown>)[];
|
|
88
|
+
/**
|
|
89
|
+
* 按条件筛选路由
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* // 获取所有 POST 请求
|
|
94
|
+
* const postRoutes = registry.filterBy(r => r.method === 'POST')
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
filterBy(predicate: (route: RouteMeta) => boolean): RouteMeta[];
|
|
98
|
+
/**
|
|
99
|
+
* 获取路由数量
|
|
100
|
+
*/
|
|
101
|
+
get size(): number;
|
|
102
|
+
/**
|
|
103
|
+
* 遍历所有路由
|
|
104
|
+
*/
|
|
105
|
+
forEach(callback: (route: RouteMeta, index: number) => void): void;
|
|
106
|
+
/**
|
|
107
|
+
* 映射所有路由
|
|
108
|
+
*/
|
|
109
|
+
map<R>(callback: (route: RouteMeta, index: number) => R): R[];
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* 创建路由注册表
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* // 定义扩展字段类型
|
|
117
|
+
* interface MyRouteMeta {
|
|
118
|
+
* webhook?: { eventKey: string }
|
|
119
|
+
* permission?: string
|
|
120
|
+
* }
|
|
121
|
+
*
|
|
122
|
+
* // 创建带类型的注册表
|
|
123
|
+
* const registry = createRouteRegistry<MyRouteMeta>(server.getRoutes())
|
|
124
|
+
*
|
|
125
|
+
* // 类型安全的查询
|
|
126
|
+
* const route = registry.get('POST', '/auth/signIn')
|
|
127
|
+
* if (route?.webhook) {
|
|
128
|
+
* console.log(route.webhook.eventKey)
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
declare function createRouteRegistry<T extends Record<string, unknown> = Record<string, unknown>>(routes: FlattenedRoute[]): RouteRegistry<T>;
|
|
133
|
+
|
|
134
|
+
export { type RouteMeta, RouteRegistry, createRouteRegistry };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// src/utils/route-registry.ts
|
|
2
|
+
var RouteRegistry = class {
|
|
3
|
+
/** 所有路由元信息 */
|
|
4
|
+
routes = [];
|
|
5
|
+
/** 路由映射表:METHOD:fullPath -> RouteMeta */
|
|
6
|
+
routeMap = /* @__PURE__ */ new Map();
|
|
7
|
+
/** 分类映射表:category -> RouteMeta[] */
|
|
8
|
+
categoryMap = /* @__PURE__ */ new Map();
|
|
9
|
+
constructor(routes) {
|
|
10
|
+
this.buildRegistry(routes);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 构建注册表
|
|
14
|
+
*/
|
|
15
|
+
buildRegistry(routes) {
|
|
16
|
+
for (const route of routes) {
|
|
17
|
+
const meta = {
|
|
18
|
+
method: route.method,
|
|
19
|
+
path: route.path,
|
|
20
|
+
fullPath: route.fullPath,
|
|
21
|
+
name: route.name,
|
|
22
|
+
description: route.description
|
|
23
|
+
};
|
|
24
|
+
for (const key of Object.keys(route)) {
|
|
25
|
+
if (!["method", "path", "fullPath", "name", "description", "handler", "middleware", "middlewareChain"].includes(key)) {
|
|
26
|
+
meta[key] = route[key];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
this.routes.push(meta);
|
|
30
|
+
this.routeMap.set(`${route.method}:${route.fullPath}`, meta);
|
|
31
|
+
const category = this.extractCategory(route.fullPath);
|
|
32
|
+
if (!this.categoryMap.has(category)) {
|
|
33
|
+
this.categoryMap.set(category, []);
|
|
34
|
+
}
|
|
35
|
+
this.categoryMap.get(category).push(meta);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 提取分类(第一段路径)
|
|
40
|
+
*/
|
|
41
|
+
extractCategory(path) {
|
|
42
|
+
const segments = path.split("/").filter(Boolean);
|
|
43
|
+
return segments[0] || "root";
|
|
44
|
+
}
|
|
45
|
+
// ============================================
|
|
46
|
+
// 查询接口
|
|
47
|
+
// ============================================
|
|
48
|
+
/**
|
|
49
|
+
* 获取所有路由元信息
|
|
50
|
+
*/
|
|
51
|
+
getAll() {
|
|
52
|
+
return [...this.routes];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 按 method + path 查询路由
|
|
56
|
+
*/
|
|
57
|
+
get(method, path) {
|
|
58
|
+
return this.routeMap.get(`${method}:${path}`);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 检查路由是否存在
|
|
62
|
+
*/
|
|
63
|
+
has(method, path) {
|
|
64
|
+
return this.routeMap.has(`${method}:${path}`);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 按分类获取路由
|
|
68
|
+
*/
|
|
69
|
+
getByCategory(category) {
|
|
70
|
+
return this.categoryMap.get(category) || [];
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 获取所有分类
|
|
74
|
+
*/
|
|
75
|
+
getCategories() {
|
|
76
|
+
return Array.from(this.categoryMap.keys()).sort();
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 筛选有特定字段的路由
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // 获取所有配置了 webhook 的路由
|
|
84
|
+
* const webhookRoutes = registry.filter('webhook')
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
filter(field) {
|
|
88
|
+
return this.routes.filter((r) => field in r && r[field] !== void 0);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 按条件筛选路由
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* // 获取所有 POST 请求
|
|
96
|
+
* const postRoutes = registry.filterBy(r => r.method === 'POST')
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
filterBy(predicate) {
|
|
100
|
+
return this.routes.filter(predicate);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 获取路由数量
|
|
104
|
+
*/
|
|
105
|
+
get size() {
|
|
106
|
+
return this.routes.length;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 遍历所有路由
|
|
110
|
+
*/
|
|
111
|
+
forEach(callback) {
|
|
112
|
+
this.routes.forEach(callback);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* 映射所有路由
|
|
116
|
+
*/
|
|
117
|
+
map(callback) {
|
|
118
|
+
return this.routes.map(callback);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
function createRouteRegistry(routes) {
|
|
122
|
+
return new RouteRegistry(routes);
|
|
123
|
+
}
|
|
124
|
+
export {
|
|
125
|
+
RouteRegistry,
|
|
126
|
+
createRouteRegistry
|
|
127
|
+
};
|
|
128
|
+
//# sourceMappingURL=route-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/route-registry.ts"],"sourcesContent":["/**\n * 路由注册表\n *\n * 提供路由元信息的收集和查询能力\n * 可用于:API 文档生成、Webhook 事件注册、权限检查、审计日志等\n *\n * @example\n * ```typescript\n * // 创建注册表\n * const registry = createRouteRegistry(server.getRoutes())\n *\n * // 查询路由\n * const route = registry.get('POST', '/auth/signIn')\n *\n * // 筛选有 webhook 配置的路由\n * const webhookRoutes = registry.filter('webhook')\n *\n * // 按分类获取\n * const authRoutes = registry.getByCategory('auth')\n * ```\n */\n\nimport type { FlattenedRoute, Method } from '../types'\n\n/**\n * 路由元信息(不含 handler 和 middleware)\n */\nexport interface RouteMeta {\n method: Method\n path: string\n fullPath: string\n name?: string\n description?: string\n /** 扩展字段 */\n [key: string]: unknown\n}\n\n/**\n * 路由注册表\n *\n * 泛型 T 用于定义扩展字段的类型\n */\nexport class RouteRegistry<T extends Record<string, unknown> = Record<string, unknown>> {\n /** 所有路由元信息 */\n private routes: RouteMeta[] = []\n\n /** 路由映射表:METHOD:fullPath -> RouteMeta */\n private routeMap = new Map<string, RouteMeta>()\n\n /** 分类映射表:category -> RouteMeta[] */\n private categoryMap = new Map<string, RouteMeta[]>()\n\n constructor(routes: FlattenedRoute[]) {\n this.buildRegistry(routes)\n }\n\n /**\n * 构建注册表\n */\n private buildRegistry(routes: FlattenedRoute[]): void {\n for (const route of routes) {\n // 提取元信息(排除 handler 和 middleware)\n const meta: RouteMeta = {\n method: route.method,\n path: route.path,\n fullPath: route.fullPath,\n name: route.name,\n description: route.description,\n }\n\n // 复制扩展字段\n for (const key of Object.keys(route)) {\n if (!['method', 'path', 'fullPath', 'name', 'description', 'handler', 'middleware', 'middlewareChain'].includes(key)) {\n meta[key] = route[key as keyof FlattenedRoute]\n }\n }\n\n this.routes.push(meta)\n this.routeMap.set(`${route.method}:${route.fullPath}`, meta)\n\n // 按分类索引\n const category = this.extractCategory(route.fullPath)\n if (!this.categoryMap.has(category)) {\n this.categoryMap.set(category, [])\n }\n this.categoryMap.get(category)!.push(meta)\n }\n }\n\n /**\n * 提取分类(第一段路径)\n */\n private extractCategory(path: string): string {\n const segments = path.split('/').filter(Boolean)\n return segments[0] || 'root'\n }\n\n // ============================================\n // 查询接口\n // ============================================\n\n /**\n * 获取所有路由元信息\n */\n getAll(): RouteMeta[] {\n return [...this.routes]\n }\n\n /**\n * 按 method + path 查询路由\n */\n get(method: string, path: string): (RouteMeta & T) | undefined {\n return this.routeMap.get(`${method}:${path}`) as (RouteMeta & T) | undefined\n }\n\n /**\n * 检查路由是否存在\n */\n has(method: string, path: string): boolean {\n return this.routeMap.has(`${method}:${path}`)\n }\n\n /**\n * 按分类获取路由\n */\n getByCategory(category: string): RouteMeta[] {\n return this.categoryMap.get(category) || []\n }\n\n /**\n * 获取所有分类\n */\n getCategories(): string[] {\n return Array.from(this.categoryMap.keys()).sort()\n }\n\n /**\n * 筛选有特定字段的路由\n *\n * @example\n * ```typescript\n * // 获取所有配置了 webhook 的路由\n * const webhookRoutes = registry.filter('webhook')\n * ```\n */\n filter<K extends string>(field: K): (RouteMeta & Record<K, unknown>)[] {\n return this.routes.filter((r) => field in r && r[field] !== undefined) as (RouteMeta & Record<K, unknown>)[]\n }\n\n /**\n * 按条件筛选路由\n *\n * @example\n * ```typescript\n * // 获取所有 POST 请求\n * const postRoutes = registry.filterBy(r => r.method === 'POST')\n * ```\n */\n filterBy(predicate: (route: RouteMeta) => boolean): RouteMeta[] {\n return this.routes.filter(predicate)\n }\n\n /**\n * 获取路由数量\n */\n get size(): number {\n return this.routes.length\n }\n\n /**\n * 遍历所有路由\n */\n forEach(callback: (route: RouteMeta, index: number) => void): void {\n this.routes.forEach(callback)\n }\n\n /**\n * 映射所有路由\n */\n map<R>(callback: (route: RouteMeta, index: number) => R): R[] {\n return this.routes.map(callback)\n }\n}\n\n/**\n * 创建路由注册表\n *\n * @example\n * ```typescript\n * // 定义扩展字段类型\n * interface MyRouteMeta {\n * webhook?: { eventKey: string }\n * permission?: string\n * }\n *\n * // 创建带类型的注册表\n * const registry = createRouteRegistry<MyRouteMeta>(server.getRoutes())\n *\n * // 类型安全的查询\n * const route = registry.get('POST', '/auth/signIn')\n * if (route?.webhook) {\n * console.log(route.webhook.eventKey)\n * }\n * ```\n */\nexport function createRouteRegistry<T extends Record<string, unknown> = Record<string, unknown>>(\n routes: FlattenedRoute[]\n): RouteRegistry<T> {\n return new RouteRegistry<T>(routes)\n}\n\n"],"mappings":";AA0CO,IAAM,gBAAN,MAAiF;AAAA;AAAA,EAE9E,SAAsB,CAAC;AAAA;AAAA,EAGvB,WAAW,oBAAI,IAAuB;AAAA;AAAA,EAGtC,cAAc,oBAAI,IAAyB;AAAA,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