router-kit 0.1.4 → 0.1.6
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,47 +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
|
-
const RouterProvider = (
|
|
7
|
+
const RouterProvider = () => {
|
|
7
8
|
const [path, setPath] = useState(window.location.pathname);
|
|
8
9
|
let fullPathWithParams = "";
|
|
9
|
-
const pathValidation = (
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
fakePath = "";
|
|
22
|
-
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;
|
|
23
22
|
}
|
|
24
|
-
|
|
25
|
-
return false;
|
|
23
|
+
return true;
|
|
26
24
|
};
|
|
27
|
-
const getComponent = (
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
|
|
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
|
+
}
|
|
32
31
|
const fullPath = join(parentPath, `/${route.path}`);
|
|
33
32
|
if (pathValidation(fullPath, currentPath)) {
|
|
34
33
|
fullPathWithParams = fullPath;
|
|
35
|
-
|
|
36
|
-
break;
|
|
34
|
+
return route.component;
|
|
37
35
|
}
|
|
38
36
|
if (route.children) {
|
|
39
|
-
|
|
37
|
+
const childMatch = getComponent(route.children, currentPath, fullPath);
|
|
38
|
+
if (childMatch)
|
|
39
|
+
return childMatch;
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
return
|
|
42
|
+
return null;
|
|
43
43
|
};
|
|
44
|
-
|
|
44
|
+
fullPathWithParams = "";
|
|
45
|
+
const matchedComponent = getComponent(routes, path);
|
|
46
|
+
const component = matchedComponent !== null && matchedComponent !== void 0 ? matchedComponent : _jsx(Page404, {});
|
|
45
47
|
const navigate = (to, options) => {
|
|
46
48
|
if (options && options.replace) {
|
|
47
49
|
window.history.replaceState({}, "", to);
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
function normalizeRoutes(
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export let routes = [];
|
|
2
|
+
function normalizeRoutes(inputRoutes) {
|
|
3
|
+
const normalizedRoutes = inputRoutes.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
|
+
routes = normalizedRoutes;
|
|
18
|
+
return normalizedRoutes;
|
|
16
19
|
}
|
|
17
|
-
function createRouter(
|
|
18
|
-
return normalizeRoutes(
|
|
20
|
+
function createRouter(inputRoutes) {
|
|
21
|
+
return (routes = normalizeRoutes(inputRoutes));
|
|
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.6",
|
|
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,18 @@
|
|
|
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
|
],
|
|
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",
|
|
43
53
|
"license": "MIT"
|
|
44
54
|
}
|