clear-react-router 1.0.13 → 1.0.15

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,31 @@ 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
+ a?.beforeLoad && await a?.beforeLoad(n), 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
+ let n = t;
249
+ if (!E(n)) return n;
250
+ history.replaceState(null, "", n.url), e({
251
+ pathname: n.url,
252
+ search: n.search
259
253
  });
260
254
  }
261
255
  }, [
@@ -280,7 +274,7 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
280
274
  }, [l.from, f]);
281
275
  return a(() => {
282
276
  let e = async (e) => {
283
- let t = w(e.target.location);
277
+ let t = C(e.target.location);
284
278
  d.current === l.from ? (u({
285
279
  from: d.current,
286
280
  to: t.pathname
@@ -288,14 +282,14 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
288
282
  };
289
283
  return window.addEventListener("popstate", e), () => window.removeEventListener("popstate", e);
290
284
  }, [l.from, f]), a(() => {
291
- let e = w(window.location);
285
+ let e = C(window.location);
292
286
  f.current(e), d.current = e.pathname;
293
287
  }, [f]), {
294
288
  blockerState: o(() => l.from && l.to ? "blocked" : l.from ? "charged" : "unblocked", [l]),
295
289
  updateLocation: m,
296
290
  updateBlockedRoute: p
297
291
  };
298
- }, k = (e) => {
292
+ }, O = (e) => {
299
293
  let [t, n] = c({}), [i, a] = c(!1), [o, l] = c({}), u = s({}), d = r(({ key: e, value: t }) => n((n) => ({
300
294
  ...n,
301
295
  [e]: t
@@ -332,7 +326,7 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
332
326
  }
333
327
  }
334
328
  }, [f, d]), m = r(async (t) => {
335
- let n = e.find((e) => T(e, t));
329
+ let n = e.find((e) => w(e, t));
336
330
  n && await p(n);
337
331
  }, [p, e]);
338
332
  return {
@@ -342,13 +336,13 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
342
336
  revalidateCache: p,
343
337
  isLoading: o[window.location.pathname]
344
338
  };
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({
339
+ }, 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 = {} }) => {
340
+ 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
341
  setLocation: r,
348
342
  routeList: e,
349
343
  context: i,
350
344
  revalidateCache: f
351
- }), y = o(() => s?.params ? C(s.params) : {}, [s]), b = o(() => ({
345
+ }), y = o(() => s?.params ? S(s.params) : {}, [s]), b = o(() => ({
352
346
  location: n,
353
347
  updateLocation: h,
354
348
  params: y,
@@ -370,45 +364,45 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
370
364
  ]);
371
365
  return s?.loader && !l && p ? /* @__PURE__ */ (0, g.jsx)(_, {
372
366
  ...b,
373
- children: A(s?.loaderFallback)
367
+ children: k(s?.loaderFallback)
374
368
  }) : l ? /* @__PURE__ */ (0, g.jsx)(_, {
375
369
  ...b,
376
- children: A(s?.errorElement)
370
+ children: k(s?.errorElement)
377
371
  }) : /* @__PURE__ */ (0, g.jsx)(_, {
378
372
  ...b,
379
- children: A(s?.element) || j
373
+ children: k(s?.element) || A
380
374
  });
381
- }, P = () => {
375
+ }, N = () => {
382
376
  let e = i(p);
383
377
  if (!Object.keys(e).length) throw Error("useNavigationState must be used within Router component");
384
378
  return e;
385
- }, F = () => {
379
+ }, P = () => {
386
380
  let e = i(d);
387
381
  if (!Object.keys(e).length) throw Error("useRouterActions must be used within Router component");
388
382
  return e;
389
- }, I = () => {
383
+ }, F = () => {
390
384
  let e = i(f);
391
385
  if (!Object.keys(e).length) throw Error("useRouterData must be used within Router component");
392
386
  return e;
393
- }, L = () => {
394
- let { updateLocation: e } = F();
387
+ }, I = () => {
388
+ let { updateLocation: e } = P();
395
389
  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();
390
+ }, L = ({ children: e, to: t, prefetch: n = !0 }) => {
391
+ let { prefetchLoader: r } = P(), i = I();
398
392
  return /* @__PURE__ */ (0, g.jsx)("a", {
399
393
  style: { cursor: "pointer" },
400
394
  onClick: () => i({ pathname: t }),
401
395
  onMouseOver: () => n && r(t),
402
396
  children: e
403
397
  });
404
- }, z = () => {
405
- let { params: e } = P();
398
+ }, R = () => {
399
+ let { params: e } = N();
406
400
  return e;
407
- }, B = () => P().location, V = () => {
408
- let { loaderCache: e } = I();
401
+ }, z = () => N().location, B = () => {
402
+ let { loaderCache: e } = F();
409
403
  return e;
410
- }, H = (e) => {
411
- let { location: { pathname: t }, blockerState: n } = P(), { updateBlockedRoute: r } = F(), i = e();
404
+ }, V = (e) => {
405
+ let { location: { pathname: t }, blockerState: n } = N(), { updateBlockedRoute: r } = P(), i = e();
412
406
  return a(() => r(i ? {
413
407
  type: "charge",
414
408
  payload: t
@@ -421,19 +415,25 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
421
415
  process: () => r({ type: "process" }),
422
416
  reset: () => r({ type: "reset" })
423
417
  };
424
- }, U = (e) => {
418
+ }, H = (e) => {
425
419
  a(() => {
426
420
  let t = (t) => {
427
421
  e && (t.preventDefault(), e());
428
422
  };
429
423
  return window.addEventListener("beforeunload", t), () => window.removeEventListener("beforeunload", t);
430
424
  }, [e]);
431
- }, W = () => {
432
- let { context: e } = I(), { setContext: t } = F();
425
+ }, U = () => {
426
+ let { context: e } = F(), { setContext: t } = P();
433
427
  return {
434
428
  context: e,
435
429
  setContext: t
436
430
  };
431
+ }, W = (e, t) => {
432
+ let n = /* @__PURE__ */ Error();
433
+ throw n.cause = "redirect", Object.assign(n, {
434
+ url: e,
435
+ search: t || ""
436
+ });
437
437
  };
438
438
  //#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 };
439
+ 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.15",
4
4
  "description": "A lightweight, type-safe routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "main": "./dist/index.js",