spinning-router 0.0.1 → 0.0.2
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/dist/Location.d.ts +8 -1
- package/dist/Location.js +1 -1
- package/dist/Routes.d.ts +10 -1
- package/dist/SpinningRouter.d.ts +0 -6
- package/dist/SpinningRouter.js +12 -57
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -1
- package/dist/link.d.ts +4 -1
- package/dist/link.js +7 -13
- package/dist/matchRoute.d.ts +11 -0
- package/dist/matchRoute.js +42 -0
- package/dist/navigate.d.ts +4 -1
- package/dist/navigate.js +2 -2
- package/package.json +2 -2
package/dist/Location.d.ts
CHANGED
@@ -1,2 +1,9 @@
|
|
1
1
|
/// <reference types="react" />
|
2
|
-
export
|
2
|
+
export type LocationType = {
|
3
|
+
path: string;
|
4
|
+
match: string;
|
5
|
+
parameters: {
|
6
|
+
[key: string]: string;
|
7
|
+
};
|
8
|
+
};
|
9
|
+
export declare const Location: import("react").Context<LocationType>;
|
package/dist/Location.js
CHANGED
package/dist/Routes.d.ts
CHANGED
@@ -5,4 +5,13 @@ export type Route = {
|
|
5
5
|
routes?: Routes;
|
6
6
|
component?: AsyncComponent;
|
7
7
|
};
|
8
|
-
export type Routes = Route[];
|
8
|
+
export type Routes = readonly Route[];
|
9
|
+
export type AllPaths<R extends Routes, Prefix extends string = ""> = R extends readonly [infer First extends Route, ...infer Rest extends readonly Route[]] ? (First extends {
|
10
|
+
path: infer P extends string;
|
11
|
+
routes?: infer S;
|
12
|
+
} ? `${Prefix}${P}` | (S extends Routes ? AllPaths<S, `${Prefix}${P}`> : never) : never) | AllPaths<Rest, Prefix> : never;
|
13
|
+
export interface Register {
|
14
|
+
}
|
15
|
+
export type Path = AllPaths<Register extends {
|
16
|
+
routes: infer Routes;
|
17
|
+
} ? Routes : never>;
|
package/dist/SpinningRouter.d.ts
CHANGED
@@ -1,10 +1,5 @@
|
|
1
1
|
import React from "react";
|
2
2
|
import { Routes } from "./Routes";
|
3
|
-
type Parameters = {
|
4
|
-
[key: string]: any;
|
5
|
-
};
|
6
|
-
export declare const doMatchRoute: (routes: Routes, path: string[], parentParameters: Parameters) => Promise<JSX.Element>;
|
7
|
-
export declare const matchRoute: (routes: Routes, path: string) => Promise<JSX.Element | undefined>;
|
8
3
|
export declare const SpinningRouter: React.FC<{
|
9
4
|
routes: Routes;
|
10
5
|
errorPage?: React.FC<{
|
@@ -13,4 +8,3 @@ export declare const SpinningRouter: React.FC<{
|
|
13
8
|
notFoundPage?: React.ComponentType;
|
14
9
|
loadingIndicator?: React.ComponentType;
|
15
10
|
}>;
|
16
|
-
export {};
|
package/dist/SpinningRouter.js
CHANGED
@@ -9,82 +9,37 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
9
9
|
});
|
10
10
|
};
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
-
exports.SpinningRouter =
|
12
|
+
exports.SpinningRouter = void 0;
|
13
13
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
14
14
|
const react_1 = require("react");
|
15
15
|
const Location_1 = require("./Location");
|
16
16
|
const DefaultLoadingIndicator_1 = require("./components/DefaultLoadingIndicator");
|
17
17
|
const DefaultErrorPage_1 = require("./components/DefaultErrorPage");
|
18
18
|
const DefaultNotFoundPage_1 = require("./components/DefaultNotFoundPage");
|
19
|
-
const
|
20
|
-
for (let r of routes) {
|
21
|
-
const rPath = r.path.split("/").filter(e => e != "");
|
22
|
-
if ((r.routes && rPath.length <= path.length) || rPath.length == path.length) {
|
23
|
-
let parameters = Object.assign({}, parentParameters);
|
24
|
-
let match = true;
|
25
|
-
for (let i in rPath) {
|
26
|
-
if (rPath[i].startsWith(":")) {
|
27
|
-
parameters[rPath[i].substring(1)] = path[i];
|
28
|
-
}
|
29
|
-
else {
|
30
|
-
if (rPath[i] != path[i]) {
|
31
|
-
match = false;
|
32
|
-
}
|
33
|
-
}
|
34
|
-
}
|
35
|
-
if (match) {
|
36
|
-
let children = undefined;
|
37
|
-
if (r.routes) {
|
38
|
-
children = yield (0, exports.doMatchRoute)(r.routes, path.slice(rPath.length), parameters);
|
39
|
-
}
|
40
|
-
if (r.component) {
|
41
|
-
parameters["children"] = children;
|
42
|
-
return r.component(parameters);
|
43
|
-
}
|
44
|
-
else {
|
45
|
-
if (children) {
|
46
|
-
return children;
|
47
|
-
}
|
48
|
-
}
|
49
|
-
}
|
50
|
-
}
|
51
|
-
}
|
52
|
-
throw "Not Found";
|
53
|
-
});
|
54
|
-
exports.doMatchRoute = doMatchRoute;
|
55
|
-
const matchRoute = (routes, path) => __awaiter(void 0, void 0, void 0, function* () {
|
56
|
-
try {
|
57
|
-
return (0, exports.doMatchRoute)(routes, path.split("/").filter(e => e != ""), {});
|
58
|
-
}
|
59
|
-
catch (error) {
|
60
|
-
if (error == "Not Found") {
|
61
|
-
return undefined;
|
62
|
-
}
|
63
|
-
else {
|
64
|
-
throw error;
|
65
|
-
}
|
66
|
-
}
|
67
|
-
});
|
68
|
-
exports.matchRoute = matchRoute;
|
19
|
+
const matchRoute_1 = require("./matchRoute");
|
69
20
|
const SpinningRouter = ({ routes, errorPage = DefaultErrorPage_1.DefaultErrorPage, notFoundPage = DefaultNotFoundPage_1.DefaultNotFoundPage, loadingIndicator = DefaultLoadingIndicator_1.DefaultLoadingIndicator }) => {
|
70
21
|
const [hash, setHash] = (0, react_1.useState)(() => location.hash.substring(1));
|
71
22
|
const [element, setElement] = (0, react_1.useState)(undefined);
|
72
23
|
const [overlay, setOverlay] = (0, react_1.useState)(undefined);
|
24
|
+
const [parameters, setParameters] = (0, react_1.useState)({});
|
73
25
|
const invokeRoute = (path) => __awaiter(void 0, void 0, void 0, function* () {
|
74
26
|
setOverlay(loadingIndicator);
|
75
27
|
try {
|
76
|
-
const
|
77
|
-
|
78
|
-
|
79
|
-
|
28
|
+
const match = yield (0, matchRoute_1.matchRoute)(routes, path);
|
29
|
+
if (match) {
|
30
|
+
setElement(match.element);
|
31
|
+
setParameters(match.parameters);
|
80
32
|
}
|
81
33
|
else {
|
82
34
|
setElement(notFoundPage);
|
35
|
+
setParameters({});
|
83
36
|
}
|
37
|
+
window.scrollTo(0, 0);
|
84
38
|
}
|
85
39
|
catch (error) {
|
86
|
-
|
40
|
+
window.scrollTo(0, 0);
|
87
41
|
setElement(errorPage({ error }));
|
42
|
+
setParameters({});
|
88
43
|
}
|
89
44
|
finally {
|
90
45
|
setOverlay(undefined);
|
@@ -105,6 +60,6 @@ const SpinningRouter = ({ routes, errorPage = DefaultErrorPage_1.DefaultErrorPag
|
|
105
60
|
window.removeEventListener("softRefresh", updateHash);
|
106
61
|
};
|
107
62
|
}, []);
|
108
|
-
return ((0, jsx_runtime_1.jsxs)(Location_1.Location.Provider, Object.assign({ value: hash }, { children: [element, overlay] })));
|
63
|
+
return ((0, jsx_runtime_1.jsxs)(Location_1.Location.Provider, Object.assign({ value: { path: hash, match: "", parameters } }, { children: [element, overlay] })));
|
109
64
|
};
|
110
65
|
exports.SpinningRouter = SpinningRouter;
|
package/dist/index.d.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
export { link } from "./link";
|
2
2
|
export { navigate } from "./navigate";
|
3
|
-
export { AsyncComponent, Route, Routes } from "./Routes";
|
3
|
+
export { AsyncComponent, Route, Routes, Path, Register } from "./Routes";
|
4
4
|
export { softRefresh } from "./softRefresh";
|
5
5
|
export { SpinningRouter } from "./SpinningRouter";
|
6
|
+
export { Location } from "./Location";
|
package/dist/index.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.SpinningRouter = exports.softRefresh = exports.navigate = exports.link = void 0;
|
3
|
+
exports.Location = exports.SpinningRouter = exports.softRefresh = exports.navigate = exports.link = void 0;
|
4
4
|
var link_1 = require("./link");
|
5
5
|
Object.defineProperty(exports, "link", { enumerable: true, get: function () { return link_1.link; } });
|
6
6
|
var navigate_1 = require("./navigate");
|
@@ -9,3 +9,5 @@ var softRefresh_1 = require("./softRefresh");
|
|
9
9
|
Object.defineProperty(exports, "softRefresh", { enumerable: true, get: function () { return softRefresh_1.softRefresh; } });
|
10
10
|
var SpinningRouter_1 = require("./SpinningRouter");
|
11
11
|
Object.defineProperty(exports, "SpinningRouter", { enumerable: true, get: function () { return SpinningRouter_1.SpinningRouter; } });
|
12
|
+
var Location_1 = require("./Location");
|
13
|
+
Object.defineProperty(exports, "Location", { enumerable: true, get: function () { return Location_1.Location; } });
|
package/dist/link.d.ts
CHANGED
package/dist/link.js
CHANGED
@@ -1,22 +1,16 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.link = void 0;
|
4
|
-
const link = (
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
return `#/${id}${s.substring(1)}`;
|
4
|
+
const link = (path, values = {}) => {
|
5
|
+
let result = [];
|
6
|
+
for (let part of path.split("/")) {
|
7
|
+
if (part.startsWith(":")) {
|
8
|
+
result.push(encodeURIComponent(values[part.substring(1)]));
|
10
9
|
}
|
11
10
|
else {
|
12
|
-
|
11
|
+
result.push(part);
|
13
12
|
}
|
14
13
|
}
|
15
|
-
|
16
|
-
return "#" + s;
|
17
|
-
}
|
18
|
-
else {
|
19
|
-
return "#/" + s;
|
20
|
-
}
|
14
|
+
return "#" + result.join("/");
|
21
15
|
};
|
22
16
|
exports.link = link;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
/// <reference types="react" />
|
2
|
+
import { Routes } from "./Routes";
|
3
|
+
type Match = {
|
4
|
+
path: string;
|
5
|
+
parameters: {
|
6
|
+
[key: string]: string;
|
7
|
+
};
|
8
|
+
element: JSX.Element;
|
9
|
+
};
|
10
|
+
export declare const matchRoute: (routes: Routes, query: string) => Promise<Match | undefined>;
|
11
|
+
export {};
|
@@ -0,0 +1,42 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
exports.matchRoute = void 0;
|
13
|
+
const doMatchRoute = (routes, querySplitted, parameters) => __awaiter(void 0, void 0, void 0, function* () {
|
14
|
+
for (let r of routes) {
|
15
|
+
const pathSplitted = r.path.split("/").filter(e => e != "");
|
16
|
+
if ((r.routes && pathSplitted.length <= querySplitted.length) || pathSplitted.length == querySplitted.length) {
|
17
|
+
if (pathSplitted.every((e, i) => e.startsWith(":") || e == querySplitted[i])) {
|
18
|
+
parameters = Object.assign(Object.assign({}, parameters), Object.fromEntries(pathSplitted.filter(e => e.startsWith(":")).map((e, i) => [e.substring(1), decodeURIComponent(querySplitted[i])])));
|
19
|
+
if (r.routes) {
|
20
|
+
let subRoute = yield doMatchRoute(r.routes, querySplitted.slice(pathSplitted.length), parameters);
|
21
|
+
return {
|
22
|
+
element: r.component ? yield r.component(Object.assign(Object.assign({}, parameters), { children: subRoute.element })) : subRoute.element,
|
23
|
+
path: r.path + "/" + subRoute.path,
|
24
|
+
parameters: subRoute.parameters
|
25
|
+
};
|
26
|
+
}
|
27
|
+
else {
|
28
|
+
return {
|
29
|
+
element: r.component ? yield r.component(Object.assign({}, parameters)) : undefined,
|
30
|
+
path: r.path,
|
31
|
+
parameters
|
32
|
+
};
|
33
|
+
}
|
34
|
+
}
|
35
|
+
}
|
36
|
+
}
|
37
|
+
return undefined;
|
38
|
+
});
|
39
|
+
const matchRoute = (routes, query) => __awaiter(void 0, void 0, void 0, function* () {
|
40
|
+
return yield doMatchRoute(routes, query.split("/").filter(e => e != ""), {});
|
41
|
+
});
|
42
|
+
exports.matchRoute = matchRoute;
|
package/dist/navigate.d.ts
CHANGED
package/dist/navigate.js
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.navigate = void 0;
|
4
4
|
const link_1 = require("./link");
|
5
|
-
const navigate = (
|
6
|
-
window.location.hash = (0, link_1.link)(
|
5
|
+
const navigate = (path, values = {}) => {
|
6
|
+
window.location.hash = (0, link_1.link)(path, values);
|
7
7
|
};
|
8
8
|
exports.navigate = navigate;
|
package/package.json
CHANGED