reactive-route 0.0.1-alpha.0
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.
- package/LICENSE +21 -0
- package/README.md +10 -0
- package/dist/cjs/index.js +186 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/react/index.js +148 -0
- package/dist/cjs/react/package.json +1 -0
- package/dist/cjs/solid/index.js +160 -0
- package/dist/cjs/solid/package.json +1 -0
- package/dist/esm/index.js +153 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/react/index.js +125 -0
- package/dist/esm/react/package.json +1 -0
- package/dist/esm/solid/index.js +137 -0
- package/dist/esm/solid/package.json +1 -0
- package/dist/types/core/createRouterConfig.d.ts +16 -0
- package/dist/types/core/createRouterConfig.d.ts.map +1 -0
- package/dist/types/core/createRouterStore.d.ts +13 -0
- package/dist/types/core/createRouterStore.d.ts.map +1 -0
- package/dist/types/core/index.d.ts +12 -0
- package/dist/types/core/index.d.ts.map +1 -0
- package/dist/types/core/types/InterfaceRouterStore.d.ts +10 -0
- package/dist/types/core/types/InterfaceRouterStore.d.ts.map +1 -0
- package/dist/types/core/types/TypeCurrentRoute.d.ts +10 -0
- package/dist/types/core/types/TypeCurrentRoute.d.ts.map +1 -0
- package/dist/types/core/types/TypeRedirectToParams.d.ts +24 -0
- package/dist/types/core/types/TypeRedirectToParams.d.ts.map +1 -0
- package/dist/types/core/types/TypeRoute.d.ts +8 -0
- package/dist/types/core/types/TypeRoute.d.ts.map +1 -0
- package/dist/types/core/types/TypeRouteRaw.d.ts +35 -0
- package/dist/types/core/types/TypeRouteRaw.d.ts.map +1 -0
- package/dist/types/core/types/TypeRouteWithParams.d.ts +6 -0
- package/dist/types/core/types/TypeRouteWithParams.d.ts.map +1 -0
- package/dist/types/core/types/TypeValidator.d.ts +2 -0
- package/dist/types/core/types/TypeValidator.d.ts.map +1 -0
- package/dist/types/core/utils/addNames.d.ts +8 -0
- package/dist/types/core/utils/addNames.d.ts.map +1 -0
- package/dist/types/core/utils/constants.d.ts +8 -0
- package/dist/types/core/utils/constants.d.ts.map +1 -0
- package/dist/types/core/utils/dynamic.d.ts +8 -0
- package/dist/types/core/utils/dynamic.d.ts.map +1 -0
- package/dist/types/core/utils/findRouteByPathname.d.ts +6 -0
- package/dist/types/core/utils/findRouteByPathname.d.ts.map +1 -0
- package/dist/types/core/utils/getDynamicValues.d.ts +6 -0
- package/dist/types/core/utils/getDynamicValues.d.ts.map +1 -0
- package/dist/types/core/utils/getInitialRoute.d.ts +8 -0
- package/dist/types/core/utils/getInitialRoute.d.ts.map +1 -0
- package/dist/types/core/utils/getQueryValues.d.ts +6 -0
- package/dist/types/core/utils/getQueryValues.d.ts.map +1 -0
- package/dist/types/core/utils/getTypedEntries.d.ts +6 -0
- package/dist/types/core/utils/getTypedEntries.d.ts.map +1 -0
- package/dist/types/core/utils/history.d.ts +7 -0
- package/dist/types/core/utils/history.d.ts.map +1 -0
- package/dist/types/core/utils/loadComponentToConfig.d.ts +5 -0
- package/dist/types/core/utils/loadComponentToConfig.d.ts.map +1 -0
- package/dist/types/core/utils/replaceDynamicValues.d.ts +8 -0
- package/dist/types/core/utils/replaceDynamicValues.d.ts.map +1 -0
- package/dist/types/react/Router.d.ts +13 -0
- package/dist/types/react/Router.d.ts.map +1 -0
- package/dist/types/react/index.d.ts +2 -0
- package/dist/types/react/index.d.ts.map +1 -0
- package/dist/types/react/useStore.d.ts +8 -0
- package/dist/types/react/useStore.d.ts.map +1 -0
- package/dist/types/solid/Router.d.ts +11 -0
- package/dist/types/solid/Router.d.ts.map +1 -0
- package/dist/types/solid/index.d.ts +2 -0
- package/dist/types/solid/index.d.ts.map +1 -0
- package/dist/types/solid/replaceObject.d.ts +2 -0
- package/dist/types/solid/replaceObject.d.ts.map +1 -0
- package/dist/types/solid/useStore.d.ts +7 -0
- package/dist/types/solid/useStore.d.ts.map +1 -0
- package/dist/types/tsconfig.types.tsbuildinfo +1 -0
- package/package.json +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dmitry
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
## Config-based routing
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
[](https://www.npmjs.com/package/reactive-route)
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
> [!WARNING]
|
|
10
|
+
> Work in progress
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
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
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// packages/core/index.ts
|
|
31
|
+
var core_exports = {};
|
|
32
|
+
__export(core_exports, {
|
|
33
|
+
createRouterConfig: () => createRouterConfig,
|
|
34
|
+
findRouteByPathname: () => findRouteByPathname,
|
|
35
|
+
getInitialRoute: () => getInitialRoute,
|
|
36
|
+
history: () => history,
|
|
37
|
+
isDynamicRoute: () => isDynamicRoute,
|
|
38
|
+
loadComponentToConfig: () => loadComponentToConfig,
|
|
39
|
+
replaceDynamicValues: () => replaceDynamicValues
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(core_exports);
|
|
42
|
+
|
|
43
|
+
// packages/core/utils/addNames.ts
|
|
44
|
+
function addNames(obj) {
|
|
45
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
46
|
+
value.name = key;
|
|
47
|
+
});
|
|
48
|
+
return obj;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// packages/core/createRouterConfig.ts
|
|
52
|
+
function createRouterConfig(config) {
|
|
53
|
+
return addNames(config);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// packages/core/utils/constants.ts
|
|
57
|
+
var constants = {
|
|
58
|
+
dynamicSeparator: ":",
|
|
59
|
+
pathPartSeparator: "/",
|
|
60
|
+
isClient: typeof window !== "undefined",
|
|
61
|
+
errorRedirect: "REDIRECT",
|
|
62
|
+
errorPrevent: "PREVENT_REDIRECT"
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// packages/core/utils/dynamic.ts
|
|
66
|
+
function isDynamic(param) {
|
|
67
|
+
return param[0] === constants.dynamicSeparator;
|
|
68
|
+
}
|
|
69
|
+
function clearDynamic(param) {
|
|
70
|
+
return param.replace(new RegExp(`^${constants.dynamicSeparator}`), "");
|
|
71
|
+
}
|
|
72
|
+
function isDynamicRoute(route) {
|
|
73
|
+
return "params" in route;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// packages/core/utils/findRouteByPathname.ts
|
|
77
|
+
function completeStaticMatch(pathname, path) {
|
|
78
|
+
return !path.includes(constants.dynamicSeparator) && (pathname === path || pathname === `${path}${constants.pathPartSeparator}`);
|
|
79
|
+
}
|
|
80
|
+
function findRouteByPathname({
|
|
81
|
+
pathname,
|
|
82
|
+
routes
|
|
83
|
+
}) {
|
|
84
|
+
let dynamicRouteMatch;
|
|
85
|
+
const pathnameArray = pathname.replace(/\?.+$/, "").split(constants.pathPartSeparator).filter(Boolean);
|
|
86
|
+
for (const routeName in routes) {
|
|
87
|
+
if (!Object.hasOwn(routes, routeName)) continue;
|
|
88
|
+
const route = routes[routeName];
|
|
89
|
+
if (completeStaticMatch(pathname, route.path)) return route;
|
|
90
|
+
if (dynamicRouteMatch) continue;
|
|
91
|
+
const routePathnameArray = route.path.split(constants.pathPartSeparator).filter(Boolean);
|
|
92
|
+
if (routePathnameArray.length !== pathnameArray.length) continue;
|
|
93
|
+
const someParamInvalid = routePathnameArray.some((paramName, i) => {
|
|
94
|
+
const paramFromUrl = pathnameArray[i];
|
|
95
|
+
if (!isDynamic(paramName)) return paramName !== paramFromUrl;
|
|
96
|
+
const validator = route.params?.[clearDynamic(paramName)];
|
|
97
|
+
if (typeof validator !== "function") {
|
|
98
|
+
throw new Error(`findRoute: missing validator for param "${paramName}"`);
|
|
99
|
+
}
|
|
100
|
+
return !validator(paramFromUrl);
|
|
101
|
+
});
|
|
102
|
+
if (!someParamInvalid) dynamicRouteMatch = route;
|
|
103
|
+
}
|
|
104
|
+
return dynamicRouteMatch;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// packages/core/utils/getDynamicValues.ts
|
|
108
|
+
function getDynamicValues(params) {
|
|
109
|
+
const { route, pathname } = params;
|
|
110
|
+
const pathnameArray = pathname.replace(/\?.+$/, "").split(constants.pathPartSeparator).filter(Boolean).map((str) => decodeURIComponent(str));
|
|
111
|
+
const routePathnameArray = route.path.split(constants.pathPartSeparator).filter(Boolean);
|
|
112
|
+
const dynamicParams = {};
|
|
113
|
+
for (let i = 0; i < routePathnameArray.length; i++) {
|
|
114
|
+
const paramName = routePathnameArray[i];
|
|
115
|
+
if (isDynamic(paramName)) dynamicParams[clearDynamic(paramName)] = pathnameArray[i];
|
|
116
|
+
}
|
|
117
|
+
return dynamicParams;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// packages/core/utils/getQueryValues.ts
|
|
121
|
+
var import_query_string = __toESM(require("query-string"));
|
|
122
|
+
|
|
123
|
+
// packages/core/utils/getTypedEntries.ts
|
|
124
|
+
var getTypedEntries = Object.entries;
|
|
125
|
+
|
|
126
|
+
// packages/core/utils/getQueryValues.ts
|
|
127
|
+
function getQueryValues(params) {
|
|
128
|
+
const { route, pathname } = params;
|
|
129
|
+
const qs = import_query_string.default.extract(pathname);
|
|
130
|
+
if (!qs || !route.query) return {};
|
|
131
|
+
const query = import_query_string.default.parse(qs);
|
|
132
|
+
getTypedEntries(query).forEach(([key, value]) => {
|
|
133
|
+
const validator = route.query[key];
|
|
134
|
+
if (typeof validator !== "function" || !validator(value)) {
|
|
135
|
+
delete query[key];
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
return query;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// packages/core/utils/getInitialRoute.ts
|
|
142
|
+
function getInitialRoute(params) {
|
|
143
|
+
const route = findRouteByPathname({ pathname: params.pathname, routes: params.routes }) || params.routes[params.fallback];
|
|
144
|
+
return {
|
|
145
|
+
route: route.name,
|
|
146
|
+
query: getQueryValues({ route, pathname: params.pathname }),
|
|
147
|
+
params: getDynamicValues({ route, pathname: params.pathname })
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// packages/core/utils/history.ts
|
|
152
|
+
var import_history = require("history");
|
|
153
|
+
var history = constants.isClient ? (0, import_history.createBrowserHistory)() : null;
|
|
154
|
+
|
|
155
|
+
// packages/core/utils/loadComponentToConfig.ts
|
|
156
|
+
function loadComponentToConfig(params) {
|
|
157
|
+
const { route } = params;
|
|
158
|
+
if (!route.component && route.loader) {
|
|
159
|
+
const loadingFn = route.loader;
|
|
160
|
+
return loadingFn().then((module2) => {
|
|
161
|
+
const { default: component, pageName, ...rest } = module2;
|
|
162
|
+
route.component = component;
|
|
163
|
+
route.otherExports = rest;
|
|
164
|
+
route.pageName = pageName;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
return Promise.resolve();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// packages/core/utils/replaceDynamicValues.ts
|
|
171
|
+
var re = new RegExp(`[^${constants.pathPartSeparator}]+`, "g");
|
|
172
|
+
function replaceDynamicValues({
|
|
173
|
+
route,
|
|
174
|
+
params = {}
|
|
175
|
+
}) {
|
|
176
|
+
return route.path.replace(re, (paramName) => {
|
|
177
|
+
if (!isDynamic(paramName)) return paramName;
|
|
178
|
+
const value = params[clearDynamic(paramName)];
|
|
179
|
+
if (!value) {
|
|
180
|
+
throw new Error(
|
|
181
|
+
`replaceDynamicValues: no param "${paramName}" passed for route ${route.name}`
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
return encodeURIComponent(value);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/react/index.ts
|
|
21
|
+
var react_exports = {};
|
|
22
|
+
__export(react_exports, {
|
|
23
|
+
Router: () => Router
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(react_exports);
|
|
26
|
+
|
|
27
|
+
// packages/react/Router.tsx
|
|
28
|
+
var import_mobx2 = require("mobx");
|
|
29
|
+
var import_mobx_react_lite = require("mobx-react-lite");
|
|
30
|
+
var import_reactive_route = require("reactive-route");
|
|
31
|
+
|
|
32
|
+
// packages/react/useStore.ts
|
|
33
|
+
var import_mobx = require("mobx");
|
|
34
|
+
var import_react = require("react");
|
|
35
|
+
function useStore(ViewModel, props) {
|
|
36
|
+
const isFirstRenderRef = (0, import_react.useRef)(true);
|
|
37
|
+
const [vm] = (0, import_react.useState)(() => {
|
|
38
|
+
const instance = new ViewModel(props || {});
|
|
39
|
+
(0, import_mobx.runInAction)(() => {
|
|
40
|
+
instance.beforeMount?.();
|
|
41
|
+
});
|
|
42
|
+
return instance;
|
|
43
|
+
});
|
|
44
|
+
(0, import_react.useEffect)(() => {
|
|
45
|
+
if (isFirstRenderRef.current) {
|
|
46
|
+
isFirstRenderRef.current = false;
|
|
47
|
+
} else if (props) {
|
|
48
|
+
(0, import_mobx.runInAction)(() => {
|
|
49
|
+
vm.props = props || {};
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}, [props]);
|
|
53
|
+
(0, import_react.useEffect)(() => {
|
|
54
|
+
vm.afterMount?.();
|
|
55
|
+
return () => {
|
|
56
|
+
vm.autorunDisposers?.forEach((disposer) => disposer());
|
|
57
|
+
};
|
|
58
|
+
}, []);
|
|
59
|
+
return vm;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// packages/react/Router.tsx
|
|
63
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
64
|
+
var VM = class {
|
|
65
|
+
constructor(props) {
|
|
66
|
+
this.props = props;
|
|
67
|
+
(0, import_mobx2.makeAutoObservable)(
|
|
68
|
+
this,
|
|
69
|
+
{ loadedComponent: false, setLoadedComponent: false, props: false },
|
|
70
|
+
{ autoBind: true }
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
autorunDisposers = [];
|
|
74
|
+
loadedComponentName = void 0;
|
|
75
|
+
loadedComponentPage = void 0;
|
|
76
|
+
loadedComponent;
|
|
77
|
+
currentProps = {};
|
|
78
|
+
beforeMount() {
|
|
79
|
+
this.props.beforeMount?.();
|
|
80
|
+
this.redirectOnHistoryPop();
|
|
81
|
+
this.setLoadedComponent();
|
|
82
|
+
this.autorunDisposers.push((0, import_mobx2.autorun)(this.setLoadedComponent));
|
|
83
|
+
}
|
|
84
|
+
redirectOnHistoryPop() {
|
|
85
|
+
if (!import_reactive_route.history) return;
|
|
86
|
+
import_reactive_route.history.listen((params) => {
|
|
87
|
+
if (params.action !== "POP") return;
|
|
88
|
+
const previousRoutePathname = this.props.routerStore.routesHistory[this.props.routerStore.routesHistory.length - 2];
|
|
89
|
+
if (previousRoutePathname === params.location.pathname) {
|
|
90
|
+
(0, import_mobx2.runInAction)(() => this.props.routerStore.routesHistory.pop());
|
|
91
|
+
}
|
|
92
|
+
void this.props.routerStore.redirectTo({
|
|
93
|
+
noHistoryPush: true,
|
|
94
|
+
...(0, import_reactive_route.getInitialRoute)({
|
|
95
|
+
routes: this.props.routes,
|
|
96
|
+
pathname: import_reactive_route.history.location.pathname,
|
|
97
|
+
fallback: "error404"
|
|
98
|
+
})
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
setLoadedComponent = () => {
|
|
103
|
+
const { loadedComponentName, loadedComponentPage } = this;
|
|
104
|
+
const { currentRoute, isRedirecting } = this.props.routerStore;
|
|
105
|
+
const currentRouteName = currentRoute.name;
|
|
106
|
+
const currentRoutePage = currentRoute.pageName;
|
|
107
|
+
let preventRedirect = false;
|
|
108
|
+
if (isRedirecting) preventRedirect = true;
|
|
109
|
+
else if (loadedComponentName === currentRouteName) preventRedirect = true;
|
|
110
|
+
else if (loadedComponentPage != null && currentRouteName != null) {
|
|
111
|
+
if (loadedComponentPage === currentRoutePage) {
|
|
112
|
+
const componentConfig = this.props.routes[currentRouteName];
|
|
113
|
+
(0, import_mobx2.runInAction)(() => {
|
|
114
|
+
this.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
|
|
115
|
+
});
|
|
116
|
+
preventRedirect = true;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (preventRedirect) return;
|
|
120
|
+
(0, import_mobx2.runInAction)(() => {
|
|
121
|
+
if (!loadedComponentName) {
|
|
122
|
+
this.setComponent(currentRouteName);
|
|
123
|
+
} else {
|
|
124
|
+
this.props.beforeUpdatePageComponent?.();
|
|
125
|
+
this.setComponent(currentRouteName);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
setComponent(currentRouteName) {
|
|
130
|
+
(0, import_mobx2.runInAction)(() => {
|
|
131
|
+
const componentConfig = this.props.routes[currentRouteName];
|
|
132
|
+
const RouteComponent = componentConfig.component;
|
|
133
|
+
this.props.beforeSetPageComponent?.(componentConfig);
|
|
134
|
+
this.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
|
|
135
|
+
this.loadedComponentName = currentRouteName;
|
|
136
|
+
this.loadedComponentPage = componentConfig.pageName;
|
|
137
|
+
this.loadedComponent = RouteComponent;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
var Router = (0, import_mobx_react_lite.observer)(
|
|
142
|
+
(props) => {
|
|
143
|
+
const vm = useStore(VM, props);
|
|
144
|
+
const LoadedComponent = vm.loadedComponentName ? vm.loadedComponent : null;
|
|
145
|
+
if (LoadedComponent) return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LoadedComponent, { ...vm.currentProps });
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/solid/index.ts
|
|
21
|
+
var solid_exports = {};
|
|
22
|
+
__export(solid_exports, {
|
|
23
|
+
Router: () => Router
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(solid_exports);
|
|
26
|
+
|
|
27
|
+
// packages/solid/Router.tsx
|
|
28
|
+
var import_web = require("solid-js/web");
|
|
29
|
+
var import_web2 = require("solid-js/web");
|
|
30
|
+
var import_reactive_route = require("reactive-route");
|
|
31
|
+
var import_solid_js2 = require("solid-js");
|
|
32
|
+
var import_store2 = require("solid-js/store");
|
|
33
|
+
var import_web3 = require("solid-js/web");
|
|
34
|
+
|
|
35
|
+
// packages/solid/replaceObject.ts
|
|
36
|
+
var import_store = require("solid-js/store");
|
|
37
|
+
function replaceObject(obj, newObj) {
|
|
38
|
+
(0, import_store.modifyMutable)(obj, (0, import_store.produce)((state) => {
|
|
39
|
+
if (typeof state === "object" && state != null) {
|
|
40
|
+
for (const variableKey in state) {
|
|
41
|
+
delete state[variableKey];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
Object.assign(state || {}, newObj);
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// packages/solid/useStore.ts
|
|
49
|
+
var import_solid_js = require("solid-js");
|
|
50
|
+
var getAllProperties = (object) => {
|
|
51
|
+
const properties = /* @__PURE__ */ new Set();
|
|
52
|
+
do {
|
|
53
|
+
for (const key of Reflect.ownKeys(object)) {
|
|
54
|
+
properties.add([object, key]);
|
|
55
|
+
}
|
|
56
|
+
} while ((object = Reflect.getPrototypeOf(object)) && object !== Object.prototype);
|
|
57
|
+
return properties;
|
|
58
|
+
};
|
|
59
|
+
function autoBind(self) {
|
|
60
|
+
for (const [object, key] of getAllProperties(self.constructor.prototype)) {
|
|
61
|
+
if (key === "constructor") continue;
|
|
62
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(object, key);
|
|
63
|
+
if (descriptor && typeof descriptor.value === "function") {
|
|
64
|
+
self[key] = self[key].bind(self);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return self;
|
|
68
|
+
}
|
|
69
|
+
function useStore(ViewModel, props) {
|
|
70
|
+
const vm = new ViewModel(props);
|
|
71
|
+
autoBind(vm);
|
|
72
|
+
vm.beforeMount?.();
|
|
73
|
+
(0, import_solid_js.onMount)(() => {
|
|
74
|
+
vm.afterMount?.();
|
|
75
|
+
});
|
|
76
|
+
return vm;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// packages/solid/Router.tsx
|
|
80
|
+
var VM = class {
|
|
81
|
+
constructor(props) {
|
|
82
|
+
this.props = props;
|
|
83
|
+
return (0, import_store2.createMutable)(this);
|
|
84
|
+
}
|
|
85
|
+
loadedComponentName = void 0;
|
|
86
|
+
loadedComponentPage = void 0;
|
|
87
|
+
currentProps = {};
|
|
88
|
+
beforeMount() {
|
|
89
|
+
this.props.beforeMount?.();
|
|
90
|
+
this.redirectOnHistoryPop();
|
|
91
|
+
(0, import_solid_js2.createRenderEffect)(() => this.setLoadedComponent());
|
|
92
|
+
}
|
|
93
|
+
redirectOnHistoryPop() {
|
|
94
|
+
if (!import_reactive_route.history) return;
|
|
95
|
+
import_reactive_route.history.listen((params) => {
|
|
96
|
+
if (params.action !== "POP") return;
|
|
97
|
+
const previousRoutePathname = this.props.routerStore.routesHistory[this.props.routerStore.routesHistory.length - 2];
|
|
98
|
+
if (previousRoutePathname === params.location.pathname) {
|
|
99
|
+
this.props.routerStore.routesHistory.pop();
|
|
100
|
+
}
|
|
101
|
+
void this.props.routerStore.redirectTo({
|
|
102
|
+
noHistoryPush: true,
|
|
103
|
+
...(0, import_reactive_route.getInitialRoute)({
|
|
104
|
+
routes: this.props.routes,
|
|
105
|
+
pathname: import_reactive_route.history.location.pathname,
|
|
106
|
+
fallback: "error404"
|
|
107
|
+
})
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
setLoadedComponent() {
|
|
112
|
+
const currentRouteName = this.props.routerStore.currentRoute.name;
|
|
113
|
+
const currentRoutePage = this.props.routerStore.currentRoute.pageName;
|
|
114
|
+
let preventRedirect = false;
|
|
115
|
+
if (this.props.routerStore.isRedirecting) preventRedirect = true;
|
|
116
|
+
else if (this.loadedComponentName === currentRouteName) {
|
|
117
|
+
preventRedirect = true;
|
|
118
|
+
} else if (this.loadedComponentPage != null && currentRouteName != null) {
|
|
119
|
+
if (this.loadedComponentPage === currentRoutePage) {
|
|
120
|
+
const componentConfig = this.props.routes[currentRouteName];
|
|
121
|
+
replaceObject(this.currentProps, "props" in componentConfig ? componentConfig.props : {});
|
|
122
|
+
preventRedirect = true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (preventRedirect) return;
|
|
126
|
+
(0, import_solid_js2.batch)(() => {
|
|
127
|
+
if (!this.loadedComponentName) {
|
|
128
|
+
this.setComponent();
|
|
129
|
+
} else {
|
|
130
|
+
this.props.beforeUpdatePageComponent?.();
|
|
131
|
+
this.setComponent();
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
setComponent() {
|
|
136
|
+
const currentRouteName = this.props.routerStore.currentRoute.name;
|
|
137
|
+
const componentConfig = this.props.routes[currentRouteName];
|
|
138
|
+
this.props.beforeSetPageComponent?.(componentConfig);
|
|
139
|
+
(0, import_solid_js2.batch)(() => {
|
|
140
|
+
replaceObject(this.currentProps, "props" in componentConfig ? componentConfig.props : {});
|
|
141
|
+
this.loadedComponentName = currentRouteName;
|
|
142
|
+
this.loadedComponentPage = componentConfig.pageName;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
function Router(props) {
|
|
147
|
+
const vm = useStore(VM, props);
|
|
148
|
+
return (0, import_web.createComponent)(import_solid_js2.Show, {
|
|
149
|
+
get when() {
|
|
150
|
+
return vm.loadedComponentName;
|
|
151
|
+
},
|
|
152
|
+
get children() {
|
|
153
|
+
return (0, import_web.createComponent)(import_web3.Dynamic, (0, import_web2.mergeProps)({
|
|
154
|
+
get component() {
|
|
155
|
+
return props.routes[vm.loadedComponentName]?.component || void 0;
|
|
156
|
+
}
|
|
157
|
+
}, () => vm.currentProps));
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// packages/core/utils/addNames.ts
|
|
2
|
+
function addNames(obj) {
|
|
3
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
4
|
+
value.name = key;
|
|
5
|
+
});
|
|
6
|
+
return obj;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// packages/core/createRouterConfig.ts
|
|
10
|
+
function createRouterConfig(config) {
|
|
11
|
+
return addNames(config);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// packages/core/utils/constants.ts
|
|
15
|
+
var constants = {
|
|
16
|
+
dynamicSeparator: ":",
|
|
17
|
+
pathPartSeparator: "/",
|
|
18
|
+
isClient: typeof window !== "undefined",
|
|
19
|
+
errorRedirect: "REDIRECT",
|
|
20
|
+
errorPrevent: "PREVENT_REDIRECT"
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// packages/core/utils/dynamic.ts
|
|
24
|
+
function isDynamic(param) {
|
|
25
|
+
return param[0] === constants.dynamicSeparator;
|
|
26
|
+
}
|
|
27
|
+
function clearDynamic(param) {
|
|
28
|
+
return param.replace(new RegExp(`^${constants.dynamicSeparator}`), "");
|
|
29
|
+
}
|
|
30
|
+
function isDynamicRoute(route) {
|
|
31
|
+
return "params" in route;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// packages/core/utils/findRouteByPathname.ts
|
|
35
|
+
function completeStaticMatch(pathname, path) {
|
|
36
|
+
return !path.includes(constants.dynamicSeparator) && (pathname === path || pathname === `${path}${constants.pathPartSeparator}`);
|
|
37
|
+
}
|
|
38
|
+
function findRouteByPathname({
|
|
39
|
+
pathname,
|
|
40
|
+
routes
|
|
41
|
+
}) {
|
|
42
|
+
let dynamicRouteMatch;
|
|
43
|
+
const pathnameArray = pathname.replace(/\?.+$/, "").split(constants.pathPartSeparator).filter(Boolean);
|
|
44
|
+
for (const routeName in routes) {
|
|
45
|
+
if (!Object.hasOwn(routes, routeName)) continue;
|
|
46
|
+
const route = routes[routeName];
|
|
47
|
+
if (completeStaticMatch(pathname, route.path)) return route;
|
|
48
|
+
if (dynamicRouteMatch) continue;
|
|
49
|
+
const routePathnameArray = route.path.split(constants.pathPartSeparator).filter(Boolean);
|
|
50
|
+
if (routePathnameArray.length !== pathnameArray.length) continue;
|
|
51
|
+
const someParamInvalid = routePathnameArray.some((paramName, i) => {
|
|
52
|
+
const paramFromUrl = pathnameArray[i];
|
|
53
|
+
if (!isDynamic(paramName)) return paramName !== paramFromUrl;
|
|
54
|
+
const validator = route.params?.[clearDynamic(paramName)];
|
|
55
|
+
if (typeof validator !== "function") {
|
|
56
|
+
throw new Error(`findRoute: missing validator for param "${paramName}"`);
|
|
57
|
+
}
|
|
58
|
+
return !validator(paramFromUrl);
|
|
59
|
+
});
|
|
60
|
+
if (!someParamInvalid) dynamicRouteMatch = route;
|
|
61
|
+
}
|
|
62
|
+
return dynamicRouteMatch;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// packages/core/utils/getDynamicValues.ts
|
|
66
|
+
function getDynamicValues(params) {
|
|
67
|
+
const { route, pathname } = params;
|
|
68
|
+
const pathnameArray = pathname.replace(/\?.+$/, "").split(constants.pathPartSeparator).filter(Boolean).map((str) => decodeURIComponent(str));
|
|
69
|
+
const routePathnameArray = route.path.split(constants.pathPartSeparator).filter(Boolean);
|
|
70
|
+
const dynamicParams = {};
|
|
71
|
+
for (let i = 0; i < routePathnameArray.length; i++) {
|
|
72
|
+
const paramName = routePathnameArray[i];
|
|
73
|
+
if (isDynamic(paramName)) dynamicParams[clearDynamic(paramName)] = pathnameArray[i];
|
|
74
|
+
}
|
|
75
|
+
return dynamicParams;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// packages/core/utils/getQueryValues.ts
|
|
79
|
+
import queryString from "query-string";
|
|
80
|
+
|
|
81
|
+
// packages/core/utils/getTypedEntries.ts
|
|
82
|
+
var getTypedEntries = Object.entries;
|
|
83
|
+
|
|
84
|
+
// packages/core/utils/getQueryValues.ts
|
|
85
|
+
function getQueryValues(params) {
|
|
86
|
+
const { route, pathname } = params;
|
|
87
|
+
const qs = queryString.extract(pathname);
|
|
88
|
+
if (!qs || !route.query) return {};
|
|
89
|
+
const query = queryString.parse(qs);
|
|
90
|
+
getTypedEntries(query).forEach(([key, value]) => {
|
|
91
|
+
const validator = route.query[key];
|
|
92
|
+
if (typeof validator !== "function" || !validator(value)) {
|
|
93
|
+
delete query[key];
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
return query;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// packages/core/utils/getInitialRoute.ts
|
|
100
|
+
function getInitialRoute(params) {
|
|
101
|
+
const route = findRouteByPathname({ pathname: params.pathname, routes: params.routes }) || params.routes[params.fallback];
|
|
102
|
+
return {
|
|
103
|
+
route: route.name,
|
|
104
|
+
query: getQueryValues({ route, pathname: params.pathname }),
|
|
105
|
+
params: getDynamicValues({ route, pathname: params.pathname })
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// packages/core/utils/history.ts
|
|
110
|
+
import { createBrowserHistory } from "history";
|
|
111
|
+
var history = constants.isClient ? createBrowserHistory() : null;
|
|
112
|
+
|
|
113
|
+
// packages/core/utils/loadComponentToConfig.ts
|
|
114
|
+
function loadComponentToConfig(params) {
|
|
115
|
+
const { route } = params;
|
|
116
|
+
if (!route.component && route.loader) {
|
|
117
|
+
const loadingFn = route.loader;
|
|
118
|
+
return loadingFn().then((module) => {
|
|
119
|
+
const { default: component, pageName, ...rest } = module;
|
|
120
|
+
route.component = component;
|
|
121
|
+
route.otherExports = rest;
|
|
122
|
+
route.pageName = pageName;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return Promise.resolve();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// packages/core/utils/replaceDynamicValues.ts
|
|
129
|
+
var re = new RegExp(`[^${constants.pathPartSeparator}]+`, "g");
|
|
130
|
+
function replaceDynamicValues({
|
|
131
|
+
route,
|
|
132
|
+
params = {}
|
|
133
|
+
}) {
|
|
134
|
+
return route.path.replace(re, (paramName) => {
|
|
135
|
+
if (!isDynamic(paramName)) return paramName;
|
|
136
|
+
const value = params[clearDynamic(paramName)];
|
|
137
|
+
if (!value) {
|
|
138
|
+
throw new Error(
|
|
139
|
+
`replaceDynamicValues: no param "${paramName}" passed for route ${route.name}`
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
return encodeURIComponent(value);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
export {
|
|
146
|
+
createRouterConfig,
|
|
147
|
+
findRouteByPathname,
|
|
148
|
+
getInitialRoute,
|
|
149
|
+
history,
|
|
150
|
+
isDynamicRoute,
|
|
151
|
+
loadComponentToConfig,
|
|
152
|
+
replaceDynamicValues
|
|
153
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "module"}
|