reactive-route 0.0.1-alpha.21 → 0.0.1-alpha.22

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.
Files changed (33) hide show
  1. package/dist/cjs/index.js +116 -93
  2. package/dist/cjs/preact/index.js +10 -10
  3. package/dist/cjs/react/index.js +10 -10
  4. package/dist/cjs/solid/index.js +14 -14
  5. package/dist/core/createRouter.d.ts +4 -0
  6. package/dist/core/createRouter.d.ts.map +1 -0
  7. package/dist/core/{createRouterConfig.d.ts → createRoutes.d.ts} +2 -2
  8. package/dist/core/createRoutes.d.ts.map +1 -0
  9. package/dist/core/index.d.ts +3 -2
  10. package/dist/core/index.d.ts.map +1 -1
  11. package/dist/core/types/TypeLifecycleConfig.d.ts +2 -0
  12. package/dist/core/types/TypeLifecycleConfig.d.ts.map +1 -1
  13. package/dist/core/types/TypePropsRouter.d.ts +1 -1
  14. package/dist/core/types/TypePropsRouter.d.ts.map +1 -1
  15. package/dist/core/utils/PreventError.d.ts +4 -0
  16. package/dist/core/utils/PreventError.d.ts.map +1 -0
  17. package/dist/core/utils/RedirectError.d.ts +4 -0
  18. package/dist/core/utils/RedirectError.d.ts.map +1 -0
  19. package/dist/core/utils/constants.d.ts +0 -2
  20. package/dist/core/utils/constants.d.ts.map +1 -1
  21. package/dist/core/utils/getQueryValues.d.ts.map +1 -1
  22. package/dist/core/utils/queryString.d.ts +8 -0
  23. package/dist/core/utils/queryString.d.ts.map +1 -0
  24. package/dist/esm/index.js +114 -81
  25. package/dist/esm/preact/index.js +10 -10
  26. package/dist/esm/react/index.js +10 -10
  27. package/dist/esm/solid/index.js +14 -14
  28. package/dist/preact/Router.d.ts.map +1 -1
  29. package/dist/tsconfig.types.react.tsbuildinfo +1 -1
  30. package/package.json +2 -3
  31. package/dist/core/createRouterConfig.d.ts.map +0 -1
  32. package/dist/core/createRouterStore.d.ts +0 -4
  33. package/dist/core/createRouterStore.d.ts.map +0 -1
package/dist/cjs/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,21 +15,14 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // packages/core/index.ts
31
21
  var core_exports = {};
32
22
  __export(core_exports, {
33
- createRouterConfig: () => createRouterConfig,
34
- createRouterStore: () => createRouterStore,
23
+ RedirectError: () => RedirectError,
24
+ createRouter: () => createRouter,
25
+ createRoutes: () => createRoutes,
35
26
  findRouteByPathname: () => findRouteByPathname,
36
27
  getInitialRoute: () => getInitialRoute,
37
28
  history: () => history,
@@ -41,29 +32,11 @@ __export(core_exports, {
41
32
  });
42
33
  module.exports = __toCommonJS(core_exports);
43
34
 
44
- // packages/core/utils/addNames.ts
45
- function addNames(obj) {
46
- Object.entries(obj).forEach(([key, value]) => {
47
- value.name = key;
48
- });
49
- return obj;
50
- }
51
-
52
- // packages/core/createRouterConfig.ts
53
- function createRouterConfig(config) {
54
- return addNames(config);
55
- }
56
-
57
- // packages/core/createRouterStore.ts
58
- var import_query_string2 = __toESM(require("query-string"));
59
-
60
35
  // packages/core/utils/constants.ts
61
36
  var constants = {
62
37
  dynamicSeparator: ":",
63
38
  pathPartSeparator: "/",
64
- isClient: typeof window !== "undefined",
65
- errorRedirect: "REDIRECT",
66
- errorPrevent: "PREVENT_REDIRECT"
39
+ isClient: typeof window !== "undefined"
67
40
  };
68
41
 
69
42
  // packages/core/utils/dynamic.ts
@@ -121,18 +94,34 @@ function findRouteByPathname({
121
94
  return dynamicRouteMatch;
122
95
  }
123
96
 
124
- // packages/core/utils/getQueryValues.ts
125
- var import_query_string = __toESM(require("query-string"));
126
-
127
97
  // packages/core/utils/getTypedEntries.ts
128
98
  var getTypedEntries = Object.entries;
129
99
 
100
+ // packages/core/utils/queryString.ts
101
+ function removeHash(input) {
102
+ const hashStart = input.indexOf("#");
103
+ return hashStart === -1 ? input : input.slice(0, hashStart);
104
+ }
105
+ var queryString = {
106
+ extract(input) {
107
+ const inputNoHash = removeHash(input);
108
+ const queryStart = inputNoHash.indexOf("?");
109
+ return queryStart === -1 ? "" : inputNoHash.slice(queryStart + 1);
110
+ },
111
+ parse(input) {
112
+ return Object.fromEntries(new URLSearchParams(input));
113
+ },
114
+ stringify(obj) {
115
+ return new URLSearchParams(obj).toString();
116
+ }
117
+ };
118
+
130
119
  // packages/core/utils/getQueryValues.ts
131
120
  function getQueryValues(params) {
132
121
  const { route, pathname } = params;
133
- const qs = import_query_string.default.extract(pathname);
122
+ const qs = queryString.extract(pathname);
134
123
  if (!qs || !route.query) return {};
135
- const query = import_query_string.default.parse(qs);
124
+ const query = queryString.parse(qs);
136
125
  getTypedEntries(query).forEach(([key, value]) => {
137
126
  const validator = route.query[key];
138
127
  if (typeof validator !== "function" || !validator(value)) {
@@ -171,6 +160,22 @@ function loadComponentToConfig(params) {
171
160
  return Promise.resolve();
172
161
  }
173
162
 
163
+ // packages/core/utils/PreventError.ts
164
+ var PreventError = class extends Error {
165
+ constructor(message) {
166
+ super(message);
167
+ this.name = "PreventError";
168
+ }
169
+ };
170
+
171
+ // packages/core/utils/RedirectError.ts
172
+ var RedirectError = class extends Error {
173
+ constructor(message) {
174
+ super(message);
175
+ this.name = "RedirectError";
176
+ }
177
+ };
178
+
174
179
  // packages/core/utils/replaceDynamicValues.ts
175
180
  var re = new RegExp(`[^${constants.pathPartSeparator}]+`, "g");
176
181
  function replaceDynamicValues({
@@ -189,53 +194,52 @@ function replaceDynamicValues({
189
194
  });
190
195
  }
191
196
 
192
- // packages/core/createRouterStore.ts
193
- function createRouterStore({
197
+ // packages/core/createRouter.ts
198
+ function createRouter({
194
199
  adapters,
195
200
  routes,
196
201
  lifecycleParams
197
202
  }) {
198
- const routerStore = adapters.makeObservable({
203
+ const router = adapters.makeObservable({
199
204
  routesHistory: [],
200
205
  currentRoute: {},
201
206
  isRedirecting: false,
202
207
  redirectTo: void 0,
203
208
  restoreFromURL: void 0,
204
209
  restoreFromServer: void 0,
205
- adapters
210
+ get adapters() {
211
+ return adapters;
212
+ }
206
213
  });
207
- routerStore.restoreFromServer = function restoreFromServer(obj) {
214
+ router.restoreFromServer = function restoreFromServer(obj) {
208
215
  adapters.batch(() => {
209
- routerStore.routesHistory.push(...obj.routesHistory || []);
210
- Object.assign(routerStore.currentRoute, obj.currentRoute);
216
+ router.routesHistory.push(...obj.routesHistory || []);
217
+ Object.assign(router.currentRoute, obj.currentRoute);
211
218
  });
212
219
  const preloadedRouteName = Object.keys(routes).find(
213
- (routeName) => routerStore.currentRoute.name === routeName
220
+ (routeName) => router.currentRoute.name === routeName
214
221
  );
215
222
  return loadComponentToConfig({ route: routes[preloadedRouteName] });
216
223
  };
217
- routerStore.restoreFromURL = function restoreFromURL(params) {
218
- return routerStore.redirectTo(getInitialRoute({ routes, pathname: params.pathname }));
224
+ router.restoreFromURL = function restoreFromURL(params) {
225
+ return router.redirectTo(getInitialRoute({ routes, pathname: params.pathname }));
219
226
  };
220
- routerStore.redirectTo = async function redirectTo(config) {
227
+ router.redirectTo = async function redirectTo(config) {
221
228
  const { route: routeName, noHistoryPush } = config;
222
229
  let currentRoute;
223
230
  let currentPathname;
224
231
  let currentUrl;
225
232
  let currentSearch;
226
233
  let currentQuery;
227
- if (routerStore.currentRoute?.name) {
228
- currentRoute = routes[routerStore.currentRoute.name];
234
+ if (router.currentRoute?.name) {
235
+ currentRoute = routes[router.currentRoute.name];
229
236
  currentPathname = replaceDynamicValues({
230
237
  route: currentRoute,
231
- params: routerStore.currentRoute.params
232
- });
233
- currentUrl = import_query_string2.default.stringifyUrl({
234
- url: currentPathname,
235
- query: routerStore.currentRoute.query
238
+ params: router.currentRoute.params
236
239
  });
237
- currentQuery = routerStore.currentRoute.query;
238
- currentSearch = import_query_string2.default.stringify(routerStore.currentRoute.query);
240
+ currentQuery = router.currentRoute.query;
241
+ currentSearch = queryString.stringify(router.currentRoute.query);
242
+ currentUrl = `${currentPathname}${currentSearch ? `?${currentSearch}` : ""}`;
239
243
  }
240
244
  const nextRoute = routes[routeName];
241
245
  const nextPathname = replaceDynamicValues({
@@ -248,23 +252,23 @@ function createRouterStore({
248
252
  if ("query" in config && config.query) {
249
253
  const clearedQuery = getQueryValues({
250
254
  route: nextRoute,
251
- pathname: `${nextPathname}?${import_query_string2.default.stringify(config.query)}`
255
+ pathname: `${nextPathname}?${queryString.stringify(config.query)}`
252
256
  });
253
257
  if (Object.keys(clearedQuery).length > 0) {
254
258
  nextQuery = clearedQuery;
255
- nextSearch = import_query_string2.default.stringify(clearedQuery);
256
- nextUrl = import_query_string2.default.stringifyUrl({ url: nextPathname, query: clearedQuery });
259
+ nextSearch = queryString.stringify(clearedQuery);
260
+ nextUrl = `${nextPathname}?${nextSearch}`;
257
261
  }
258
262
  }
259
263
  if (currentUrl === nextUrl) return Promise.resolve();
260
264
  if (currentPathname === nextPathname) {
261
265
  if (currentSearch !== nextSearch) {
262
266
  adapters.batch(() => {
263
- adapters.replaceObject(routerStore.currentRoute, {
264
- ...routerStore.currentRoute,
267
+ adapters.replaceObject(router.currentRoute, {
268
+ ...router.currentRoute,
265
269
  query: nextQuery || {}
266
270
  });
267
- routerStore.routesHistory.push(nextUrl);
271
+ router.routesHistory.push(nextUrl);
268
272
  });
269
273
  if (history && !noHistoryPush) {
270
274
  history.push({
@@ -277,7 +281,7 @@ function createRouterStore({
277
281
  return Promise.resolve();
278
282
  }
279
283
  adapters.batch(() => {
280
- routerStore.isRedirecting = true;
284
+ router.isRedirecting = true;
281
285
  });
282
286
  try {
283
287
  const config2 = {
@@ -290,39 +294,45 @@ function createRouterStore({
290
294
  currentQuery,
291
295
  currentRoute,
292
296
  currentSearch,
293
- currentPathname
294
- };
295
- await currentRoute?.beforeLeave?.(config2, ...lifecycleParams || []);
296
- const redirectConfig = await nextRoute.beforeEnter?.(config2, ...lifecycleParams || []);
297
- if (typeof redirectConfig === "object") {
298
- if (constants.isClient) return redirectTo(redirectConfig);
299
- const redirectRoute = routes[redirectConfig.route];
300
- const redirectParams = "params" in redirectConfig && redirectConfig.params ? redirectConfig.params : void 0;
301
- let redirectUrl = replaceDynamicValues({
302
- params: redirectParams,
303
- route: redirectRoute
304
- });
305
- if ("query" in redirectConfig && redirectConfig.query) {
306
- const clearedQuery = getQueryValues({
307
- route: nextRoute,
308
- pathname: `${nextPathname}?${import_query_string2.default.stringify(redirectConfig.query)}`
297
+ currentPathname,
298
+ redirect: (redirectConfig2) => {
299
+ if (constants.isClient) return redirectConfig2;
300
+ const redirectRoute = routes[redirectConfig2.route];
301
+ const redirectParams = "params" in redirectConfig2 && redirectConfig2.params ? redirectConfig2.params : void 0;
302
+ let redirectUrl = replaceDynamicValues({
303
+ params: redirectParams,
304
+ route: redirectRoute
309
305
  });
310
- if (Object.keys(clearedQuery).length > 0) {
311
- redirectUrl = import_query_string2.default.stringifyUrl({ url: redirectUrl, query: clearedQuery });
306
+ if ("query" in redirectConfig2 && redirectConfig2.query) {
307
+ const clearedQuery = getQueryValues({
308
+ route: nextRoute,
309
+ pathname: `${nextPathname}?${queryString.stringify(redirectConfig2.query)}`
310
+ });
311
+ if (Object.keys(clearedQuery).length > 0) {
312
+ redirectUrl = `${redirectUrl}?${queryString.stringify(clearedQuery)}`;
313
+ }
312
314
  }
315
+ throw new RedirectError(redirectUrl);
316
+ },
317
+ preventRedirect: () => {
318
+ throw new PreventError(`Redirect to ${nextUrl} was prevented`);
313
319
  }
314
- throw Object.assign(new Error(redirectUrl), { name: constants.errorRedirect });
315
- }
320
+ };
321
+ await currentRoute?.beforeLeave?.(config2, ...lifecycleParams || []);
322
+ const redirectConfig = await nextRoute.beforeEnter?.(config2, ...lifecycleParams || []);
323
+ if (redirectConfig) return redirectTo(redirectConfig);
316
324
  await loadComponentToConfig({ route: routes[nextRoute.name] });
317
325
  } catch (error) {
318
- if (error?.name === constants.errorPrevent) return Promise.resolve();
319
- if (error?.name === constants.errorRedirect) {
326
+ if (error instanceof PreventError) {
327
+ return Promise.resolve();
328
+ }
329
+ if (error instanceof RedirectError) {
320
330
  throw error;
321
331
  }
322
332
  console.error(error);
323
333
  await loadComponentToConfig({ route: routes.internalError });
324
334
  adapters.batch(() => {
325
- adapters.replaceObject(routerStore.currentRoute, {
335
+ adapters.replaceObject(router.currentRoute, {
326
336
  name: routes.internalError.name,
327
337
  path: routes.internalError.path,
328
338
  props: routes[routes.internalError.name].props,
@@ -330,12 +340,12 @@ function createRouterStore({
330
340
  params: adapters.makeObservable({}),
331
341
  pageName: routes[routes.internalError.name].pageName
332
342
  });
333
- routerStore.isRedirecting = false;
343
+ router.isRedirecting = false;
334
344
  });
335
345
  return Promise.resolve();
336
346
  }
337
347
  adapters.batch(() => {
338
- adapters.replaceObject(routerStore.currentRoute, {
348
+ adapters.replaceObject(router.currentRoute, {
339
349
  name: nextRoute.name,
340
350
  path: nextRoute.path,
341
351
  props: routes[nextRoute.name].props,
@@ -343,20 +353,33 @@ function createRouterStore({
343
353
  params: getDynamicValues({ route: nextRoute, pathname: nextUrl }),
344
354
  pageName: routes[nextRoute.name].pageName
345
355
  });
346
- const lastUrl = routerStore.routesHistory[routerStore.routesHistory.length - 1];
356
+ const lastUrl = router.routesHistory[router.routesHistory.length - 1];
347
357
  if (lastUrl !== nextUrl) {
348
- routerStore.routesHistory.push(nextUrl);
358
+ router.routesHistory.push(nextUrl);
349
359
  }
350
360
  if (history && !noHistoryPush) {
351
361
  history.push({
352
362
  hash: history.location.hash,
353
- search: "query" in config ? `?${import_query_string2.default.stringify(config.query)}` : "",
363
+ search: "query" in config ? `?${queryString.stringify(config.query)}` : "",
354
364
  pathname: nextPathname
355
365
  });
356
366
  }
357
- routerStore.isRedirecting = false;
367
+ router.isRedirecting = false;
358
368
  });
359
369
  return Promise.resolve();
360
370
  };
361
- return routerStore;
371
+ return router;
372
+ }
373
+
374
+ // packages/core/utils/addNames.ts
375
+ function addNames(obj) {
376
+ Object.entries(obj).forEach(([key, value]) => {
377
+ value.name = key;
378
+ });
379
+ return obj;
380
+ }
381
+
382
+ // packages/core/createRoutes.ts
383
+ function createRoutes(config) {
384
+ return addNames(config);
362
385
  }
@@ -34,11 +34,11 @@ function RouterInner(props) {
34
34
  if (!import_reactive_route.history) return;
35
35
  import_reactive_route.history.listen((params) => {
36
36
  if (params.action !== "POP") return;
37
- const previousRoutePathname = props.routerStore.routesHistory[props.routerStore.routesHistory.length - 2];
37
+ const previousRoutePathname = props.router.routesHistory[props.router.routesHistory.length - 2];
38
38
  if (previousRoutePathname === params.location.pathname) {
39
- props.routerStore.adapters.batch(() => props.routerStore.routesHistory.pop());
39
+ props.router.adapters.batch(() => props.router.routesHistory.pop());
40
40
  }
41
- void props.routerStore.redirectTo({
41
+ void props.router.redirectTo({
42
42
  noHistoryPush: true,
43
43
  ...(0, import_reactive_route.getInitialRoute)({
44
44
  routes: props.routes,
@@ -48,7 +48,7 @@ function RouterInner(props) {
48
48
  });
49
49
  }, []);
50
50
  const [config] = (0, import_hooks.useState)(
51
- () => props.routerStore.adapters.makeObservable({
51
+ () => props.router.adapters.makeObservable({
52
52
  loadedComponentName: void 0,
53
53
  loadedComponentPage: void 0,
54
54
  currentProps: {}
@@ -56,21 +56,21 @@ function RouterInner(props) {
56
56
  );
57
57
  const setLoadedComponent = (0, import_hooks.useCallback)(() => {
58
58
  const { loadedComponentName, loadedComponentPage } = config;
59
- const { currentRoute, isRedirecting } = props.routerStore;
59
+ const { currentRoute, isRedirecting } = props.router;
60
60
  const componentConfig = props.routes[currentRoute.name];
61
61
  let preventRedirect = false;
62
62
  if (isRedirecting) preventRedirect = true;
63
63
  else if (loadedComponentName === currentRoute.name) preventRedirect = true;
64
64
  else if (loadedComponentPage != null && currentRoute.name != null) {
65
65
  if (loadedComponentPage === currentRoute.pageName) {
66
- props.routerStore.adapters.batch(() => {
66
+ props.router.adapters.batch(() => {
67
67
  config.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
68
68
  });
69
69
  preventRedirect = true;
70
70
  }
71
71
  }
72
72
  if (preventRedirect) return;
73
- props.routerStore.adapters.batch(() => {
73
+ props.router.adapters.batch(() => {
74
74
  if (loadedComponentName) props.beforeUpdatePageComponent?.();
75
75
  props.beforeSetPageComponent?.(componentConfig);
76
76
  config.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
@@ -79,11 +79,11 @@ function RouterInner(props) {
79
79
  });
80
80
  }, []);
81
81
  (0, import_hooks.useState)(() => {
82
- props.routerStore.adapters.batch(() => {
82
+ props.router.adapters.batch(() => {
83
83
  props.beforeMount?.();
84
84
  redirectOnHistoryPop();
85
85
  setLoadedComponent();
86
- disposerRef.current = props.routerStore.adapters.autorun(setLoadedComponent);
86
+ disposerRef.current = props.router.adapters.autorun(setLoadedComponent);
87
87
  });
88
88
  });
89
89
  (0, import_hooks.useEffect)(() => {
@@ -98,7 +98,7 @@ function RouterInner(props) {
98
98
  }
99
99
  function Router(props) {
100
100
  const [Component] = (0, import_hooks.useState)(
101
- () => props.routerStore.adapters.observer ? props.routerStore.adapters.observer(RouterInner) : RouterInner
101
+ () => props.router.adapters.observer ? props.router.adapters.observer(RouterInner) : RouterInner
102
102
  );
103
103
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, { ...props });
104
104
  }
@@ -34,11 +34,11 @@ function RouterInner(props) {
34
34
  if (!import_reactive_route.history) return;
35
35
  import_reactive_route.history.listen((params) => {
36
36
  if (params.action !== "POP") return;
37
- const previousRoutePathname = props.routerStore.routesHistory[props.routerStore.routesHistory.length - 2];
37
+ const previousRoutePathname = props.router.routesHistory[props.router.routesHistory.length - 2];
38
38
  if (previousRoutePathname === params.location.pathname) {
39
- props.routerStore.adapters.batch(() => props.routerStore.routesHistory.pop());
39
+ props.router.adapters.batch(() => props.router.routesHistory.pop());
40
40
  }
41
- void props.routerStore.redirectTo({
41
+ void props.router.redirectTo({
42
42
  noHistoryPush: true,
43
43
  ...(0, import_reactive_route.getInitialRoute)({
44
44
  routes: props.routes,
@@ -48,7 +48,7 @@ function RouterInner(props) {
48
48
  });
49
49
  }, []);
50
50
  const [config] = (0, import_react.useState)(
51
- () => props.routerStore.adapters.makeObservable({
51
+ () => props.router.adapters.makeObservable({
52
52
  loadedComponentName: void 0,
53
53
  loadedComponentPage: void 0,
54
54
  currentProps: {}
@@ -56,21 +56,21 @@ function RouterInner(props) {
56
56
  );
57
57
  const setLoadedComponent = (0, import_react.useCallback)(() => {
58
58
  const { loadedComponentName, loadedComponentPage } = config;
59
- const { currentRoute, isRedirecting } = props.routerStore;
59
+ const { currentRoute, isRedirecting } = props.router;
60
60
  const componentConfig = props.routes[currentRoute.name];
61
61
  let preventRedirect = false;
62
62
  if (isRedirecting) preventRedirect = true;
63
63
  else if (loadedComponentName === currentRoute.name) preventRedirect = true;
64
64
  else if (loadedComponentPage != null && currentRoute.name != null) {
65
65
  if (loadedComponentPage === currentRoute.pageName) {
66
- props.routerStore.adapters.batch(() => {
66
+ props.router.adapters.batch(() => {
67
67
  config.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
68
68
  });
69
69
  preventRedirect = true;
70
70
  }
71
71
  }
72
72
  if (preventRedirect) return;
73
- props.routerStore.adapters.batch(() => {
73
+ props.router.adapters.batch(() => {
74
74
  if (loadedComponentName) props.beforeUpdatePageComponent?.();
75
75
  props.beforeSetPageComponent?.(componentConfig);
76
76
  config.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
@@ -79,11 +79,11 @@ function RouterInner(props) {
79
79
  });
80
80
  }, []);
81
81
  (0, import_react.useState)(() => {
82
- props.routerStore.adapters.batch(() => {
82
+ props.router.adapters.batch(() => {
83
83
  props.beforeMount?.();
84
84
  redirectOnHistoryPop();
85
85
  setLoadedComponent();
86
- disposerRef.current = props.routerStore.adapters.autorun(setLoadedComponent);
86
+ disposerRef.current = props.router.adapters.autorun(setLoadedComponent);
87
87
  });
88
88
  });
89
89
  (0, import_react.useEffect)(() => {
@@ -97,7 +97,7 @@ function RouterInner(props) {
97
97
  return null;
98
98
  }
99
99
  function RouterWrapper(props) {
100
- const Component = props.routerStore.adapters.observer ? props.routerStore.adapters.observer(RouterInner) : RouterInner;
100
+ const Component = props.router.adapters.observer ? props.router.adapters.observer(RouterInner) : RouterInner;
101
101
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, { ...props });
102
102
  }
103
103
  var Router = (0, import_react.memo)(RouterWrapper);
@@ -31,7 +31,7 @@ var import_reactive_route = require("reactive-route");
31
31
  var import_solid_js = require("solid-js");
32
32
  var import_web3 = require("solid-js/web");
33
33
  function Router(props) {
34
- const config = props.routerStore.adapters.makeObservable({
34
+ const config = props.router.adapters.makeObservable({
35
35
  loadedComponentName: void 0,
36
36
  loadedComponentPage: void 0,
37
37
  currentProps: {}
@@ -40,11 +40,11 @@ function Router(props) {
40
40
  if (!import_reactive_route.history) return;
41
41
  import_reactive_route.history.listen((params) => {
42
42
  if (params.action !== "POP") return;
43
- const previousRoutePathname = props.routerStore.routesHistory[props.routerStore.routesHistory.length - 2];
43
+ const previousRoutePathname = props.router.routesHistory[props.router.routesHistory.length - 2];
44
44
  if (previousRoutePathname === params.location.pathname) {
45
- props.routerStore.routesHistory.pop();
45
+ props.router.routesHistory.pop();
46
46
  }
47
- void props.routerStore.redirectTo({
47
+ void props.router.redirectTo({
48
48
  noHistoryPush: true,
49
49
  ...(0, import_reactive_route.getInitialRoute)({
50
50
  routes: props.routes,
@@ -54,27 +54,27 @@ function Router(props) {
54
54
  });
55
55
  }
56
56
  function setLoadedComponent() {
57
- const currentRouteName = props.routerStore.currentRoute.name;
58
- const currentRoutePage = props.routerStore.currentRoute.pageName;
57
+ const currentRouteName = props.router.currentRoute.name;
58
+ const currentRoutePage = props.router.currentRoute.pageName;
59
59
  const componentConfig = props.routes[currentRouteName];
60
60
  let preventRedirect = false;
61
- if (props.routerStore.isRedirecting) preventRedirect = true;
61
+ if (props.router.isRedirecting) preventRedirect = true;
62
62
  else if (config.loadedComponentName === currentRouteName) {
63
63
  preventRedirect = true;
64
64
  } else if (config.loadedComponentPage != null && currentRouteName != null) {
65
65
  if (config.loadedComponentPage === currentRoutePage) {
66
- props.routerStore.adapters.replaceObject(config.currentProps, "props" in componentConfig ? componentConfig.props : {});
66
+ props.router.adapters.replaceObject(config.currentProps, "props" in componentConfig ? componentConfig.props : {});
67
67
  preventRedirect = true;
68
68
  }
69
69
  }
70
70
  if (preventRedirect) return;
71
- props.routerStore.adapters.batch(() => {
71
+ props.router.adapters.batch(() => {
72
72
  if (config.loadedComponentName) {
73
73
  props.beforeUpdatePageComponent?.();
74
74
  }
75
75
  props.beforeSetPageComponent?.(componentConfig);
76
- props.routerStore.adapters.batch(() => {
77
- props.routerStore.adapters.replaceObject(config.currentProps, "props" in componentConfig ? componentConfig.props : {});
76
+ props.router.adapters.batch(() => {
77
+ props.router.adapters.replaceObject(config.currentProps, "props" in componentConfig ? componentConfig.props : {});
78
78
  config.loadedComponentName = currentRouteName;
79
79
  config.loadedComponentPage = componentConfig.pageName;
80
80
  });
@@ -82,12 +82,12 @@ function Router(props) {
82
82
  }
83
83
  props.beforeMount?.();
84
84
  redirectOnHistoryPop();
85
- if (props.routerStore.adapters.immediateSetComponent) {
86
- props.routerStore.adapters.batch(() => {
85
+ if (props.router.adapters.immediateSetComponent) {
86
+ props.router.adapters.batch(() => {
87
87
  setLoadedComponent();
88
88
  });
89
89
  }
90
- props.routerStore.adapters.autorun(() => setLoadedComponent());
90
+ props.router.adapters.autorun(() => setLoadedComponent());
91
91
  return (
92
92
  // @ts-ignore
93
93
  (0, import_web.createComponent)(import_solid_js.Show, {
@@ -0,0 +1,4 @@
1
+ import { InterfaceRouterStore, TypeCreateRouterStore } from './types/InterfaceRouterStore';
2
+ import { TypeRoute } from './types/TypeRoute';
3
+ export declare function createRouter<TRoutes extends Record<string | 'notFound' | 'internalError', TypeRoute>>({ adapters, routes, lifecycleParams, }: TypeCreateRouterStore<TRoutes>): InterfaceRouterStore<TRoutes>;
4
+ //# sourceMappingURL=createRouter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createRouter.d.ts","sourceRoot":"","sources":["../../packages/core/createRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAG3F,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAY9C,wBAAgB,YAAY,CAC1B,OAAO,SAAS,MAAM,CAAC,MAAM,GAAG,UAAU,GAAG,eAAe,EAAE,SAAS,CAAC,EACxE,EACA,QAAQ,EACR,MAAM,EACN,eAAe,GAChB,EAAE,qBAAqB,CAAC,OAAO,CAAC,GAAG,oBAAoB,CAAC,OAAO,CAAC,CAiPhE"}
@@ -1,5 +1,5 @@
1
1
  import { TypeRouteRaw } from './types/TypeRouteRaw';
2
- export declare function createRouterConfig<TConfig extends {
2
+ export declare function createRoutes<TConfig extends {
3
3
  [Key in keyof TConfig | 'notFound' | 'internalError']: TypeRouteRaw;
4
4
  }>(config: TConfig): {
5
5
  [Key in keyof TConfig]: TConfig[Key] & {
@@ -9,4 +9,4 @@ export declare function createRouterConfig<TConfig extends {
9
9
  otherExports?: Record<string, any>;
10
10
  };
11
11
  };
12
- //# sourceMappingURL=createRouterConfig.d.ts.map
12
+ //# sourceMappingURL=createRoutes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createRoutes.d.ts","sourceRoot":"","sources":["../../packages/core/createRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAGpD,wBAAgB,YAAY,CAC1B,OAAO,SAAS;KAAG,GAAG,IAAI,MAAM,OAAO,GAAG,UAAU,GAAG,eAAe,GAAG,YAAY;CAAE,EAEvF,MAAM,EAAE,OAAO,GACd;KACA,GAAG,IAAI,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG;QACrC,IAAI,EAAE,GAAG,CAAC;QACV,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,SAAS,CAAC,EAAE,GAAG,CAAC;QAChB,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;KACpC;CACF,CAEA"}
@@ -1,5 +1,5 @@
1
- export { createRouterConfig } from './createRouterConfig';
2
- export { createRouterStore } from './createRouterStore';
1
+ export { createRouter } from './createRouter';
2
+ export { createRoutes } from './createRoutes';
3
3
  export type { InterfaceRouterStore, TypeAdapters } from './types/InterfaceRouterStore';
4
4
  export type { TypeCurrentRoute } from './types/TypeCurrentRoute';
5
5
  export type { TypePropsRouter } from './types/TypePropsRouter';
@@ -10,5 +10,6 @@ export { findRouteByPathname } from './utils/findRouteByPathname';
10
10
  export { getInitialRoute } from './utils/getInitialRoute';
11
11
  export { history } from './utils/history';
12
12
  export { loadComponentToConfig } from './utils/loadComponentToConfig';
13
+ export { RedirectError } from './utils/RedirectError';
13
14
  export { replaceDynamicValues } from './utils/replaceDynamicValues';
14
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../packages/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,YAAY,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AACvF,YAAY,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AACjE,YAAY,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC/D,YAAY,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACzE,YAAY,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../packages/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,YAAY,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AACvF,YAAY,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AACjE,YAAY,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC/D,YAAY,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACzE,YAAY,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC"}
@@ -9,5 +9,7 @@ export type TypeLifecycleConfig = {
9
9
  currentRoute?: any;
10
10
  currentSearch?: string;
11
11
  currentPathname?: string;
12
+ redirect: (params: any) => void;
13
+ preventRedirect: () => void;
12
14
  };
13
15
  //# sourceMappingURL=TypeLifecycleConfig.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"TypeLifecycleConfig.d.ts","sourceRoot":"","sources":["../../../packages/core/types/TypeLifecycleConfig.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,GAAG,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,GAAG,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC"}
1
+ {"version":3,"file":"TypeLifecycleConfig.d.ts","sourceRoot":"","sources":["../../../packages/core/types/TypeLifecycleConfig.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,GAAG,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,GAAG,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB,QAAQ,EAAE,CAAC,MAAM,EAAE,GAAG,KAAK,IAAI,CAAC;IAChC,eAAe,EAAE,MAAM,IAAI,CAAC;CAC7B,CAAC"}
@@ -2,7 +2,7 @@ import { InterfaceRouterStore } from './InterfaceRouterStore';
2
2
  import { TypeRoute } from './TypeRoute';
3
3
  export type TypePropsRouter<TRoutes extends Record<string, TypeRoute>> = {
4
4
  routes: TRoutes;
5
- routerStore: InterfaceRouterStore<TRoutes>;
5
+ router: InterfaceRouterStore<TRoutes>;
6
6
  beforeMount?: () => void;
7
7
  beforeSetPageComponent?: (componentConfig: TRoutes[keyof TRoutes]) => void;
8
8
  beforeUpdatePageComponent?: () => void;
@@ -1 +1 @@
1
- {"version":3,"file":"TypePropsRouter.d.ts","sourceRoot":"","sources":["../../../packages/core/types/TypePropsRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,MAAM,eAAe,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,IAAI;IACvE,MAAM,EAAE,OAAO,CAAC;IAChB,WAAW,EAAE,oBAAoB,CAAC,OAAO,CAAC,CAAC;IAC3C,WAAW,CAAC,EAAE,MAAM,IAAI,CAAC;IACzB,sBAAsB,CAAC,EAAE,CAAC,eAAe,EAAE,OAAO,CAAC,MAAM,OAAO,CAAC,KAAK,IAAI,CAAC;IAC3E,yBAAyB,CAAC,EAAE,MAAM,IAAI,CAAC;CACxC,CAAC"}
1
+ {"version":3,"file":"TypePropsRouter.d.ts","sourceRoot":"","sources":["../../../packages/core/types/TypePropsRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,MAAM,eAAe,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,IAAI;IACvE,MAAM,EAAE,OAAO,CAAC;IAChB,MAAM,EAAE,oBAAoB,CAAC,OAAO,CAAC,CAAC;IACtC,WAAW,CAAC,EAAE,MAAM,IAAI,CAAC;IACzB,sBAAsB,CAAC,EAAE,CAAC,eAAe,EAAE,OAAO,CAAC,MAAM,OAAO,CAAC,KAAK,IAAI,CAAC;IAC3E,yBAAyB,CAAC,EAAE,MAAM,IAAI,CAAC;CACxC,CAAC"}