@esportsplus/routing 0.0.9 → 0.0.11
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/hash.d.ts +70 -0
- package/build/hash.js +75 -0
- package/build/index.d.ts +6 -92
- package/build/index.js +6 -8
- package/build/middleware/dispatch.d.ts +5 -0
- package/build/middleware/dispatch.js +7 -0
- package/build/middleware/factory.d.ts +3 -3
- package/build/middleware/factory.js +13 -13
- package/build/middleware/index.d.ts +17 -47
- package/build/middleware/index.js +5 -4
- package/build/middleware/match.d.ts +5 -0
- package/build/middleware/match.js +23 -0
- package/build/router/index.d.ts +26 -0
- package/build/router/index.js +160 -0
- package/build/router/node.d.ts +17 -0
- package/build/router/node.js +93 -0
- package/build/router/path.d.ts +6 -0
- package/build/router/path.js +20 -0
- package/build/router/route.d.ts +13 -0
- package/build/router/route.js +24 -0
- package/build/slugify.d.ts +2 -0
- package/build/slugify.js +3 -0
- package/build/symbols.d.ts +4 -0
- package/build/symbols.js +4 -0
- package/build/types.d.ts +18 -26
- package/build/types.js +2 -1
- package/package.json +5 -4
- package/readme.md +1 -0
- package/src/hash.ts +106 -0
- package/src/index.ts +4 -6
- package/src/middleware/dispatch.ts +12 -0
- package/src/middleware/index.ts +5 -4
- package/src/middleware/match.ts +32 -0
- package/src/router/index.ts +209 -0
- package/src/router/node.ts +141 -0
- package/src/router/path.ts +28 -0
- package/src/router/route.ts +34 -0
- package/src/slugify.ts +4 -0
- package/src/symbols.ts +8 -0
- package/src/types.ts +16 -28
- package/tsconfig.json +5 -19
- package/build/group.d.ts +0 -3
- package/build/group.js +0 -8
- package/build/listener.d.ts +0 -4
- package/build/listener.js +0 -13
- package/build/middleware/common/dispatch.d.ts +0 -3
- package/build/middleware/common/dispatch.js +0 -11
- package/build/middleware/common/index.d.ts +0 -29
- package/build/middleware/common/index.js +0 -3
- package/build/middleware/common/match.d.ts +0 -3
- package/build/middleware/common/match.js +0 -12
- package/build/redirect.d.ts +0 -2
- package/build/redirect.js +0 -10
- package/build/routes.d.ts +0 -20
- package/build/routes.js +0 -45
- package/build/url.d.ts +0 -30
- package/build/url.js +0 -22
- package/src/group.ts +0 -11
- package/src/listener.ts +0 -24
- package/src/middleware/common/dispatch.ts +0 -17
- package/src/middleware/common/index.ts +0 -5
- package/src/middleware/common/match.ts +0 -24
- package/src/middleware/factory.ts +0 -21
- package/src/redirect.ts +0 -16
- package/src/routes.ts +0 -68
- package/src/url.ts +0 -30
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { PLACEHOLDER, STATIC, WILDCARD } from "../symbols";
|
|
2
|
+
class Node {
|
|
3
|
+
children = null;
|
|
4
|
+
parent = null;
|
|
5
|
+
path = null;
|
|
6
|
+
property = null;
|
|
7
|
+
route = null;
|
|
8
|
+
type = null;
|
|
9
|
+
constructor(parent = null) {
|
|
10
|
+
this.parent = parent;
|
|
11
|
+
}
|
|
12
|
+
add(path, route) {
|
|
13
|
+
let node = this, segments = path.split('/'), type = STATIC, unnamed = 0;
|
|
14
|
+
for (let i = 0, n = segments.length; i < n; i++) {
|
|
15
|
+
let child = node.children?.get(segments[i]);
|
|
16
|
+
if (!child) {
|
|
17
|
+
let segment = segments[i], symbol = segment[0];
|
|
18
|
+
if (!node.children) {
|
|
19
|
+
node.children = new Map();
|
|
20
|
+
}
|
|
21
|
+
node.children.set(segment, (child = new Node(node)));
|
|
22
|
+
if (symbol === ':') {
|
|
23
|
+
child.property = segment.slice(1) || `${unnamed++}`;
|
|
24
|
+
node.children.set(PLACEHOLDER, child);
|
|
25
|
+
type = null;
|
|
26
|
+
}
|
|
27
|
+
else if (symbol === '*') {
|
|
28
|
+
child.property = segment.slice(2) || `${unnamed++}`;
|
|
29
|
+
node.children.set(WILDCARD, child);
|
|
30
|
+
type = null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
node = child;
|
|
34
|
+
}
|
|
35
|
+
node.path = path;
|
|
36
|
+
node.route = route;
|
|
37
|
+
node.type = type;
|
|
38
|
+
return node;
|
|
39
|
+
}
|
|
40
|
+
find(path) {
|
|
41
|
+
let node = this, parameters = {}, segments = path.split('/'), wildcard = null;
|
|
42
|
+
for (let i = 0, n = segments.length; i < n; i++) {
|
|
43
|
+
let segment = segments[i], wc = node.children?.get(WILDCARD);
|
|
44
|
+
if (wc) {
|
|
45
|
+
wildcard = {
|
|
46
|
+
node: wc,
|
|
47
|
+
value: segments.slice(i).join('/')
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
let next = node.children?.get(segment);
|
|
51
|
+
if (next) {
|
|
52
|
+
node = next;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
node = node.children?.get(PLACEHOLDER);
|
|
56
|
+
if (!node) {
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
parameters[node.property] = segment;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if ((!node || !node.route) && wildcard) {
|
|
63
|
+
node = wildcard.node;
|
|
64
|
+
parameters[node.property] = wildcard.value;
|
|
65
|
+
}
|
|
66
|
+
if (!node) {
|
|
67
|
+
return {};
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
parameters,
|
|
71
|
+
route: node.route
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
remove(path) {
|
|
75
|
+
let node = this, segments = path.split('/');
|
|
76
|
+
for (let i = 0, n = segments.length; i < n; i++) {
|
|
77
|
+
node = node.children?.get(segments[i]);
|
|
78
|
+
if (!node) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (!node?.children?.size) {
|
|
83
|
+
let parent = node.parent;
|
|
84
|
+
if (parent && parent.children) {
|
|
85
|
+
parent.children.delete(segments[segments.length - 1]);
|
|
86
|
+
parent.children.delete(WILDCARD);
|
|
87
|
+
parent.children.delete(PLACEHOLDER);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return node;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export { Node };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const normalize = (path) => {
|
|
2
|
+
if (path[0] !== '/') {
|
|
3
|
+
path = '/' + path;
|
|
4
|
+
}
|
|
5
|
+
if (path.endsWith('/')) {
|
|
6
|
+
path = path.slice(0, -1);
|
|
7
|
+
}
|
|
8
|
+
return path || '/';
|
|
9
|
+
};
|
|
10
|
+
const radixkey = (path, { method, subdomain } = {}) => {
|
|
11
|
+
let prefix = '';
|
|
12
|
+
if (subdomain) {
|
|
13
|
+
prefix = subdomain + ' ';
|
|
14
|
+
}
|
|
15
|
+
if (method) {
|
|
16
|
+
prefix += method + ' ';
|
|
17
|
+
}
|
|
18
|
+
return prefix.toUpperCase() + normalize(path);
|
|
19
|
+
};
|
|
20
|
+
export { normalize, radixkey };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Middleware, Responder } from '../types';
|
|
2
|
+
import { factory } from '../middleware';
|
|
3
|
+
declare class Route {
|
|
4
|
+
dispatch: ReturnType<typeof factory> | null;
|
|
5
|
+
name: string | null;
|
|
6
|
+
path: string | null;
|
|
7
|
+
responder: Responder;
|
|
8
|
+
stack: Middleware<unknown, unknown>[] | null;
|
|
9
|
+
subdomain: string | null;
|
|
10
|
+
constructor(responder: Responder);
|
|
11
|
+
get dispatcher(): import("@esportsplus/middleware/build/types").Next<unknown, unknown>;
|
|
12
|
+
}
|
|
13
|
+
export { Route };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { factory } from '../middleware';
|
|
2
|
+
class Route {
|
|
3
|
+
dispatch = null;
|
|
4
|
+
name = null;
|
|
5
|
+
path = null;
|
|
6
|
+
responder;
|
|
7
|
+
stack = null;
|
|
8
|
+
subdomain = null;
|
|
9
|
+
constructor(responder) {
|
|
10
|
+
this.responder = responder;
|
|
11
|
+
}
|
|
12
|
+
get dispatcher() {
|
|
13
|
+
if (this.dispatch === null) {
|
|
14
|
+
if (!this.stack?.length) {
|
|
15
|
+
this.dispatch = (request) => this.responder(request);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
this.dispatch = factory(...this.stack, (request => this.responder(request)));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return this.dispatch;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export { Route };
|
package/build/slugify.js
ADDED
package/build/symbols.js
ADDED
package/build/types.d.ts
CHANGED
|
@@ -1,26 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
type
|
|
4
|
-
middleware
|
|
5
|
-
name
|
|
6
|
-
path
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
type
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
19
|
-
type Route = {
|
|
20
|
-
middleware: Middleware[];
|
|
21
|
-
name: string;
|
|
22
|
-
path?: string;
|
|
23
|
-
responder: Responder;
|
|
24
|
-
subdomain?: string;
|
|
25
|
-
};
|
|
26
|
-
export { Group, Middleware, Next, Request, Responder, Route, Routes };
|
|
1
|
+
import { Middleware, Next } from '@esportsplus/middleware';
|
|
2
|
+
import { Route, Router } from './router';
|
|
3
|
+
type Options = {
|
|
4
|
+
middleware?: Middleware<Request, unknown>[];
|
|
5
|
+
name?: string;
|
|
6
|
+
path?: string;
|
|
7
|
+
responder: Responder;
|
|
8
|
+
subdomain?: string;
|
|
9
|
+
};
|
|
10
|
+
type Request = {
|
|
11
|
+
data: ReturnType<Router['match']>;
|
|
12
|
+
hostname: string;
|
|
13
|
+
method: string;
|
|
14
|
+
path: string;
|
|
15
|
+
subdomain?: string;
|
|
16
|
+
};
|
|
17
|
+
type Responder = <T>(request: T) => Promise<unknown> | unknown;
|
|
18
|
+
export { Middleware, Next, Options, Request, Responder, Route, Router };
|
package/build/types.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { Route, Router } from './router';
|
|
2
|
+
export { Route, Router };
|
package/package.json
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "ICJR",
|
|
3
|
-
"description": "Routing",
|
|
4
3
|
"devDependencies": {
|
|
5
|
-
"
|
|
6
|
-
"typescript": "^4.9.4"
|
|
4
|
+
"@esportsplus/rspack": "^0.0.15"
|
|
7
5
|
},
|
|
8
6
|
"main": "./build/index.js",
|
|
9
7
|
"name": "@esportsplus/routing",
|
|
@@ -15,5 +13,8 @@
|
|
|
15
13
|
"prepublishOnly": "npm run build"
|
|
16
14
|
},
|
|
17
15
|
"types": "./build/index.d.ts",
|
|
18
|
-
"version": "0.0.
|
|
16
|
+
"version": "0.0.11",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@esportsplus/middleware": "^0.0.4"
|
|
19
|
+
}
|
|
19
20
|
}
|
package/readme.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
https://github.com/unjs/radix3
|
package/src/hash.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Router } from './types';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
let cache: {
|
|
5
|
+
factory: typeof request;
|
|
6
|
+
state: Record<PropertyKey, unknown>;
|
|
7
|
+
}[] = [],
|
|
8
|
+
registered = false;
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
function update() {
|
|
12
|
+
for (let i = 0, n = cache.length; i < n; i++) {
|
|
13
|
+
let { factory, state } = cache[i],
|
|
14
|
+
values = factory();
|
|
15
|
+
|
|
16
|
+
for (let key in values) {
|
|
17
|
+
state[key] = values[key as keyof typeof values];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
const back = () => window.history.back();
|
|
24
|
+
|
|
25
|
+
const forward = () => window.history.forward();
|
|
26
|
+
|
|
27
|
+
const listener = {
|
|
28
|
+
register: (factory: typeof cache[0]['factory'], state: typeof cache[0]['state']) => {
|
|
29
|
+
cache.push({ factory, state });
|
|
30
|
+
|
|
31
|
+
if (!registered) {
|
|
32
|
+
registered = true;
|
|
33
|
+
update();
|
|
34
|
+
|
|
35
|
+
window.addEventListener('popstate', update);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return () => {
|
|
39
|
+
listener.remove(state);
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
remove: (state: typeof cache[0]['state']) => {
|
|
43
|
+
for (let i = 0, n = cache.length; i < n; i++) {
|
|
44
|
+
if (cache[i].state !== state) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
cache[i] = cache[n - 1];
|
|
49
|
+
cache.pop();
|
|
50
|
+
|
|
51
|
+
if (cache.length === 0) {
|
|
52
|
+
window.removeEventListener('popstate', update);
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const factory = {
|
|
60
|
+
redirect: (router: Router) => {
|
|
61
|
+
return (path: string, { state, values }: { state?: Record<PropertyKey, unknown>; values?: unknown[] }) => {
|
|
62
|
+
if (path.startsWith('http://') || path.startsWith('https://')) {
|
|
63
|
+
return window.location.replace(path);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let uri = router.uri(path, values || []);
|
|
67
|
+
|
|
68
|
+
if (uri[0] === '/') {
|
|
69
|
+
uri = '#' + uri;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
window.history.pushState(state || {}, '', uri);
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
uri: (router: Router) => {
|
|
76
|
+
return (path: string, values: unknown[] = []) => {
|
|
77
|
+
let uri = router.uri(path, values || []);
|
|
78
|
+
|
|
79
|
+
if (uri[0] === '/') {
|
|
80
|
+
uri = '#' + uri;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return uri;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const request = (url: string = window?.location?.href || '') => {
|
|
89
|
+
let { hash, hostname, href, origin, port, protocol } = new URL( url ),
|
|
90
|
+
path = hash?.replace('#/', '/')?.split('?') || ['/', ''];
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
href,
|
|
94
|
+
hostname,
|
|
95
|
+
method: 'GET',
|
|
96
|
+
origin,
|
|
97
|
+
path: path[0],
|
|
98
|
+
port,
|
|
99
|
+
protocol,
|
|
100
|
+
query: Object.fromEntries( (new URLSearchParams(path[1])).entries() )
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
export default { back, factory, forward, listener, request };
|
|
106
|
+
export { back, factory, forward, listener, request };
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
+
import hash from './hash';
|
|
1
2
|
import middleware from './middleware';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import routes from './routes';
|
|
5
|
-
import url from './url';
|
|
3
|
+
import router from './router';
|
|
4
|
+
import slugify from './slugify';
|
|
6
5
|
|
|
7
6
|
|
|
8
|
-
export
|
|
9
|
-
export { listener, middleware, redirect, routes, url };
|
|
7
|
+
export { hash, middleware, router, slugify };
|
|
10
8
|
export * from './types';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Router } from '~/types';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export default (request: { data: ReturnType<Router['match']> }) => {
|
|
5
|
+
let { route } = request.data;
|
|
6
|
+
|
|
7
|
+
if (!route) {
|
|
8
|
+
throw new Error(`Routing: route dispatching failed, route is undefined!`);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return route.dispatcher(request);
|
|
12
|
+
};
|
package/src/middleware/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import factory from '@esportsplus/middleware';
|
|
2
|
+
import dispatch from './dispatch';
|
|
3
|
+
import match from './match';
|
|
3
4
|
|
|
4
5
|
|
|
5
|
-
export default {
|
|
6
|
-
export {
|
|
6
|
+
export default { dispatch, factory, match };
|
|
7
|
+
export { dispatch, factory, match };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Next, Request, Router } from '~/types';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export default (router: Router, { spa }: { spa?: boolean } = {}) => {
|
|
5
|
+
let subdomain: string | null = null;
|
|
6
|
+
|
|
7
|
+
return (request: Request, next: Next<Request, unknown>) => {
|
|
8
|
+
if ((typeof request.subdomain !== 'string' && !spa) || subdomain === null) {
|
|
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
|
+
subdomain = router.subdomains[i];
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (subdomain === null) {
|
|
21
|
+
subdomain = '';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let { parameters, route } = router.match(request.method, request.path, request.subdomain || subdomain);
|
|
26
|
+
|
|
27
|
+
request.data.parameters = parameters;
|
|
28
|
+
request.data.route = route;
|
|
29
|
+
|
|
30
|
+
return next(request);
|
|
31
|
+
};
|
|
32
|
+
};
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { STATIC } from "~/symbols";
|
|
2
|
+
import { Options } from '~/types';
|
|
3
|
+
import { Node } from './node';
|
|
4
|
+
import { normalize, radixkey } from './path';
|
|
5
|
+
import { Route } from './route';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
let { isArray } = Array;
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
function set(route: Route, key: keyof Route, value?: any) {
|
|
12
|
+
if (!value) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!route[key]) {
|
|
17
|
+
(route[key] as any) = value;
|
|
18
|
+
}
|
|
19
|
+
else if (typeof value === 'string') {
|
|
20
|
+
if (typeof route[key] === 'string') {
|
|
21
|
+
(route[key] as string) += value;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else if (isArray(value)) {
|
|
25
|
+
if (isArray(route[key])) {
|
|
26
|
+
(route[key] as any[]).push( ...value );
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Router {
|
|
33
|
+
groups: Omit<Options, 'responder'>[] = [];
|
|
34
|
+
root: Node;
|
|
35
|
+
routes: Record<string, Route> = {};
|
|
36
|
+
static: Record<string, Route> = {};
|
|
37
|
+
subdomains: string[] | null = null;
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
constructor() {
|
|
41
|
+
this.root = new Node();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
private add(radixkey: string, route: Route) {
|
|
46
|
+
let node = this.root.add(radixkey, route);
|
|
47
|
+
|
|
48
|
+
if (node.type === STATIC) {
|
|
49
|
+
if (this.static[radixkey]) {
|
|
50
|
+
throw new Error(`Routing: static path '${radixkey}' is already in use`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
this.static[radixkey] = route;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private route({ middleware, name, path, responder, subdomain }: Options) {
|
|
58
|
+
let route = new Route(responder);
|
|
59
|
+
|
|
60
|
+
for (let i = 0, n = this.groups.length; i < n; i++) {
|
|
61
|
+
let { middleware, name, path, subdomain } = this.groups[i];
|
|
62
|
+
|
|
63
|
+
set(route, 'name', name);
|
|
64
|
+
set(route, 'path', path);
|
|
65
|
+
set(route, 'stack', middleware);
|
|
66
|
+
set(route, 'subdomain', subdomain);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
set(route, 'name', name);
|
|
70
|
+
set(route, 'path', path);
|
|
71
|
+
set(route, 'stack', middleware);
|
|
72
|
+
set(route, 'subdomain', subdomain);
|
|
73
|
+
|
|
74
|
+
if (route.path) {
|
|
75
|
+
route.path = normalize(route.path);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (route.subdomain === 'www') {
|
|
79
|
+
route.subdomain = '';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return route;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
delete(options: Options) {
|
|
87
|
+
this.on(['DELETE'], options);
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get(options: Options) {
|
|
92
|
+
this.on(['GET'], options);
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
group(options: Router['groups'][0]) {
|
|
97
|
+
return {
|
|
98
|
+
routes: (fn: (router: Router) => void) => {
|
|
99
|
+
this.groups.push(options);
|
|
100
|
+
fn(this);
|
|
101
|
+
this.groups.pop();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
match(method: string, path: string, subdomain?: string | null): ReturnType<Node['find']> {
|
|
107
|
+
let key = radixkey(path, { method, subdomain });
|
|
108
|
+
|
|
109
|
+
if (this.static[key]) {
|
|
110
|
+
return {
|
|
111
|
+
route: this.static[key]
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return this.root.find(key);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
on(methods: string[], options: Options) {
|
|
119
|
+
let route = this.route(options);
|
|
120
|
+
|
|
121
|
+
if (route.name) {
|
|
122
|
+
if (this.routes[route.name]) {
|
|
123
|
+
throw new Error(`Routing: '${route.name}' is already in use`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
this.routes[route.name] = route;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (route.path) {
|
|
130
|
+
for (let i = 0, n = methods.length; i < n; i++) {
|
|
131
|
+
let key = radixkey(route.path, {
|
|
132
|
+
method: methods[i],
|
|
133
|
+
subdomain: route.subdomain
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
if (key.indexOf('?:') !== -1) {
|
|
137
|
+
let segments = key.split('?:'),
|
|
138
|
+
url = '';
|
|
139
|
+
|
|
140
|
+
for (let i = 0, n = segments.length; i < n; i++) {
|
|
141
|
+
this.add((url += (i > 0 ? '/:' : '/') + segments[i]), route);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
this.add(key, route);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (route.subdomain) {
|
|
151
|
+
if (!this.subdomains) {
|
|
152
|
+
this.subdomains = [route.subdomain];
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
this.subdomains.push(route.subdomain);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
post(options: Options) {
|
|
161
|
+
this.on(['POST'], options);
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
put(options: Options) {
|
|
166
|
+
this.on(['PUT'], options);
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
uri(name: string, values: unknown[] = []) {
|
|
171
|
+
let path = this.routes?.[name]?.path;
|
|
172
|
+
|
|
173
|
+
if (!path) {
|
|
174
|
+
throw new Error(`Routing: route name '${name}' does not exist or it does not provide a path`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
let resolved = [] as typeof values,
|
|
178
|
+
segments = path.split('/');
|
|
179
|
+
|
|
180
|
+
for (let i = 0, n = segments.length; i < n; i++) {
|
|
181
|
+
let segment = segments[i],
|
|
182
|
+
symbol = segment[0];
|
|
183
|
+
|
|
184
|
+
if (symbol === ':') {
|
|
185
|
+
resolved.push(values[i]);
|
|
186
|
+
}
|
|
187
|
+
else if (symbol === '?') {
|
|
188
|
+
if (values[i] === undefined) {
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
resolved.push(values[i]);
|
|
193
|
+
}
|
|
194
|
+
else if (symbol === '*') {
|
|
195
|
+
resolved.push( ...values.slice(i) );
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
resolved.push(segment);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return resolved.join('/');
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
export default () => new Router();
|
|
209
|
+
export { Router, Route };
|