@matthesketh/utopia-router 0.3.1 → 0.4.0

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
@@ -31,9 +31,14 @@ __export(index_exports, {
31
31
  destroy: () => destroy,
32
32
  filePathToRoute: () => filePathToRoute,
33
33
  forward: () => forward,
34
+ getQueryParam: () => getQueryParam,
35
+ getRouteParam: () => getRouteParam,
34
36
  isNavigating: () => isNavigating,
35
37
  matchRoute: () => matchRoute,
36
- navigate: () => navigate
38
+ navigate: () => navigate,
39
+ queryParams: () => queryParams,
40
+ setQueryParam: () => setQueryParam,
41
+ setQueryParams: () => setQueryParams
37
42
  });
38
43
  module.exports = __toCommonJS(index_exports);
39
44
 
@@ -570,6 +575,54 @@ function clearContainer(container) {
570
575
  function escapeHtml(str) {
571
576
  return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#x27;");
572
577
  }
578
+
579
+ // src/query.ts
580
+ var import_utopia_core3 = require("@matthesketh/utopia-core");
581
+ var queryParams = (0, import_utopia_core3.computed)(() => {
582
+ const match = currentRoute();
583
+ if (!match) return {};
584
+ const result = {};
585
+ match.url.searchParams.forEach((value, key) => {
586
+ result[key] = value;
587
+ });
588
+ return result;
589
+ });
590
+ function getQueryParam(name) {
591
+ return (0, import_utopia_core3.computed)(() => {
592
+ const match = currentRoute();
593
+ if (!match) return null;
594
+ return match.url.searchParams.get(name);
595
+ });
596
+ }
597
+ function setQueryParam(name, value) {
598
+ if (typeof window === "undefined") return;
599
+ const url = new URL(window.location.href);
600
+ if (value === null) {
601
+ url.searchParams.delete(name);
602
+ } else {
603
+ url.searchParams.set(name, value);
604
+ }
605
+ navigate(url.pathname + url.search + url.hash, { replace: true });
606
+ }
607
+ function setQueryParams(params) {
608
+ if (typeof window === "undefined") return;
609
+ const url = new URL(window.location.href);
610
+ for (const [key, value] of Object.entries(params)) {
611
+ if (value === null) {
612
+ url.searchParams.delete(key);
613
+ } else {
614
+ url.searchParams.set(key, value);
615
+ }
616
+ }
617
+ navigate(url.pathname + url.search + url.hash, { replace: true });
618
+ }
619
+ function getRouteParam(name) {
620
+ return (0, import_utopia_core3.computed)(() => {
621
+ const match = currentRoute();
622
+ if (!match) return null;
623
+ return match.params[name] ?? null;
624
+ });
625
+ }
573
626
  // Annotate the CommonJS export names for ESM import in node:
574
627
  0 && (module.exports = {
575
628
  back,
@@ -583,7 +636,12 @@ function escapeHtml(str) {
583
636
  destroy,
584
637
  filePathToRoute,
585
638
  forward,
639
+ getQueryParam,
640
+ getRouteParam,
586
641
  isNavigating,
587
642
  matchRoute,
588
- navigate
643
+ navigate,
644
+ queryParams,
645
+ setQueryParam,
646
+ setQueryParams
589
647
  });
package/dist/index.d.cts CHANGED
@@ -214,4 +214,29 @@ declare function createLink(props: {
214
214
  activeClass?: string;
215
215
  }): HTMLAnchorElement;
216
216
 
217
- export { type BeforeNavigateHook, type Route, type RouteMatch, type RouterState, back, beforeNavigate, buildRouteTable, compilePattern, createLink, createRouter, createRouterView, currentRoute, destroy, filePathToRoute, forward, isNavigating, matchRoute, navigate };
217
+ /**
218
+ * Reactive computed signal returning all current query parameters as a plain object.
219
+ */
220
+ declare const queryParams: _matthesketh_utopia_core.ReadonlySignal<Record<string, string>>;
221
+ /**
222
+ * Returns a computed signal for a specific query parameter.
223
+ * Returns null if the parameter is not present.
224
+ */
225
+ declare function getQueryParam(name: string): _matthesketh_utopia_core.ReadonlySignal<string | null>;
226
+ /**
227
+ * Update a single query parameter and navigate (replace mode).
228
+ * Pass null to remove the parameter.
229
+ */
230
+ declare function setQueryParam(name: string, value: string | null): void;
231
+ /**
232
+ * Update multiple query parameters in a single navigation.
233
+ * Pass null for a value to remove that parameter.
234
+ */
235
+ declare function setQueryParams(params: Record<string, string | null>): void;
236
+ /**
237
+ * Returns a computed signal for a specific route path parameter.
238
+ * Returns null if the parameter is not present.
239
+ */
240
+ declare function getRouteParam(name: string): _matthesketh_utopia_core.ReadonlySignal<string | null>;
241
+
242
+ 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, queryParams, setQueryParam, setQueryParams };
package/dist/index.d.ts CHANGED
@@ -214,4 +214,29 @@ declare function createLink(props: {
214
214
  activeClass?: string;
215
215
  }): HTMLAnchorElement;
216
216
 
217
- export { type BeforeNavigateHook, type Route, type RouteMatch, type RouterState, back, beforeNavigate, buildRouteTable, compilePattern, createLink, createRouter, createRouterView, currentRoute, destroy, filePathToRoute, forward, isNavigating, matchRoute, navigate };
217
+ /**
218
+ * Reactive computed signal returning all current query parameters as a plain object.
219
+ */
220
+ declare const queryParams: _matthesketh_utopia_core.ReadonlySignal<Record<string, string>>;
221
+ /**
222
+ * Returns a computed signal for a specific query parameter.
223
+ * Returns null if the parameter is not present.
224
+ */
225
+ declare function getQueryParam(name: string): _matthesketh_utopia_core.ReadonlySignal<string | null>;
226
+ /**
227
+ * Update a single query parameter and navigate (replace mode).
228
+ * Pass null to remove the parameter.
229
+ */
230
+ declare function setQueryParam(name: string, value: string | null): void;
231
+ /**
232
+ * Update multiple query parameters in a single navigation.
233
+ * Pass null for a value to remove that parameter.
234
+ */
235
+ declare function setQueryParams(params: Record<string, string | null>): void;
236
+ /**
237
+ * Returns a computed signal for a specific route path parameter.
238
+ * Returns null if the parameter is not present.
239
+ */
240
+ declare function getRouteParam(name: string): _matthesketh_utopia_core.ReadonlySignal<string | null>;
241
+
242
+ 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, queryParams, setQueryParam, setQueryParams };
package/dist/index.js CHANGED
@@ -531,6 +531,54 @@ function clearContainer(container) {
531
531
  function escapeHtml(str) {
532
532
  return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#x27;");
533
533
  }
534
+
535
+ // src/query.ts
536
+ import { computed } from "@matthesketh/utopia-core";
537
+ var queryParams = computed(() => {
538
+ const match = currentRoute();
539
+ if (!match) return {};
540
+ const result = {};
541
+ match.url.searchParams.forEach((value, key) => {
542
+ result[key] = value;
543
+ });
544
+ return result;
545
+ });
546
+ function getQueryParam(name) {
547
+ return computed(() => {
548
+ const match = currentRoute();
549
+ if (!match) return null;
550
+ return match.url.searchParams.get(name);
551
+ });
552
+ }
553
+ function setQueryParam(name, value) {
554
+ if (typeof window === "undefined") return;
555
+ const url = new URL(window.location.href);
556
+ if (value === null) {
557
+ url.searchParams.delete(name);
558
+ } else {
559
+ url.searchParams.set(name, value);
560
+ }
561
+ navigate(url.pathname + url.search + url.hash, { replace: true });
562
+ }
563
+ function setQueryParams(params) {
564
+ if (typeof window === "undefined") return;
565
+ const url = new URL(window.location.href);
566
+ for (const [key, value] of Object.entries(params)) {
567
+ if (value === null) {
568
+ url.searchParams.delete(key);
569
+ } else {
570
+ url.searchParams.set(key, value);
571
+ }
572
+ }
573
+ navigate(url.pathname + url.search + url.hash, { replace: true });
574
+ }
575
+ function getRouteParam(name) {
576
+ return computed(() => {
577
+ const match = currentRoute();
578
+ if (!match) return null;
579
+ return match.params[name] ?? null;
580
+ });
581
+ }
534
582
  export {
535
583
  back,
536
584
  beforeNavigate,
@@ -543,7 +591,12 @@ export {
543
591
  destroy,
544
592
  filePathToRoute,
545
593
  forward,
594
+ getQueryParam,
595
+ getRouteParam,
546
596
  isNavigating,
547
597
  matchRoute,
548
- navigate
598
+ navigate,
599
+ queryParams,
600
+ setQueryParam,
601
+ setQueryParams
549
602
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matthesketh/utopia-router",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "File-based routing for UtopiaJS",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -40,8 +40,8 @@
40
40
  "dist"
41
41
  ],
42
42
  "dependencies": {
43
- "@matthesketh/utopia-core": "0.3.1",
44
- "@matthesketh/utopia-runtime": "0.3.1"
43
+ "@matthesketh/utopia-core": "0.4.0",
44
+ "@matthesketh/utopia-runtime": "0.4.0"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "tsup src/index.ts --format esm,cjs --dts",