@matthesketh/utopia-router 0.7.0 → 0.7.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.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.7.0",
3
+ "version": "0.7.2",
4
4
  "description": "File-based routing for UtopiaJS",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -39,12 +39,12 @@
39
39
  "files": [
40
40
  "dist"
41
41
  ],
42
- "dependencies": {
43
- "@matthesketh/utopia-core": "0.7.0",
44
- "@matthesketh/utopia-runtime": "0.7.0"
45
- },
46
42
  "scripts": {
47
43
  "build": "tsup src/index.ts --format esm,cjs --dts",
48
44
  "dev": "tsup src/index.ts --format esm,cjs --dts --watch"
45
+ },
46
+ "dependencies": {
47
+ "@matthesketh/utopia-core": "^0.7.0",
48
+ "@matthesketh/utopia-runtime": "^0.7.0"
49
49
  }
50
- }
50
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Matt Hesketh
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.