@esportsplus/routing 0.0.41 → 0.0.43

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.

Potentially problematic release.


This version of @esportsplus/routing might be problematic. Click here for more details.

@@ -1,141 +1,141 @@
1
- import { PLACEHOLDER, STATIC, WILDCARD } from '~/constants';
2
- import { Route } from './index';
3
-
4
-
5
- class Node<T> {
6
- children: Map<string | number, Node<T>> | null = null;
7
- parent: Node<T> | null = null;
8
- path: string | null = null;
9
- property: string | null = null;
10
- route: Route<T> | null = null;
11
- type: number | null = null;
12
-
13
-
14
- constructor(parent: Node<T>['parent'] = null) {
15
- this.parent = parent;
16
- }
17
-
18
-
19
- add(path: string, route: Route<T>) {
20
- let node: Node<T> | undefined = this,
21
- segments = path.split('/'),
22
- type: Node<T>['type'] = STATIC,
23
- unnamed = 0;
24
-
25
- for (let i = 0, n = segments.length; i < n; i++) {
26
- let child: Node<T> | undefined = node.children?.get(segments[i]);
27
-
28
- if (!child) {
29
- let segment = segments[i],
30
- symbol = segment[0];
31
-
32
- if (!node.children) {
33
- node.children = new Map();
34
- }
35
-
36
- node.children.set(segment, (child = new Node<T>(node)));
37
-
38
- // Named property
39
- if (symbol === ':') {
40
- child.property = (segment.slice(1) || unnamed++).toString();
41
- node.children.set(PLACEHOLDER, child);
42
- type = null;
43
- }
44
- // "*:" Wildcard property
45
- else if (symbol === '*') {
46
- child.property = (segment.slice(2) || unnamed++).toString();
47
- node.children.set(WILDCARD, child);
48
- type = null;
49
- }
50
- }
51
-
52
- node = child;
53
- }
54
-
55
- node.path = path;
56
- node.route = route;
57
- node.type = type;
58
-
59
- return node;
60
- }
61
-
62
- find(path: string): {
63
- parameters?: Record<PropertyKey, unknown>;
64
- route?: Route<T>;
65
- } {
66
- let node: Node<T> | undefined = this,
67
- parameters: Record<PropertyKey, unknown> = {},
68
- segments = path.split('/'),
69
- wildcard: { node: Node<T>, value: string } | null = null;
70
-
71
- for (let i = 0, n = segments.length; i < n; i++) {
72
- let segment = segments[i],
73
- wc = node.children?.get(WILDCARD);
74
-
75
- if (wc) {
76
- wildcard = {
77
- node: wc,
78
- value: segments.slice(i).join('/')
79
- };
80
- }
81
-
82
- // Exact matches take precedence over placeholders
83
- let next: Node<T> | undefined = node.children?.get(segment);
84
-
85
- if (next) {
86
- node = next;
87
- }
88
- else {
89
- node = node.children?.get(PLACEHOLDER);
90
-
91
- if (!node) {
92
- break;
93
- }
94
-
95
- parameters[ node.property! ] = segment;
96
- }
97
- }
98
-
99
- if ((!node || !node.route) && wildcard) {
100
- node = wildcard.node;
101
- parameters[ node.property! ] = wildcard.value;
102
- }
103
-
104
- if (!node) {
105
- return {};
106
- }
107
-
108
- return {
109
- parameters,
110
- route: node.route!
111
- };
112
- }
113
-
114
- remove(path: string) {
115
- let node: Node<T> | undefined = this,
116
- segments = path.split('/');
117
-
118
- for (let i = 0, n = segments.length; i < n; i++) {
119
- node = node.children?.get( segments[i] );
120
-
121
- if (!node) {
122
- return;
123
- }
124
- }
125
-
126
- if (node.children?.size) {
127
- return;
128
- }
129
-
130
- let parent = node.parent;
131
-
132
- if (parent && parent.children) {
133
- parent.children.delete( segments[segments.length - 1] );
134
- parent.children.delete(WILDCARD);
135
- parent.children.delete(PLACEHOLDER);
136
- }
137
- }
138
- }
139
-
140
-
1
+ import { PLACEHOLDER, STATIC, WILDCARD } from '~/constants';
2
+ import { Route } from './index';
3
+
4
+
5
+ class Node<T> {
6
+ children: Map<string | number, Node<T>> | null = null;
7
+ parent: Node<T> | null = null;
8
+ path: string | null = null;
9
+ property: string | null = null;
10
+ route: Route<T> | null = null;
11
+ type: number | null = null;
12
+
13
+
14
+ constructor(parent: Node<T>['parent'] = null) {
15
+ this.parent = parent;
16
+ }
17
+
18
+
19
+ add(path: string, route: Route<T>) {
20
+ let node: Node<T> | undefined = this,
21
+ segments = path.split('/'),
22
+ type: Node<T>['type'] = STATIC,
23
+ unnamed = 0;
24
+
25
+ for (let i = 0, n = segments.length; i < n; i++) {
26
+ let child: Node<T> | undefined = node.children?.get(segments[i]);
27
+
28
+ if (!child) {
29
+ let segment = segments[i],
30
+ symbol = segment[0];
31
+
32
+ if (!node.children) {
33
+ node.children = new Map();
34
+ }
35
+
36
+ node.children.set(segment, (child = new Node<T>(node)));
37
+
38
+ // Named property
39
+ if (symbol === ':') {
40
+ child.property = (segment.slice(1) || unnamed++).toString();
41
+ node.children.set(PLACEHOLDER, child);
42
+ type = null;
43
+ }
44
+ // "*:" Wildcard property
45
+ else if (symbol === '*') {
46
+ child.property = (segment.slice(2) || unnamed++).toString();
47
+ node.children.set(WILDCARD, child);
48
+ type = null;
49
+ }
50
+ }
51
+
52
+ node = child;
53
+ }
54
+
55
+ node.path = path;
56
+ node.route = route;
57
+ node.type = type;
58
+
59
+ return node;
60
+ }
61
+
62
+ find(path: string): {
63
+ parameters?: Record<PropertyKey, unknown>;
64
+ route?: Route<T>;
65
+ } {
66
+ let node: Node<T> | undefined = this,
67
+ parameters: Record<PropertyKey, unknown> = {},
68
+ segments = path.split('/'),
69
+ wildcard: { node: Node<T>, value: string } | null = null;
70
+
71
+ for (let i = 0, n = segments.length; i < n; i++) {
72
+ let segment = segments[i],
73
+ wc = node.children?.get(WILDCARD);
74
+
75
+ if (wc) {
76
+ wildcard = {
77
+ node: wc,
78
+ value: segments.slice(i).join('/')
79
+ };
80
+ }
81
+
82
+ // Exact matches take precedence over placeholders
83
+ let next: Node<T> | undefined = node.children?.get(segment);
84
+
85
+ if (next) {
86
+ node = next;
87
+ }
88
+ else {
89
+ node = node.children?.get(PLACEHOLDER);
90
+
91
+ if (!node) {
92
+ break;
93
+ }
94
+
95
+ parameters[ node.property! ] = segment;
96
+ }
97
+ }
98
+
99
+ if ((!node || !node.route) && wildcard) {
100
+ node = wildcard.node;
101
+ parameters[ node.property! ] = wildcard.value;
102
+ }
103
+
104
+ if (!node) {
105
+ return {};
106
+ }
107
+
108
+ return {
109
+ parameters,
110
+ route: node.route!
111
+ };
112
+ }
113
+
114
+ remove(path: string) {
115
+ let node: Node<T> | undefined = this,
116
+ segments = path.split('/');
117
+
118
+ for (let i = 0, n = segments.length; i < n; i++) {
119
+ node = node.children?.get( segments[i] );
120
+
121
+ if (!node) {
122
+ return;
123
+ }
124
+ }
125
+
126
+ if (node.children?.size) {
127
+ return;
128
+ }
129
+
130
+ let parent = node.parent;
131
+
132
+ if (parent && parent.children) {
133
+ parent.children.delete( segments[segments.length - 1] );
134
+ parent.children.delete(WILDCARD);
135
+ parent.children.delete(PLACEHOLDER);
136
+ }
137
+ }
138
+ }
139
+
140
+
141
141
  export { Node };
@@ -1,28 +1,28 @@
1
- import { Middleware, Next, Request } from '~/types';
2
- import pipeline from '@esportsplus/pipeline';
3
-
4
-
5
- class Route<T> {
6
- middleware: Middleware<T>[] | null = null;
7
- name: string | null = null;
8
- path: string | null = null;
9
- responder: Next<T>;
10
- subdomain: string | null = null;
11
-
12
-
13
- constructor(responder: Next<T>) {
14
- this.responder = responder;
15
- }
16
-
17
-
18
- dispatch(request: Request<T>) {
19
- if (this.middleware === null) {
20
- return this.responder(request);
21
- }
22
-
23
- return pipeline(...this.middleware, (request) => this.responder(request))(request);
24
- }
25
- }
26
-
27
-
1
+ import { Middleware, Next, Request } from '~/types';
2
+ import pipeline from '@esportsplus/pipeline';
3
+
4
+
5
+ class Route<T> {
6
+ middleware: Middleware<T>[] | null = null;
7
+ name: string | null = null;
8
+ path: string | null = null;
9
+ responder: Next<T>;
10
+ subdomain: string | null = null;
11
+
12
+
13
+ constructor(responder: Next<T>) {
14
+ this.responder = responder;
15
+ }
16
+
17
+
18
+ dispatch(request: Request<T>) {
19
+ if (this.middleware === null) {
20
+ return this.responder(request);
21
+ }
22
+
23
+ return pipeline(...this.middleware, (request) => this.responder(request))(request);
24
+ }
25
+ }
26
+
27
+
28
28
  export { Route };
package/src/slugify.ts CHANGED
@@ -1,4 +1,4 @@
1
- // https://twitter.com/Swizec/status/1589416111971635201
2
- export default (value: string) => {
3
- return value.replace(/\W+/g, '-').replace(/[-]+$/, '').toLowerCase();
1
+ // https://twitter.com/Swizec/status/1589416111971635201
2
+ export default (value: string) => {
3
+ return value.replace(/\W+/g, '-').replace(/[-]+$/, '').toLowerCase();
4
4
  };
package/src/types.ts CHANGED
@@ -1,31 +1,31 @@
1
- import { NeverAsync } from '@esportsplus/typescript';
2
- import { Route, Router } from './router';
3
-
4
-
5
- type Middleware<T> = NeverAsync<(input: Request<T>, next: Next<T>) => T>;
6
-
7
- type Next<T> = NeverAsync<(input: Request<T>) => T>;
8
-
9
- type Options<T> = {
10
- middleware?: Middleware<T>[];
11
- name?: string;
12
- path?: string;
13
- responder: Next<T>;
14
- subdomain?: string;
15
- };
16
-
17
- type Request<T> = {
18
- data: Record<PropertyKey, unknown> & ReturnType<Router<T>['match']>;
19
- href: string;
20
- hostname: string;
21
- method: string;
22
- origin: string;
23
- path: string;
24
- port: string;
25
- protocol: string;
26
- query: Record<string, unknown>;
27
- subdomain?: string;
28
- };
29
-
30
-
1
+ import { NeverAsync } from '@esportsplus/typescript';
2
+ import { Route, Router } from './router';
3
+
4
+
5
+ type Middleware<T> = NeverAsync<(input: Request<T>, next: Next<T>) => T>;
6
+
7
+ type Next<T> = NeverAsync<(input: Request<T>) => T>;
8
+
9
+ type Options<T> = {
10
+ middleware?: Middleware<T>[];
11
+ name?: string;
12
+ path?: string;
13
+ responder: Next<T>;
14
+ subdomain?: string;
15
+ };
16
+
17
+ type Request<T> = {
18
+ data: Record<PropertyKey, unknown> & ReturnType<Router<T>['match']>;
19
+ href: string;
20
+ hostname: string;
21
+ method: string;
22
+ origin: string;
23
+ path: string;
24
+ port: string;
25
+ protocol: string;
26
+ query: Record<string, unknown>;
27
+ subdomain?: string;
28
+ };
29
+
30
+
31
31
  export { Middleware, Next, Options, Request, Route, Router };
package/tsconfig.json CHANGED
@@ -1,10 +1,11 @@
1
- {
2
- "compilerOptions": {
3
- "baseUrl": ".",
4
- "declarationDir": "build",
5
- "outDir": "build",
6
- },
7
- "exclude": ["node_modules"],
8
- "extends": "@esportsplus/typescript/tsconfig.base.json",
9
- "include": ["src"]
1
+ {
2
+ "compilerOptions": {
3
+ "baseUrl": ".",
4
+ "declaration": true,
5
+ "declarationDir": "build",
6
+ "outDir": "build"
7
+ },
8
+ "exclude": ["node_modules"],
9
+ "extends": "./node_modules/@esportsplus/typescript/tsconfig.base.json",
10
+ "include": ["src"]
10
11
  }