@esportsplus/routing 0.0.31 → 0.0.33
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 +2 -22
- package/build/browser.js +55 -30
- package/build/router/route.d.ts +2 -2
- package/build/types.d.ts +4 -3
- package/package.json +4 -3
- package/src/browser.ts +70 -39
- package/src/router/route.ts +2 -2
- package/src/types.ts +4 -3
package/build/browser.d.ts
CHANGED
|
@@ -1,30 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { 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
|
-
|
|
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
|
-
};
|
|
26
|
-
};
|
|
27
|
-
middleware: (...middleware: Middleware<T>[]) => () => T;
|
|
7
|
+
middleware: void;
|
|
28
8
|
redirect: (path: string, values?: unknown[]) => void;
|
|
29
9
|
router: Router<T>;
|
|
30
10
|
uri: (path: string, values?: unknown[]) => string;
|
package/build/browser.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { effect, 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
5
|
let cache = [];
|
|
@@ -22,6 +23,58 @@ function href() {
|
|
|
22
23
|
query: path[1] ? Object.fromEntries((new URLSearchParams(path[1])).entries()) : {},
|
|
23
24
|
};
|
|
24
25
|
}
|
|
26
|
+
function match(request, router, subdomain) {
|
|
27
|
+
if (router.subdomains !== null) {
|
|
28
|
+
for (let i = 0, n = router.subdomains.length; i < n; i++) {
|
|
29
|
+
if (!request.hostname.startsWith(router.subdomains[i])) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
subdomain = router.subdomains[i];
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return router.match(request.method, request.path, subdomain || '');
|
|
37
|
+
}
|
|
38
|
+
function middleware(request, router) {
|
|
39
|
+
function host(...middleware) {
|
|
40
|
+
let instance = pipeline(...middleware);
|
|
41
|
+
return () => instance(request);
|
|
42
|
+
}
|
|
43
|
+
;
|
|
44
|
+
host.dispatch = (request) => {
|
|
45
|
+
let { route } = request.data;
|
|
46
|
+
if (route === undefined) {
|
|
47
|
+
throw new Error(`Middleware: route is undefined!`);
|
|
48
|
+
}
|
|
49
|
+
return route.dispatch(request);
|
|
50
|
+
};
|
|
51
|
+
host.match = (fallback, scheduler, subdomain) => {
|
|
52
|
+
let state = reactive({
|
|
53
|
+
parameters: undefined,
|
|
54
|
+
route: undefined
|
|
55
|
+
});
|
|
56
|
+
if (fallback === undefined) {
|
|
57
|
+
throw new Error('Middleware: fallback route does not exist');
|
|
58
|
+
}
|
|
59
|
+
effect(() => {
|
|
60
|
+
let { parameters, route } = match(request, router, subdomain);
|
|
61
|
+
state.parameters = parameters;
|
|
62
|
+
state.route = route || fallback;
|
|
63
|
+
});
|
|
64
|
+
return (request, next) => {
|
|
65
|
+
return html `${() => {
|
|
66
|
+
if (state.route === undefined) {
|
|
67
|
+
throw new Error('Routing: route is undefined');
|
|
68
|
+
}
|
|
69
|
+
return root(() => {
|
|
70
|
+
request.data.parameters = state.parameters;
|
|
71
|
+
request.data.route = state.route;
|
|
72
|
+
return next(request);
|
|
73
|
+
}, { scheduler });
|
|
74
|
+
}}`;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
}
|
|
25
78
|
function normalize(uri) {
|
|
26
79
|
if (uri[0] === '/') {
|
|
27
80
|
return '#' + uri;
|
|
@@ -42,38 +95,10 @@ export default (instance) => {
|
|
|
42
95
|
if (cache.push(request) === 1) {
|
|
43
96
|
window.addEventListener('hashchange', onpopstate);
|
|
44
97
|
}
|
|
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
|
-
};
|
|
69
98
|
return {
|
|
70
99
|
back,
|
|
71
100
|
forward,
|
|
72
|
-
|
|
73
|
-
middleware: (...middleware) => {
|
|
74
|
-
let instance = pipeline(...middleware);
|
|
75
|
-
return () => instance(request);
|
|
76
|
-
},
|
|
101
|
+
middleware: middleware(request, router),
|
|
77
102
|
redirect: (path, values = []) => {
|
|
78
103
|
if (path.indexOf('://') !== -1) {
|
|
79
104
|
return window.location.replace(path);
|
package/build/router/route.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Middleware, Next, Request } from '../types';
|
|
1
|
+
import { Middleware, Next, NeverAsync, Request } from '../types';
|
|
2
2
|
declare class Route<T> {
|
|
3
3
|
middleware: Middleware<T>[] | null;
|
|
4
4
|
name: string | null;
|
|
@@ -6,6 +6,6 @@ declare class Route<T> {
|
|
|
6
6
|
responder: Next<T>;
|
|
7
7
|
subdomain: string | null;
|
|
8
8
|
constructor(responder: Next<T>);
|
|
9
|
-
dispatch(request: Request<T>): T
|
|
9
|
+
dispatch(request: Request<T>): NeverAsync<T>;
|
|
10
10
|
}
|
|
11
11
|
export { Route };
|
package/build/types.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { NeverAsync } from '@esportsplus/typescript';
|
|
1
2
|
import { Next as N, Stage } from '@esportsplus/pipeline';
|
|
2
3
|
import { Route, Router } from './router';
|
|
3
|
-
type Middleware<T> = Stage<Request<T>, T
|
|
4
|
-
type Next<T> = N<Request<T>, T
|
|
4
|
+
type Middleware<T> = Stage<Request<T>, NeverAsync<T>>;
|
|
5
|
+
type Next<T> = N<Request<T>, NeverAsync<T>>;
|
|
5
6
|
type Options<T> = {
|
|
6
7
|
middleware?: Middleware<T>[];
|
|
7
8
|
name?: string;
|
|
@@ -21,4 +22,4 @@ type Request<T> = {
|
|
|
21
22
|
query: Record<string, unknown>;
|
|
22
23
|
subdomain?: string;
|
|
23
24
|
};
|
|
24
|
-
export { Middleware, Next, Options, Request, Route, Router };
|
|
25
|
+
export { Middleware, Next, NeverAsync, Options, Request, Route, Router };
|
package/package.json
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
"author": "ICJR",
|
|
3
3
|
"dependencies": {
|
|
4
4
|
"@esportsplus/pipeline": "^0.0.5",
|
|
5
|
-
"@esportsplus/reactivity": "^0.1.
|
|
5
|
+
"@esportsplus/reactivity": "^0.1.7",
|
|
6
|
+
"@esportsplus/template": "^0.1.5"
|
|
6
7
|
},
|
|
7
8
|
"devDependencies": {
|
|
8
|
-
"@esportsplus/typescript": "^0.0.
|
|
9
|
+
"@esportsplus/typescript": "^0.0.7"
|
|
9
10
|
},
|
|
10
11
|
"main": "./build/index.js",
|
|
11
12
|
"name": "@esportsplus/routing",
|
|
@@ -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.33"
|
|
21
22
|
}
|
package/src/browser.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { effect, reactive } from '@esportsplus/reactivity';
|
|
2
|
-
import {
|
|
1
|
+
import { effect, reactive, root, Scheduler } from '@esportsplus/reactivity';
|
|
2
|
+
import { html } from '@esportsplus/template';
|
|
3
|
+
import { Middleware, Next, Request, Route, Router } from './types';
|
|
3
4
|
import pipeline from '@esportsplus/pipeline';
|
|
4
5
|
import factory from './router';
|
|
5
6
|
|
|
@@ -32,6 +33,72 @@ function href<T>(): Request<T> {
|
|
|
32
33
|
};
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
function match<T>(request: Request<T>, router: Router<T>, subdomain?: string) {
|
|
37
|
+
if (router.subdomains !== null) {
|
|
38
|
+
for (let i = 0, n = router.subdomains.length; i < n; i++) {
|
|
39
|
+
if (!request.hostname.startsWith(router.subdomains[i])) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
subdomain = router.subdomains[i];
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return router.match(request.method, request.path, subdomain || '');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function middleware<T>(request: Request<T>, router: Router<T>) {
|
|
52
|
+
function host(...middleware: Middleware<T>[]) {
|
|
53
|
+
let instance = pipeline(...middleware);
|
|
54
|
+
|
|
55
|
+
return () => instance(request);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
host.dispatch = (request: Request<T>) => {
|
|
59
|
+
let { route } = request.data;
|
|
60
|
+
|
|
61
|
+
if (route === undefined) {
|
|
62
|
+
throw new Error(`Middleware: route is undefined!`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return route.dispatch(request);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
host.match = (fallback: Route<T>, scheduler: Scheduler, subdomain?: string) => {
|
|
69
|
+
let state = reactive<ReturnType<typeof router.match>>({
|
|
70
|
+
parameters: undefined,
|
|
71
|
+
route: undefined
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
if (fallback === undefined) {
|
|
75
|
+
throw new Error('Middleware: fallback route does not exist');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
effect(() => {
|
|
79
|
+
let { parameters, route } = match(request, router, subdomain);
|
|
80
|
+
|
|
81
|
+
state.parameters = parameters;
|
|
82
|
+
state.route = route || fallback;
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return (request: Request<T>, next: Next<T>) => {
|
|
86
|
+
return html`${() => {
|
|
87
|
+
if (state.route === undefined) {
|
|
88
|
+
throw new Error('Routing: route is undefined');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return root(() => {
|
|
92
|
+
request.data.parameters = state.parameters;
|
|
93
|
+
request.data.route = state.route;
|
|
94
|
+
|
|
95
|
+
return next(request);
|
|
96
|
+
}, { scheduler });
|
|
97
|
+
}}`;
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
35
102
|
function normalize(uri: string) {
|
|
36
103
|
if (uri[0] === '/') {
|
|
37
104
|
return '#' + uri;
|
|
@@ -62,46 +129,10 @@ export default <T>(instance?: Router<T>) => {
|
|
|
62
129
|
window.addEventListener('hashchange', onpopstate);
|
|
63
130
|
}
|
|
64
131
|
|
|
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;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
subdomain = router.subdomains[i];
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
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
132
|
return {
|
|
97
133
|
back,
|
|
98
134
|
forward,
|
|
99
|
-
|
|
100
|
-
middleware: (...middleware: Middleware<T>[]) => {
|
|
101
|
-
let instance = pipeline(...middleware);
|
|
102
|
-
|
|
103
|
-
return () => instance(request);
|
|
104
|
-
},
|
|
135
|
+
middleware: middleware(request, router),
|
|
105
136
|
redirect: (path: string, values: unknown[] = []) => {
|
|
106
137
|
if (path.indexOf('://') !== -1) {
|
|
107
138
|
return window.location.replace(path);
|
package/src/router/route.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Middleware, Next, Request } from '~/types';
|
|
1
|
+
import { Middleware, Next, NeverAsync, Request } from '~/types';
|
|
2
2
|
import pipeline from '@esportsplus/pipeline';
|
|
3
3
|
|
|
4
4
|
|
|
@@ -15,7 +15,7 @@ class Route<T> {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
dispatch(request: Request<T>) {
|
|
18
|
+
dispatch(request: Request<T>): NeverAsync<T> {
|
|
19
19
|
if (this.middleware === null) {
|
|
20
20
|
return this.responder(request);
|
|
21
21
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { NeverAsync } from '@esportsplus/typescript';
|
|
1
2
|
import { Next as N, Stage } from '@esportsplus/pipeline';
|
|
2
3
|
import { Route, Router } from './router';
|
|
3
4
|
|
|
4
5
|
|
|
5
|
-
type Middleware<T> = Stage<Request<T>, T
|
|
6
|
+
type Middleware<T> = Stage<Request<T>, NeverAsync<T>>;
|
|
6
7
|
|
|
7
|
-
type Next<T> = N<Request<T>, T
|
|
8
|
+
type Next<T> = N<Request<T>, NeverAsync<T>>;
|
|
8
9
|
|
|
9
10
|
type Options<T> = {
|
|
10
11
|
middleware?: Middleware<T>[];
|
|
@@ -28,4 +29,4 @@ type Request<T> = {
|
|
|
28
29
|
};
|
|
29
30
|
|
|
30
31
|
|
|
31
|
-
export { Middleware, Next, Options, Request, Route, Router };
|
|
32
|
+
export { Middleware, Next, NeverAsync, Options, Request, Route, Router };
|