UNPKG

vont

Version:

A full-stack framework combining Koa and React with file-based routing

85 lines 2.44 kB
import { generateApiRoutes, scanPageRoutes, normalizeRoutePath } from './router-generator.js'; /** * 路由注册表 */ export class RouteRegistry { constructor(apiDir, pagesDir, apiPrefix = '/api') { this.apiRoutes = []; this.pageRoutes = new Map(); this.apiDir = apiDir; this.pagesDir = pagesDir; this.apiPrefix = apiPrefix; } /** * 扫描并注册所有路由 */ async scan() { console.log('\n📍 Scanning routes...\n'); // 扫描 API 路由 await this.scanApiRoutes(); // 扫描页面路由 await this.scanPageRoutes(); console.log(`\n✅ Found ${this.apiRoutes.length} API routes`); } /** * 扫描 API 路由 */ async scanApiRoutes() { try { const routes = await generateApiRoutes(this.apiDir, this.apiPrefix); this.apiRoutes = routes; if (routes.length > 0) { console.log('📡 API Routes:'); for (const route of routes) { console.log(` ${route.method.padEnd(6)} ${route.path}`); } } } catch (error) { console.error('Error scanning API routes:', error); } } /** * 扫描页面路由 */ async scanPageRoutes() { try { const routes = await scanPageRoutes(this.pagesDir); this.pageRoutes.clear(); for (const route of routes) { const normalizedRoute = normalizeRoutePath(route); this.pageRoutes.set(normalizedRoute, normalizedRoute); } if (this.pageRoutes.size > 0) { console.log('📄 Page Routes:'); for (const route of this.pageRoutes.keys()) { console.log(` ${route}`); } } } catch (error) { console.error('Error scanning page routes:', error); } } /** * 获取 API 路由 */ getApiRoutes() { return this.apiRoutes; } /** * 获取页面路由 */ getPageRoutes() { return this.pageRoutes; } /** * 重新扫描路由(用于开发时动态更新) */ async rescan() { this.apiRoutes = []; this.pageRoutes.clear(); await this.scan(); } } //# sourceMappingURL=route-registry.js.map