router-kit 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,48 +1,49 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useEffect, useState } from "react";
|
|
3
3
|
import join from "url-join";
|
|
4
|
+
import { routes } from "../core/createRouter";
|
|
4
5
|
import Page404 from "../pages/404";
|
|
5
6
|
import RouterContext from "./RouterContext";
|
|
6
|
-
import { router } from "../core/createRouter";
|
|
7
7
|
const RouterProvider = () => {
|
|
8
8
|
const [path, setPath] = useState(window.location.pathname);
|
|
9
9
|
let fullPathWithParams = "";
|
|
10
|
-
const pathValidation = (
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
fakePath = "";
|
|
23
|
-
return true;
|
|
10
|
+
const pathValidation = (routeFullPath, currentPath) => {
|
|
11
|
+
const routeParts = routeFullPath.split("/").filter(Boolean);
|
|
12
|
+
const pathParts = currentPath.split("/").filter(Boolean);
|
|
13
|
+
if (routeParts.length !== pathParts.length)
|
|
14
|
+
return false;
|
|
15
|
+
for (let i = 0; i < routeParts.length; i++) {
|
|
16
|
+
const r = routeParts[i];
|
|
17
|
+
const p = pathParts[i];
|
|
18
|
+
if (r.startsWith(":"))
|
|
19
|
+
continue;
|
|
20
|
+
if (r !== p)
|
|
21
|
+
return false;
|
|
24
22
|
}
|
|
25
|
-
|
|
26
|
-
return false;
|
|
23
|
+
return true;
|
|
27
24
|
};
|
|
28
|
-
const getComponent = (
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (
|
|
32
|
-
|
|
25
|
+
const getComponent = (routesList, currentPath, parentPath = "/") => {
|
|
26
|
+
for (const route of routesList) {
|
|
27
|
+
const is404 = route.path === "404" || route.path === "/404";
|
|
28
|
+
if (is404 && route.component) {
|
|
29
|
+
// don't return here; keep as fallback at top-level
|
|
30
|
+
}
|
|
33
31
|
const fullPath = join(parentPath, `/${route.path}`);
|
|
34
32
|
if (pathValidation(fullPath, currentPath)) {
|
|
35
33
|
fullPathWithParams = fullPath;
|
|
36
|
-
|
|
37
|
-
break;
|
|
34
|
+
return route.component;
|
|
38
35
|
}
|
|
39
36
|
if (route.children) {
|
|
40
|
-
|
|
37
|
+
const childMatch = getComponent(route.children, currentPath, fullPath);
|
|
38
|
+
if (childMatch)
|
|
39
|
+
return childMatch;
|
|
41
40
|
}
|
|
42
41
|
}
|
|
43
|
-
return
|
|
42
|
+
return null;
|
|
44
43
|
};
|
|
45
|
-
|
|
44
|
+
fullPathWithParams = "";
|
|
45
|
+
const matchedComponent = getComponent(routes, path);
|
|
46
|
+
const component = matchedComponent !== null && matchedComponent !== void 0 ? matchedComponent : _jsx(Page404, {});
|
|
46
47
|
const navigate = (to, options) => {
|
|
47
48
|
if (options && options.replace) {
|
|
48
49
|
window.history.replaceState({}, "", to);
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const normalizedRoutes = routes.map((route) => {
|
|
1
|
+
function normalizeRoutes(inputRoutes) {
|
|
2
|
+
const normalizedRoutes = inputRoutes.map((route) => {
|
|
4
3
|
var _a, _b;
|
|
5
4
|
const normalizedPath = ((_a = route.path) === null || _a === void 0 ? void 0 : _a.startsWith("/"))
|
|
6
5
|
? route.path.replace(/^\/+/, "")
|
|
@@ -14,10 +13,13 @@ function normalizeRoutes(routes) {
|
|
|
14
13
|
}
|
|
15
14
|
return normalized;
|
|
16
15
|
});
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
routes.length = 0;
|
|
17
|
+
routes.push(...normalizedRoutes);
|
|
18
|
+
return routes;
|
|
19
19
|
}
|
|
20
|
-
function createRouter(
|
|
21
|
-
|
|
20
|
+
function createRouter(inputRoutes) {
|
|
21
|
+
// normalize and mutate the exported `routes` array in-place
|
|
22
|
+
return normalizeRoutes(inputRoutes);
|
|
22
23
|
}
|
|
24
|
+
export const routes = [];
|
|
23
25
|
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.7",
|
|
9
9
|
"description": "A small React routing provider library",
|
|
10
10
|
"main": "dist/index.js",
|
|
11
11
|
"types": "dist/index.d.ts",
|
|
@@ -42,5 +42,13 @@
|
|
|
42
42
|
"route-provider",
|
|
43
43
|
"route-kit"
|
|
44
44
|
],
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "https://github.com/Mohammed-Ben-Cheikh/router-kit.git"
|
|
48
|
+
},
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/Mohammed-Ben-Cheikh/router-kit/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/Mohammed-Ben-Cheikh/router-kit#readme",
|
|
45
53
|
"license": "MIT"
|
|
46
54
|
}
|