@esportsplus/routing 0.0.29 → 0.0.31
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/build/browser.d.ts +20 -7
- package/build/browser.js +45 -44
- package/build/router/route.d.ts +1 -1
- package/build/router/route.js +3 -3
- package/package.json +1 -1
- package/src/browser.ts +55 -54
- package/src/router/route.ts +3 -3
package/build/browser.d.ts
CHANGED
|
@@ -4,14 +4,27 @@ declare function forward(): void;
|
|
|
4
4
|
declare const _default: <T>(instance?: Router<T> | undefined) => {
|
|
5
5
|
back: typeof back;
|
|
6
6
|
forward: typeof forward;
|
|
7
|
-
match:
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
match: {
|
|
8
|
+
(subdomain?: string): {
|
|
9
|
+
parameters?: Record<PropertyKey, unknown> | undefined;
|
|
10
|
+
route?: import("./router/route").Route<T> | undefined;
|
|
11
|
+
};
|
|
12
|
+
reactive(subdomain?: string): {
|
|
13
|
+
parameters?: {
|
|
14
|
+
[x: string]: unknown;
|
|
15
|
+
[x: number]: unknown;
|
|
16
|
+
[x: symbol]: unknown;
|
|
17
|
+
} | undefined;
|
|
18
|
+
route?: import("./router/route").Route<T> | undefined;
|
|
19
|
+
dispose: () => void;
|
|
20
|
+
reset: () => void;
|
|
21
|
+
nodes: {
|
|
22
|
+
parameters?: import("@esportsplus/reactivity/build/signal").default<undefined> | import("@esportsplus/reactivity/build/signal").default<Record<PropertyKey, unknown>> | undefined;
|
|
23
|
+
route?: import("@esportsplus/reactivity/build/signal").default<undefined> | import("@esportsplus/reactivity/build/signal").default<import("./router/route").Route<T>> | undefined;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
10
26
|
};
|
|
11
|
-
middleware: (...middleware: Middleware<T>[]) => (
|
|
12
|
-
parameters?: Record<PropertyKey, unknown> | undefined;
|
|
13
|
-
route?: import("./router/route").Route<T> | undefined;
|
|
14
|
-
}) => T;
|
|
27
|
+
middleware: (...middleware: Middleware<T>[]) => () => T;
|
|
15
28
|
redirect: (path: string, values?: unknown[]) => void;
|
|
16
29
|
router: Router<T>;
|
|
17
30
|
uri: (path: string, values?: unknown[]) => string;
|
package/build/browser.js
CHANGED
|
@@ -1,13 +1,27 @@
|
|
|
1
|
-
import { reactive } from '@esportsplus/reactivity';
|
|
1
|
+
import { effect, reactive } from '@esportsplus/reactivity';
|
|
2
2
|
import pipeline from '@esportsplus/pipeline';
|
|
3
3
|
import factory from './router';
|
|
4
|
-
let cache = []
|
|
4
|
+
let cache = [];
|
|
5
5
|
function back() {
|
|
6
6
|
window.history.back();
|
|
7
7
|
}
|
|
8
8
|
function forward() {
|
|
9
9
|
window.history.forward();
|
|
10
10
|
}
|
|
11
|
+
function href() {
|
|
12
|
+
let { hash, hostname, href, origin, port, protocol } = new URL(window.location?.href || ''), path = hash ? hash.slice(1).split('?') : ['/', ''];
|
|
13
|
+
return {
|
|
14
|
+
data: {},
|
|
15
|
+
href,
|
|
16
|
+
hostname,
|
|
17
|
+
method: 'GET',
|
|
18
|
+
origin,
|
|
19
|
+
path: path[0],
|
|
20
|
+
port,
|
|
21
|
+
protocol,
|
|
22
|
+
query: path[1] ? Object.fromEntries((new URLSearchParams(path[1])).entries()) : {},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
11
25
|
function normalize(uri) {
|
|
12
26
|
if (uri[0] === '/') {
|
|
13
27
|
return '#' + uri;
|
|
@@ -15,7 +29,7 @@ function normalize(uri) {
|
|
|
15
29
|
return uri;
|
|
16
30
|
}
|
|
17
31
|
function onpopstate() {
|
|
18
|
-
let values =
|
|
32
|
+
let values = href();
|
|
19
33
|
for (let i = 0, n = cache.length; i < n; i++) {
|
|
20
34
|
let state = cache[i];
|
|
21
35
|
for (let key in values) {
|
|
@@ -23,55 +37,42 @@ function onpopstate() {
|
|
|
23
37
|
}
|
|
24
38
|
}
|
|
25
39
|
}
|
|
26
|
-
function request() {
|
|
27
|
-
let { hash, hostname, href, origin, port, protocol } = new URL(window.location?.href || ''), path = hash ? hash.slice(1).split('?') : ['/', ''];
|
|
28
|
-
return {
|
|
29
|
-
data: {},
|
|
30
|
-
href,
|
|
31
|
-
hostname,
|
|
32
|
-
method: 'GET',
|
|
33
|
-
origin,
|
|
34
|
-
path: path[0],
|
|
35
|
-
port,
|
|
36
|
-
protocol,
|
|
37
|
-
query: path[1] ? Object.fromEntries((new URLSearchParams(path[1])).entries()) : {},
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
40
|
export default (instance) => {
|
|
41
|
-
let
|
|
42
|
-
cache.push(
|
|
43
|
-
if (!registered) {
|
|
44
|
-
registered = true;
|
|
41
|
+
let request = reactive(href()), router = instance || factory();
|
|
42
|
+
if (cache.push(request) === 1) {
|
|
45
43
|
window.addEventListener('hashchange', onpopstate);
|
|
46
44
|
}
|
|
45
|
+
function match(subdomain) {
|
|
46
|
+
if (router.subdomains !== null) {
|
|
47
|
+
for (let i = 0, n = router.subdomains.length; i < n; i++) {
|
|
48
|
+
if (!request.hostname.startsWith(router.subdomains[i])) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
subdomain = router.subdomains[i];
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return router.match(request.method, request.path, subdomain || '');
|
|
56
|
+
}
|
|
57
|
+
match.reactive = (subdomain) => {
|
|
58
|
+
let state = reactive({
|
|
59
|
+
parameters: undefined,
|
|
60
|
+
route: undefined
|
|
61
|
+
});
|
|
62
|
+
effect(() => {
|
|
63
|
+
let { parameters, route } = match(subdomain);
|
|
64
|
+
state.parameters = parameters;
|
|
65
|
+
state.route = route;
|
|
66
|
+
});
|
|
67
|
+
return state;
|
|
68
|
+
};
|
|
47
69
|
return {
|
|
48
70
|
back,
|
|
49
71
|
forward,
|
|
50
|
-
match
|
|
51
|
-
let match = subdomain || state.subdomain;
|
|
52
|
-
if (match === undefined) {
|
|
53
|
-
if (router.subdomains) {
|
|
54
|
-
for (let i = 0, n = router.subdomains.length; i < n; i++) {
|
|
55
|
-
if (!state.hostname.startsWith(router.subdomains[i])) {
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
match = router.subdomains[i];
|
|
59
|
-
break;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (match === undefined) {
|
|
63
|
-
match = '';
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return router.match(state.method, state.path, match);
|
|
67
|
-
},
|
|
72
|
+
match,
|
|
68
73
|
middleware: (...middleware) => {
|
|
69
74
|
let instance = pipeline(...middleware);
|
|
70
|
-
return (
|
|
71
|
-
state.data.parameters = parameters;
|
|
72
|
-
state.data.route = route;
|
|
73
|
-
return instance(state);
|
|
74
|
-
};
|
|
75
|
+
return () => instance(request);
|
|
75
76
|
},
|
|
76
77
|
redirect: (path, values = []) => {
|
|
77
78
|
if (path.indexOf('://') !== -1) {
|
package/build/router/route.d.ts
CHANGED
package/build/router/route.js
CHANGED
|
@@ -8,11 +8,11 @@ class Route {
|
|
|
8
8
|
constructor(responder) {
|
|
9
9
|
this.responder = responder;
|
|
10
10
|
}
|
|
11
|
-
|
|
11
|
+
dispatch(request) {
|
|
12
12
|
if (this.middleware === null) {
|
|
13
|
-
return
|
|
13
|
+
return this.responder(request);
|
|
14
14
|
}
|
|
15
|
-
return pipeline(...this.middleware, (request) => this.responder(request));
|
|
15
|
+
return pipeline(...this.middleware, (request) => this.responder(request))(request);
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
export { Route };
|
package/package.json
CHANGED
package/src/browser.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { reactive } from '@esportsplus/reactivity';
|
|
1
|
+
import { effect, reactive } from '@esportsplus/reactivity';
|
|
2
2
|
import { Middleware, Request, Router } from './types';
|
|
3
3
|
import pipeline from '@esportsplus/pipeline';
|
|
4
4
|
import factory from './router';
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
let cache: Request<any>[] = []
|
|
8
|
-
registered = false;
|
|
7
|
+
let cache: Request<any>[] = [];
|
|
9
8
|
|
|
10
9
|
|
|
11
10
|
function back() {
|
|
@@ -16,6 +15,23 @@ function forward() {
|
|
|
16
15
|
window.history.forward();
|
|
17
16
|
}
|
|
18
17
|
|
|
18
|
+
function href<T>(): Request<T> {
|
|
19
|
+
let { hash, hostname, href, origin, port, protocol } = new URL( window.location?.href || '' ),
|
|
20
|
+
path = hash ? hash.slice(1).split('?') : ['/', ''];
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
data: {},
|
|
24
|
+
href,
|
|
25
|
+
hostname,
|
|
26
|
+
method: 'GET',
|
|
27
|
+
origin,
|
|
28
|
+
path: path[0],
|
|
29
|
+
port,
|
|
30
|
+
protocol,
|
|
31
|
+
query: path[1] ? Object.fromEntries( (new URLSearchParams(path[1])).entries() ) : {},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
19
35
|
function normalize(uri: string) {
|
|
20
36
|
if (uri[0] === '/') {
|
|
21
37
|
return '#' + uri;
|
|
@@ -25,7 +41,7 @@ function normalize(uri: string) {
|
|
|
25
41
|
}
|
|
26
42
|
|
|
27
43
|
function onpopstate() {
|
|
28
|
-
let values =
|
|
44
|
+
let values = href();
|
|
29
45
|
|
|
30
46
|
for (let i = 0, n = cache.length; i < n; i++) {
|
|
31
47
|
let state = cache[i];
|
|
@@ -37,69 +53,54 @@ function onpopstate() {
|
|
|
37
53
|
}
|
|
38
54
|
}
|
|
39
55
|
|
|
40
|
-
function request<T>(): Request<T> {
|
|
41
|
-
let { hash, hostname, href, origin, port, protocol } = new URL( window.location?.href || '' ),
|
|
42
|
-
path = hash ? hash.slice(1).split('?') : ['/', ''];
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
data: {},
|
|
46
|
-
href,
|
|
47
|
-
hostname,
|
|
48
|
-
method: 'GET',
|
|
49
|
-
origin,
|
|
50
|
-
path: path[0],
|
|
51
|
-
port,
|
|
52
|
-
protocol,
|
|
53
|
-
query: path[1] ? Object.fromEntries( (new URLSearchParams(path[1])).entries() ) : {},
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
56
|
|
|
58
57
|
export default <T>(instance?: Router<T>) => {
|
|
59
|
-
let
|
|
60
|
-
|
|
58
|
+
let request = reactive( href<T>() ),
|
|
59
|
+
router = instance || factory<T>();
|
|
61
60
|
|
|
62
|
-
cache.push(
|
|
63
|
-
|
|
64
|
-
if (!registered) {
|
|
65
|
-
registered = true;
|
|
61
|
+
if (cache.push(request) === 1) {
|
|
66
62
|
window.addEventListener('hashchange', onpopstate);
|
|
67
63
|
}
|
|
68
64
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (match === undefined) {
|
|
76
|
-
if (router.subdomains) {
|
|
77
|
-
for (let i = 0, n = router.subdomains.length; i < n; i++) {
|
|
78
|
-
if (!state.hostname.startsWith(router.subdomains[i])) {
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
match = router.subdomains[i];
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
65
|
+
function match(subdomain?: string) {
|
|
66
|
+
if (router.subdomains !== null) {
|
|
67
|
+
for (let i = 0, n = router.subdomains.length; i < n; i++) {
|
|
68
|
+
if (!request.hostname.startsWith(router.subdomains[i])) {
|
|
69
|
+
continue;
|
|
85
70
|
}
|
|
86
71
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
72
|
+
subdomain = router.subdomains[i];
|
|
73
|
+
break;
|
|
90
74
|
}
|
|
75
|
+
}
|
|
91
76
|
|
|
92
|
-
|
|
93
|
-
|
|
77
|
+
return router.match(request.method, request.path, subdomain || '');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
match.reactive = (subdomain?: string) => {
|
|
81
|
+
let state = reactive<ReturnType<typeof router.match>>({
|
|
82
|
+
parameters: undefined,
|
|
83
|
+
route: undefined
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
effect(() => {
|
|
87
|
+
let { parameters, route } = match(subdomain);
|
|
88
|
+
|
|
89
|
+
state.parameters = parameters;
|
|
90
|
+
state.route = route;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
return state;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
back,
|
|
98
|
+
forward,
|
|
99
|
+
match,
|
|
94
100
|
middleware: (...middleware: Middleware<T>[]) => {
|
|
95
101
|
let instance = pipeline(...middleware);
|
|
96
102
|
|
|
97
|
-
return (
|
|
98
|
-
state.data.parameters = parameters;
|
|
99
|
-
state.data.route = route;
|
|
100
|
-
|
|
101
|
-
return instance(state);
|
|
102
|
-
};
|
|
103
|
+
return () => instance(request);
|
|
103
104
|
},
|
|
104
105
|
redirect: (path: string, values: unknown[] = []) => {
|
|
105
106
|
if (path.indexOf('://') !== -1) {
|
package/src/router/route.ts
CHANGED
|
@@ -15,12 +15,12 @@ class Route<T> {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
dispatch(request: Request<T>) {
|
|
19
19
|
if (this.middleware === null) {
|
|
20
|
-
return
|
|
20
|
+
return this.responder(request);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
return pipeline(...this.middleware, (request) => this.responder(request));
|
|
23
|
+
return pipeline(...this.middleware, (request) => this.responder(request))(request);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|