vafast 0.4.0 → 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/dist/index.d.ts CHANGED
@@ -15,7 +15,7 @@ export { DependencyManager } from './utils/dependency-manager.js';
15
15
  export { SchemaConfig, ValidationError, ValidationResult, createValidator, getValidatorCacheStats, precompileSchemas, validateAllSchemas, validateFast, validateSchema, validateSchemaOrThrow } from './utils/validators/validators.js';
16
16
  export { Patterns, hasFormat, registerFormat, registerFormats } from './utils/formats.js';
17
17
  export { SSEEvent, SSEHandler, SSEMarker, createSSEHandler } from './utils/sse.js';
18
- export { RouteMeta, RouteRegistry, createRouteRegistry } from './utils/route-registry.js';
18
+ export { RouteMeta, RouteRegistry, createRouteRegistry, filterRoutes, getAllRoutes, getRoute, getRouteRegistry } from './utils/route-registry.js';
19
19
  export { flattenNestedRoutes, normalizePath } from './router.js';
20
20
  export { requireAuth } from './middleware/authMiddleware.js';
21
21
  export { rateLimit } from './middleware/rateLimit.js';
package/dist/index.js CHANGED
@@ -495,6 +495,149 @@ var RadixRouter = class {
495
495
  }
496
496
  };
497
497
 
498
+ // src/utils/route-registry.ts
499
+ var RouteRegistry = class {
500
+ /** 所有路由元信息 */
501
+ routes = [];
502
+ /** 路由映射表:METHOD:fullPath -> RouteMeta */
503
+ routeMap = /* @__PURE__ */ new Map();
504
+ /** 分类映射表:category -> RouteMeta[] */
505
+ categoryMap = /* @__PURE__ */ new Map();
506
+ constructor(routes) {
507
+ this.buildRegistry(routes);
508
+ }
509
+ /**
510
+ * 构建注册表
511
+ */
512
+ buildRegistry(routes) {
513
+ for (const route2 of routes) {
514
+ const meta = {
515
+ method: route2.method,
516
+ path: route2.path,
517
+ fullPath: route2.fullPath,
518
+ name: route2.name,
519
+ description: route2.description
520
+ };
521
+ for (const key of Object.keys(route2)) {
522
+ if (!["method", "path", "fullPath", "name", "description", "handler", "middleware", "middlewareChain"].includes(key)) {
523
+ meta[key] = route2[key];
524
+ }
525
+ }
526
+ this.routes.push(meta);
527
+ this.routeMap.set(`${route2.method}:${route2.fullPath}`, meta);
528
+ const category = this.extractCategory(route2.fullPath);
529
+ if (!this.categoryMap.has(category)) {
530
+ this.categoryMap.set(category, []);
531
+ }
532
+ this.categoryMap.get(category).push(meta);
533
+ }
534
+ }
535
+ /**
536
+ * 提取分类(第一段路径)
537
+ */
538
+ extractCategory(path) {
539
+ const segments = path.split("/").filter(Boolean);
540
+ return segments[0] || "root";
541
+ }
542
+ // ============================================
543
+ // 查询接口
544
+ // ============================================
545
+ /**
546
+ * 获取所有路由元信息
547
+ */
548
+ getAll() {
549
+ return [...this.routes];
550
+ }
551
+ /**
552
+ * 按 method + path 查询路由
553
+ */
554
+ get(method, path) {
555
+ return this.routeMap.get(`${method}:${path}`);
556
+ }
557
+ /**
558
+ * 检查路由是否存在
559
+ */
560
+ has(method, path) {
561
+ return this.routeMap.has(`${method}:${path}`);
562
+ }
563
+ /**
564
+ * 按分类获取路由
565
+ */
566
+ getByCategory(category) {
567
+ return this.categoryMap.get(category) || [];
568
+ }
569
+ /**
570
+ * 获取所有分类
571
+ */
572
+ getCategories() {
573
+ return Array.from(this.categoryMap.keys()).sort();
574
+ }
575
+ /**
576
+ * 筛选有特定字段的路由
577
+ *
578
+ * @example
579
+ * ```typescript
580
+ * // 获取所有配置了 webhook 的路由
581
+ * const webhookRoutes = registry.filter('webhook')
582
+ * ```
583
+ */
584
+ filter(field) {
585
+ return this.routes.filter((r) => field in r && r[field] !== void 0);
586
+ }
587
+ /**
588
+ * 按条件筛选路由
589
+ *
590
+ * @example
591
+ * ```typescript
592
+ * // 获取所有 POST 请求
593
+ * const postRoutes = registry.filterBy(r => r.method === 'POST')
594
+ * ```
595
+ */
596
+ filterBy(predicate) {
597
+ return this.routes.filter(predicate);
598
+ }
599
+ /**
600
+ * 获取路由数量
601
+ */
602
+ get size() {
603
+ return this.routes.length;
604
+ }
605
+ /**
606
+ * 遍历所有路由
607
+ */
608
+ forEach(callback) {
609
+ this.routes.forEach(callback);
610
+ }
611
+ /**
612
+ * 映射所有路由
613
+ */
614
+ map(callback) {
615
+ return this.routes.map(callback);
616
+ }
617
+ };
618
+ function createRouteRegistry(routes) {
619
+ return new RouteRegistry(routes);
620
+ }
621
+ var globalRegistry = null;
622
+ function setGlobalRegistry(registry) {
623
+ globalRegistry = registry;
624
+ }
625
+ function getRouteRegistry() {
626
+ if (!globalRegistry) {
627
+ throw new Error("RouteRegistry not initialized. Make sure Server is created first.");
628
+ }
629
+ return globalRegistry;
630
+ }
631
+ function getRoute(method, path) {
632
+ return getRouteRegistry().get(method, path);
633
+ }
634
+ function getAllRoutes() {
635
+ return getRouteRegistry().getAll();
636
+ }
637
+ function filterRoutes(field) {
638
+ return getRouteRegistry().filter(field);
639
+ }
640
+
498
641
  // src/server/server.ts
499
642
  var Server = class extends BaseServer {
500
643
  router;
@@ -540,6 +683,7 @@ var Server = class extends BaseServer {
540
683
  if (this.globalMiddleware.length === 0 && !this.isCompiled) {
541
684
  this.compile();
542
685
  }
686
+ setGlobalRegistry(new RouteRegistry(this.routes));
543
687
  }
544
688
  /** 快速提取 pathname */
545
689
  extractPathname(url) {
@@ -1700,130 +1844,6 @@ function createSSEHandler(schemaOrGenerator, maybeGenerator) {
1700
1844
  return handler;
1701
1845
  }
1702
1846
 
1703
- // src/utils/route-registry.ts
1704
- var RouteRegistry = class {
1705
- /** 所有路由元信息 */
1706
- routes = [];
1707
- /** 路由映射表:METHOD:fullPath -> RouteMeta */
1708
- routeMap = /* @__PURE__ */ new Map();
1709
- /** 分类映射表:category -> RouteMeta[] */
1710
- categoryMap = /* @__PURE__ */ new Map();
1711
- constructor(routes) {
1712
- this.buildRegistry(routes);
1713
- }
1714
- /**
1715
- * 构建注册表
1716
- */
1717
- buildRegistry(routes) {
1718
- for (const route2 of routes) {
1719
- const meta = {
1720
- method: route2.method,
1721
- path: route2.path,
1722
- fullPath: route2.fullPath,
1723
- name: route2.name,
1724
- description: route2.description
1725
- };
1726
- for (const key of Object.keys(route2)) {
1727
- if (!["method", "path", "fullPath", "name", "description", "handler", "middleware", "middlewareChain"].includes(key)) {
1728
- meta[key] = route2[key];
1729
- }
1730
- }
1731
- this.routes.push(meta);
1732
- this.routeMap.set(`${route2.method}:${route2.fullPath}`, meta);
1733
- const category = this.extractCategory(route2.fullPath);
1734
- if (!this.categoryMap.has(category)) {
1735
- this.categoryMap.set(category, []);
1736
- }
1737
- this.categoryMap.get(category).push(meta);
1738
- }
1739
- }
1740
- /**
1741
- * 提取分类(第一段路径)
1742
- */
1743
- extractCategory(path) {
1744
- const segments = path.split("/").filter(Boolean);
1745
- return segments[0] || "root";
1746
- }
1747
- // ============================================
1748
- // 查询接口
1749
- // ============================================
1750
- /**
1751
- * 获取所有路由元信息
1752
- */
1753
- getAll() {
1754
- return [...this.routes];
1755
- }
1756
- /**
1757
- * 按 method + path 查询路由
1758
- */
1759
- get(method, path) {
1760
- return this.routeMap.get(`${method}:${path}`);
1761
- }
1762
- /**
1763
- * 检查路由是否存在
1764
- */
1765
- has(method, path) {
1766
- return this.routeMap.has(`${method}:${path}`);
1767
- }
1768
- /**
1769
- * 按分类获取路由
1770
- */
1771
- getByCategory(category) {
1772
- return this.categoryMap.get(category) || [];
1773
- }
1774
- /**
1775
- * 获取所有分类
1776
- */
1777
- getCategories() {
1778
- return Array.from(this.categoryMap.keys()).sort();
1779
- }
1780
- /**
1781
- * 筛选有特定字段的路由
1782
- *
1783
- * @example
1784
- * ```typescript
1785
- * // 获取所有配置了 webhook 的路由
1786
- * const webhookRoutes = registry.filter('webhook')
1787
- * ```
1788
- */
1789
- filter(field) {
1790
- return this.routes.filter((r) => field in r && r[field] !== void 0);
1791
- }
1792
- /**
1793
- * 按条件筛选路由
1794
- *
1795
- * @example
1796
- * ```typescript
1797
- * // 获取所有 POST 请求
1798
- * const postRoutes = registry.filterBy(r => r.method === 'POST')
1799
- * ```
1800
- */
1801
- filterBy(predicate) {
1802
- return this.routes.filter(predicate);
1803
- }
1804
- /**
1805
- * 获取路由数量
1806
- */
1807
- get size() {
1808
- return this.routes.length;
1809
- }
1810
- /**
1811
- * 遍历所有路由
1812
- */
1813
- forEach(callback) {
1814
- this.routes.forEach(callback);
1815
- }
1816
- /**
1817
- * 映射所有路由
1818
- */
1819
- map(callback) {
1820
- return this.routes.map(callback);
1821
- }
1822
- };
1823
- function createRouteRegistry(routes) {
1824
- return new RouteRegistry(routes);
1825
- }
1826
-
1827
1847
  // src/middleware/authMiddleware.ts
1828
1848
  var requireAuth = async (req, next) => {
1829
1849
  const token = getCookie2(req, "auth");
@@ -2465,12 +2485,16 @@ export {
2465
2485
  defineRoutes,
2466
2486
  del,
2467
2487
  empty,
2488
+ filterRoutes,
2468
2489
  flattenNestedRoutes,
2469
2490
  generateToken,
2470
2491
  get,
2492
+ getAllRoutes,
2471
2493
  getCookie,
2472
2494
  getHeader,
2473
2495
  getLocals,
2496
+ getRoute,
2497
+ getRouteRegistry,
2474
2498
  getTokenTimeRemaining,
2475
2499
  getValidatorCacheStats,
2476
2500
  goAwait,