@esportsplus/routing 0.0.20 → 0.0.21
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 +15 -0
- package/build/browser.js +65 -0
- package/build/index.d.ts +2 -3
- package/build/index.js +2 -3
- package/build/router/route.js +2 -2
- package/build/spa.js +3 -3
- package/build/types.d.ts +2 -2
- package/package.json +2 -2
- package/src/{spa.ts → browser.ts} +23 -20
- package/src/index.ts +2 -3
- package/src/router/route.ts +2 -2
- package/src/types.ts +2 -2
- package/src/middleware/dispatch.ts +0 -13
- package/src/middleware/index.ts +0 -6
- package/src/middleware/match.ts +0 -32
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Middleware, Response, Router } from './types';
|
|
2
|
+
declare function back(): void;
|
|
3
|
+
declare function forward(): void;
|
|
4
|
+
declare const _default: <R>(instance?: Router<R> | undefined) => {
|
|
5
|
+
back: typeof back;
|
|
6
|
+
forward: typeof forward;
|
|
7
|
+
middleware: (...fns: Middleware<R>[]) => () => Response<R>;
|
|
8
|
+
redirect: (path: string, { state, values }: {
|
|
9
|
+
state?: Record<PropertyKey, unknown> | undefined;
|
|
10
|
+
values?: unknown[] | undefined;
|
|
11
|
+
}) => void;
|
|
12
|
+
router: Router<R>;
|
|
13
|
+
uri: (path: string, values?: unknown[]) => string;
|
|
14
|
+
};
|
|
15
|
+
export default _default;
|
package/build/browser.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { reactive } from '@esportsplus/reactivity';
|
|
2
|
+
import pipeline from '@esportsplus/pipeline';
|
|
3
|
+
import factory from './router';
|
|
4
|
+
let cache = [], registered = false;
|
|
5
|
+
function back() {
|
|
6
|
+
window.history.back();
|
|
7
|
+
}
|
|
8
|
+
function forward() {
|
|
9
|
+
window.history.forward();
|
|
10
|
+
}
|
|
11
|
+
function normalize(uri) {
|
|
12
|
+
if (uri[0] === '/') {
|
|
13
|
+
return '#' + uri;
|
|
14
|
+
}
|
|
15
|
+
return uri;
|
|
16
|
+
}
|
|
17
|
+
function onpopstate() {
|
|
18
|
+
let values = request();
|
|
19
|
+
for (let i = 0, n = cache.length; i < n; i++) {
|
|
20
|
+
let state = cache[i];
|
|
21
|
+
for (let key in state) {
|
|
22
|
+
state[key] = values[key];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
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
|
+
export default (instance) => {
|
|
41
|
+
let router = instance || factory(), state = reactive(request());
|
|
42
|
+
cache.push(state);
|
|
43
|
+
if (!registered) {
|
|
44
|
+
registered = true;
|
|
45
|
+
window.addEventListener('popstate', onpopstate);
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
back,
|
|
49
|
+
forward,
|
|
50
|
+
middleware: (...fns) => {
|
|
51
|
+
let instance = pipeline(...fns);
|
|
52
|
+
return () => instance(state);
|
|
53
|
+
},
|
|
54
|
+
redirect: (path, { state, values }) => {
|
|
55
|
+
if (path.startsWith('https://') || path.startsWith('http://')) {
|
|
56
|
+
return window.location.replace(path);
|
|
57
|
+
}
|
|
58
|
+
window.history.pushState((state || {}), '', normalize(router.uri(path, values || [])));
|
|
59
|
+
},
|
|
60
|
+
router,
|
|
61
|
+
uri: (path, values = []) => {
|
|
62
|
+
return normalize(router.uri(path, values || []));
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
};
|
package/build/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import browser from './browser';
|
|
2
2
|
import router from './router';
|
|
3
3
|
import slugify from './slugify';
|
|
4
|
-
|
|
5
|
-
export { middleware, router, slugify, spa };
|
|
4
|
+
export { browser, router, slugify };
|
|
6
5
|
export * from './types';
|
package/build/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import browser from './browser';
|
|
2
2
|
import router from './router';
|
|
3
3
|
import slugify from './slugify';
|
|
4
|
-
|
|
5
|
-
export { middleware, router, slugify, spa };
|
|
4
|
+
export { browser, router, slugify };
|
|
6
5
|
export * from './types';
|
package/build/router/route.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import pipeline from '@esportsplus/pipeline';
|
|
2
2
|
class Route {
|
|
3
3
|
dispatch = null;
|
|
4
4
|
name = null;
|
|
@@ -15,7 +15,7 @@ class Route {
|
|
|
15
15
|
this.dispatch = (request) => this.responder(request);
|
|
16
16
|
}
|
|
17
17
|
else {
|
|
18
|
-
this.dispatch =
|
|
18
|
+
this.dispatch = pipeline(...this.stack, (request => this.responder(request)));
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
return this.dispatch;
|
package/build/spa.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { reactive } from '@esportsplus/reactivity';
|
|
2
|
-
import
|
|
2
|
+
import pipeline from '@esportsplus/pipeline';
|
|
3
3
|
let cache = [], registered = false;
|
|
4
4
|
function back() {
|
|
5
5
|
window.history.back();
|
|
@@ -47,8 +47,8 @@ export default (router) => {
|
|
|
47
47
|
back,
|
|
48
48
|
forward,
|
|
49
49
|
middleware: (...fns) => {
|
|
50
|
-
let
|
|
51
|
-
return () =>
|
|
50
|
+
let instance = pipeline(...fns);
|
|
51
|
+
return () => instance(state);
|
|
52
52
|
},
|
|
53
53
|
redirect: (path, { state, values }) => {
|
|
54
54
|
if (path.startsWith('https://') || path.startsWith('http://')) {
|
package/build/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Next as N, Stage } from '@esportsplus/pipeline';
|
|
2
2
|
import { Route, Router } from './router';
|
|
3
|
-
type Middleware<R> =
|
|
3
|
+
type Middleware<R> = Stage<Request, Response<R>>;
|
|
4
4
|
type Next<R> = N<Request, Response<R>>;
|
|
5
5
|
type Options<R> = {
|
|
6
6
|
middleware?: Middleware<R>[];
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "ICJR",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"@esportsplus/
|
|
4
|
+
"@esportsplus/pipeline": "^0.0.1",
|
|
5
5
|
"@esportsplus/reactivity": "^0.0.30"
|
|
6
6
|
},
|
|
7
7
|
"devDependencies": {
|
|
@@ -17,5 +17,5 @@
|
|
|
17
17
|
"prepublishOnly": "npm run build"
|
|
18
18
|
},
|
|
19
19
|
"types": "./build/index.d.ts",
|
|
20
|
-
"version": "0.0.
|
|
20
|
+
"version": "0.0.21"
|
|
21
21
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { reactive } from '@esportsplus/reactivity';
|
|
2
2
|
import { Middleware, Request, Response, Router } from './types';
|
|
3
|
-
import
|
|
3
|
+
import pipeline from '@esportsplus/pipeline';
|
|
4
|
+
import factory from './router';
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
let cache: Request[] = [],
|
|
@@ -23,6 +24,19 @@ function normalize(uri: string) {
|
|
|
23
24
|
return uri;
|
|
24
25
|
}
|
|
25
26
|
|
|
27
|
+
function onpopstate() {
|
|
28
|
+
let values = request();
|
|
29
|
+
|
|
30
|
+
for (let i = 0, n = cache.length; i < n; i++) {
|
|
31
|
+
let state = cache[i];
|
|
32
|
+
|
|
33
|
+
for (let key in state) {
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
state[key] = values[key];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
26
40
|
function request(): Request {
|
|
27
41
|
let { hash, hostname, href, origin, port, protocol } = new URL( window.location?.href || '' ),
|
|
28
42
|
path = hash ? hash.slice(1).split('?') : ['/', ''];
|
|
@@ -36,41 +50,29 @@ function request(): Request {
|
|
|
36
50
|
path: path[0],
|
|
37
51
|
port,
|
|
38
52
|
protocol,
|
|
39
|
-
query: path[1] ? Object.fromEntries( (new URLSearchParams(path[1])).entries() ) : {}
|
|
53
|
+
query: path[1] ? Object.fromEntries( (new URLSearchParams(path[1])).entries() ) : {},
|
|
40
54
|
};
|
|
41
55
|
}
|
|
42
56
|
|
|
43
|
-
function event() {
|
|
44
|
-
let values = request();
|
|
45
|
-
|
|
46
|
-
for (let i = 0, n = cache.length; i < n; i++) {
|
|
47
|
-
let state = cache[i];
|
|
48
|
-
|
|
49
|
-
for (let key in values) {
|
|
50
|
-
// @ts-ignore
|
|
51
|
-
state[key] = values[key];
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
57
|
|
|
57
|
-
export default <R>(
|
|
58
|
-
let
|
|
58
|
+
export default <R>(instance?: Router<R>) => {
|
|
59
|
+
let router = instance || factory<R>(),
|
|
60
|
+
state = reactive( request() );
|
|
59
61
|
|
|
60
62
|
cache.push(state);
|
|
61
63
|
|
|
62
64
|
if (!registered) {
|
|
63
65
|
registered = true;
|
|
64
|
-
window.addEventListener('popstate',
|
|
66
|
+
window.addEventListener('popstate', onpopstate);
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
return {
|
|
68
70
|
back,
|
|
69
71
|
forward,
|
|
70
72
|
middleware: (...fns: Middleware<R>[]) => {
|
|
71
|
-
let
|
|
73
|
+
let instance = pipeline<Request, Response<R>>(...fns);
|
|
72
74
|
|
|
73
|
-
return () =>
|
|
75
|
+
return () => instance(state);
|
|
74
76
|
},
|
|
75
77
|
redirect: (path: string, { state, values }: { state?: Record<PropertyKey, unknown>; values?: unknown[] }) => {
|
|
76
78
|
if (path.startsWith('https://') || path.startsWith('http://')) {
|
|
@@ -79,6 +81,7 @@ export default <R>(router: Router<R>) => {
|
|
|
79
81
|
|
|
80
82
|
window.history.pushState( (state || {}), '', normalize(router.uri(path, values || [])) );
|
|
81
83
|
},
|
|
84
|
+
router,
|
|
82
85
|
uri: (path: string, values: unknown[] = []) => {
|
|
83
86
|
return normalize( router.uri(path, values || []) );
|
|
84
87
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import browser from './browser';
|
|
2
2
|
import router from './router';
|
|
3
3
|
import slugify from './slugify';
|
|
4
|
-
import spa from './spa';
|
|
5
4
|
|
|
6
5
|
|
|
7
|
-
export {
|
|
6
|
+
export { browser, router, slugify };
|
|
8
7
|
export * from './types';
|
package/src/router/route.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Middleware, Next, Responder } from '~/types';
|
|
2
|
-
import
|
|
2
|
+
import pipeline from '@esportsplus/pipeline';
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
class Route<R> {
|
|
@@ -22,7 +22,7 @@ class Route<R> {
|
|
|
22
22
|
this.dispatch = (request) => this.responder(request);
|
|
23
23
|
}
|
|
24
24
|
else {
|
|
25
|
-
this.dispatch =
|
|
25
|
+
this.dispatch = pipeline(...this.stack, (request => this.responder(request)));
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
|
package/src/types.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Next as N, Stage } from '@esportsplus/pipeline';
|
|
2
2
|
import { Route, Router } from './router';
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
type Middleware<R> =
|
|
5
|
+
type Middleware<R> = Stage<Request, Response<R>>;
|
|
6
6
|
|
|
7
7
|
type Next<R> = N<Request, Response<R>>;
|
|
8
8
|
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Request } from '~/types';
|
|
2
|
-
import { Router } from '~/router';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export default <R>(request: Request & { data: ReturnType<Router<R>['match']> }) => {
|
|
6
|
-
let { route } = request.data;
|
|
7
|
-
|
|
8
|
-
if (route === undefined) {
|
|
9
|
-
throw new Error(`Routing: route dispatching failed, route is undefined!`);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return route.dispatcher(request);
|
|
13
|
-
};
|
package/src/middleware/index.ts
DELETED
package/src/middleware/match.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { Middleware, Router } from '~/types';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export default <R>(router: Router<R>, subdomain?: string): Middleware<R> => {
|
|
5
|
-
return (request, next) => {
|
|
6
|
-
let match = subdomain || request.subdomain;
|
|
7
|
-
|
|
8
|
-
if (match === undefined) {
|
|
9
|
-
if (router.subdomains) {
|
|
10
|
-
for (let i = 0, n = router.subdomains.length; i < n; i++) {
|
|
11
|
-
if (!request.hostname.startsWith(router.subdomains[i])) {
|
|
12
|
-
continue;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
match = router.subdomains[i];
|
|
16
|
-
break;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (match === undefined) {
|
|
21
|
-
match = '';
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
let { parameters, route } = router.match(request.method, request.path, match);
|
|
26
|
-
|
|
27
|
-
request.data.parameters = parameters;
|
|
28
|
-
request.data.route = route;
|
|
29
|
-
|
|
30
|
-
return next(request);
|
|
31
|
-
};
|
|
32
|
-
};
|