clear-react-router 1.0.13 → 1.0.16

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,4 +1,4 @@
1
- import type { BlockerState, Location, RouteItem, UpdateBlockedRouteProps } from '../types/global';
1
+ import type { BlockerState, Location, RouteItem, UpdateBlockedRouteProps } from '../types/global.ts';
2
2
  type UseHandleNavigation = {
3
3
  routeList: RouteItem[];
4
4
  setLocation: (arg: Location) => void;
package/dist/index.js CHANGED
@@ -199,29 +199,24 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
199
199
  children: e
200
200
  })
201
201
  })
202
- }), v = (e) => {
203
- let t = s(e);
204
- return a(() => {
205
- t.current = e;
206
- }, [e]), t;
207
- }, y = (t, r) => {
202
+ }), v = (t, r) => {
208
203
  let i = n(() => t().then((e) => ({ default: e.default || e })));
209
204
  return () => /* @__PURE__ */ (0, g.jsx)(e, {
210
205
  fallback: typeof r == "function" ? r() : r || null,
211
206
  children: /* @__PURE__ */ (0, g.jsx)(i, {})
212
207
  });
213
- }, b = (e) => typeof e.element == "function" && e.element.toString().includes("import("), x = (e, t = [], n = "") => {
208
+ }, y = (e) => typeof e.element == "function" && e.element.toString().includes("import("), b = (e, t = [], n = "") => {
214
209
  let r = e.path.match(/:[^/]+/g), i = e.path.replaceAll(/:[^/]+(\/|$)/g, "").split("/").filter(Boolean), a = e.path.split("/"), o = r ? [...t, ...r.map((e, t) => ({
215
210
  key: i[t],
216
211
  value: e.slice(1)
217
- }))] : t, s = o.length ? `${n}${a.slice(0, a.length - 1).join("/")}` : e.path, c = b(e) ? y(e.element, e.fallback) : e.element;
212
+ }))] : t, s = o.length ? `${n}${a.slice(0, a.length - 1).join("/")}` : e.path, c = y(e) ? v(e.element, e.fallback) : e.element;
218
213
  return [{
219
214
  ...e,
220
215
  path: s,
221
216
  params: o,
222
217
  element: c
223
- }, ...e.children?.flatMap((e) => x(e, o, s)) || []];
224
- }, S = (e) => e.flatMap((e) => x(e, [])), C = (e) => {
218
+ }, ...e.children?.flatMap((e) => b(e, o, s)) || []];
219
+ }, x = (e) => e.flatMap((e) => b(e, [])), S = (e) => {
225
220
  let { pathname: t } = window.location, n = t.split("/");
226
221
  return (e || []).map((e) => ({
227
222
  index: n.findIndex((t) => t === e.key),
@@ -230,32 +225,32 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
230
225
  ...e,
231
226
  [t.value]: n[t.index + 1]
232
227
  }), {});
233
- }, w = (e) => ({
228
+ }, C = (e) => ({
234
229
  pathname: e.pathname,
235
230
  search: e.search
236
- }), T = (e, t) => {
231
+ }), w = (e, t) => {
237
232
  let n = e.path.split("/").filter(Boolean), r = e.params ? Object.keys(e.params).length : 0, i = t.split("/").filter(Boolean);
238
233
  return n.every((e, t) => e === i[t + +!!t]) && i.length === n.length + r;
239
- }, E = class {
240
- url;
241
- search;
242
- cause;
243
- constructor(e, t) {
244
- this.url = e, this.search = t, this.cause = "redirect";
245
- }
246
- }, D = (e, t) => Promise.reject(new E(e, t)), O = ({ setLocation: e, routeList: t, context: n, revalidateCache: i }) => {
234
+ }, T = (e) => {
235
+ let t = s(e);
236
+ return a(() => {
237
+ t.current = e;
238
+ }, [e]), t;
239
+ }, E = (e) => typeof e == "object" && !!e && e.cause === "redirect", D = ({ setLocation: e, routeList: t, context: n, revalidateCache: i }) => {
247
240
  let [l, u] = c({
248
241
  from: "",
249
242
  to: ""
250
- }), d = s(""), f = v(r(async (r) => {
243
+ }), d = s(""), f = T(r(async (r) => {
251
244
  try {
252
- let a = t.find((e) => T(e, r.pathname));
253
- console.log("before beforeLoad execution", a, n), a?.beforeLoad && await a?.beforeLoad(n), console.log("after beforeLoad execution", a, n), e(r), console.log(r.pathname !== window.location.pathname), r.pathname !== window.location.pathname && (history.pushState(null, "", r.pathname), d.current = r.pathname), await i(a), a?.afterLoad && await a?.afterLoad(n);
245
+ let a = t.find((e) => w(e, r.pathname));
246
+ console.log("before beforeLoad execution", a), a?.beforeLoad && await a?.beforeLoad(n), console.log("after beforeLoad execution", a), e(r), r.pathname !== window.location.pathname && (history.pushState(null, "", r.pathname), d.current = r.pathname), await i(a), a?.afterLoad && await a?.afterLoad(n);
254
247
  } catch (t) {
255
- if (console.log("redirect", t), !(t instanceof E)) return t;
256
- history.replaceState(null, "", `${t.url}${t.search || ""}`), e({
257
- pathname: t.url,
258
- search: t.search
248
+ console.log("redirect caught", t);
249
+ let n = t;
250
+ if (!E(n)) return n;
251
+ console.log("redirect after type guard"), history.replaceState(null, "", n.url), e({
252
+ pathname: n.url,
253
+ search: n.search
259
254
  });
260
255
  }
261
256
  }, [
@@ -280,7 +275,7 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
280
275
  }, [l.from, f]);
281
276
  return a(() => {
282
277
  let e = async (e) => {
283
- let t = w(e.target.location);
278
+ let t = C(e.target.location);
284
279
  d.current === l.from ? (u({
285
280
  from: d.current,
286
281
  to: t.pathname
@@ -288,14 +283,14 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
288
283
  };
289
284
  return window.addEventListener("popstate", e), () => window.removeEventListener("popstate", e);
290
285
  }, [l.from, f]), a(() => {
291
- let e = w(window.location);
286
+ let e = C(window.location);
292
287
  f.current(e), d.current = e.pathname;
293
288
  }, [f]), {
294
289
  blockerState: o(() => l.from && l.to ? "blocked" : l.from ? "charged" : "unblocked", [l]),
295
290
  updateLocation: m,
296
291
  updateBlockedRoute: p
297
292
  };
298
- }, k = (e) => {
293
+ }, O = (e) => {
299
294
  let [t, n] = c({}), [i, a] = c(!1), [o, l] = c({}), u = s({}), d = r(({ key: e, value: t }) => n((n) => ({
300
295
  ...n,
301
296
  [e]: t
@@ -332,7 +327,7 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
332
327
  }
333
328
  }
334
329
  }, [f, d]), m = r(async (t) => {
335
- let n = e.find((e) => T(e, t));
330
+ let n = e.find((e) => w(e, t));
336
331
  n && await p(n);
337
332
  }, [p, e]);
338
333
  return {
@@ -342,13 +337,13 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
342
337
  revalidateCache: p,
343
338
  isLoading: o[window.location.pathname]
344
339
  };
345
- }, A = (e) => e ? typeof e == "function" ? /* @__PURE__ */ (0, g.jsx)(e, {}) : e : null, j = "error 404. Page not found", M = "*", N = ({ routeList: e, context: t = {} }) => {
346
- let [n, r] = c(w(window.location)), [i, a] = c(t), s = o(() => e.find((e) => e.path === M || T(e, n.pathname)), [n.pathname, e]), { loaderError: l, loaderCache: u, prefetchLoader: d, revalidateCache: f, isLoading: p } = k(e), { blockerState: m, updateLocation: h, updateBlockedRoute: v } = O({
340
+ }, k = (e) => e ? typeof e == "function" ? /* @__PURE__ */ (0, g.jsx)(e, {}) : e : null, A = "error 404. Page not found", j = "*", M = ({ routeList: e, context: t = {} }) => {
341
+ let [n, r] = c(C(window.location)), [i, a] = c(t), s = o(() => e.find((e) => e.path === j || w(e, n.pathname)), [n.pathname, e]), { loaderError: l, loaderCache: u, prefetchLoader: d, revalidateCache: f, isLoading: p } = O(e), { blockerState: m, updateLocation: h, updateBlockedRoute: v } = D({
347
342
  setLocation: r,
348
343
  routeList: e,
349
344
  context: i,
350
345
  revalidateCache: f
351
- }), y = o(() => s?.params ? C(s.params) : {}, [s]), b = o(() => ({
346
+ }), y = o(() => s?.params ? S(s.params) : {}, [s]), b = o(() => ({
352
347
  location: n,
353
348
  updateLocation: h,
354
349
  params: y,
@@ -370,45 +365,45 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
370
365
  ]);
371
366
  return s?.loader && !l && p ? /* @__PURE__ */ (0, g.jsx)(_, {
372
367
  ...b,
373
- children: A(s?.loaderFallback)
368
+ children: k(s?.loaderFallback)
374
369
  }) : l ? /* @__PURE__ */ (0, g.jsx)(_, {
375
370
  ...b,
376
- children: A(s?.errorElement)
371
+ children: k(s?.errorElement)
377
372
  }) : /* @__PURE__ */ (0, g.jsx)(_, {
378
373
  ...b,
379
- children: A(s?.element) || j
374
+ children: k(s?.element) || A
380
375
  });
381
- }, P = () => {
376
+ }, N = () => {
382
377
  let e = i(p);
383
378
  if (!Object.keys(e).length) throw Error("useNavigationState must be used within Router component");
384
379
  return e;
385
- }, F = () => {
380
+ }, P = () => {
386
381
  let e = i(d);
387
382
  if (!Object.keys(e).length) throw Error("useRouterActions must be used within Router component");
388
383
  return e;
389
- }, I = () => {
384
+ }, F = () => {
390
385
  let e = i(f);
391
386
  if (!Object.keys(e).length) throw Error("useRouterData must be used within Router component");
392
387
  return e;
393
- }, L = () => {
394
- let { updateLocation: e } = F();
388
+ }, I = () => {
389
+ let { updateLocation: e } = P();
395
390
  return r(async (t) => t === -1 ? history.go(-1) : await e(t), [e]);
396
- }, R = ({ children: e, to: t, prefetch: n = !0 }) => {
397
- let { prefetchLoader: r } = F(), i = L();
391
+ }, L = ({ children: e, to: t, prefetch: n = !0 }) => {
392
+ let { prefetchLoader: r } = P(), i = I();
398
393
  return /* @__PURE__ */ (0, g.jsx)("a", {
399
394
  style: { cursor: "pointer" },
400
395
  onClick: () => i({ pathname: t }),
401
396
  onMouseOver: () => n && r(t),
402
397
  children: e
403
398
  });
404
- }, z = () => {
405
- let { params: e } = P();
399
+ }, R = () => {
400
+ let { params: e } = N();
406
401
  return e;
407
- }, B = () => P().location, V = () => {
408
- let { loaderCache: e } = I();
402
+ }, z = () => N().location, B = () => {
403
+ let { loaderCache: e } = F();
409
404
  return e;
410
- }, H = (e) => {
411
- let { location: { pathname: t }, blockerState: n } = P(), { updateBlockedRoute: r } = F(), i = e();
405
+ }, V = (e) => {
406
+ let { location: { pathname: t }, blockerState: n } = N(), { updateBlockedRoute: r } = P(), i = e();
412
407
  return a(() => r(i ? {
413
408
  type: "charge",
414
409
  payload: t
@@ -421,19 +416,25 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
421
416
  process: () => r({ type: "process" }),
422
417
  reset: () => r({ type: "reset" })
423
418
  };
424
- }, U = (e) => {
419
+ }, H = (e) => {
425
420
  a(() => {
426
421
  let t = (t) => {
427
422
  e && (t.preventDefault(), e());
428
423
  };
429
424
  return window.addEventListener("beforeunload", t), () => window.removeEventListener("beforeunload", t);
430
425
  }, [e]);
431
- }, W = () => {
432
- let { context: e } = I(), { setContext: t } = F();
426
+ }, U = () => {
427
+ let { context: e } = F(), { setContext: t } = P();
433
428
  return {
434
429
  context: e,
435
430
  setContext: t
436
431
  };
432
+ }, W = (e, t) => {
433
+ let n = /* @__PURE__ */ Error();
434
+ throw n.cause = "redirect", Object.assign(n, {
435
+ url: e,
436
+ search: t || ""
437
+ });
437
438
  };
438
439
  //#endregion
439
- export { R as Link, N as Router, S as createRouter, D as redirect, U as useBeforeUnload, H as useBlocker, V as useLoaderState, B as useLocation, L as useNavigate, z as useParams, W as useRouterContext };
440
+ export { L as Link, M as Router, x as createRouter, W as redirect, H as useBeforeUnload, V as useBlocker, B as useLoaderState, z as useLocation, I as useNavigate, R as useParams, U as useRouterContext };
@@ -1,7 +1 @@
1
- export declare class RedirectInstance {
2
- url: string;
3
- search?: string;
4
- cause: 'redirect';
5
- constructor(url: string, search?: string);
6
- }
7
- export declare const redirect: (url: string, search?: string) => Promise<never>;
1
+ export declare const redirect: (url: string, search?: string) => never;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "1.0.13",
3
+ "version": "1.0.16",
4
4
  "description": "A lightweight, type-safe routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "main": "./dist/index.js",