@esportsplus/routing 0.0.30 → 0.0.32
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 +8 -8
- package/build/browser.js +57 -44
- package/package.json +3 -2
- package/src/browser.ts +70 -55
package/build/browser.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { Middleware, Router } from './types';
|
|
1
|
+
import { Middleware, Next, Request, Router } from './types';
|
|
2
2
|
declare function back(): void;
|
|
3
3
|
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
|
+
middleware(subdomain?: string): (request: Request<T>, next: Next<T>) => import("@esportsplus/template/build/types").Template;
|
|
10
13
|
};
|
|
11
|
-
middleware: (...middleware: Middleware<T>[]) => (
|
|
12
|
-
parameters?: Record<PropertyKey, unknown> | undefined;
|
|
13
|
-
route?: import("./router/route").Route<T> | undefined;
|
|
14
|
-
}) => T;
|
|
14
|
+
middleware: (...middleware: Middleware<T>[]) => () => T;
|
|
15
15
|
redirect: (path: string, values?: unknown[]) => void;
|
|
16
16
|
router: Router<T>;
|
|
17
17
|
uri: (path: string, values?: unknown[]) => string;
|
package/build/browser.js
CHANGED
|
@@ -1,13 +1,28 @@
|
|
|
1
|
-
import { reactive } from '@esportsplus/reactivity';
|
|
1
|
+
import { effect, reactive, root } from '@esportsplus/reactivity';
|
|
2
|
+
import { html } from '@esportsplus/template';
|
|
2
3
|
import pipeline from '@esportsplus/pipeline';
|
|
3
4
|
import factory from './router';
|
|
4
|
-
let cache = []
|
|
5
|
+
let cache = [];
|
|
5
6
|
function back() {
|
|
6
7
|
window.history.back();
|
|
7
8
|
}
|
|
8
9
|
function forward() {
|
|
9
10
|
window.history.forward();
|
|
10
11
|
}
|
|
12
|
+
function href() {
|
|
13
|
+
let { hash, hostname, href, origin, port, protocol } = new URL(window.location?.href || ''), path = hash ? hash.slice(1).split('?') : ['/', ''];
|
|
14
|
+
return {
|
|
15
|
+
data: {},
|
|
16
|
+
href,
|
|
17
|
+
hostname,
|
|
18
|
+
method: 'GET',
|
|
19
|
+
origin,
|
|
20
|
+
path: path[0],
|
|
21
|
+
port,
|
|
22
|
+
protocol,
|
|
23
|
+
query: path[1] ? Object.fromEntries((new URLSearchParams(path[1])).entries()) : {},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
11
26
|
function normalize(uri) {
|
|
12
27
|
if (uri[0] === '/') {
|
|
13
28
|
return '#' + uri;
|
|
@@ -15,7 +30,7 @@ function normalize(uri) {
|
|
|
15
30
|
return uri;
|
|
16
31
|
}
|
|
17
32
|
function onpopstate() {
|
|
18
|
-
let values =
|
|
33
|
+
let values = href();
|
|
19
34
|
for (let i = 0, n = cache.length; i < n; i++) {
|
|
20
35
|
let state = cache[i];
|
|
21
36
|
for (let key in values) {
|
|
@@ -23,55 +38,53 @@ function onpopstate() {
|
|
|
23
38
|
}
|
|
24
39
|
}
|
|
25
40
|
}
|
|
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
41
|
export default (instance) => {
|
|
41
|
-
let
|
|
42
|
-
cache.push(
|
|
43
|
-
if (!registered) {
|
|
44
|
-
registered = true;
|
|
42
|
+
let request = reactive(href()), router = instance || factory();
|
|
43
|
+
if (cache.push(request) === 1) {
|
|
45
44
|
window.addEventListener('hashchange', onpopstate);
|
|
46
45
|
}
|
|
46
|
+
function match(subdomain) {
|
|
47
|
+
if (router.subdomains !== null) {
|
|
48
|
+
for (let i = 0, n = router.subdomains.length; i < n; i++) {
|
|
49
|
+
if (!request.hostname.startsWith(router.subdomains[i])) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
subdomain = router.subdomains[i];
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return router.match(request.method, request.path, subdomain || '');
|
|
57
|
+
}
|
|
58
|
+
match.middleware = (subdomain) => {
|
|
59
|
+
let state = reactive({
|
|
60
|
+
parameters: undefined,
|
|
61
|
+
route: undefined
|
|
62
|
+
});
|
|
63
|
+
effect(() => {
|
|
64
|
+
let { parameters, route } = match(subdomain);
|
|
65
|
+
state.parameters = parameters;
|
|
66
|
+
state.route = route;
|
|
67
|
+
});
|
|
68
|
+
return (request, next) => {
|
|
69
|
+
return html `${() => {
|
|
70
|
+
if (state.route === undefined) {
|
|
71
|
+
throw new Error(`Routing: route is undefined!`);
|
|
72
|
+
}
|
|
73
|
+
return root(() => {
|
|
74
|
+
request.data.parameters = state.parameters;
|
|
75
|
+
request.data.route = state.route;
|
|
76
|
+
return next(request);
|
|
77
|
+
});
|
|
78
|
+
}}`;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
47
81
|
return {
|
|
48
82
|
back,
|
|
49
83
|
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
|
-
},
|
|
84
|
+
match,
|
|
68
85
|
middleware: (...middleware) => {
|
|
69
86
|
let instance = pipeline(...middleware);
|
|
70
|
-
return (
|
|
71
|
-
state.data.parameters = parameters;
|
|
72
|
-
state.data.route = route;
|
|
73
|
-
return instance(state);
|
|
74
|
-
};
|
|
87
|
+
return () => instance(request);
|
|
75
88
|
},
|
|
76
89
|
redirect: (path, values = []) => {
|
|
77
90
|
if (path.indexOf('://') !== -1) {
|
package/package.json
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
"author": "ICJR",
|
|
3
3
|
"dependencies": {
|
|
4
4
|
"@esportsplus/pipeline": "^0.0.5",
|
|
5
|
-
"@esportsplus/reactivity": "^0.1.
|
|
5
|
+
"@esportsplus/reactivity": "^0.1.3",
|
|
6
|
+
"@esportsplus/template": "^0.1.5"
|
|
6
7
|
},
|
|
7
8
|
"devDependencies": {
|
|
8
9
|
"@esportsplus/typescript": "^0.0.3"
|
|
@@ -17,5 +18,5 @@
|
|
|
17
18
|
"prepublishOnly": "npm run build"
|
|
18
19
|
},
|
|
19
20
|
"types": "./build/index.d.ts",
|
|
20
|
-
"version": "0.0.
|
|
21
|
+
"version": "0.0.32"
|
|
21
22
|
}
|
package/src/browser.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { reactive } from '@esportsplus/reactivity';
|
|
2
|
-
import {
|
|
1
|
+
import { effect, reactive, root } from '@esportsplus/reactivity';
|
|
2
|
+
import { html } from '@esportsplus/template';
|
|
3
|
+
import { Middleware, Next, Request, Router } from './types';
|
|
3
4
|
import pipeline from '@esportsplus/pipeline';
|
|
4
5
|
import factory from './router';
|
|
5
6
|
|
|
6
7
|
|
|
7
|
-
let cache: Request<any>[] = []
|
|
8
|
-
registered = false;
|
|
8
|
+
let cache: Request<any>[] = [];
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
function back() {
|
|
@@ -16,6 +16,23 @@ function forward() {
|
|
|
16
16
|
window.history.forward();
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
function href<T>(): Request<T> {
|
|
20
|
+
let { hash, hostname, href, origin, port, protocol } = new URL( window.location?.href || '' ),
|
|
21
|
+
path = hash ? hash.slice(1).split('?') : ['/', ''];
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
data: {},
|
|
25
|
+
href,
|
|
26
|
+
hostname,
|
|
27
|
+
method: 'GET',
|
|
28
|
+
origin,
|
|
29
|
+
path: path[0],
|
|
30
|
+
port,
|
|
31
|
+
protocol,
|
|
32
|
+
query: path[1] ? Object.fromEntries( (new URLSearchParams(path[1])).entries() ) : {},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
19
36
|
function normalize(uri: string) {
|
|
20
37
|
if (uri[0] === '/') {
|
|
21
38
|
return '#' + uri;
|
|
@@ -25,7 +42,7 @@ function normalize(uri: string) {
|
|
|
25
42
|
}
|
|
26
43
|
|
|
27
44
|
function onpopstate() {
|
|
28
|
-
let values =
|
|
45
|
+
let values = href();
|
|
29
46
|
|
|
30
47
|
for (let i = 0, n = cache.length; i < n; i++) {
|
|
31
48
|
let state = cache[i];
|
|
@@ -37,69 +54,67 @@ function onpopstate() {
|
|
|
37
54
|
}
|
|
38
55
|
}
|
|
39
56
|
|
|
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
57
|
|
|
58
58
|
export default <T>(instance?: Router<T>) => {
|
|
59
|
-
let
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
cache.push(state);
|
|
59
|
+
let request = reactive( href<T>() ),
|
|
60
|
+
router = instance || factory<T>();
|
|
63
61
|
|
|
64
|
-
if (
|
|
65
|
-
registered = true;
|
|
62
|
+
if (cache.push(request) === 1) {
|
|
66
63
|
window.addEventListener('hashchange', onpopstate);
|
|
67
64
|
}
|
|
68
65
|
|
|
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
|
-
}
|
|
66
|
+
function match(subdomain?: string) {
|
|
67
|
+
if (router.subdomains !== null) {
|
|
68
|
+
for (let i = 0, n = router.subdomains.length; i < n; i++) {
|
|
69
|
+
if (!request.hostname.startsWith(router.subdomains[i])) {
|
|
70
|
+
continue;
|
|
85
71
|
}
|
|
86
72
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
73
|
+
subdomain = router.subdomains[i];
|
|
74
|
+
break;
|
|
90
75
|
}
|
|
76
|
+
}
|
|
91
77
|
|
|
92
|
-
|
|
93
|
-
|
|
78
|
+
return router.match(request.method, request.path, subdomain || '');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
match.middleware = (subdomain?: string) => {
|
|
82
|
+
let state = reactive<ReturnType<typeof router.match>>({
|
|
83
|
+
parameters: undefined,
|
|
84
|
+
route: undefined
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
effect(() => {
|
|
88
|
+
let { parameters, route } = match(subdomain);
|
|
89
|
+
|
|
90
|
+
state.parameters = parameters;
|
|
91
|
+
state.route = route;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return (request: Request<T>, next: Next<T>) => {
|
|
95
|
+
return html`${() => {
|
|
96
|
+
if (state.route === undefined) {
|
|
97
|
+
throw new Error(`Routing: route is undefined!`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return root(() => {
|
|
101
|
+
request.data.parameters = state.parameters;
|
|
102
|
+
request.data.route = state.route;
|
|
103
|
+
|
|
104
|
+
return next(request);
|
|
105
|
+
});
|
|
106
|
+
}}`;
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
back,
|
|
112
|
+
forward,
|
|
113
|
+
match,
|
|
94
114
|
middleware: (...middleware: Middleware<T>[]) => {
|
|
95
115
|
let instance = pipeline(...middleware);
|
|
96
116
|
|
|
97
|
-
return (
|
|
98
|
-
state.data.parameters = parameters;
|
|
99
|
-
state.data.route = route;
|
|
100
|
-
|
|
101
|
-
return instance(state);
|
|
102
|
-
};
|
|
117
|
+
return () => instance(request);
|
|
103
118
|
},
|
|
104
119
|
redirect: (path: string, values: unknown[] = []) => {
|
|
105
120
|
if (path.indexOf('://') !== -1) {
|