@ereo/router 0.1.6

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 ADDED
@@ -0,0 +1,73 @@
1
+ # @ereo/router
2
+
3
+ File-based routing for the EreoJS framework. Supports dynamic routes, catch-all routes, layouts, route groups, and middleware chains.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bun add @ereo/router
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { createFileRouter, matchRoute } from '@ereo/router';
15
+
16
+ // Initialize the file router
17
+ const router = createFileRouter({
18
+ routesDir: './src/routes',
19
+ });
20
+
21
+ // Match a URL to a route
22
+ const match = matchRoute(router, '/users/123');
23
+ // { params: { id: '123' }, route: { ... } }
24
+ ```
25
+
26
+ ## Key Features
27
+
28
+ - **File-Based Routing** - Automatic route generation from your file structure
29
+ - **Dynamic Routes** - Support for `[param]`, `[...catchAll]`, and `[[optional]]` segments
30
+ - **Route Groups** - Organize routes with `(group)` folders without affecting URLs
31
+ - **Layouts** - Nested layouts with automatic composition
32
+ - **Middleware Chain** - Register and compose middleware with `createMiddleware` and `chainMiddleware`
33
+ - **Route Validation** - Validate params and search params with built-in validators
34
+ - **Built-in Middleware** - Logger, CORS, and rate limiting out of the box
35
+
36
+ ## Middleware Example
37
+
38
+ ```typescript
39
+ import { createMiddleware, chainMiddleware, createLoggerMiddleware } from '@ereo/router';
40
+
41
+ const authMiddleware = createMiddleware(async (ctx, next) => {
42
+ if (!ctx.request.headers.get('Authorization')) {
43
+ return new Response('Unauthorized', { status: 401 });
44
+ }
45
+ return next();
46
+ });
47
+
48
+ const chain = chainMiddleware([
49
+ createLoggerMiddleware(),
50
+ authMiddleware,
51
+ ]);
52
+ ```
53
+
54
+ ## Route Patterns
55
+
56
+ | Pattern | Example | Matches |
57
+ |---------|---------|---------|
58
+ | `[param]` | `/users/[id].tsx` | `/users/123` |
59
+ | `[...all]` | `/docs/[...slug].tsx` | `/docs/a/b/c` |
60
+ | `[[opt]]` | `/posts/[[page]].tsx` | `/posts`, `/posts/2` |
61
+ | `(group)` | `/(auth)/login.tsx` | `/login` |
62
+
63
+ ## Documentation
64
+
65
+ For full documentation, visit [https://ereojs.dev/docs/router](https://ereojs.dev/docs/router)
66
+
67
+ ## Part of EreoJS
68
+
69
+ This package is part of the [EreoJS](https://github.com/ereojs/ereo) monorepo - a modern full-stack framework built for Bun.
70
+
71
+ ## License
72
+
73
+ MIT
@@ -0,0 +1,122 @@
1
+ /**
2
+ * @ereo/router - File-based Route Discovery
3
+ *
4
+ * Discovers routes from the filesystem and builds a route tree.
5
+ * Supports watching for changes in development mode.
6
+ */
7
+ import type { Route, RouteConfig } from '@ereo/core';
8
+ import type { RouterOptions, RouterEvents } from './types';
9
+ import { RouteTree } from './route-tree';
10
+ import { RouteMatcher } from './matcher';
11
+ /**
12
+ * File-based router.
13
+ */
14
+ export declare class FileRouter {
15
+ private options;
16
+ private routesDir;
17
+ private routes;
18
+ private tree;
19
+ private matcher;
20
+ private watcher;
21
+ private eventHandlers;
22
+ constructor(options?: RouterOptions);
23
+ /**
24
+ * Initialize the router by discovering routes.
25
+ */
26
+ init(): Promise<void>;
27
+ /**
28
+ * Discover all routes from the filesystem.
29
+ */
30
+ discoverRoutes(): Promise<Route[]>;
31
+ /**
32
+ * Scan a directory recursively for route files.
33
+ */
34
+ private scanDirectory;
35
+ /**
36
+ * Start watching for file changes.
37
+ */
38
+ private startWatching;
39
+ /**
40
+ * Watch using Node.js file system watcher (works in Bun too).
41
+ */
42
+ private watchWithNode;
43
+ /**
44
+ * Handle file change event.
45
+ */
46
+ private debounceTimer;
47
+ private handleFileChange;
48
+ /**
49
+ * Convert route node to Route type.
50
+ */
51
+ private nodeToRoute;
52
+ /**
53
+ * Stop watching for changes.
54
+ */
55
+ stopWatching(): void;
56
+ /**
57
+ * Get all discovered routes.
58
+ */
59
+ getRoutes(): Route[];
60
+ /**
61
+ * Get the route tree.
62
+ */
63
+ getTree(): RouteTree | null;
64
+ /**
65
+ * Get the route matcher.
66
+ */
67
+ getMatcher(): RouteMatcher | null;
68
+ /**
69
+ * Match a URL pathname.
70
+ */
71
+ match(pathname: string): import("@ereo/core").RouteMatch | null;
72
+ /**
73
+ * Register an event handler.
74
+ */
75
+ on<K extends keyof RouterEvents>(event: K, handler: RouterEvents[K]): void;
76
+ /**
77
+ * Emit an event.
78
+ */
79
+ private emit;
80
+ /**
81
+ * Load a route module and parse its configuration.
82
+ */
83
+ loadModule(route: Route): Promise<void>;
84
+ /**
85
+ * Find parent route for a given route.
86
+ */
87
+ private findParentRoute;
88
+ /**
89
+ * Get route configuration (loads module if needed).
90
+ */
91
+ getRouteConfig(route: Route): Promise<RouteConfig | undefined>;
92
+ /**
93
+ * Get all routes with their configurations loaded.
94
+ * Only loads modules for routes that don't already have configs.
95
+ */
96
+ getRoutesWithConfig(): Promise<Route[]>;
97
+ /**
98
+ * Find routes by render mode.
99
+ */
100
+ findRoutesByRenderMode(mode: 'ssg' | 'ssr' | 'csr' | 'json' | 'xml'): Promise<Route[]>;
101
+ /**
102
+ * Find routes that require authentication.
103
+ */
104
+ findProtectedRoutes(): Promise<Route[]>;
105
+ /**
106
+ * Get all prerender paths from routes with SSG config.
107
+ */
108
+ getPrerenderPaths(): Promise<string[]>;
109
+ /**
110
+ * Load all route modules.
111
+ */
112
+ loadAllModules(): Promise<void>;
113
+ }
114
+ /**
115
+ * Create a file-based router.
116
+ */
117
+ export declare function createFileRouter(options?: RouterOptions): FileRouter;
118
+ /**
119
+ * Initialize a file router and discover routes.
120
+ */
121
+ export declare function initFileRouter(options?: RouterOptions): Promise<FileRouter>;
122
+ //# sourceMappingURL=file-router.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-router.d.ts","sourceRoot":"","sources":["../src/file-router.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACrD,O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AAC,IAAI,CAAC;IAuB7C;;OAEG;IACH,OAAO,CAAC,eAAe;IAKvB;;OAEG;IACG,cAAc,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC;IAOpE;;;OAGG;IACG,mBAAmB,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;IAW7C;;OAEG;IACG,sBAAsB,CAAC,IAAI,EAAE,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,CAAC,KAAK,EAAE,CAAC;IAK5F;;OAEG;IACG,mBAAmB,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;IAK7C;;OAEG;IACG,iBAAiB,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAmB5C;;OAEG;IACG,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;CAYtC;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,UAAU,CAEpE;AAED;;GAEG;AACH,wBAAsB,cAAc,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,CAAC,CAIjF"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @ereo/router
3
+ *
4
+ * File-based routing for the EreoJS framework.
5
+ * Supports dynamic routes, catch-all routes, layouts, and route groups.
6
+ */
7
+ export { FileRouter, createFileRouter, initFileRouter, } from './file-router';
8
+ export { parseMiddleware, parseRenderConfig, parseIslandsConfig, parseCacheConfig, parseProgressiveConfig, parseAuthConfig, parseDevConfig, parseVariants, parseRouteConfig, mergeRouteConfigs, } from './route-config';
9
+ export { RouteTree, buildRouteTree, createRouteTree, filePathToUrlPath, } from './route-tree';
10
+ export { RouteMatcher, createMatcher, matchRoute, matchWithLayouts, parsePathSegments, calculateRouteScore, patternToRegex, } from './matcher';
11
+ export { registerMiddleware, getMiddleware, hasMiddleware, unregisterMiddleware, clearMiddlewareRegistry, resolveMiddleware, executeMiddlewareChain, createMiddlewareExecutor, composeMiddleware, when, method, path, createLoggerMiddleware, createCorsMiddleware, createRateLimitMiddleware, createMiddleware, chainMiddleware, registerTypedMiddleware, getTypedMiddleware, validateMiddlewareChain, globToRegex, } from './middleware-chain';
12
+ export type { TypedMiddlewareContext, TypedMiddlewareHandler, TypedMiddleware, MiddlewareChainOptions, } from './middleware-chain';
13
+ export type { MiddlewareHandler, NextFunction, Middleware, MiddlewareReference, AppContext, } from '@ereo/core';
14
+ export { ParamValidationError, validators, validateParams, safeValidateParams, validateSearchParams, createRouteValidator, matchParamPattern, extractParamNames, } from './validation';
15
+ export type { FileRoute, RouteSegment, RouteNode, RouterOptions, MatchResult, RouteRegistry, RouterEvents, } from './types';
16
+ export { ROUTE_SCORE, SPECIAL_FILES, ROUTE_GROUP_PATTERN, DYNAMIC_SEGMENT_PATTERN, CATCH_ALL_PATTERN, OPTIONAL_PATTERN, } from './types';
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EACL,UAAU,EACV,gBAAgB,EAChB,cAAc,GACf,MAAM,eAAe,CAAC;AAGvB,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,eAAe,EACf,cAAc,EACd,aAAa,EACb,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EACL,SAAS,EACT,cAAc,EACd,eAAe,EACf,iBAAiB,GAClB,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,YAAY,EACZ,aAAa,EACb,UAAU,EACV,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACnB,cAAc,GACf,MAAM,WAAW,CAAC;AAGnB,OAAO,EAEL,kBAAkB,EAClB,aAAa,EACb,aAAa,EACb,oBAAoB,EACpB,uBAAuB,EACvB,iBAAiB,E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