router-kit 0.1.4 → 0.1.5
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.
|
@@ -3,7 +3,8 @@ import { useEffect, useState } from "react";
|
|
|
3
3
|
import join from "url-join";
|
|
4
4
|
import Page404 from "../pages/404";
|
|
5
5
|
import RouterContext from "./RouterContext";
|
|
6
|
-
|
|
6
|
+
import { router } from "../core/createRouter";
|
|
7
|
+
const RouterProvider = () => {
|
|
7
8
|
const [path, setPath] = useState(window.location.pathname);
|
|
8
9
|
let fullPathWithParams = "";
|
|
9
10
|
const pathValidation = (fullPath, path) => {
|
|
@@ -41,7 +42,7 @@ const RouterProvider = ({ routes }) => {
|
|
|
41
42
|
}
|
|
42
43
|
return component;
|
|
43
44
|
};
|
|
44
|
-
const component = getComponent(
|
|
45
|
+
const component = getComponent(router, path);
|
|
45
46
|
const navigate = (to, options) => {
|
|
46
47
|
if (options && options.replace) {
|
|
47
48
|
window.history.replaceState({}, "", to);
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
export let router = [];
|
|
2
2
|
function normalizeRoutes(routes) {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const normalizedRoutes = routes.map((route) => {
|
|
4
|
+
var _a, _b;
|
|
5
|
+
const normalizedPath = ((_a = route.path) === null || _a === void 0 ? void 0 : _a.startsWith("/"))
|
|
5
6
|
? route.path.replace(/^\/+/, "")
|
|
6
|
-
: route.path;
|
|
7
|
+
: (_b = route.path) !== null && _b !== void 0 ? _b : "";
|
|
7
8
|
const normalized = {
|
|
8
9
|
...route,
|
|
9
10
|
path: normalizedPath,
|
|
@@ -13,8 +14,10 @@ function normalizeRoutes(routes) {
|
|
|
13
14
|
}
|
|
14
15
|
return normalized;
|
|
15
16
|
});
|
|
17
|
+
router = normalizedRoutes;
|
|
18
|
+
return normalizedRoutes;
|
|
16
19
|
}
|
|
17
20
|
function createRouter(routes) {
|
|
18
|
-
|
|
21
|
+
normalizeRoutes(routes);
|
|
19
22
|
}
|
|
20
23
|
export default createRouter;
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"email": "mohammed.bencheikh.dev@gmail.com",
|
|
6
6
|
"url": "https://mohammedbencheikh.com/"
|
|
7
7
|
},
|
|
8
|
-
"version": "0.1.
|
|
8
|
+
"version": "0.1.5",
|
|
9
9
|
"description": "A small React routing provider library",
|
|
10
10
|
"main": "dist/index.js",
|
|
11
11
|
"types": "dist/index.d.ts",
|
|
@@ -37,8 +37,10 @@
|
|
|
37
37
|
"react",
|
|
38
38
|
"route",
|
|
39
39
|
"provider",
|
|
40
|
+
"kit",
|
|
40
41
|
"routing",
|
|
41
|
-
"route-provider"
|
|
42
|
+
"route-provider",
|
|
43
|
+
"route-kit"
|
|
42
44
|
],
|
|
43
45
|
"license": "MIT"
|
|
44
46
|
}
|