mobx-route 0.15.0 → 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.
Files changed (155) hide show
  1. package/README.md +5 -7
  2. package/core/config/config.cjs +43 -0
  3. package/core/config/config.d.cts +7 -0
  4. package/core/config/config.d.cts.map +1 -0
  5. package/core/config/config.d.ts +1 -1
  6. package/core/config/config.d.ts.map +1 -1
  7. package/core/config/config.types.cjs +2 -0
  8. package/core/config/config.types.d.cts +11 -0
  9. package/core/config/config.types.d.cts.map +1 -0
  10. package/core/config/config.types.d.ts +1 -1
  11. package/core/config/config.types.d.ts.map +1 -1
  12. package/core/config/index.cjs +18 -0
  13. package/core/config/index.d.cts +3 -0
  14. package/core/config/index.d.cts.map +1 -0
  15. package/core/config/index.d.ts +2 -2
  16. package/core/config/index.d.ts.map +1 -1
  17. package/core/config/index.js +2 -2
  18. package/core/index.cjs +21 -0
  19. package/core/index.d.cts +6 -0
  20. package/core/index.d.cts.map +1 -0
  21. package/core/index.d.ts +5 -5
  22. package/core/index.d.ts.map +1 -1
  23. package/core/index.js +5 -5
  24. package/core/route/index.cjs +18 -0
  25. package/core/route/index.d.cts +3 -0
  26. package/core/route/index.d.cts.map +1 -0
  27. package/core/route/index.d.ts +2 -2
  28. package/core/route/index.d.ts.map +1 -1
  29. package/core/route/index.js +2 -2
  30. package/core/route/route.cjs +296 -0
  31. package/core/route/route.d.cts +107 -0
  32. package/core/route/route.d.cts.map +1 -0
  33. package/core/route/route.d.ts +1 -1
  34. package/core/route/route.d.ts.map +1 -1
  35. package/core/route/route.js +1 -1
  36. package/core/route/route.types.cjs +2 -0
  37. package/core/route/route.types.d.cts +142 -0
  38. package/core/route/route.types.d.cts.map +1 -0
  39. package/core/route/route.types.d.ts +4 -4
  40. package/core/route/route.types.d.ts.map +1 -1
  41. package/core/route-group/index.cjs +18 -0
  42. package/core/route-group/index.d.cts +3 -0
  43. package/core/route-group/index.d.cts.map +1 -0
  44. package/core/route-group/index.d.ts +2 -2
  45. package/core/route-group/index.d.ts.map +1 -1
  46. package/core/route-group/index.js +2 -2
  47. package/core/route-group/route-group.cjs +67 -0
  48. package/core/route-group/route-group.d.cts +31 -0
  49. package/core/route-group/route-group.d.cts.map +1 -0
  50. package/core/route-group/route-group.d.ts +1 -1
  51. package/core/route-group/route-group.d.ts.map +1 -1
  52. package/core/route-group/route-group.types.cjs +2 -0
  53. package/core/route-group/route-group.types.d.cts +15 -0
  54. package/core/route-group/route-group.types.d.cts.map +1 -0
  55. package/core/route-group/route-group.types.d.ts +3 -3
  56. package/core/route-group/route-group.types.d.ts.map +1 -1
  57. package/core/router/index.cjs +18 -0
  58. package/core/router/index.d.cts +3 -0
  59. package/core/router/index.d.cts.map +1 -0
  60. package/core/router/index.d.ts +2 -2
  61. package/core/router/index.d.ts.map +1 -1
  62. package/core/router/index.js +2 -2
  63. package/core/router/router.cjs +42 -0
  64. package/core/router/router.d.cts +18 -0
  65. package/core/router/router.d.cts.map +1 -0
  66. package/core/router/router.d.ts +2 -2
  67. package/core/router/router.d.ts.map +1 -1
  68. package/core/router/router.js +1 -1
  69. package/core/router/router.types.cjs +2 -0
  70. package/core/router/router.types.d.cts +13 -0
  71. package/core/router/router.types.d.cts.map +1 -0
  72. package/core/router/router.types.d.ts +3 -3
  73. package/core/router/router.types.d.ts.map +1 -1
  74. package/core/utils/is-route-entity.cjs +5 -0
  75. package/core/utils/is-route-entity.d.cts +3 -0
  76. package/core/utils/is-route-entity.d.cts.map +1 -0
  77. package/core/utils/is-route-entity.d.ts +1 -1
  78. package/core/utils/is-route-entity.d.ts.map +1 -1
  79. package/core/virtual-route/index.cjs +18 -0
  80. package/core/virtual-route/index.d.cts +3 -0
  81. package/core/virtual-route/index.d.cts.map +1 -0
  82. package/core/virtual-route/index.d.ts +2 -2
  83. package/core/virtual-route/index.d.ts.map +1 -1
  84. package/core/virtual-route/index.js +2 -2
  85. package/core/virtual-route/virtual-route.cjs +133 -0
  86. package/core/virtual-route/virtual-route.d.cts +39 -0
  87. package/core/virtual-route/virtual-route.d.cts.map +1 -0
  88. package/core/virtual-route/virtual-route.d.ts +1 -1
  89. package/core/virtual-route/virtual-route.d.ts.map +1 -1
  90. package/core/virtual-route/virtual-route.js +1 -1
  91. package/core/virtual-route/virtual-route.types.cjs +2 -0
  92. package/core/virtual-route/virtual-route.types.d.cts +57 -0
  93. package/core/virtual-route/virtual-route.types.d.cts.map +1 -0
  94. package/core/virtual-route/virtual-route.types.d.ts +2 -2
  95. package/core/virtual-route/virtual-route.types.d.ts.map +1 -1
  96. package/index.cjs +18 -0
  97. package/index.d.cts +3 -0
  98. package/index.d.cts.map +1 -0
  99. package/index.d.ts +1 -1
  100. package/index.d.ts.map +1 -1
  101. package/index.js +1 -1
  102. package/package.json +29 -128
  103. package/react/components/index.cjs +21 -0
  104. package/react/components/index.d.cts +4 -0
  105. package/react/components/index.d.cts.map +1 -0
  106. package/react/components/index.d.ts +3 -3
  107. package/react/components/index.d.ts.map +1 -1
  108. package/react/components/index.js +3 -3
  109. package/react/components/link.cjs +79 -0
  110. package/react/components/link.d.cts +23 -0
  111. package/react/components/link.d.cts.map +1 -0
  112. package/react/components/link.d.ts +1 -1
  113. package/react/components/link.d.ts.map +1 -1
  114. package/react/components/link.js +2 -4
  115. package/react/components/route-view-group.cjs +57 -0
  116. package/react/components/route-view-group.d.cts +26 -0
  117. package/react/components/route-view-group.d.cts.map +1 -0
  118. package/react/components/route-view-group.d.ts +1 -1
  119. package/react/components/route-view-group.d.ts.map +1 -1
  120. package/react/components/route-view-group.js +3 -3
  121. package/react/components/route-view.cjs +43 -0
  122. package/react/components/route-view.d.cts +25 -0
  123. package/react/components/route-view.d.cts.map +1 -0
  124. package/react/components/route-view.d.ts +1 -1
  125. package/react/components/route-view.d.ts.map +1 -1
  126. package/react/index.cjs +17 -0
  127. package/react/index.d.cts +2 -0
  128. package/react/index.d.cts.map +1 -0
  129. package/react/index.d.ts +1 -1
  130. package/react/index.d.ts.map +1 -1
  131. package/react/index.js +1 -1
  132. package/view-model/index.cjs +17 -0
  133. package/view-model/index.d.cts +2 -0
  134. package/view-model/index.d.cts.map +1 -0
  135. package/view-model/index.d.ts +1 -1
  136. package/view-model/index.d.ts.map +1 -1
  137. package/view-model/index.js +1 -1
  138. package/view-model/route-view-model.cjs +37 -0
  139. package/view-model/route-view-model.d.cts +13 -0
  140. package/view-model/route-view-model.d.cts.map +1 -0
  141. package/view-model/route-view-model.d.ts +1 -1
  142. package/view-model/route-view-model.d.ts.map +1 -1
  143. package/view-model/route-view-model.js +1 -1
  144. package/assets/logo.png +0 -0
  145. package/assets/logo.pxz +0 -0
  146. package/assets/new-logo.pxz +0 -0
  147. package/core/route/route.test.d.ts +0 -19
  148. package/core/route/route.test.d.ts.map +0 -1
  149. package/core/route/route.test.js +0 -250
  150. package/core/utils/build-url.d.ts +0 -3
  151. package/core/utils/build-url.d.ts.map +0 -1
  152. package/core/utils/build-url.js +0 -12
  153. package/react/components/route-view-group.test.d.ts +0 -2
  154. package/react/components/route-view-group.test.d.ts.map +0 -1
  155. package/react/components/route-view-group.test.js +0 -101
package/README.md CHANGED
@@ -2,13 +2,12 @@
2
2
 
3
3
  # mobx-route
4
4
 
5
- [![NPM version][npm-image]][npm-url] [![test status][github-test-actions-image]][github-actions-url] [![build status][github-build-actions-image]][github-actions-url] [![npm download][download-image]][download-url] [![bundle size][bundlephobia-image]][bundlephobia-url]
5
+ [![NPM version][npm-image]][npm-url] [![build status][github-build-actions-image]][github-actions-url] [![npm download][download-image]][download-url] [![bundle size][bundlephobia-image]][bundlephobia-url]
6
6
 
7
7
 
8
8
  [npm-image]: http://img.shields.io/npm/v/mobx-route.svg
9
9
  [npm-url]: http://npmjs.org/package/mobx-route
10
- [github-build-actions-image]: https://github.com/js2me/mobx-route/workflows/Build/badge.svg
11
- [github-test-actions-image]: https://github.com/js2me/mobx-route/workflows/Test/badge.svg
10
+ [github-build-actions-image]: https://github.com/js2me/mobx-route/workflows/Builds,%20tests%20&%20co/badge.svg
12
11
  [github-actions-url]: https://github.com/js2me/mobx-route/actions
13
12
  [download-image]: https://img.shields.io/npm/dm/mobx-route.svg
14
13
  [download-url]: https://npmjs.org/package/mobx-route
@@ -16,11 +15,10 @@
16
15
  [bundlephobia-image]: https://badgen.net/bundlephobia/minzip/mobx-route
17
16
 
18
17
 
18
+ 🚀 Simple and lightweight typed MobX router 🚀
19
19
  _Uses [`path-to-regexp` power](https://www.npmjs.com/package/path-to-regexp)_
20
20
 
21
-
22
- ## WIP
23
- ## Documentation is [here](https://js2me.github.io/mobx-route)
21
+ ### [Read the docs →](https://js2me.github.io/mobx-route/)
24
22
 
25
23
 
26
24
  ```ts
@@ -28,7 +26,7 @@ import { Route } from "mobx-route";
28
26
 
29
27
  const userDetails = new Route("/users/:id");
30
28
 
31
- userDetails.open({ id: 1 }); // path params are required
29
+ await userDetails.open({ id: 1 }); // path params are required
32
30
 
33
31
  userDetails.isOpened; // true;
34
32
  ```
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.routeConfig = void 0;
4
+ const mobx_location_history_1 = require("mobx-location-history");
5
+ const complex_1 = require("yummies/complex");
6
+ let localHistory;
7
+ let localQueryParams;
8
+ exports.routeConfig = (0, complex_1.createGlobalDynamicConfig)((update) => {
9
+ if (localHistory && update?.history && (0, mobx_location_history_1.isObservableHistory)(localHistory)) {
10
+ localHistory.destroy();
11
+ }
12
+ let history;
13
+ if (update?.history) {
14
+ history = update.history;
15
+ }
16
+ else {
17
+ history = localHistory = (0, mobx_location_history_1.createBrowserHistory)();
18
+ }
19
+ let queryParams;
20
+ if (update?.history && !update.queryParams) {
21
+ if (localQueryParams) {
22
+ localQueryParams.destroy();
23
+ }
24
+ queryParams = localQueryParams = new mobx_location_history_1.QueryParams({ history });
25
+ }
26
+ else {
27
+ if (localQueryParams && update?.queryParams) {
28
+ localQueryParams.destroy();
29
+ }
30
+ if (update?.queryParams) {
31
+ queryParams = update.queryParams;
32
+ }
33
+ else {
34
+ queryParams = localQueryParams = new mobx_location_history_1.QueryParams({ history });
35
+ }
36
+ }
37
+ return {
38
+ ...update,
39
+ history,
40
+ location,
41
+ queryParams,
42
+ };
43
+ });
@@ -0,0 +1,7 @@
1
+ import type { RouteGlobalConfig } from "./config.types.cjs";
2
+ export declare const routeConfig: {
3
+ get: () => RouteGlobalConfig;
4
+ set: (value: RouteGlobalConfig | null | undefined) => RouteGlobalConfig | null | undefined;
5
+ update: (value: Partial<RouteGlobalConfig>) => void;
6
+ };
7
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/core/config/config.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,iBAAiB,EAAE,2BAA0B;AAK3D,eAAO,MAAM,WAAW;;;;CAuCvB,CAAC"}
@@ -1,4 +1,4 @@
1
- import type { RouteGlobalConfig } from './config.types.js';
1
+ import type { RouteGlobalConfig } from "./config.types.js";
2
2
  export declare const routeConfig: {
3
3
  get: () => RouteGlobalConfig;
4
4
  set: (value: RouteGlobalConfig | null | undefined) => RouteGlobalConfig | null | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/core/config/config.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAK3D,eAAO,MAAM,WAAW;;;;CAuCvB,CAAC"}
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/core/config/config.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,iBAAiB,EAAE,0BAA0B;AAK3D,eAAO,MAAM,WAAW;;;;CAuCvB,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,11 @@
1
+ import type { History, IQueryParams } from 'mobx-location-history';
2
+ import type { UrlCreateParamsFn } from "../route/route.types.cjs";
3
+ export interface RouteGlobalConfig {
4
+ history: History;
5
+ queryParams: IQueryParams;
6
+ baseUrl?: string;
7
+ mergeQuery?: boolean;
8
+ createUrl?: UrlCreateParamsFn;
9
+ formatLinkHref?: (href: string) => string;
10
+ }
11
+ //# sourceMappingURL=config.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.types.d.ts","sourceRoot":"","sources":["../../../src/core/config/config.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,KAAK,EAAE,iBAAiB,EAAE,iCAAgC;AAEjE,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,YAAY,CAAC;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,SAAS,CAAC,EAAE,iBAAiB,CAAC;IAC9B,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;CAC3C"}
@@ -1,5 +1,5 @@
1
1
  import type { History, IQueryParams } from 'mobx-location-history';
2
- import type { UrlCreateParamsFn } from '../route/route.types.js';
2
+ import type { UrlCreateParamsFn } from "../route/route.types.js";
3
3
  export interface RouteGlobalConfig {
4
4
  history: History;
5
5
  queryParams: IQueryParams;
@@ -1 +1 @@
1
- {"version":3,"file":"config.types.d.ts","sourceRoot":"","sources":["../../../src/core/config/config.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAEjE,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,YAAY,CAAC;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,SAAS,CAAC,EAAE,iBAAiB,CAAC;IAC9B,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;CAC3C"}
1
+ {"version":3,"file":"config.types.d.ts","sourceRoot":"","sources":["../../../src/core/config/config.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAAgC;AAEjE,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,YAAY,CAAC;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,SAAS,CAAC,EAAE,iBAAiB,CAAC;IAC9B,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;CAC3C"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./config.cjs"), exports);
18
+ __exportStar(require("./config.types.cjs"), exports);
@@ -0,0 +1,3 @@
1
+ export * from "./config.cjs";
2
+ export * from "./config.types.cjs";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/config/index.ts"],"names":[],"mappings":"AAAA,6BAA4B;AAC5B,mCAAkC"}
@@ -1,3 +1,3 @@
1
- export * from './config.js';
2
- export * from './config.types.js';
1
+ export * from "./config.js";
2
+ export * from "./config.types.js";
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/config/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/config/index.ts"],"names":[],"mappings":"AAAA,4BAA4B;AAC5B,kCAAkC"}
@@ -1,2 +1,2 @@
1
- export * from './config.js';
2
- export * from './config.types.js';
1
+ export * from "./config.js";
2
+ export * from "./config.types.js";
package/core/index.cjs ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./config/index.cjs"), exports);
18
+ __exportStar(require("./route/index.cjs"), exports);
19
+ __exportStar(require("./route-group/index.cjs"), exports);
20
+ __exportStar(require("./router/index.cjs"), exports);
21
+ __exportStar(require("./virtual-route/index.cjs"), exports);
@@ -0,0 +1,6 @@
1
+ export * from "./config/index.cjs";
2
+ export * from "./route/index.cjs";
3
+ export * from "./route-group/index.cjs";
4
+ export * from "./router/index.cjs";
5
+ export * from "./virtual-route/index.cjs";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,mCAAkC;AAClC,kCAAiC;AACjC,wCAAuC;AACvC,mCAAkC;AAClC,0CAAyC"}
package/core/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export * from './config/index.js';
2
- export * from './route/index.js';
3
- export * from './route-group/index.js';
4
- export * from './router/index.js';
5
- export * from './virtual-route/index.js';
1
+ export * from "./config/index.js";
2
+ export * from "./route/index.js";
3
+ export * from "./route-group/index.js";
4
+ export * from "./router/index.js";
5
+ export * from "./virtual-route/index.js";
6
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,iCAAiC;AACjC,uCAAuC;AACvC,kCAAkC;AAClC,yCAAyC"}
package/core/index.js CHANGED
@@ -1,5 +1,5 @@
1
- export * from './config/index.js';
2
- export * from './route/index.js';
3
- export * from './route-group/index.js';
4
- export * from './router/index.js';
5
- export * from './virtual-route/index.js';
1
+ export * from "./config/index.js";
2
+ export * from "./route/index.js";
3
+ export * from "./route-group/index.js";
4
+ export * from "./router/index.js";
5
+ export * from "./virtual-route/index.js";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./route.cjs"), exports);
18
+ __exportStar(require("./route.types.cjs"), exports);
@@ -0,0 +1,3 @@
1
+ export * from "./route.cjs";
2
+ export * from "./route.types.cjs";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/route/index.ts"],"names":[],"mappings":"AAAA,4BAA2B;AAC3B,kCAAiC"}
@@ -1,3 +1,3 @@
1
- export * from './route.js';
2
- export * from './route.types.js';
1
+ export * from "./route.js";
2
+ export * from "./route.types.js";
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/route/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/route/index.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAC3B,iCAAiC"}
@@ -1,2 +1,2 @@
1
- export * from './route.js';
2
- export * from './route.types.js';
1
+ export * from "./route.js";
2
+ export * from "./route.types.js";
@@ -0,0 +1,296 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRoute = exports.Route = void 0;
4
+ const linked_abort_controller_1 = require("linked-abort-controller");
5
+ const mobx_1 = require("mobx");
6
+ const mobx_location_history_1 = require("mobx-location-history");
7
+ const path_to_regexp_1 = require("path-to-regexp");
8
+ const config_js_1 = require("../config/config.cjs");
9
+ /**
10
+ * Class for creating path based route.
11
+ *
12
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html)
13
+ */
14
+ class Route {
15
+ path;
16
+ config;
17
+ abortController;
18
+ history;
19
+ parent;
20
+ query;
21
+ _tokenData;
22
+ _matcher;
23
+ _compiler;
24
+ reactionDisposer;
25
+ meta;
26
+ /**
27
+ * Indicates if this route is an index route. Index routes activate when parent route path matches exactly.
28
+ *
29
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#isindex-boolean)
30
+ */
31
+ isIndex;
32
+ /**
33
+ * Indicates if this route is an hash route.
34
+ *
35
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#ishash-boolean)
36
+ */
37
+ isHash;
38
+ children = [];
39
+ constructor(path, config = {}) {
40
+ this.path = path;
41
+ this.config = config;
42
+ this.abortController = new linked_abort_controller_1.LinkedAbortController(config.abortSignal);
43
+ this.history = config.history ?? config_js_1.routeConfig.get().history;
44
+ this.query = config.queryParams ?? config_js_1.routeConfig.get().queryParams;
45
+ this.isIndex = !!this.config.index;
46
+ this.isHash = !!this.config.hash;
47
+ this.meta = this.config.meta;
48
+ this.parent = config.parent ?? null;
49
+ mobx_1.computed.struct(this, 'isOpened');
50
+ mobx_1.computed.struct(this, 'data');
51
+ mobx_1.computed.struct(this, 'params');
52
+ mobx_1.computed.struct(this, 'currentPath');
53
+ mobx_1.computed.struct(this, 'hasOpenedChildren');
54
+ mobx_1.computed.struct(this, 'isAbleToMergeQuery');
55
+ (0, mobx_1.computed)(this, 'baseUrl');
56
+ (0, mobx_1.observable)(this, 'children');
57
+ mobx_1.observable.ref(this, 'parent');
58
+ (0, mobx_1.action)(this, 'addChildren');
59
+ (0, mobx_1.action)(this, 'removeChildren');
60
+ (0, mobx_1.makeObservable)(this);
61
+ (0, mobx_1.onBecomeObserved)(this, 'isOpened', () => {
62
+ if (!config.afterOpen && !config.afterClose) {
63
+ return;
64
+ }
65
+ this.reactionDisposer = (0, mobx_1.reaction)(() => this.isOpened, this.processOpenedState, {
66
+ signal: this.abortController.signal,
67
+ fireImmediately: true,
68
+ });
69
+ });
70
+ (0, mobx_1.onBecomeUnobserved)(this, 'isOpened', () => {
71
+ this.reactionDisposer?.();
72
+ this.reactionDisposer = undefined;
73
+ });
74
+ }
75
+ get baseUrl() {
76
+ const baseUrl = this.config.baseUrl ?? config_js_1.routeConfig.get().baseUrl;
77
+ return baseUrl?.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
78
+ }
79
+ get parsedPathData() {
80
+ let pathnameToCheck;
81
+ if (this.isHash) {
82
+ pathnameToCheck = this.history.location.hash.slice(1);
83
+ }
84
+ else {
85
+ pathnameToCheck = this.history.location.pathname;
86
+ }
87
+ if (this.baseUrl) {
88
+ if (!this.history.location.pathname.startsWith(this.baseUrl)) {
89
+ return null;
90
+ }
91
+ pathnameToCheck = pathnameToCheck.replace(this.baseUrl, '');
92
+ }
93
+ if ((this.path === '' || this.path === '/') &&
94
+ (pathnameToCheck === '/' || pathnameToCheck === '')) {
95
+ return { params: {}, path: pathnameToCheck };
96
+ }
97
+ this._matcher ??= (0, path_to_regexp_1.match)(this.tokenData);
98
+ const parsed = this._matcher(pathnameToCheck);
99
+ if (parsed === false) {
100
+ return null;
101
+ }
102
+ return parsed;
103
+ }
104
+ /**
105
+ * Matched path segment for current URL.
106
+ *
107
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#currentpath-parsedpathname-null)
108
+ */
109
+ get currentPath() {
110
+ return this.parsedPathData?.path ?? null;
111
+ }
112
+ /**
113
+ * Current parsed path parameters.
114
+ *
115
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#params-parsedpathparams-null)
116
+ */
117
+ get params() {
118
+ if (!this.parsedPathData?.params) {
119
+ return null;
120
+ }
121
+ let params = this.parsedPathData?.params ?? null;
122
+ if (this.config.params) {
123
+ const result = this.config.params(this.parsedPathData.params, this.config.meta);
124
+ if (result) {
125
+ params = result;
126
+ }
127
+ else {
128
+ return null;
129
+ }
130
+ }
131
+ return params;
132
+ }
133
+ /**
134
+ * Defines the "open" state for this route.
135
+ *
136
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#isopened-boolean)
137
+ */
138
+ get isOpened() {
139
+ if (this.params === null || this.parsedPathData === null) {
140
+ return false;
141
+ }
142
+ return (!this.config.checkOpened || this.config.checkOpened(this.parsedPathData));
143
+ }
144
+ /**
145
+ * Allows to create child route based on this route with merging this route path and extending path.
146
+ *
147
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#extend-path-config-route)
148
+ */
149
+ extend(path, config) {
150
+ // biome-ignore lint/correctness/noUnusedVariables: this is need to extract unused fields
151
+ const { index, params, ...configFromCurrentRoute } = this.config;
152
+ const extendedChild = new Route(`${this.path}${path}`, {
153
+ ...configFromCurrentRoute,
154
+ ...config,
155
+ parent: this,
156
+ });
157
+ this.addChildren(extendedChild);
158
+ return extendedChild;
159
+ }
160
+ addChildren(...routes) {
161
+ this.children.push(...routes);
162
+ }
163
+ removeChildren(...routes) {
164
+ this.children = this.children.filter((child) => !routes.includes(child));
165
+ }
166
+ /**
167
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#hasopenedchildren-boolean)
168
+ */
169
+ get hasOpenedChildren() {
170
+ return this.children.some((child) => child.isOpened || child.hasOpenedChildren);
171
+ }
172
+ processParams(params) {
173
+ if (params == null)
174
+ return undefined;
175
+ return Object.entries(params).reduce((acc, [key, value]) => {
176
+ if (value != null) {
177
+ acc[key] = Array.isArray(value) ? value.map(String) : String(value);
178
+ }
179
+ return acc;
180
+ }, {});
181
+ }
182
+ createUrl(...args) {
183
+ const params = args[0];
184
+ const rawQuery = args[1];
185
+ const mergeQueryOrOutputParams = args[2] ?? this.isAbleToMergeQuery;
186
+ const outputParams = typeof mergeQueryOrOutputParams === 'boolean'
187
+ ? { mergeQuery: mergeQueryOrOutputParams }
188
+ : mergeQueryOrOutputParams;
189
+ const query = outputParams?.mergeQuery
190
+ ? { ...this.query.data, ...rawQuery }
191
+ : (rawQuery ?? {});
192
+ this._compiler ??= (0, path_to_regexp_1.compile)(this.tokenData);
193
+ const defaultUrlCreateParams = {
194
+ baseUrl: this.baseUrl,
195
+ params: params,
196
+ query,
197
+ };
198
+ const urlCreateParams = this.config.createUrl?.(defaultUrlCreateParams, this.query.data) ??
199
+ config_js_1.routeConfig.get().createUrl?.(defaultUrlCreateParams, this.query.data) ??
200
+ defaultUrlCreateParams;
201
+ const path = this._compiler(this.processParams(urlCreateParams.params));
202
+ const url = [urlCreateParams.baseUrl, this.isHash ? '#' : '', path].join('');
203
+ if (outputParams?.omitQuery) {
204
+ return url;
205
+ }
206
+ return `${url}${(0, mobx_location_history_1.buildSearchString)(urlCreateParams.query)}`;
207
+ }
208
+ /**
209
+ * Navigates to this route.
210
+ *
211
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#open-args)
212
+ */
213
+ async open(...args) {
214
+ const { replace, state: rawState, query: rawQuery, mergeQuery: rawMergeQuery, } = typeof args[1] === 'boolean' || args.length > 2
215
+ ? { replace: args[1], query: args[2] }
216
+ : (args[1] ?? {});
217
+ let url;
218
+ let params;
219
+ const mergeQuery = rawMergeQuery ?? this.isAbleToMergeQuery;
220
+ const query = mergeQuery ? { ...this.query.data, ...rawQuery } : rawQuery;
221
+ if (typeof args[0] === 'string') {
222
+ url = args[0];
223
+ }
224
+ else {
225
+ params = args[0];
226
+ url = this.createUrl(args[0], query);
227
+ }
228
+ const state = rawState ?? null;
229
+ const navigationData = {
230
+ url,
231
+ params: params,
232
+ replace,
233
+ state,
234
+ query,
235
+ };
236
+ const feedback = await this.beforeOpen(navigationData);
237
+ if (feedback === false) {
238
+ return;
239
+ }
240
+ if (typeof feedback === 'object') {
241
+ Object.assign(navigationData, feedback);
242
+ }
243
+ if (replace) {
244
+ this.history.replace(url, state);
245
+ }
246
+ else {
247
+ this.history.push(url, state);
248
+ }
249
+ if (!this.reactionDisposer && this.isOpened) {
250
+ this.config.afterOpen?.(this.parsedPathData, this);
251
+ }
252
+ }
253
+ beforeOpen(openData) {
254
+ if (this.config.beforeOpen) {
255
+ return this.config.beforeOpen(openData);
256
+ }
257
+ return true;
258
+ }
259
+ afterClose() {
260
+ if (this.config.afterClose) {
261
+ return this.config.afterClose();
262
+ }
263
+ return true;
264
+ }
265
+ get tokenData() {
266
+ if (!this._tokenData) {
267
+ this._tokenData = (0, path_to_regexp_1.parse)(this.path, this.config.parseOptions);
268
+ }
269
+ return this._tokenData;
270
+ }
271
+ firstOpenedStateCheck = true;
272
+ processOpenedState = (isOpened) => {
273
+ if (this.firstOpenedStateCheck) {
274
+ this.firstOpenedStateCheck = false;
275
+ // ignore first 'afterClose' callback call
276
+ if (!isOpened) {
277
+ return;
278
+ }
279
+ }
280
+ if (isOpened) {
281
+ this.config.afterOpen?.(this.parsedPathData, this);
282
+ }
283
+ else {
284
+ this.config.afterClose?.();
285
+ }
286
+ };
287
+ get isAbleToMergeQuery() {
288
+ return this.config.mergeQuery ?? config_js_1.routeConfig.get().mergeQuery;
289
+ }
290
+ destroy() {
291
+ this.abortController.abort();
292
+ }
293
+ }
294
+ exports.Route = Route;
295
+ const createRoute = (path, config) => new Route(path, config);
296
+ exports.createRoute = createRoute;