@solidjs/router 0.15.4 → 0.16.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.
@@ -1,7 +1,7 @@
1
1
  import { $TRACK, createMemo, createSignal, onCleanup, getOwner } from "solid-js";
2
2
  import { isServer } from "solid-js/web";
3
3
  import { useRouter } from "../routing.js";
4
- import { mockBase } from "../utils.js";
4
+ import { mockBase, setFunctionName } from "../utils.js";
5
5
  import { cacheKeyOp, hashKey, revalidate, query } from "./query.js";
6
6
  export const actions = /* #__PURE__ */ new Map();
7
7
  export function useSubmissions(fn, filter) {
@@ -53,7 +53,7 @@ export function action(fn, options = {}) {
53
53
  error: result?.error,
54
54
  pending: false,
55
55
  retry() {
56
- return retry = submission.retry();
56
+ return (retry = submission.retry());
57
57
  }
58
58
  });
59
59
  if (retry)
@@ -93,10 +93,11 @@ export function action(fn, options = {}) {
93
93
  return p.then(handler(), handler(true));
94
94
  }
95
95
  const o = typeof options === "string" ? { name: options } : options;
96
- const url = fn.url ||
97
- (o.name && `https://action/${o.name}`) ||
98
- (!isServer ? `https://action/${hashString(fn.toString())}` : "");
96
+ const name = o.name || (!isServer ? String(hashString(fn.toString())) : undefined);
97
+ const url = fn.url || (name && `https://action/${name}`) || "";
99
98
  mutate.base = url;
99
+ if (name)
100
+ setFunctionName(mutate, name);
100
101
  return toAction(mutate, url);
101
102
  }
102
103
  function toAction(fn, url) {
@@ -4,11 +4,14 @@
4
4
  import { createResource, sharedConfig, untrack, catchError } from "solid-js";
5
5
  import { createStore, reconcile, unwrap } from "solid-js/store";
6
6
  import { isServer } from "solid-js/web";
7
+ import { setFunctionName } from "../utils.js";
7
8
  export function createAsync(fn, options) {
8
9
  let resource;
9
10
  let prev = () => !resource || resource.state === "unresolved" ? undefined : resource.latest;
10
11
  [resource] = createResource(() => subFetch(fn, catchError(() => untrack(prev), () => undefined)), v => v, options);
11
12
  const resultAccessor = (() => resource());
13
+ if (options?.name)
14
+ setFunctionName(resultAccessor, options.name);
12
15
  Object.defineProperty(resultAccessor, "latest", {
13
16
  get() {
14
17
  return resource.latest;
@@ -1,2 +1,9 @@
1
1
  import type { RouterContext } from "../types.js";
2
- export declare function setupNativeEvents(preload?: boolean, explicitLinks?: boolean, actionBase?: string, transformUrl?: (url: string) => string): (router: RouterContext) => void;
2
+ type NativeEventConfig = {
3
+ preload?: boolean;
4
+ explicitLinks?: boolean;
5
+ actionBase?: string;
6
+ transformUrl?: (url: string) => string;
7
+ };
8
+ export declare function setupNativeEvents({ preload, explicitLinks, actionBase, transformUrl }?: NativeEventConfig): (router: RouterContext) => void;
9
+ export {};
@@ -2,7 +2,7 @@ import { delegateEvents } from "solid-js/web";
2
2
  import { onCleanup } from "solid-js";
3
3
  import { actions } from "./action.js";
4
4
  import { mockBase } from "../utils.js";
5
- export function setupNativeEvents(preload = true, explicitLinks = false, actionBase = "/_server", transformUrl) {
5
+ export function setupNativeEvents({ preload = true, explicitLinks = false, actionBase = "/_server", transformUrl } = {}) {
6
6
  return (router) => {
7
7
  const basePath = router.base.path();
8
8
  const navigateFromRoute = router.navigatorFactory(router.base);
@@ -65,7 +65,7 @@ export function setupNativeEvents(preload = true, explicitLinks = false, actionB
65
65
  clearTimeout(preloadTimeout);
66
66
  const res = handleAnchor(evt);
67
67
  if (!res)
68
- return lastElement = null;
68
+ return (lastElement = null);
69
69
  const [a, url] = res;
70
70
  if (lastElement === a)
71
71
  return;
@@ -1,4 +1,4 @@
1
- import type { RouterResponseInit, CustomResponse } from "../types";
1
+ import type { RouterResponseInit, CustomResponse } from "../types.js";
2
2
  export declare function redirect(url: string, init?: number | RouterResponseInit): CustomResponse<never>;
3
3
  export declare function reload(init?: RouterResponseInit): CustomResponse<never>;
4
4
  export declare function json<T>(data: T, init?: RouterResponseInit): CustomResponse<T>;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from "./routers/index.js";
2
2
  export * from "./components.jsx";
3
3
  export * from "./lifecycle.js";
4
- export { useHref, useIsRouting, useLocation, useMatch, useCurrentMatches, useNavigate, useParams, useResolvedPath, useSearchParams, useBeforeLeave, usePreloadRoute } from "./routing.js";
4
+ export { useHref, useIsRouting, useLocation, useMatch, useCurrentMatches, useNavigate, useParams, useResolvedPath, useSearchParams, useBeforeLeave, usePreloadRoute, RouterContextObj as RouterContext } from "./routing.js";
5
5
  export { mergeSearchString as _mergeSearchString } from "./utils.js";
6
6
  export * from "./data/index.js";
7
7
  export type { Location, LocationChange, SearchParams, MatchFilter, MatchFilters, NavigateOptions, Navigator, OutputMatch, Params, PathMatch, RouteSectionProps, RoutePreloadFunc, RoutePreloadFuncArgs, RouteDefinition, RouteDescription, RouteMatch, RouterIntegration, RouterUtils, SetParams, Submission, BeforeLeaveEventArgs, RouteLoadFunc, RouteLoadFuncArgs, RouterResponseInit, CustomResponse } from "./types.js";
package/dist/index.js CHANGED
@@ -237,8 +237,18 @@ function expandOptionals(pattern) {
237
237
  }
238
238
  return expandOptionals(suffix).reduce((results, expansion) => [...results, ...prefixes.map(p => p + expansion)], []);
239
239
  }
240
+ function setFunctionName(obj, value) {
241
+ Object.defineProperty(obj, "name", {
242
+ value,
243
+ writable: false,
244
+ configurable: false
245
+ });
246
+ return obj;
247
+ }
240
248
 
241
249
  const MAX_REDIRECTS = 100;
250
+
251
+ /** Consider this API opaque and internal. It is likely to change in the future. */
242
252
  const RouterContextObj = createContext();
243
253
  const RouteContextObj = createContext();
244
254
  const useRouter = () => invariant(useContext(RouterContextObj), "<A> and 'use' router primitives can be only used inside a Route.");
@@ -1323,8 +1333,10 @@ function action(fn, options = {}) {
1323
1333
  const o = typeof options === "string" ? {
1324
1334
  name: options
1325
1335
  } : options;
1326
- const url = fn.url || o.name && `https://action/${o.name}` || (!isServer ? `https://action/${hashString(fn.toString())}` : "");
1336
+ const name = o.name || (!isServer ? String(hashString(fn.toString())) : undefined);
1337
+ const url = fn.url || name && `https://action/${name}` || "";
1327
1338
  mutate.base = url;
1339
+ if (name) setFunctionName(mutate, name);
1328
1340
  return toAction(mutate, url);
1329
1341
  }
1330
1342
  function toAction(fn, url) {
@@ -1386,7 +1398,12 @@ async function handleResponse(response, error, navigate) {
1386
1398
  } : undefined;
1387
1399
  }
1388
1400
 
1389
- function setupNativeEvents(preload = true, explicitLinks = false, actionBase = "/_server", transformUrl) {
1401
+ function setupNativeEvents({
1402
+ preload = true,
1403
+ explicitLinks = false,
1404
+ actionBase = "/_server",
1405
+ transformUrl
1406
+ } = {}) {
1390
1407
  return router => {
1391
1408
  const basePath = router.base.path();
1392
1409
  const navigateFromRoute = router.navigatorFactory(router.base);
@@ -1528,7 +1545,12 @@ function Router(props) {
1528
1545
  });
1529
1546
  }
1530
1547
  })),
1531
- create: setupNativeEvents(props.preload, props.explicitLinks, props.actionBase, props.transformUrl),
1548
+ create: setupNativeEvents({
1549
+ preload: props.preload,
1550
+ explicitLinks: props.explicitLinks,
1551
+ actionBase: props.actionBase,
1552
+ transformUrl: props.transformUrl
1553
+ }),
1532
1554
  utils: {
1533
1555
  go: delta => window.history.go(delta),
1534
1556
  beforeLeave
@@ -1569,7 +1591,11 @@ function HashRouter(props) {
1569
1591
  saveCurrentDepth();
1570
1592
  },
1571
1593
  init: notify => bindEvent(window, "hashchange", notifyIfNotBlocked(notify, delta => !beforeLeave.confirm(delta && delta < 0 ? delta : getSource()))),
1572
- create: setupNativeEvents(props.preload, props.explicitLinks, props.actionBase),
1594
+ create: setupNativeEvents({
1595
+ preload: props.preload,
1596
+ explicitLinks: props.explicitLinks,
1597
+ actionBase: props.actionBase
1598
+ }),
1573
1599
  utils: {
1574
1600
  go: delta => window.history.go(delta),
1575
1601
  renderPath: path => `#${path}`,
@@ -1631,7 +1657,11 @@ function MemoryRouter(props) {
1631
1657
  get: memoryHistory.get,
1632
1658
  set: memoryHistory.set,
1633
1659
  init: memoryHistory.listen,
1634
- create: setupNativeEvents(props.preload, props.explicitLinks, props.actionBase),
1660
+ create: setupNativeEvents({
1661
+ preload: props.preload,
1662
+ explicitLinks: props.explicitLinks,
1663
+ actionBase: props.actionBase
1664
+ }),
1635
1665
  utils: {
1636
1666
  go: memoryHistory.go
1637
1667
  }
@@ -1715,6 +1745,7 @@ function createAsync(fn, options) {
1715
1745
  let prev = () => !resource || resource.state === "unresolved" ? undefined : resource.latest;
1716
1746
  [resource] = createResource(() => subFetch(fn, catchError(() => untrack(prev), () => undefined)), v => v, options);
1717
1747
  const resultAccessor = () => resource();
1748
+ if (options?.name) setFunctionName(resultAccessor, options.name);
1718
1749
  Object.defineProperty(resultAccessor, "latest", {
1719
1750
  get() {
1720
1751
  return resource.latest;
@@ -1845,4 +1876,4 @@ function json(data, init = {}) {
1845
1876
  return response;
1846
1877
  }
1847
1878
 
1848
- export { A, HashRouter, MemoryRouter, Navigate, Route, Router, StaticRouter, mergeSearchString as _mergeSearchString, action, cache, createAsync, createAsyncStore, createBeforeLeave, createMemoryHistory, createRouter, json, keepDepth, notifyIfNotBlocked, query, redirect, reload, revalidate, saveCurrentDepth, useAction, useBeforeLeave, useCurrentMatches, useHref, useIsRouting, useLocation, useMatch, useNavigate, useParams, usePreloadRoute, useResolvedPath, useSearchParams, useSubmission, useSubmissions };
1879
+ export { A, HashRouter, MemoryRouter, Navigate, Route, Router, RouterContextObj as RouterContext, StaticRouter, mergeSearchString as _mergeSearchString, action, cache, createAsync, createAsyncStore, createBeforeLeave, createMemoryHistory, createRouter, json, keepDepth, notifyIfNotBlocked, query, redirect, reload, revalidate, saveCurrentDepth, useAction, useBeforeLeave, useCurrentMatches, useHref, useIsRouting, useLocation, useMatch, useNavigate, useParams, usePreloadRoute, useResolvedPath, useSearchParams, useSubmission, useSubmissions };
package/dist/index.jsx CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from "./routers/index.js";
2
2
  export * from "./components.jsx";
3
3
  export * from "./lifecycle.js";
4
- export { useHref, useIsRouting, useLocation, useMatch, useCurrentMatches, useNavigate, useParams, useResolvedPath, useSearchParams, useBeforeLeave, usePreloadRoute } from "./routing.js";
4
+ export { useHref, useIsRouting, useLocation, useMatch, useCurrentMatches, useNavigate, useParams, useResolvedPath, useSearchParams, useBeforeLeave, usePreloadRoute, RouterContextObj as RouterContext } from "./routing.js";
5
5
  export { mergeSearchString as _mergeSearchString } from "./utils.js";
6
6
  export * from "./data/index.js";
@@ -1,5 +1,5 @@
1
1
  import type { JSX } from "solid-js";
2
- import type { BaseRouterProps } from "./components.js";
2
+ import type { BaseRouterProps } from "./components.jsx";
3
3
  export declare function hashParser(str: string): string;
4
4
  export type HashRouterProps = BaseRouterProps & {
5
5
  actionBase?: string;
@@ -30,7 +30,7 @@ export function HashRouter(props) {
30
30
  saveCurrentDepth();
31
31
  },
32
32
  init: notify => bindEvent(window, "hashchange", notifyIfNotBlocked(notify, delta => !beforeLeave.confirm(delta && delta < 0 ? delta : getSource()))),
33
- create: setupNativeEvents(props.preload, props.explicitLinks, props.actionBase),
33
+ create: setupNativeEvents({ preload: props.preload, explicitLinks: props.explicitLinks, actionBase: props.actionBase }),
34
34
  utils: {
35
35
  go: delta => window.history.go(delta),
36
36
  renderPath: path => `#${path}`,
@@ -49,7 +49,7 @@ export function MemoryRouter(props) {
49
49
  get: memoryHistory.get,
50
50
  set: memoryHistory.set,
51
51
  init: memoryHistory.listen,
52
- create: setupNativeEvents(props.preload, props.explicitLinks, props.actionBase),
52
+ create: setupNativeEvents({ preload: props.preload, explicitLinks: props.explicitLinks, actionBase: props.actionBase }),
53
53
  utils: {
54
54
  go: memoryHistory.go
55
55
  }
@@ -36,7 +36,7 @@ export function Router(props) {
36
36
  return !beforeLeave.confirm(s.value, { state: s.state });
37
37
  }
38
38
  })),
39
- create: setupNativeEvents(props.preload, props.explicitLinks, props.actionBase, props.transformUrl),
39
+ create: setupNativeEvents({ preload: props.preload, explicitLinks: props.explicitLinks, actionBase: props.actionBase, transformUrl: props.transformUrl }),
40
40
  utils: {
41
41
  go: delta => window.history.go(delta),
42
42
  beforeLeave
@@ -1,4 +1,4 @@
1
- import type { LocationChange, RouterContext, RouterUtils } from "../types.ts";
1
+ import type { LocationChange, RouterContext, RouterUtils } from "../types.js";
2
2
  export declare function createRouter(config: {
3
3
  get: () => string | LocationChange;
4
4
  set: (next: LocationChange) => void;
package/dist/routing.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import { JSX, Accessor } from "solid-js";
2
2
  import type { BeforeLeaveEventArgs, Branch, Intent, Location, MatchFilters, NavigateOptions, Navigator, Params, RouteDescription, RouteContext, RouteDefinition, RouteMatch, RouterContext, RouterIntegration, SearchParams, SetSearchParams } from "./types.js";
3
+ /** Consider this API opaque and internal. It is likely to change in the future. */
3
4
  export declare const RouterContextObj: import("solid-js").Context<RouterContext | undefined>;
4
5
  export declare const RouteContextObj: import("solid-js").Context<RouteContext | undefined>;
5
6
  export declare const useRouter: () => RouterContext;
6
7
  export declare const useRoute: () => RouteContext;
7
8
  export declare const useResolvedPath: (path: () => string) => Accessor<string | undefined>;
8
- export declare const useHref: (to: () => string | undefined) => Accessor<string | undefined>;
9
+ export declare const useHref: <T extends string | undefined>(to: () => T) => () => string | T;
9
10
  /**
10
11
  * Retrieves method to do navigation. The method accepts a path to navigate to and an optional object with the following options:
11
12
  *
package/dist/routing.js CHANGED
@@ -4,6 +4,7 @@ import { isServer, getRequestEvent } from "solid-js/web";
4
4
  import { createBeforeLeave } from "./lifecycle.js";
5
5
  import { mockBase, createMemoObject, extractSearchParams, invariant, resolvePath, createMatcher, joinPaths, scoreRoute, mergeSearchString, expandOptionals } from "./utils.js";
6
6
  const MAX_REDIRECTS = 100;
7
+ /** Consider this API opaque and internal. It is likely to change in the future. */
7
8
  export const RouterContextObj = createContext();
8
9
  export const RouteContextObj = createContext();
9
10
  export const useRouter = () => invariant(useContext(RouterContextObj), "<A> and 'use' router primitives can be only used inside a Route.");
package/dist/types.d.ts CHANGED
@@ -42,7 +42,7 @@ export interface NavigateOptions<S = unknown> {
42
42
  state: S;
43
43
  }
44
44
  export interface Navigator {
45
- (to: string, options?: Partial<NavigateOptions>): void;
45
+ (to: string | number, options?: Partial<NavigateOptions>): void;
46
46
  (delta: number): void;
47
47
  }
48
48
  export type NavigatorFactory = (route?: RouteContext) => Navigator;
package/dist/utils.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { MatchFilters, PathMatch, RouteDescription, SearchParams, SetSearchParams } from "./types.ts";
1
+ import type { MatchFilters, PathMatch, RouteDescription, SearchParams, SetSearchParams } from "./types.js";
2
2
  export declare const mockBase = "http://sr";
3
3
  export declare function normalizePath(path: string, omitSlash?: boolean): string;
4
4
  export declare function resolvePath(base: string, path: string, from?: string): string | undefined;
@@ -10,3 +10,4 @@ export declare function scoreRoute(route: RouteDescription): number;
10
10
  export declare function createMemoObject<T extends Record<string | symbol, unknown>>(fn: () => T): T;
11
11
  export declare function mergeSearchString(search: string, params: SetSearchParams): string;
12
12
  export declare function expandOptionals(pattern: string): string[];
13
+ export declare function setFunctionName<T>(obj: T, value: string): T;
package/dist/utils.js CHANGED
@@ -175,3 +175,11 @@ export function expandOptionals(pattern) {
175
175
  }
176
176
  return expandOptionals(suffix).reduce((results, expansion) => [...results, ...prefixes.map(p => p + expansion)], []);
177
177
  }
178
+ export function setFunctionName(obj, value) {
179
+ Object.defineProperty(obj, "name", {
180
+ value,
181
+ writable: false,
182
+ configurable: false
183
+ });
184
+ return obj;
185
+ }
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "Ryan Turnquist"
7
7
  ],
8
8
  "license": "MIT",
9
- "version": "0.15.4",
9
+ "version": "0.16.1",
10
10
  "homepage": "https://github.com/solidjs/solid-router#readme",
11
11
  "repository": {
12
12
  "type": "git",
@@ -51,7 +51,7 @@
51
51
  "solid-js": "^1.8.6"
52
52
  },
53
53
  "scripts": {
54
- "build": "tsc && rollup -c",
54
+ "build": "rm -rf dist && tsc && rollup -c",
55
55
  "test": "vitest run && npm run test:types",
56
56
  "test:watch": "vitest",
57
57
  "test:types": "tsc --project tsconfig.test.json",