vafast 0.4.0 → 0.4.2

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
@@ -1,22 +1,25 @@
1
1
  // src/router.ts
2
2
  function flattenNestedRoutes(routes) {
3
3
  const flattened = [];
4
- function processRoute(route2, parentPath = "", parentMiddleware = []) {
4
+ function processRoute(route2, parentPath = "", parentMiddleware = [], parentName) {
5
5
  const currentPath = normalizePath(parentPath + route2.path);
6
6
  const currentMiddleware = [
7
7
  ...parentMiddleware,
8
8
  ...route2.middleware || []
9
9
  ];
10
+ const currentName = route2.name || parentName;
10
11
  if ("method" in route2 && "handler" in route2) {
11
12
  const leafRoute = route2;
12
13
  flattened.push({
13
14
  ...leafRoute,
14
15
  fullPath: currentPath,
15
- middlewareChain: currentMiddleware
16
+ middlewareChain: currentMiddleware,
17
+ parentName
18
+ // 保存父级名称
16
19
  });
17
20
  } else if ("children" in route2 && route2.children) {
18
21
  for (const child of route2.children) {
19
- processRoute(child, currentPath, currentMiddleware);
22
+ processRoute(child, currentPath, currentMiddleware, currentName);
20
23
  }
21
24
  }
22
25
  }
@@ -495,6 +498,149 @@ var RadixRouter = class {
495
498
  }
496
499
  };
497
500
 
501
+ // src/utils/route-registry.ts
502
+ var RouteRegistry = class {
503
+ /** 所有路由元信息 */
504
+ routes = [];
505
+ /** 路由映射表:METHOD:fullPath -> RouteMeta */
506
+ routeMap = /* @__PURE__ */ new Map();
507
+ /** 分类映射表:category -> RouteMeta[] */
508
+ categoryMap = /* @__PURE__ */ new Map();
509
+ constructor(routes) {
510
+ this.buildRegistry(routes);
511
+ }
512
+ /**
513
+ * 构建注册表
514
+ */
515
+ buildRegistry(routes) {
516
+ for (const route2 of routes) {
517
+ const meta = {
518
+ method: route2.method,
519
+ path: route2.path,
520
+ fullPath: route2.fullPath,
521
+ name: route2.name,
522
+ description: route2.description
523
+ };
524
+ for (const key of Object.keys(route2)) {
525
+ if (!["method", "path", "fullPath", "name", "description", "handler", "middleware", "middlewareChain"].includes(key)) {
526
+ meta[key] = route2[key];
527
+ }
528
+ }
529
+ this.routes.push(meta);
530
+ this.routeMap.set(`${route2.method}:${route2.fullPath}`, meta);
531
+ const category = this.extractCategory(route2.fullPath);
532
+ if (!this.categoryMap.has(category)) {
533
+ this.categoryMap.set(category, []);
534
+ }
535
+ this.categoryMap.get(category).push(meta);
536
+ }
537
+ }
538
+ /**
539
+ * 提取分类(第一段路径)
540
+ */
541
+ extractCategory(path) {
542
+ const segments = path.split("/").filter(Boolean);
543
+ return segments[0] || "root";
544
+ }
545
+ // ============================================
546
+ // 查询接口
547
+ // ============================================
548
+ /**
549
+ * 获取所有路由元信息
550
+ */
551
+ getAll() {
552
+ return [...this.routes];
553
+ }
554
+ /**
555
+ * 按 method + path 查询路由
556
+ */
557
+ get(method, path) {
558
+ return this.routeMap.get(`${method}:${path}`);
559
+ }
560
+ /**
561
+ * 检查路由是否存在
562
+ */
563
+ has(method, path) {
564
+ return this.routeMap.has(`${method}:${path}`);
565
+ }
566
+ /**
567
+ * 按分类获取路由
568
+ */
569
+ getByCategory(category) {
570
+ return this.categoryMap.get(category) || [];
571
+ }
572
+ /**
573
+ * 获取所有分类
574
+ */
575
+ getCategories() {
576
+ return Array.from(this.categoryMap.keys()).sort();
577
+ }
578
+ /**
579
+ * 筛选有特定字段的路由
580
+ *
581
+ * @example
582
+ * ```typescript
583
+ * // 获取所有配置了 webhook 的路由
584
+ * const webhookRoutes = registry.filter('webhook')
585
+ * ```
586
+ */
587
+ filter(field) {
588
+ return this.routes.filter((r) => field in r && r[field] !== void 0);
589
+ }
590
+ /**
591
+ * 按条件筛选路由
592
+ *
593
+ * @example
594
+ * ```typescript
595
+ * // 获取所有 POST 请求
596
+ * const postRoutes = registry.filterBy(r => r.method === 'POST')
597
+ * ```
598
+ */
599
+ filterBy(predicate) {
600
+ return this.routes.filter(predicate);
601
+ }
602
+ /**
603
+ * 获取路由数量
604
+ */
605
+ get size() {
606
+ return this.routes.length;
607
+ }
608
+ /**
609
+ * 遍历所有路由
610
+ */
611
+ forEach(callback) {
612
+ this.routes.forEach(callback);
613
+ }
614
+ /**
615
+ * 映射所有路由
616
+ */
617
+ map(callback) {
618
+ return this.routes.map(callback);
619
+ }
620
+ };
621
+ function createRouteRegistry(routes) {
622
+ return new RouteRegistry(routes);
623
+ }
624
+ var globalRegistry = null;
625
+ function setGlobalRegistry(registry) {
626
+ globalRegistry = registry;
627
+ }
628
+ function getRouteRegistry() {
629
+ if (!globalRegistry) {
630
+ throw new Error("RouteRegistry not initialized. Make sure Server is created first.");
631
+ }
632
+ return globalRegistry;
633
+ }
634
+ function getRoute(method, path) {
635
+ return getRouteRegistry().get(method, path);
636
+ }
637
+ function getAllRoutes() {
638
+ return getRouteRegistry().getAll();
639
+ }
640
+ function filterRoutes(field) {
641
+ return getRouteRegistry().filter(field);
642
+ }
643
+
498
644
  // src/server/server.ts
499
645
  var Server = class extends BaseServer {
500
646
  router;
@@ -540,6 +686,7 @@ var Server = class extends BaseServer {
540
686
  if (this.globalMiddleware.length === 0 && !this.isCompiled) {
541
687
  this.compile();
542
688
  }
689
+ setGlobalRegistry(new RouteRegistry(this.routes));
543
690
  }
544
691
  /** 快速提取 pathname */
545
692
  extractPathname(url) {
@@ -1700,130 +1847,6 @@ function createSSEHandler(schemaOrGenerator, maybeGenerator) {
1700
1847
  return handler;
1701
1848
  }
1702
1849
 
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
1850
  // src/middleware/authMiddleware.ts
1828
1851
  var requireAuth = async (req, next) => {
1829
1852
  const token = getCookie2(req, "auth");
@@ -2465,12 +2488,16 @@ export {
2465
2488
  defineRoutes,
2466
2489
  del,
2467
2490
  empty,
2491
+ filterRoutes,
2468
2492
  flattenNestedRoutes,
2469
2493
  generateToken,
2470
2494
  get,
2495
+ getAllRoutes,
2471
2496
  getCookie,
2472
2497
  getHeader,
2473
2498
  getLocals,
2499
+ getRoute,
2500
+ getRouteRegistry,
2474
2501
  getTokenTimeRemaining,
2475
2502
  getValidatorCacheStats,
2476
2503
  goAwait,