@esportsplus/routing 0.0.49 → 0.1.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/browser.d.ts +2 -3
- package/build/browser.js +8 -9
- package/build/constants.d.ts +5 -1
- package/build/constants.js +5 -1
- package/build/index.d.ts +3 -5
- package/build/index.js +3 -5
- package/build/router/index.d.ts +15 -13
- package/build/router/index.js +33 -36
- package/build/router/node.d.ts +2 -2
- package/build/router/node.js +1 -1
- package/build/types.d.ts +13 -3
- package/build/types.js +2 -2
- package/package.json +2 -2
- package/src/browser.ts +9 -11
- package/src/constants.ts +10 -1
- package/src/index.ts +3 -7
- package/src/router/index.ts +47 -51
- package/src/router/node.ts +3 -3
- package/src/types.ts +16 -3
- package/build/router/route.d.ts +0 -11
- package/build/router/route.js +0 -18
- package/build/router2/constants.d.ts +0 -3
- package/build/router2/constants.js +0 -3
- package/build/router2/index.d.ts +0 -11
- package/build/router2/index.js +0 -226
- package/build/router2/node.d.ts +0 -10
- package/build/router2/node.js +0 -117
- package/build/router2/trie.d.ts +0 -11
- package/build/router2/trie.js +0 -53
- package/build/router2/types.d.ts +0 -11
- package/build/router2/types.js +0 -1
- package/src/router/route.ts +0 -28
- package/src/router2/constants.ts +0 -6
- package/src/router2/index.ts +0 -330
- package/src/router2/node.ts +0 -184
- package/src/router2/trie.ts +0 -88
- package/src/router2/types.ts +0 -59
package/build/browser.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Scheduler } from '@esportsplus/reactivity';
|
|
2
1
|
import { Middleware, Next, Request, Route, Router } from './types';
|
|
3
2
|
declare function back(): void;
|
|
4
3
|
declare function forward(): void;
|
|
@@ -6,9 +5,9 @@ declare const _default: <T>(instance?: Router<T>) => {
|
|
|
6
5
|
back: typeof back;
|
|
7
6
|
forward: typeof forward;
|
|
8
7
|
middleware: {
|
|
9
|
-
(...middleware: Middleware<T>[]):
|
|
8
|
+
(...middleware: Middleware<T>[]): T;
|
|
10
9
|
dispatch(request: Request<T>): T;
|
|
11
|
-
match(fallback: Route<T
|
|
10
|
+
match(fallback: Route<T>): (request: Request<T>, next: Next<T>) => T;
|
|
12
11
|
};
|
|
13
12
|
redirect: (path: string, values?: unknown[]) => void;
|
|
14
13
|
router: Router<T>;
|
package/build/browser.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { effect, reactive, root } from '@esportsplus/reactivity';
|
|
2
|
-
import
|
|
2
|
+
import { next } from '@esportsplus/pipeline';
|
|
3
3
|
import factory from './router';
|
|
4
4
|
let cache = [];
|
|
5
5
|
function back() {
|
|
@@ -40,28 +40,27 @@ function match(request, router, subdomain) {
|
|
|
40
40
|
}
|
|
41
41
|
function middleware(request, router) {
|
|
42
42
|
function host(...middleware) {
|
|
43
|
-
|
|
44
|
-
return () => instance(request);
|
|
43
|
+
return middleware[0](request, next(1, middleware));
|
|
45
44
|
}
|
|
46
45
|
;
|
|
47
46
|
host.dispatch = (request) => {
|
|
48
47
|
let { route } = request.data;
|
|
49
48
|
if (route === undefined) {
|
|
50
|
-
throw new Error(`
|
|
49
|
+
throw new Error(`Routing: route is undefined!`);
|
|
51
50
|
}
|
|
52
|
-
return route.dispatch(request);
|
|
51
|
+
return route.pipeline.dispatch(request);
|
|
53
52
|
};
|
|
54
|
-
host.match = (fallback
|
|
53
|
+
host.match = (fallback) => {
|
|
55
54
|
let state = reactive({
|
|
56
55
|
parameters: undefined,
|
|
57
56
|
root: undefined,
|
|
58
57
|
route: undefined
|
|
59
58
|
});
|
|
60
59
|
if (fallback === undefined) {
|
|
61
|
-
throw new Error('
|
|
60
|
+
throw new Error('Routing: fallback route does not exist');
|
|
62
61
|
}
|
|
63
62
|
effect(() => {
|
|
64
|
-
let { parameters, route } = match(request, router
|
|
63
|
+
let { parameters, route } = match(request, router);
|
|
65
64
|
state.parameters = parameters;
|
|
66
65
|
state.route = route || fallback;
|
|
67
66
|
});
|
|
@@ -79,7 +78,7 @@ function middleware(request, router) {
|
|
|
79
78
|
};
|
|
80
79
|
state.root = root;
|
|
81
80
|
return next(request);
|
|
82
|
-
}
|
|
81
|
+
});
|
|
83
82
|
};
|
|
84
83
|
};
|
|
85
84
|
return host;
|
package/build/constants.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
declare const ON_DELETE: string[];
|
|
2
|
+
declare const ON_GET: string[];
|
|
3
|
+
declare const ON_POST: string[];
|
|
4
|
+
declare const ON_PUT: string[];
|
|
1
5
|
declare const PLACEHOLDER = 0;
|
|
2
6
|
declare const STATIC = 1;
|
|
3
7
|
declare const WILDCARD = 2;
|
|
4
|
-
export { PLACEHOLDER, STATIC, WILDCARD };
|
|
8
|
+
export { ON_DELETE, ON_GET, ON_POST, ON_PUT, PLACEHOLDER, STATIC, WILDCARD };
|
package/build/constants.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
const ON_DELETE = ['DELETE'];
|
|
2
|
+
const ON_GET = ['GET'];
|
|
3
|
+
const ON_POST = ['POST'];
|
|
4
|
+
const ON_PUT = ['PUT'];
|
|
1
5
|
const PLACEHOLDER = 0;
|
|
2
6
|
const STATIC = 1;
|
|
3
7
|
const WILDCARD = 2;
|
|
4
|
-
export { PLACEHOLDER, STATIC, WILDCARD };
|
|
8
|
+
export { ON_DELETE, ON_GET, ON_POST, ON_PUT, PLACEHOLDER, STATIC, WILDCARD };
|
package/build/index.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import slugify from './slugify';
|
|
5
|
-
export { browser, router, slugify, R2 };
|
|
1
|
+
export { default as browser } from './browser';
|
|
2
|
+
export { default as router } from './router';
|
|
3
|
+
export { default as slugify } from './slugify';
|
|
6
4
|
export * from './types';
|
package/build/index.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import slugify from './slugify';
|
|
5
|
-
export { browser, router, slugify, R2 };
|
|
1
|
+
export { default as browser } from './browser';
|
|
2
|
+
export { default as router } from './router';
|
|
3
|
+
export { default as slugify } from './slugify';
|
|
6
4
|
export * from './types';
|
package/build/router/index.d.ts
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
|
-
import { Options } from '../types';
|
|
1
|
+
import { Name, Options, Route, RouteOptions } from '../types';
|
|
2
2
|
import { Node } from './node';
|
|
3
|
-
import { Route } from './route';
|
|
4
3
|
declare class Router<T> {
|
|
5
|
-
groups:
|
|
4
|
+
groups: Options<T>[];
|
|
6
5
|
root: Node<T>;
|
|
7
|
-
routes: Record<
|
|
8
|
-
static: Record<
|
|
6
|
+
routes: Record<Name, Route<T>>;
|
|
7
|
+
static: Record<Name, Route<T>>;
|
|
9
8
|
subdomains: string[] | null;
|
|
10
9
|
constructor();
|
|
11
10
|
private add;
|
|
12
11
|
private route;
|
|
13
|
-
delete(options:
|
|
14
|
-
get(options:
|
|
15
|
-
group(options:
|
|
12
|
+
delete(options: RouteOptions<T>): this;
|
|
13
|
+
get(options: RouteOptions<T>): this;
|
|
14
|
+
group(options: Options<T>): {
|
|
16
15
|
routes: (fn: (router: Router<T>) => void) => void;
|
|
17
16
|
};
|
|
18
|
-
match(method: string, path: string, subdomain?: string | null):
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
match(method: string, path: string, subdomain?: string | null): {
|
|
18
|
+
parameters?: Readonly<Record<PropertyKey, unknown>> | undefined;
|
|
19
|
+
route?: Readonly<Route<T>> | undefined;
|
|
20
|
+
};
|
|
21
|
+
on(methods: string[], options: RouteOptions<T>): this;
|
|
22
|
+
post(options: RouteOptions<T>): this;
|
|
23
|
+
put(options: RouteOptions<T>): this;
|
|
24
|
+
uri(name: Name, values?: unknown[]): string;
|
|
23
25
|
}
|
|
24
26
|
declare const _default: <T>() => Router<T>;
|
|
25
27
|
export default _default;
|
package/build/router/index.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { STATIC } from '../constants';
|
|
1
|
+
import { ON_DELETE, ON_GET, ON_POST, ON_PUT, STATIC } from '../constants';
|
|
2
2
|
import { Node } from './node';
|
|
3
|
-
import
|
|
4
|
-
let { isArray } = Array;
|
|
3
|
+
import pipeline from '@esportsplus/pipeline';
|
|
5
4
|
function normalize(path) {
|
|
6
5
|
if (path[0] !== '/') {
|
|
7
6
|
path = '/' + path;
|
|
8
7
|
}
|
|
9
|
-
if (path
|
|
8
|
+
if (path.at(-1) === '/') {
|
|
10
9
|
path = path.slice(0, -1);
|
|
11
10
|
}
|
|
12
11
|
return path || '/';
|
|
@@ -14,21 +13,19 @@ function normalize(path) {
|
|
|
14
13
|
function radixkey(method, path, subdomain) {
|
|
15
14
|
return ((subdomain ? subdomain + ' ' : '') + method).toUpperCase() + ' ' + normalize(path);
|
|
16
15
|
}
|
|
17
|
-
function set(route,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
else if (typeof value === 'string') {
|
|
25
|
-
if (typeof route[key] === 'string') {
|
|
26
|
-
route[key] += value;
|
|
16
|
+
function set(route, options) {
|
|
17
|
+
for (let key in options) {
|
|
18
|
+
let value = options[key];
|
|
19
|
+
if (key === 'middleware') {
|
|
20
|
+
for (let i = 0, n = value.length; i < n; i++) {
|
|
21
|
+
route.pipeline.add(value[i]);
|
|
22
|
+
}
|
|
27
23
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
else if (key === 'responder') {
|
|
25
|
+
route.pipeline.add(value);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
route[key] = (route[key] || '') + value;
|
|
32
29
|
}
|
|
33
30
|
}
|
|
34
31
|
}
|
|
@@ -50,19 +47,17 @@ class Router {
|
|
|
50
47
|
}
|
|
51
48
|
return this;
|
|
52
49
|
}
|
|
53
|
-
route(
|
|
54
|
-
let route =
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
50
|
+
route(options) {
|
|
51
|
+
let groups = this.groups, route = {
|
|
52
|
+
name: null,
|
|
53
|
+
path: null,
|
|
54
|
+
pipeline: pipeline(),
|
|
55
|
+
subdomain: null
|
|
56
|
+
};
|
|
57
|
+
for (let i = 0, n = groups.length; i < n; i++) {
|
|
58
|
+
set(route, groups[i]);
|
|
61
59
|
}
|
|
62
|
-
set(route,
|
|
63
|
-
set(route, 'middleware', middleware);
|
|
64
|
-
set(route, 'path', path);
|
|
65
|
-
set(route, 'subdomain', subdomain);
|
|
60
|
+
set(route, options);
|
|
66
61
|
if (route.path) {
|
|
67
62
|
route.path = normalize(route.path);
|
|
68
63
|
}
|
|
@@ -72,10 +67,10 @@ class Router {
|
|
|
72
67
|
return route;
|
|
73
68
|
}
|
|
74
69
|
delete(options) {
|
|
75
|
-
return this.on(
|
|
70
|
+
return this.on(ON_DELETE, options);
|
|
76
71
|
}
|
|
77
72
|
get(options) {
|
|
78
|
-
return this.on(
|
|
73
|
+
return this.on(ON_GET, options);
|
|
79
74
|
}
|
|
80
75
|
group(options) {
|
|
81
76
|
return {
|
|
@@ -128,10 +123,10 @@ class Router {
|
|
|
128
123
|
return this;
|
|
129
124
|
}
|
|
130
125
|
post(options) {
|
|
131
|
-
return this.on(
|
|
126
|
+
return this.on(ON_POST, options);
|
|
132
127
|
}
|
|
133
128
|
put(options) {
|
|
134
|
-
return this.on(
|
|
129
|
+
return this.on(ON_PUT, options);
|
|
135
130
|
}
|
|
136
131
|
uri(name, values = []) {
|
|
137
132
|
let path = this.routes[name]?.path;
|
|
@@ -151,7 +146,9 @@ class Router {
|
|
|
151
146
|
resolved.push(values[i]);
|
|
152
147
|
}
|
|
153
148
|
else if (symbol === '*') {
|
|
154
|
-
|
|
149
|
+
for (let n = values.length; i < n; i++) {
|
|
150
|
+
resolved.push(values[i]);
|
|
151
|
+
}
|
|
155
152
|
break;
|
|
156
153
|
}
|
|
157
154
|
else {
|
|
@@ -162,4 +159,4 @@ class Router {
|
|
|
162
159
|
}
|
|
163
160
|
}
|
|
164
161
|
export default () => new Router();
|
|
165
|
-
export { Router
|
|
162
|
+
export { Router };
|
package/build/router/node.d.ts
CHANGED
|
@@ -9,8 +9,8 @@ declare class Node<T> {
|
|
|
9
9
|
constructor(parent?: Node<T>['parent']);
|
|
10
10
|
add(path: string, route: Route<T>): Node<T>;
|
|
11
11
|
find(path: string): {
|
|
12
|
-
parameters?: Record<PropertyKey, unknown
|
|
13
|
-
route?: Route<T
|
|
12
|
+
parameters?: Readonly<Record<PropertyKey, unknown>>;
|
|
13
|
+
route?: Readonly<Route<T>>;
|
|
14
14
|
};
|
|
15
15
|
remove(path: string): void;
|
|
16
16
|
}
|
package/build/router/node.js
CHANGED
package/build/types.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { NeverAsync } from '@esportsplus/typescript';
|
|
2
|
-
import {
|
|
2
|
+
import { Router } from './router';
|
|
3
|
+
import pipeline from '@esportsplus/pipeline';
|
|
3
4
|
type Middleware<T> = NeverAsync<(input: Request<T>, next: Next<T>) => T>;
|
|
5
|
+
type Name = string;
|
|
4
6
|
type Next<T> = NeverAsync<(input: Request<T>) => T>;
|
|
5
7
|
type Options<T> = {
|
|
6
8
|
middleware?: Middleware<T>[];
|
|
7
9
|
name?: string;
|
|
8
10
|
path?: string;
|
|
9
|
-
responder: Next<T>;
|
|
10
11
|
subdomain?: string;
|
|
11
12
|
};
|
|
12
13
|
type Request<T> = {
|
|
@@ -21,4 +22,13 @@ type Request<T> = {
|
|
|
21
22
|
query: Record<string, unknown>;
|
|
22
23
|
subdomain?: string;
|
|
23
24
|
};
|
|
24
|
-
|
|
25
|
+
type Route<T> = {
|
|
26
|
+
name: Name | null;
|
|
27
|
+
path: string | null;
|
|
28
|
+
pipeline: ReturnType<typeof pipeline<Request<T>, T>>;
|
|
29
|
+
subdomain: string | null;
|
|
30
|
+
};
|
|
31
|
+
type RouteOptions<T> = Options<T> & {
|
|
32
|
+
responder: Next<T>;
|
|
33
|
+
};
|
|
34
|
+
export { Middleware, Name, Next, Options, Request, Route, RouteOptions, Router };
|
package/build/types.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { Router } from './router';
|
|
2
|
+
export { Router };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "ICJR",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"@esportsplus/pipeline": "^
|
|
4
|
+
"@esportsplus/pipeline": "^1.1.0",
|
|
5
5
|
"@esportsplus/reactivity": "^0.1.21"
|
|
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.
|
|
20
|
+
"version": "0.1.1"
|
|
21
21
|
}
|
package/src/browser.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { effect, reactive, root, Root
|
|
1
|
+
import { effect, reactive, root, Root } from '@esportsplus/reactivity';
|
|
2
2
|
import { Middleware, Next, Request, Route, Router } from './types';
|
|
3
|
-
import
|
|
3
|
+
import { next } from '@esportsplus/pipeline';
|
|
4
4
|
import factory from './router';
|
|
5
5
|
|
|
6
6
|
|
|
@@ -55,22 +55,20 @@ function match<T>(request: Request<T>, router: Router<T>, subdomain?: string) {
|
|
|
55
55
|
|
|
56
56
|
function middleware<T>(request: Request<T>, router: Router<T>) {
|
|
57
57
|
function host(...middleware: Middleware<T>[]) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return () => instance(request);
|
|
58
|
+
return middleware[0](request, next(1, middleware));
|
|
61
59
|
};
|
|
62
60
|
|
|
63
61
|
host.dispatch = (request: Request<T>) => {
|
|
64
62
|
let { route } = request.data;
|
|
65
63
|
|
|
66
64
|
if (route === undefined) {
|
|
67
|
-
throw new Error(`
|
|
65
|
+
throw new Error(`Routing: route is undefined!`);
|
|
68
66
|
}
|
|
69
67
|
|
|
70
|
-
return route.dispatch(request);
|
|
68
|
+
return route.pipeline.dispatch(request);
|
|
71
69
|
};
|
|
72
70
|
|
|
73
|
-
host.match = (fallback: Route<T
|
|
71
|
+
host.match = (fallback: Route<T>) => {
|
|
74
72
|
let state = reactive<ReturnType<typeof router.match> & { root?: Root }>({
|
|
75
73
|
parameters: undefined,
|
|
76
74
|
root: undefined,
|
|
@@ -78,11 +76,11 @@ function middleware<T>(request: Request<T>, router: Router<T>) {
|
|
|
78
76
|
});
|
|
79
77
|
|
|
80
78
|
if (fallback === undefined) {
|
|
81
|
-
throw new Error('
|
|
79
|
+
throw new Error('Routing: fallback route does not exist');
|
|
82
80
|
}
|
|
83
81
|
|
|
84
82
|
effect(() => {
|
|
85
|
-
let { parameters, route } = match(request, router
|
|
83
|
+
let { parameters, route } = match(request, router);
|
|
86
84
|
|
|
87
85
|
state.parameters = parameters;
|
|
88
86
|
state.route = route || fallback;
|
|
@@ -105,7 +103,7 @@ function middleware<T>(request: Request<T>, router: Router<T>) {
|
|
|
105
103
|
state.root = root;
|
|
106
104
|
|
|
107
105
|
return next(request);
|
|
108
|
-
}
|
|
106
|
+
});
|
|
109
107
|
};
|
|
110
108
|
};
|
|
111
109
|
|
package/src/constants.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
const ON_DELETE = ['DELETE'];
|
|
2
|
+
|
|
3
|
+
const ON_GET = ['GET'];
|
|
4
|
+
|
|
5
|
+
const ON_POST = ['POST'];
|
|
6
|
+
|
|
7
|
+
const ON_PUT = ['PUT'];
|
|
8
|
+
|
|
9
|
+
|
|
1
10
|
const PLACEHOLDER = 0;
|
|
2
11
|
|
|
3
12
|
const STATIC = 1;
|
|
@@ -5,4 +14,4 @@ const STATIC = 1;
|
|
|
5
14
|
const WILDCARD = 2;
|
|
6
15
|
|
|
7
16
|
|
|
8
|
-
export { PLACEHOLDER, STATIC, WILDCARD };
|
|
17
|
+
export { ON_DELETE, ON_GET, ON_POST, ON_PUT, PLACEHOLDER, STATIC, WILDCARD };
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import slugify from './slugify';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export { browser, router, slugify, R2 };
|
|
1
|
+
export { default as browser } from './browser';
|
|
2
|
+
export { default as router } from './router';
|
|
3
|
+
export { default as slugify } from './slugify';
|
|
8
4
|
export * from './types';
|
package/src/router/index.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import { STATIC } from '~/constants';
|
|
2
|
-
import { Options } from '~/types';
|
|
1
|
+
import { ON_DELETE, ON_GET, ON_POST, ON_PUT, STATIC } from '~/constants';
|
|
2
|
+
import { Name, Options, Request, Route, RouteOptions } from '~/types';
|
|
3
3
|
import { Node } from './node';
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
let { isArray } = Array;
|
|
4
|
+
import pipeline from '@esportsplus/pipeline';
|
|
8
5
|
|
|
9
6
|
|
|
10
7
|
function normalize(path: string) {
|
|
@@ -12,7 +9,7 @@ function normalize(path: string) {
|
|
|
12
9
|
path = '/' + path;
|
|
13
10
|
}
|
|
14
11
|
|
|
15
|
-
if (path
|
|
12
|
+
if (path.at(-1) === '/') {
|
|
16
13
|
path = path.slice(0, -1);
|
|
17
14
|
}
|
|
18
15
|
|
|
@@ -23,32 +20,31 @@ function radixkey(method: string, path: string, subdomain?: string | null) {
|
|
|
23
20
|
return ((subdomain ? subdomain + ' ' : '') + method).toUpperCase() + ' ' + normalize(path);
|
|
24
21
|
}
|
|
25
22
|
|
|
26
|
-
function set<T>(route: Route<T>,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
23
|
+
function set<T>(route: Route<T>, options: Options<T> | RouteOptions<T>) {
|
|
24
|
+
for (let key in options) {
|
|
25
|
+
let value = options[key as keyof typeof options] as any;
|
|
30
26
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (typeof route[key] === 'string') {
|
|
36
|
-
(route[key] as string) += value;
|
|
27
|
+
if (key === 'middleware') {
|
|
28
|
+
for (let i = 0, n = value.length; i < n; i++) {
|
|
29
|
+
route.pipeline.add(value[i]);
|
|
30
|
+
}
|
|
37
31
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
32
|
+
else if (key === 'responder') {
|
|
33
|
+
route.pipeline.add(value);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
route[key] = (route[key] || '') + value;
|
|
42
38
|
}
|
|
43
39
|
}
|
|
44
40
|
}
|
|
45
41
|
|
|
46
42
|
|
|
47
43
|
class Router<T> {
|
|
48
|
-
groups:
|
|
44
|
+
groups: Options<T>[] = [];
|
|
49
45
|
root: Node<T>;
|
|
50
|
-
routes: Record<
|
|
51
|
-
static: Record<
|
|
46
|
+
routes: Record<Name, Route<T>> = {};
|
|
47
|
+
static: Record<Name, Route<T>> = {};
|
|
52
48
|
subdomains: string[] | null = null;
|
|
53
49
|
|
|
54
50
|
|
|
@@ -69,22 +65,20 @@ class Router<T> {
|
|
|
69
65
|
return this;
|
|
70
66
|
}
|
|
71
67
|
|
|
72
|
-
private route(
|
|
73
|
-
let
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
68
|
+
private route(options: RouteOptions<T>) {
|
|
69
|
+
let groups = this.groups,
|
|
70
|
+
route: Route<T> = {
|
|
71
|
+
name: null,
|
|
72
|
+
path: null,
|
|
73
|
+
pipeline: pipeline<Request<T>, T>(),
|
|
74
|
+
subdomain: null
|
|
75
|
+
};
|
|
77
76
|
|
|
78
|
-
|
|
79
|
-
set(route,
|
|
80
|
-
set(route, 'path', path);
|
|
81
|
-
set(route, 'subdomain', subdomain);
|
|
77
|
+
for (let i = 0, n = groups.length; i < n; i++) {
|
|
78
|
+
set(route, groups[i]);
|
|
82
79
|
}
|
|
83
80
|
|
|
84
|
-
set(route,
|
|
85
|
-
set(route, 'middleware', middleware);
|
|
86
|
-
set(route, 'path', path);
|
|
87
|
-
set(route, 'subdomain', subdomain);
|
|
81
|
+
set(route, options);
|
|
88
82
|
|
|
89
83
|
if (route.path) {
|
|
90
84
|
route.path = normalize(route.path);
|
|
@@ -98,15 +92,15 @@ class Router<T> {
|
|
|
98
92
|
}
|
|
99
93
|
|
|
100
94
|
|
|
101
|
-
delete(options:
|
|
102
|
-
return this.on(
|
|
95
|
+
delete(options: RouteOptions<T>) {
|
|
96
|
+
return this.on(ON_DELETE, options);
|
|
103
97
|
}
|
|
104
98
|
|
|
105
|
-
get(options:
|
|
106
|
-
return this.on(
|
|
99
|
+
get(options: RouteOptions<T>) {
|
|
100
|
+
return this.on(ON_GET, options);
|
|
107
101
|
}
|
|
108
102
|
|
|
109
|
-
group(options:
|
|
103
|
+
group(options: Options<T>) {
|
|
110
104
|
return {
|
|
111
105
|
routes: (fn: (router: Router<T>) => void) => {
|
|
112
106
|
this.groups.push(options);
|
|
@@ -116,19 +110,19 @@ class Router<T> {
|
|
|
116
110
|
}
|
|
117
111
|
}
|
|
118
112
|
|
|
119
|
-
match(method: string, path: string, subdomain?: string | null)
|
|
113
|
+
match(method: string, path: string, subdomain?: string | null) {
|
|
120
114
|
let key = radixkey(method, path, subdomain);
|
|
121
115
|
|
|
122
116
|
if (key in this.static) {
|
|
123
117
|
return {
|
|
124
|
-
route: this.static[key]
|
|
118
|
+
route: this.static[key] as Readonly<Route<T>>
|
|
125
119
|
};
|
|
126
120
|
}
|
|
127
121
|
|
|
128
122
|
return this.root.find(key);
|
|
129
123
|
}
|
|
130
124
|
|
|
131
|
-
on(methods: string[], options:
|
|
125
|
+
on(methods: string[], options: RouteOptions<T>) {
|
|
132
126
|
let route = this.route(options);
|
|
133
127
|
|
|
134
128
|
if (route.name) {
|
|
@@ -169,15 +163,15 @@ class Router<T> {
|
|
|
169
163
|
return this;
|
|
170
164
|
}
|
|
171
165
|
|
|
172
|
-
post(options:
|
|
173
|
-
return this.on(
|
|
166
|
+
post(options: RouteOptions<T>) {
|
|
167
|
+
return this.on(ON_POST, options);
|
|
174
168
|
}
|
|
175
169
|
|
|
176
|
-
put(options:
|
|
177
|
-
return this.on(
|
|
170
|
+
put(options: RouteOptions<T>) {
|
|
171
|
+
return this.on(ON_PUT, options);
|
|
178
172
|
}
|
|
179
173
|
|
|
180
|
-
uri(name:
|
|
174
|
+
uri(name: Name, values: unknown[] = []) {
|
|
181
175
|
let path = this.routes[name]?.path;
|
|
182
176
|
|
|
183
177
|
if (!path) {
|
|
@@ -202,7 +196,9 @@ class Router<T> {
|
|
|
202
196
|
resolved.push(values[i]);
|
|
203
197
|
}
|
|
204
198
|
else if (symbol === '*') {
|
|
205
|
-
|
|
199
|
+
for (let n = values.length; i < n; i++) {
|
|
200
|
+
resolved.push( values[i] );
|
|
201
|
+
}
|
|
206
202
|
break;
|
|
207
203
|
}
|
|
208
204
|
else {
|
package/src/router/node.ts
CHANGED
|
@@ -60,8 +60,8 @@ class Node<T> {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
find(path: string): {
|
|
63
|
-
parameters?: Record<PropertyKey, unknown
|
|
64
|
-
route?: Route<T
|
|
63
|
+
parameters?: Readonly<Record<PropertyKey, unknown>>;
|
|
64
|
+
route?: Readonly<Route<T>>;
|
|
65
65
|
} {
|
|
66
66
|
let node: Node<T> | undefined = this,
|
|
67
67
|
parameters: Record<PropertyKey, unknown> = {},
|
|
@@ -96,7 +96,7 @@ class Node<T> {
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
if ((
|
|
99
|
+
if ((node === undefined || node.route === null) && wildcard !== null) {
|
|
100
100
|
node = wildcard.node;
|
|
101
101
|
parameters[ node.property! ] = wildcard.value;
|
|
102
102
|
}
|