@matthesketh/utopia-router 0.5.1 → 0.7.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.cjs CHANGED
@@ -229,7 +229,13 @@ function createRouter(routeTable) {
229
229
  if (cleanup) {
230
230
  cleanup();
231
231
  }
232
- routes = routeTable;
232
+ routes = routeTable.map((r) => {
233
+ if ("pattern" in r && r.pattern instanceof RegExp) {
234
+ return r;
235
+ }
236
+ const { regex, params } = compilePattern(r.path);
237
+ return { ...r, pattern: regex, params };
238
+ });
233
239
  beforeNavigateHooks = [];
234
240
  scrollPositions.clear();
235
241
  navIndex = 0;
package/dist/index.d.cts CHANGED
@@ -22,6 +22,24 @@ interface Route {
22
22
  /** Optional metadata (e.g. page title, auth requirements). */
23
23
  meta?: Record<string, unknown>;
24
24
  }
25
+ /**
26
+ * A simple route config for manual route definitions.
27
+ *
28
+ * Unlike `Route`, this does not require pre-compiled `pattern` and `params`.
29
+ * Pass these to `createRouter()` and they will be compiled automatically.
30
+ */
31
+ interface RouteConfig {
32
+ /** URL pattern like '/users/:id' or '/blog/:slug'. */
33
+ path: string;
34
+ /** Lazy component import — called only when the route is matched. */
35
+ component: () => Promise<Record<string, unknown>>;
36
+ /** Optional layout component that wraps the page. */
37
+ layout?: () => Promise<Record<string, unknown>>;
38
+ /** Optional error boundary component shown when loading fails. */
39
+ error?: () => Promise<Record<string, unknown>>;
40
+ /** Optional metadata (e.g. page title, auth requirements). */
41
+ meta?: Record<string, unknown>;
42
+ }
25
43
  /**
26
44
  * The result of successfully matching a URL against a route.
27
45
  */
@@ -123,7 +141,11 @@ declare const currentRoute: _matthesketh_utopia_core.Signal<RouteMatch | null>;
123
141
  /** Whether a navigation is currently in progress (loading component, etc.). */
124
142
  declare const isNavigating: _matthesketh_utopia_core.Signal<boolean>;
125
143
  /**
126
- * Initialize the router with a compiled route table.
144
+ * Initialize the router with a route table.
145
+ *
146
+ * Accepts either compiled routes (with `pattern` and `params`) or simple
147
+ * route configs (with just `path` and `component`). Simple configs are
148
+ * compiled automatically.
127
149
  *
128
150
  * This must be called once at application startup. It:
129
151
  * 1. Stores the route table
@@ -131,9 +153,9 @@ declare const isNavigating: _matthesketh_utopia_core.Signal<boolean>;
131
153
  * 3. Sets up popstate listener for browser back/forward
132
154
  * 4. Sets up click listener for <a> interception
133
155
  *
134
- * @param routeTable - Array of compiled routes from `buildRouteTable()`
156
+ * @param routeTable - Array of routes or route configs
135
157
  */
136
- declare function createRouter(routeTable: Route[]): void;
158
+ declare function createRouter(routeTable: (Route | RouteConfig)[]): void;
137
159
  /**
138
160
  * Navigate to a new URL.
139
161
  *
@@ -253,4 +275,4 @@ declare function setQueryParams(params: Record<string, string | null>): void;
253
275
  */
254
276
  declare function getRouteParam(name: string): _matthesketh_utopia_core.ReadonlySignal<string | null>;
255
277
 
256
- export { type BeforeNavigateHook, type Route, type RouteMatch, type RouterState, back, beforeNavigate, buildRouteTable, compilePattern, createLink, createRouter, createRouterView, currentRoute, destroy, filePathToRoute, forward, getQueryParam, getRouteParam, isNavigating, matchRoute, navigate, preloadRoute, queryParams, setQueryParam, setQueryParams };
278
+ export { type BeforeNavigateHook, type Route, type RouteConfig, type RouteMatch, type RouterState, back, beforeNavigate, buildRouteTable, compilePattern, createLink, createRouter, createRouterView, currentRoute, destroy, filePathToRoute, forward, getQueryParam, getRouteParam, isNavigating, matchRoute, navigate, preloadRoute, queryParams, setQueryParam, setQueryParams };
package/dist/index.d.ts CHANGED
@@ -22,6 +22,24 @@ interface Route {
22
22
  /** Optional metadata (e.g. page title, auth requirements). */
23
23
  meta?: Record<string, unknown>;
24
24
  }
25
+ /**
26
+ * A simple route config for manual route definitions.
27
+ *
28
+ * Unlike `Route`, this does not require pre-compiled `pattern` and `params`.
29
+ * Pass these to `createRouter()` and they will be compiled automatically.
30
+ */
31
+ interface RouteConfig {
32
+ /** URL pattern like '/users/:id' or '/blog/:slug'. */
33
+ path: string;
34
+ /** Lazy component import — called only when the route is matched. */
35
+ component: () => Promise<Record<string, unknown>>;
36
+ /** Optional layout component that wraps the page. */
37
+ layout?: () => Promise<Record<string, unknown>>;
38
+ /** Optional error boundary component shown when loading fails. */
39
+ error?: () => Promise<Record<string, unknown>>;
40
+ /** Optional metadata (e.g. page title, auth requirements). */
41
+ meta?: Record<string, unknown>;
42
+ }
25
43
  /**
26
44
  * The result of successfully matching a URL against a route.
27
45
  */
@@ -123,7 +141,11 @@ declare const currentRoute: _matthesketh_utopia_core.Signal<RouteMatch | null>;
123
141
  /** Whether a navigation is currently in progress (loading component, etc.). */
124
142
  declare const isNavigating: _matthesketh_utopia_core.Signal<boolean>;
125
143
  /**
126
- * Initialize the router with a compiled route table.
144
+ * Initialize the router with a route table.
145
+ *
146
+ * Accepts either compiled routes (with `pattern` and `params`) or simple
147
+ * route configs (with just `path` and `component`). Simple configs are
148
+ * compiled automatically.
127
149
  *
128
150
  * This must be called once at application startup. It:
129
151
  * 1. Stores the route table
@@ -131,9 +153,9 @@ declare const isNavigating: _matthesketh_utopia_core.Signal<boolean>;
131
153
  * 3. Sets up popstate listener for browser back/forward
132
154
  * 4. Sets up click listener for <a> interception
133
155
  *
134
- * @param routeTable - Array of compiled routes from `buildRouteTable()`
156
+ * @param routeTable - Array of routes or route configs
135
157
  */
136
- declare function createRouter(routeTable: Route[]): void;
158
+ declare function createRouter(routeTable: (Route | RouteConfig)[]): void;
137
159
  /**
138
160
  * Navigate to a new URL.
139
161
  *
@@ -253,4 +275,4 @@ declare function setQueryParams(params: Record<string, string | null>): void;
253
275
  */
254
276
  declare function getRouteParam(name: string): _matthesketh_utopia_core.ReadonlySignal<string | null>;
255
277
 
256
- export { type BeforeNavigateHook, type Route, type RouteMatch, type RouterState, back, beforeNavigate, buildRouteTable, compilePattern, createLink, createRouter, createRouterView, currentRoute, destroy, filePathToRoute, forward, getQueryParam, getRouteParam, isNavigating, matchRoute, navigate, preloadRoute, queryParams, setQueryParam, setQueryParams };
278
+ export { type BeforeNavigateHook, type Route, type RouteConfig, type RouteMatch, type RouterState, back, beforeNavigate, buildRouteTable, compilePattern, createLink, createRouter, createRouterView, currentRoute, destroy, filePathToRoute, forward, getQueryParam, getRouteParam, isNavigating, matchRoute, navigate, preloadRoute, queryParams, setQueryParam, setQueryParams };
package/dist/index.js CHANGED
@@ -184,7 +184,13 @@ function createRouter(routeTable) {
184
184
  if (cleanup) {
185
185
  cleanup();
186
186
  }
187
- routes = routeTable;
187
+ routes = routeTable.map((r) => {
188
+ if ("pattern" in r && r.pattern instanceof RegExp) {
189
+ return r;
190
+ }
191
+ const { regex, params } = compilePattern(r.path);
192
+ return { ...r, pattern: regex, params };
193
+ });
188
194
  beforeNavigateHooks = [];
189
195
  scrollPositions.clear();
190
196
  navIndex = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matthesketh/utopia-router",
3
- "version": "0.5.1",
3
+ "version": "0.7.1",
4
4
  "description": "File-based routing for UtopiaJS",
5
5
  "type": "module",
6
6
  "license": "MIT",