stx-router 0.2.149 → 0.2.151

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
@@ -23,7 +23,7 @@ export type {
23
23
  // File-based router
24
24
  export { createRouter, findErrorPage, formatRoutes, Router } from './file-router';
25
25
  // Route pattern matching
26
- export { filePathToPattern, matchRoute, patternToRegex } from './matcher';
26
+ export { bracketPathToRegex, filePathToPattern, matchRoute, patternToRegex } from './matcher';
27
27
  // Named routes
28
28
  export { defineRoute, defineRoutes, resetRoutes, route, setAppUrl } from './named-routes';
29
29
  // Middleware system
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export { createRouter, findErrorPage, formatRoutes, Router } from "./file-router.js";
2
- export { filePathToPattern, matchRoute, patternToRegex } from "./matcher.js";
2
+ export { bracketPathToRegex, filePathToPattern, matchRoute, patternToRegex } from "./matcher.js";
3
3
  export { defineRoute, defineRoutes, resetRoutes, route, setAppUrl } from "./named-routes.js";
4
4
  export {
5
5
  abortNavigation,
package/dist/matcher.d.ts CHANGED
@@ -17,3 +17,22 @@ export declare function filePathToPattern(filePath: string, pagesDir: string): s
17
17
  */
18
18
  export declare function patternToRegex(pattern: string): { regex: RegExp, params: string[] };
19
19
  export declare function matchRoute(pathname: string, routes: { pattern: string, regex: RegExp, params: string[] }[]): { route: typeof routes[0], params: Record<string, string> } | null;
20
+ /**
21
+ * Compile a *file-style* route path - the one with `[param]` brackets - into a
22
+ * regex and its parameter names.
23
+ *
24
+ * File-based routers keep re-deriving this by hand, and the hand-rolled version
25
+ * is always the same two lines:
26
+ *
27
+ * paramNames = [...p.matchAll(/\[([^\]]+)\]/g)].map(m => m[1])
28
+ * regex = p.replace(/\[([^\]]+)\]/g, '([^/]+)')
29
+ *
30
+ * which is correct for `[id]` and wrong for `[...path]` in two ways at once:
31
+ * the name keeps its dots, so a lookup by `path` finds nothing, and the segment
32
+ * compiles to `([^/]+)`, which cannot span a separator, so any multi-segment
33
+ * URL simply fails to match. Both symptoms, one cause, and neither is obvious
34
+ * from the outside - the route just quietly does not exist.
35
+ *
36
+ * Exported so nothing has to write those two lines again.
37
+ */
38
+ export declare function bracketPathToRegex(routePath: string): { regex: RegExp, params: string[] };
package/dist/matcher.js CHANGED
@@ -55,3 +55,7 @@ export function matchRoute(pathname, routes) {
55
55
  }
56
56
  return null;
57
57
  }
58
+ export function bracketPathToRegex(routePath) {
59
+ const pattern = routePath.replace(/\[\[([^\]]+)\]\]/g, ":$1?").replace(/\[([^\]]+)\]/g, ":$1").replace(/:\.\.\.([^/]+)/g, ":$1*");
60
+ return patternToRegex(pattern);
61
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "stx-router",
3
3
  "type": "module",
4
- "version": "0.2.149",
4
+ "version": "0.2.151",
5
5
  "description": "File-based router for STX — .stx template discovery, nested layouts, typed route params, middleware, and client-side SPA navigation.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",