@esportsplus/routing 0.6.7 → 0.7.1
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/client/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ declare const router: <const Factories extends readonly RouteFactory<any>[]>(...
|
|
|
5
5
|
back: typeof back;
|
|
6
6
|
forward: typeof forward;
|
|
7
7
|
middleware: {
|
|
8
|
-
(...
|
|
8
|
+
(...middleware: Middleware<InferOutput<Factories[number]>>[]): InferOutput<Factories[number]>;
|
|
9
9
|
dispatch(request: Request<InferOutput<Factories[number]>>): InferOutput<Factories[number]>;
|
|
10
10
|
match(fallback: Route<InferOutput<Factories[number]>>): (request: Request<InferOutput<Factories[number]>>, next: Next<InferOutput<Factories[number]>>) => InferOutput<Factories[number]>;
|
|
11
11
|
};
|
package/build/client/index.js
CHANGED
|
@@ -1,33 +1,40 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as reactivity_f249ad01565249ca98ebd6d980e99aa90 from '@esportsplus/reactivity';
|
|
2
2
|
import { effect, root } from '@esportsplus/reactivity';
|
|
3
3
|
import { Router } from './router/index.js';
|
|
4
|
-
import pipeline from '@esportsplus/pipeline';
|
|
5
4
|
import { PACKAGE_NAME } from './constants.js';
|
|
6
|
-
class
|
|
5
|
+
class ReactiveObject_f249ad01565249ca98ebd6d980e99aa91 extends reactivity_f249ad01565249ca98ebd6d980e99aa90.ReactiveObject {
|
|
7
6
|
#parameters;
|
|
8
7
|
#route;
|
|
9
8
|
constructor(_p0, _p1) {
|
|
10
9
|
super(null);
|
|
11
|
-
this.#parameters = this[
|
|
12
|
-
this.#route = this[
|
|
10
|
+
this.#parameters = this[reactivity_f249ad01565249ca98ebd6d980e99aa90.SIGNAL](_p0);
|
|
11
|
+
this.#route = this[reactivity_f249ad01565249ca98ebd6d980e99aa90.SIGNAL](_p1);
|
|
13
12
|
}
|
|
14
13
|
get parameters() {
|
|
15
|
-
return
|
|
14
|
+
return reactivity_f249ad01565249ca98ebd6d980e99aa90.read(this.#parameters);
|
|
16
15
|
}
|
|
17
16
|
set parameters(_v0) {
|
|
18
|
-
|
|
17
|
+
reactivity_f249ad01565249ca98ebd6d980e99aa90.write(this.#parameters, _v0);
|
|
19
18
|
}
|
|
20
19
|
get route() {
|
|
21
|
-
return
|
|
20
|
+
return reactivity_f249ad01565249ca98ebd6d980e99aa90.read(this.#route);
|
|
22
21
|
}
|
|
23
22
|
set route(_v1) {
|
|
24
|
-
|
|
23
|
+
reactivity_f249ad01565249ca98ebd6d980e99aa90.write(this.#route, _v1);
|
|
25
24
|
}
|
|
26
25
|
}
|
|
27
26
|
let cache = [], location = window.location;
|
|
28
27
|
function back() {
|
|
29
28
|
window.history.back();
|
|
30
29
|
}
|
|
30
|
+
function build(stages) {
|
|
31
|
+
let chain = () => { throw new Error(`${PACKAGE_NAME}: final stage did not return a value`); };
|
|
32
|
+
for (let i = stages.length - 1; i >= 0; i--) {
|
|
33
|
+
let next = chain, stage = stages[i];
|
|
34
|
+
chain = (input) => stage(input, next);
|
|
35
|
+
}
|
|
36
|
+
return chain;
|
|
37
|
+
}
|
|
31
38
|
function forward() {
|
|
32
39
|
window.history.forward();
|
|
33
40
|
}
|
|
@@ -64,12 +71,12 @@ function match(request, router, subdomain) {
|
|
|
64
71
|
return router.match(request.method, request.path, subdomain || '');
|
|
65
72
|
}
|
|
66
73
|
function middleware(request, router) {
|
|
67
|
-
let
|
|
68
|
-
function host(...
|
|
69
|
-
for (let i = 0, n =
|
|
70
|
-
|
|
74
|
+
let stages = [];
|
|
75
|
+
function host(...middleware) {
|
|
76
|
+
for (let i = 0, n = middleware.length; i < n; i++) {
|
|
77
|
+
stages.push(middleware[i]);
|
|
71
78
|
}
|
|
72
|
-
return
|
|
79
|
+
return build(stages)(request);
|
|
73
80
|
}
|
|
74
81
|
;
|
|
75
82
|
host.dispatch = (request) => {
|
|
@@ -77,10 +84,13 @@ function middleware(request, router) {
|
|
|
77
84
|
if (route === undefined) {
|
|
78
85
|
throw new Error(`${PACKAGE_NAME}: route is undefined!`);
|
|
79
86
|
}
|
|
80
|
-
|
|
87
|
+
if (typeof route.middleware !== 'function') {
|
|
88
|
+
route.middleware = build(route.middleware);
|
|
89
|
+
}
|
|
90
|
+
return route.middleware(request);
|
|
81
91
|
};
|
|
82
92
|
host.match = (fallback) => {
|
|
83
|
-
let state = new
|
|
93
|
+
let state = new ReactiveObject_f249ad01565249ca98ebd6d980e99aa91(undefined, undefined);
|
|
84
94
|
if (fallback === undefined) {
|
|
85
95
|
throw new Error(`${PACKAGE_NAME}: fallback route does not exist`);
|
|
86
96
|
}
|
|
@@ -120,7 +130,7 @@ function onpopstate() {
|
|
|
120
130
|
}
|
|
121
131
|
}
|
|
122
132
|
const router = (...factories) => {
|
|
123
|
-
let instance = factories.reduce((r, factory) => factory(r), new Router()), request =
|
|
133
|
+
let instance = factories.reduce((r, factory) => factory(r), new Router()), request = reactivity_f249ad01565249ca98ebd6d980e99aa90.reactive(Object.assign(href(), { data: {} }));
|
|
124
134
|
if (cache.push(request) === 1) {
|
|
125
135
|
window.addEventListener('hashchange', onpopstate);
|
|
126
136
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ON_DELETE, ON_GET, ON_POST, ON_PUT, PACKAGE_NAME } from '../constants.js';
|
|
2
2
|
import { Node } from './node.js';
|
|
3
|
-
import pipeline from '@esportsplus/pipeline';
|
|
4
3
|
function key(method, subdomain) {
|
|
5
4
|
return (method + (subdomain ? ' ' + subdomain : '')).toUpperCase();
|
|
6
5
|
}
|
|
@@ -16,16 +15,16 @@ function normalize(path) {
|
|
|
16
15
|
return path || '/';
|
|
17
16
|
}
|
|
18
17
|
function set(route, options) {
|
|
19
|
-
let
|
|
18
|
+
let middleware = route.middleware;
|
|
20
19
|
for (let key in options) {
|
|
21
20
|
let value = options[key];
|
|
22
21
|
if (key === 'middleware') {
|
|
23
22
|
for (let i = 0, n = value.length; i < n; i++) {
|
|
24
|
-
|
|
23
|
+
middleware.push(value[i]);
|
|
25
24
|
}
|
|
26
25
|
}
|
|
27
26
|
else if (key === 'responder') {
|
|
28
|
-
|
|
27
|
+
middleware.push(value);
|
|
29
28
|
}
|
|
30
29
|
else {
|
|
31
30
|
route[key] = (route[key] || '') + value;
|
|
@@ -57,7 +56,7 @@ class Router {
|
|
|
57
56
|
let groups = this.groups, route = {
|
|
58
57
|
name: null,
|
|
59
58
|
path: null,
|
|
60
|
-
|
|
59
|
+
middleware: [],
|
|
61
60
|
subdomain: null
|
|
62
61
|
};
|
|
63
62
|
for (let i = 0, n = groups.length; i < n; i++) {
|
package/build/client/types.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { NeverAsync } from '@esportsplus/utilities';
|
|
2
2
|
import { Router } from './router/index.js';
|
|
3
|
-
import pipeline from '@esportsplus/pipeline';
|
|
4
3
|
type AccumulateRoutes<T extends readonly RouteFactory<any>[]> = T extends readonly [infer F extends RouteFactory<any>, ...infer Rest extends readonly RouteFactory<any>[]] ? ExtractRoutes<F> & AccumulateRoutes<Rest> : {};
|
|
5
4
|
type ExtractOptionalParams<Path extends string> = Path extends `/${infer Segment}/${infer Rest}` ? (Segment extends `?:${infer Param}` ? Param : never) | ExtractOptionalParams<`/${Rest}`> : Path extends `/${infer Segment}` ? (Segment extends `?:${infer Param}` ? Param : never) : never;
|
|
6
5
|
type ExtractParamsTuple<Path extends string> = Path extends `${infer _Start}:${infer Param}/${infer Rest}` ? [Param, ...ExtractParamsTuple<`/${Rest}`>] : Path extends `${infer _Start}:${infer Param}` ? [Param] : [];
|
|
@@ -38,7 +37,7 @@ type Request<T> = {
|
|
|
38
37
|
type Route<T> = {
|
|
39
38
|
name: Name | null;
|
|
40
39
|
path: string | null;
|
|
41
|
-
|
|
40
|
+
middleware: Middleware<T>[] | Next<T>;
|
|
42
41
|
subdomain: string | null;
|
|
43
42
|
};
|
|
44
43
|
type RouteFactory<T> = (router: Router<T, any>) => Router<T, RouteRegistry>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "ICJR",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"@esportsplus/pipeline": "^1.2.2",
|
|
5
4
|
"@esportsplus/reactivity": "^0.30.2",
|
|
6
5
|
"@esportsplus/typescript": "^0.29.0",
|
|
7
6
|
"@esportsplus/utilities": "^0.27.2"
|
|
@@ -29,7 +28,7 @@
|
|
|
29
28
|
},
|
|
30
29
|
"type": "module",
|
|
31
30
|
"types": "./build/index.d.ts",
|
|
32
|
-
"version": "0.
|
|
31
|
+
"version": "0.7.1",
|
|
33
32
|
"scripts": {
|
|
34
33
|
"build": "tsc",
|
|
35
34
|
"build:test": "vite build --config test/vite.config.ts",
|
package/src/client/index.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { effect, reactive, root } from '@esportsplus/reactivity';
|
|
2
2
|
import { AccumulateRoutes, ExtractOptionalParams, ExtractRequiredParams, InferOutput, Middleware, Next, PathParamsObject, Request, Route, RouteFactory, RoutePath } from './types';
|
|
3
3
|
import { Router } from './router';
|
|
4
|
-
import pipeline from '@esportsplus/pipeline';
|
|
5
4
|
import { PACKAGE_NAME } from './constants';
|
|
6
5
|
|
|
7
6
|
|
|
@@ -13,6 +12,19 @@ function back() {
|
|
|
13
12
|
window.history.back();
|
|
14
13
|
}
|
|
15
14
|
|
|
15
|
+
function build<T>(stages: Middleware<T>[]): Next<T> {
|
|
16
|
+
let chain: Next<T> = () => { throw new Error(`${PACKAGE_NAME}: final stage did not return a value`); };
|
|
17
|
+
|
|
18
|
+
for (let i = stages.length - 1; i >= 0; i--) {
|
|
19
|
+
let next = chain,
|
|
20
|
+
stage = stages[i];
|
|
21
|
+
|
|
22
|
+
chain = (input) => stage(input, next);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return chain;
|
|
26
|
+
}
|
|
27
|
+
|
|
16
28
|
function forward() {
|
|
17
29
|
window.history.forward();
|
|
18
30
|
}
|
|
@@ -62,24 +74,28 @@ function match<T>(request: Request<T>, router: Router<T>, subdomain?: string) {
|
|
|
62
74
|
}
|
|
63
75
|
|
|
64
76
|
function middleware<T>(request: Request<T>, router: Router<T>) {
|
|
65
|
-
let
|
|
77
|
+
let stages: Middleware<T>[] = [];
|
|
66
78
|
|
|
67
|
-
function host(...
|
|
68
|
-
for (let i = 0, n =
|
|
69
|
-
|
|
79
|
+
function host(...middleware: Middleware<T>[]) {
|
|
80
|
+
for (let i = 0, n = middleware.length; i < n; i++) {
|
|
81
|
+
stages.push( middleware[i] );
|
|
70
82
|
}
|
|
71
83
|
|
|
72
|
-
return
|
|
84
|
+
return build(stages)(request);
|
|
73
85
|
};
|
|
74
86
|
|
|
75
87
|
host.dispatch = (request: Request<T>) => {
|
|
76
|
-
let { route } = request.data;
|
|
88
|
+
let { route } = request.data as { route: Route<T> | undefined };
|
|
77
89
|
|
|
78
90
|
if (route === undefined) {
|
|
79
91
|
throw new Error(`${PACKAGE_NAME}: route is undefined!`);
|
|
80
92
|
}
|
|
81
93
|
|
|
82
|
-
|
|
94
|
+
if (typeof route.middleware !== 'function') {
|
|
95
|
+
route.middleware = build(route.middleware);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return route.middleware(request);
|
|
83
99
|
};
|
|
84
100
|
|
|
85
101
|
host.match = (fallback: Route<T>) => {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ON_DELETE, ON_GET, ON_POST, ON_PUT, PACKAGE_NAME } from '../constants';
|
|
2
|
-
import { Route, Name, Options, PathParamsTuple,
|
|
2
|
+
import { Middleware, Route, Name, Options, PathParamsTuple, RouteOptions, RouteRegistry } from '../types';
|
|
3
3
|
import { Node } from './node';
|
|
4
|
-
import pipeline from '@esportsplus/pipeline';
|
|
5
4
|
|
|
6
5
|
|
|
7
6
|
function key(method: string, subdomain?: string | null) {
|
|
@@ -23,18 +22,18 @@ function normalize(path: string) {
|
|
|
23
22
|
}
|
|
24
23
|
|
|
25
24
|
function set<T>(route: Route<T>, options: Options<T> | RouteOptions<T>) {
|
|
26
|
-
let
|
|
25
|
+
let middleware = route.middleware as Middleware<T>[];
|
|
27
26
|
|
|
28
27
|
for (let key in options) {
|
|
29
28
|
let value = options[key as keyof typeof options] as any;
|
|
30
29
|
|
|
31
30
|
if (key === 'middleware') {
|
|
32
31
|
for (let i = 0, n = value.length; i < n; i++) {
|
|
33
|
-
|
|
32
|
+
middleware.push(value[i]);
|
|
34
33
|
}
|
|
35
34
|
}
|
|
36
35
|
else if (key === 'responder') {
|
|
37
|
-
|
|
36
|
+
middleware.push(value);
|
|
38
37
|
}
|
|
39
38
|
else {
|
|
40
39
|
// @ts-ignore
|
|
@@ -76,7 +75,7 @@ class Router<T, TRoutes extends RouteRegistry = {}> {
|
|
|
76
75
|
route: Route<T> = {
|
|
77
76
|
name: null,
|
|
78
77
|
path: null,
|
|
79
|
-
|
|
78
|
+
middleware: [],
|
|
80
79
|
subdomain: null
|
|
81
80
|
};
|
|
82
81
|
|
package/src/client/types.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { NeverAsync } from '@esportsplus/utilities';
|
|
2
2
|
import { Router } from './router';
|
|
3
|
-
import pipeline from '@esportsplus/pipeline';
|
|
4
3
|
|
|
5
4
|
|
|
6
5
|
type AccumulateRoutes<T extends readonly RouteFactory<any>[]> =
|
|
@@ -77,7 +76,7 @@ type Request<T> = {
|
|
|
77
76
|
type Route<T> = {
|
|
78
77
|
name: Name | null;
|
|
79
78
|
path: string | null;
|
|
80
|
-
|
|
79
|
+
middleware: Middleware<T>[] | Next<T>,
|
|
81
80
|
subdomain: string | null;
|
|
82
81
|
};
|
|
83
82
|
|