@esportsplus/routing 0.1.30 → 0.1.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.js +14 -12
- package/build/router/index.js +11 -8
- package/package.json +2 -2
- package/src/browser.ts +19 -13
- package/src/router/index.ts +12 -8
package/build/browser.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { effect, reactive, root } from '@esportsplus/reactivity';
|
|
2
2
|
import { next } from '@esportsplus/pipeline';
|
|
3
3
|
import factory from './router/index.js';
|
|
4
|
-
let cache = [];
|
|
4
|
+
let cache = [], location = window.location;
|
|
5
5
|
function back() {
|
|
6
6
|
window.history.back();
|
|
7
7
|
}
|
|
@@ -9,30 +9,32 @@ function forward() {
|
|
|
9
9
|
window.history.forward();
|
|
10
10
|
}
|
|
11
11
|
function href() {
|
|
12
|
-
let
|
|
13
|
-
href:
|
|
14
|
-
hostname:
|
|
12
|
+
let hash = location.hash || '#/', path = hash ? hash.slice(1).split('?') : ['/', ''], request = {
|
|
13
|
+
href: location.href,
|
|
14
|
+
hostname: location.hostname,
|
|
15
15
|
method: 'GET',
|
|
16
|
-
origin:
|
|
16
|
+
origin: location.origin,
|
|
17
17
|
path: path[0],
|
|
18
|
-
port:
|
|
19
|
-
protocol:
|
|
18
|
+
port: location.port,
|
|
19
|
+
protocol: location.protocol,
|
|
20
20
|
query: {}
|
|
21
21
|
};
|
|
22
22
|
if (path[1]) {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
let query = request.query, params = new URLSearchParams(path[1]);
|
|
24
|
+
for (let [key, value] of params.entries()) {
|
|
25
|
+
query[key] = value;
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
return request;
|
|
28
29
|
}
|
|
29
30
|
function match(request, router, subdomain) {
|
|
30
31
|
if (router.subdomains !== null) {
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
let subdomains = router.subdomains;
|
|
33
|
+
for (let i = 0, n = subdomains.length; i < n; i++) {
|
|
34
|
+
if (!request.hostname.startsWith(subdomains[i])) {
|
|
33
35
|
continue;
|
|
34
36
|
}
|
|
35
|
-
subdomain =
|
|
37
|
+
subdomain = subdomains[i];
|
|
36
38
|
break;
|
|
37
39
|
}
|
|
38
40
|
}
|
package/build/router/index.js
CHANGED
|
@@ -2,11 +2,13 @@ import { ON_DELETE, ON_GET, ON_POST, ON_PUT, STATIC } from '../constants.js';
|
|
|
2
2
|
import { Node } from './node.js';
|
|
3
3
|
import pipeline from '@esportsplus/pipeline';
|
|
4
4
|
function normalize(path) {
|
|
5
|
-
if (path
|
|
6
|
-
path
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
if (path) {
|
|
6
|
+
if (path[0] !== '/') {
|
|
7
|
+
path = '/' + path;
|
|
8
|
+
}
|
|
9
|
+
if (path.at(-1) === '/') {
|
|
10
|
+
path = path.slice(0, -1);
|
|
11
|
+
}
|
|
10
12
|
}
|
|
11
13
|
return path || '/';
|
|
12
14
|
}
|
|
@@ -40,7 +42,7 @@ class Router {
|
|
|
40
42
|
}
|
|
41
43
|
add(radixkey, route) {
|
|
42
44
|
if (radixkey.indexOf(':') === -1 || this.root.add(radixkey, route).type === STATIC) {
|
|
43
|
-
if (this.static
|
|
45
|
+
if (radixkey in this.static) {
|
|
44
46
|
throw new Error(`Routing: static path '${radixkey}' is already in use`);
|
|
45
47
|
}
|
|
46
48
|
this.static[radixkey] = route;
|
|
@@ -113,11 +115,12 @@ class Router {
|
|
|
113
115
|
}
|
|
114
116
|
}
|
|
115
117
|
if (route.subdomain) {
|
|
118
|
+
let subdomain = route.subdomain.toLowerCase();
|
|
116
119
|
if (!this.subdomains) {
|
|
117
|
-
this.subdomains = [
|
|
120
|
+
this.subdomains = [subdomain];
|
|
118
121
|
}
|
|
119
122
|
else {
|
|
120
|
-
this.subdomains.push(
|
|
123
|
+
this.subdomains.push(subdomain);
|
|
121
124
|
}
|
|
122
125
|
}
|
|
123
126
|
return this;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"author": "ICJR",
|
|
3
3
|
"dependencies": {
|
|
4
4
|
"@esportsplus/pipeline": "^1.1.5",
|
|
5
|
-
"@esportsplus/reactivity": "^0.
|
|
5
|
+
"@esportsplus/reactivity": "^0.12.4",
|
|
6
6
|
"@esportsplus/utilities": "^0.21.1"
|
|
7
7
|
},
|
|
8
8
|
"devDependencies": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"private": false,
|
|
14
14
|
"type": "module",
|
|
15
15
|
"types": "./build/index.d.ts",
|
|
16
|
-
"version": "0.1.
|
|
16
|
+
"version": "0.1.32",
|
|
17
17
|
"scripts": {
|
|
18
18
|
"build": "tsc && tsc-alias",
|
|
19
19
|
"-": "-"
|
package/src/browser.ts
CHANGED
|
@@ -4,7 +4,8 @@ import { next } from '@esportsplus/pipeline';
|
|
|
4
4
|
import factory from './router';
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
let cache: Request<any>[] = []
|
|
7
|
+
let cache: Request<any>[] = [],
|
|
8
|
+
location = window.location;
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
function back() {
|
|
@@ -16,22 +17,25 @@ function forward() {
|
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
function href<T>() {
|
|
19
|
-
let
|
|
20
|
-
path =
|
|
20
|
+
let hash = location.hash || '#/',
|
|
21
|
+
path = hash ? hash.slice(1).split('?') : ['/', ''],
|
|
21
22
|
request = {
|
|
22
|
-
href:
|
|
23
|
-
hostname:
|
|
23
|
+
href: location.href,
|
|
24
|
+
hostname: location.hostname,
|
|
24
25
|
method: 'GET',
|
|
25
|
-
origin:
|
|
26
|
+
origin: location.origin,
|
|
26
27
|
path: path[0],
|
|
27
|
-
port:
|
|
28
|
-
protocol:
|
|
28
|
+
port: location.port,
|
|
29
|
+
protocol: location.protocol,
|
|
29
30
|
query: {} as Record<PropertyKey, unknown>
|
|
30
31
|
};
|
|
31
32
|
|
|
32
33
|
if (path[1]) {
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
let query = request.query,
|
|
35
|
+
params = new URLSearchParams(path[1]);
|
|
36
|
+
|
|
37
|
+
for (let [key, value] of params.entries()) {
|
|
38
|
+
query[key] = value;
|
|
35
39
|
}
|
|
36
40
|
}
|
|
37
41
|
|
|
@@ -40,12 +44,14 @@ function href<T>() {
|
|
|
40
44
|
|
|
41
45
|
function match<T>(request: Request<T>, router: Router<T>, subdomain?: string) {
|
|
42
46
|
if (router.subdomains !== null) {
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
let subdomains = router.subdomains;
|
|
48
|
+
|
|
49
|
+
for (let i = 0, n = subdomains.length; i < n; i++) {
|
|
50
|
+
if (!request.hostname.startsWith(subdomains[i])) {
|
|
45
51
|
continue;
|
|
46
52
|
}
|
|
47
53
|
|
|
48
|
-
subdomain =
|
|
54
|
+
subdomain = subdomains[i];
|
|
49
55
|
break;
|
|
50
56
|
}
|
|
51
57
|
}
|
package/src/router/index.ts
CHANGED
|
@@ -5,12 +5,14 @@ import pipeline from '@esportsplus/pipeline';
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
function normalize(path: string) {
|
|
8
|
-
if (path
|
|
9
|
-
path
|
|
10
|
-
|
|
8
|
+
if (path) {
|
|
9
|
+
if (path[0] !== '/') {
|
|
10
|
+
path = '/' + path;
|
|
11
|
+
}
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
if (path.at(-1) === '/') {
|
|
14
|
+
path = path.slice(0, -1);
|
|
15
|
+
}
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
return path || '/';
|
|
@@ -55,7 +57,7 @@ class Router<T> {
|
|
|
55
57
|
|
|
56
58
|
private add(radixkey: string, route: Route<T>) {
|
|
57
59
|
if (radixkey.indexOf(':') === -1 || this.root.add(radixkey, route).type === STATIC) {
|
|
58
|
-
if (this.static
|
|
60
|
+
if (radixkey in this.static) {
|
|
59
61
|
throw new Error(`Routing: static path '${radixkey}' is already in use`);
|
|
60
62
|
}
|
|
61
63
|
|
|
@@ -152,11 +154,13 @@ class Router<T> {
|
|
|
152
154
|
}
|
|
153
155
|
|
|
154
156
|
if (route.subdomain) {
|
|
157
|
+
let subdomain = route.subdomain.toLowerCase();
|
|
158
|
+
|
|
155
159
|
if (!this.subdomains) {
|
|
156
|
-
this.subdomains = [
|
|
160
|
+
this.subdomains = [subdomain];
|
|
157
161
|
}
|
|
158
162
|
else {
|
|
159
|
-
this.subdomains.push(
|
|
163
|
+
this.subdomains.push(subdomain);
|
|
160
164
|
}
|
|
161
165
|
}
|
|
162
166
|
|