vafast 0.3.11 → 0.4.1
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 +168 -1
- package/dist/index.js.map +1 -1
- package/dist/monitoring/index.js +151 -1
- package/dist/monitoring/index.js.map +1 -1
- package/dist/monitoring/native-monitor.js +151 -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 +144 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/server-factory.js +144 -1
- package/dist/server/server-factory.js.map +1 -1
- package/dist/server/server.d.ts +15 -1
- package/dist/server/server.js +144 -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 +146 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/route-registry.d.ts +195 -0
- package/dist/utils/route-registry.js +152 -0
- package/dist/utils/route-registry.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,152 @@
|
|
|
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
|
+
var globalRegistry = null;
|
|
125
|
+
function setGlobalRegistry(registry) {
|
|
126
|
+
globalRegistry = registry;
|
|
127
|
+
}
|
|
128
|
+
function getRouteRegistry() {
|
|
129
|
+
if (!globalRegistry) {
|
|
130
|
+
throw new Error("RouteRegistry not initialized. Make sure Server is created first.");
|
|
131
|
+
}
|
|
132
|
+
return globalRegistry;
|
|
133
|
+
}
|
|
134
|
+
function getRoute(method, path) {
|
|
135
|
+
return getRouteRegistry().get(method, path);
|
|
136
|
+
}
|
|
137
|
+
function getAllRoutes() {
|
|
138
|
+
return getRouteRegistry().getAll();
|
|
139
|
+
}
|
|
140
|
+
function filterRoutes(field) {
|
|
141
|
+
return getRouteRegistry().filter(field);
|
|
142
|
+
}
|
|
143
|
+
export {
|
|
144
|
+
RouteRegistry,
|
|
145
|
+
createRouteRegistry,
|
|
146
|
+
filterRoutes,
|
|
147
|
+
getAllRoutes,
|
|
148
|
+
getRoute,
|
|
149
|
+
getRouteRegistry,
|
|
150
|
+
setGlobalRegistry
|
|
151
|
+
};
|
|
152
|
+
//# 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// ============================================\n// 全局 Registry(单例模式)\n// ============================================\n\n/** 全局 registry 实例 */\nlet globalRegistry: RouteRegistry | null = null\n\n/**\n * 设置全局 registry(框架内部使用)\n * @internal\n */\nexport function setGlobalRegistry(registry: RouteRegistry): void {\n globalRegistry = registry\n}\n\n/**\n * 获取全局路由注册表\n *\n * @example\n * ```typescript\n * // 在任意文件中\n * import { getRouteRegistry } from 'vafast'\n *\n * const registry = getRouteRegistry()\n * const webhookRoutes = registry.filter('webhook')\n * ```\n *\n * @throws 如果 Server 尚未创建\n */\nexport function getRouteRegistry<T extends Record<string, unknown> = Record<string, unknown>>(): RouteRegistry<T> {\n if (!globalRegistry) {\n throw new Error('RouteRegistry not initialized. Make sure Server is created first.')\n }\n return globalRegistry as RouteRegistry<T>\n}\n\n/**\n * 按 method + path 获取单个路由\n *\n * 便捷函数,无需先获取 registry\n *\n * @example\n * ```typescript\n * // 在中间件或接口中\n * import { getRoute } from 'vafast'\n *\n * const route = getRoute('POST', '/auth/signIn')\n * if (route?.webhook) {\n * console.log('This route has webhook:', route.webhook)\n * }\n * ```\n */\nexport function getRoute<T extends Record<string, unknown> = Record<string, unknown>>(\n method: string,\n path: string\n): (RouteMeta & T) | undefined {\n return getRouteRegistry<T>().get(method, path)\n}\n\n/**\n * 获取所有路由\n *\n * @example\n * ```typescript\n * import { getAllRoutes } from 'vafast'\n *\n * const routes = getAllRoutes()\n * console.log(`Total ${routes.length} routes`)\n * ```\n */\nexport function getAllRoutes(): RouteMeta[] {\n return getRouteRegistry().getAll()\n}\n\n/**\n * 筛选有特定字段的路由\n *\n * @example\n * ```typescript\n * import { filterRoutes } from 'vafast'\n *\n * // 获取所有配置了 webhook 的路由\n * const webhookRoutes = filterRoutes('webhook')\n * ```\n */\nexport function filterRoutes<K extends string>(field: K): (RouteMeta & Record<K, unknown>)[] {\n return getRouteRegistry().filter(field)\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,EAEnD,YAAY,QAA0B;AACpC,SAAK,cAAc,MAAM;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA,EAKQ,cAAc,QAAgC;AACpD,eAAW,SAAS,QAAQ;AAE1B,YAAM,OAAkB;AAAA,QACtB,QAAQ,MAAM;AAAA,QACd,MAAM,MAAM;AAAA,QACZ,UAAU,MAAM;AAAA,QAChB,MAAM,MAAM;AAAA,QACZ,aAAa,MAAM;AAAA,MACrB;AAGA,iBAAW,O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