@esportsplus/routing 0.0.23 → 0.0.24

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.
@@ -1,15 +1,15 @@
1
1
  import { Middleware, Response, Router } from './types';
2
2
  declare function back(): void;
3
3
  declare function forward(): void;
4
- declare const _default: <R>(instance?: Router<R> | undefined) => {
4
+ declare const _default: <T>(instance?: Router<T> | undefined) => {
5
5
  back: typeof back;
6
6
  forward: typeof forward;
7
- middleware: (...middleware: Middleware<R>[]) => () => Response<R>;
7
+ middleware: (...middleware: Middleware<T>[]) => () => Response<T>;
8
8
  redirect: (path: string, { state, values }: {
9
9
  state?: Record<PropertyKey, unknown> | undefined;
10
10
  values?: unknown[] | undefined;
11
11
  }) => void;
12
- router: Router<R>;
12
+ router: Router<T>;
13
13
  uri: (path: string, values?: unknown[]) => string;
14
14
  };
15
15
  export default _default;
@@ -1,26 +1,26 @@
1
1
  import { Options } from '../types';
2
2
  import { Node } from './node';
3
3
  import { Route } from './route';
4
- declare class Router<R> {
5
- groups: Omit<Options<R>, 'responder'>[];
6
- root: Node<R>;
7
- routes: Record<string, Route<R>>;
8
- static: Record<string, Route<R>>;
4
+ declare class Router<T> {
5
+ groups: Omit<Options<T>, 'responder'>[];
6
+ root: Node<T>;
7
+ routes: Record<string, Route<T>>;
8
+ static: Record<string, Route<T>>;
9
9
  subdomains: string[] | null;
10
10
  constructor();
11
11
  private add;
12
12
  private route;
13
- delete(options: Options<R>): this;
14
- get(options: Options<R>): this;
15
- group(options: Router<R>['groups'][0]): {
16
- routes: (fn: (router: Router<R>) => void) => void;
13
+ delete(options: Options<T>): this;
14
+ get(options: Options<T>): this;
15
+ group(options: Router<T>['groups'][0]): {
16
+ routes: (fn: (router: Router<T>) => void) => void;
17
17
  };
18
- match(method: string, path: string, subdomain?: string | null): ReturnType<Node<R>['find']>;
19
- on(methods: string[], options: Options<R>): this;
20
- post(options: Options<R>): this;
21
- put(options: Options<R>): this;
18
+ match(method: string, path: string, subdomain?: string | null): ReturnType<Node<T>['find']>;
19
+ on(methods: string[], options: Options<T>): this;
20
+ post(options: Options<T>): this;
21
+ put(options: Options<T>): this;
22
22
  uri(name: string, values?: unknown[]): string;
23
23
  }
24
- declare const _default: <R>() => Router<R>;
24
+ declare const _default: <T>() => Router<T>;
25
25
  export default _default;
26
26
  export { Router, Route };
@@ -1,16 +1,16 @@
1
1
  import { Route } from './index';
2
- declare class Node<R> {
3
- children: Map<string | number, Node<R>> | null;
4
- parent: Node<R> | null;
2
+ declare class Node<T> {
3
+ children: Map<string | number, Node<T>> | null;
4
+ parent: Node<T> | null;
5
5
  path: string | null;
6
6
  property: string | null;
7
- route: Route<R> | null;
7
+ route: Route<T> | null;
8
8
  type: number | null;
9
- constructor(parent?: Node<R>['parent']);
10
- add(path: string, route: Route<R>): Node<R>;
9
+ constructor(parent?: Node<T>['parent']);
10
+ add(path: string, route: Route<T>): Node<T>;
11
11
  find(path: string): {
12
12
  parameters?: Record<PropertyKey, unknown>;
13
- route?: Route<R>;
13
+ route?: Route<T>;
14
14
  };
15
15
  remove(path: string): void;
16
16
  }
@@ -1,12 +1,12 @@
1
1
  import { Middleware, Next, Responder } from '../types';
2
- declare class Route<R> {
3
- dispatch: Next<R> | null;
2
+ declare class Route<T> {
3
+ dispatch: Next<T> | null;
4
4
  name: string | null;
5
5
  path: string | null;
6
- responder: Responder<R>;
7
- stack: Middleware<R>[] | null;
6
+ responder: Responder<T>;
7
+ stack: Middleware<T>[] | null;
8
8
  subdomain: string | null;
9
- constructor(responder: Responder<R>);
10
- get dispatcher(): Next<R>;
9
+ constructor(responder: Responder<T>);
10
+ get dispatcher(): Next<T>;
11
11
  }
12
12
  export { Route };
package/build/types.d.ts CHANGED
@@ -1,16 +1,16 @@
1
1
  import { Next as N, Stage } from '@esportsplus/pipeline';
2
2
  import { Route, Router } from './router';
3
- type Middleware<R> = Stage<Request, Response<R>>;
4
- type Next<R> = N<Request, Response<R>>;
5
- type Options<R> = {
6
- middleware?: Middleware<R>[];
3
+ type Middleware<T> = Stage<Request<T>, Response<T>>;
4
+ type Next<T> = N<Request<T>, Response<T>>;
5
+ type Options<T> = {
6
+ middleware?: Middleware<T>[];
7
7
  name?: string;
8
8
  path?: string;
9
- responder: Responder<R>;
9
+ responder: Responder<T>;
10
10
  subdomain?: string;
11
11
  };
12
- type Request = {
13
- data: Record<PropertyKey, unknown>;
12
+ type Request<T> = {
13
+ data: Record<PropertyKey, unknown> & ReturnType<Router<T>['match']>;
14
14
  href: string;
15
15
  hostname: string;
16
16
  method: string;
@@ -21,6 +21,6 @@ type Request = {
21
21
  query: Record<string, unknown>;
22
22
  subdomain?: string;
23
23
  };
24
- type Responder<R> = (request: Request) => Response<R>;
25
- type Response<R> = Promise<R> | R;
24
+ type Responder<T> = (request: Request<T>) => Response<T>;
25
+ type Response<T> = Promise<T> | T;
26
26
  export { Middleware, Next, Options, Request, Responder, Response, Route, Router };
package/package.json CHANGED
@@ -17,5 +17,5 @@
17
17
  "prepublishOnly": "npm run build"
18
18
  },
19
19
  "types": "./build/index.d.ts",
20
- "version": "0.0.23"
20
+ "version": "0.0.24"
21
21
  }
package/src/browser.ts CHANGED
@@ -4,7 +4,7 @@ import pipeline from '@esportsplus/pipeline';
4
4
  import factory from './router';
5
5
 
6
6
 
7
- let cache: Request[] = [],
7
+ let cache: Request<any>[] = [],
8
8
  registered = false;
9
9
 
10
10
 
@@ -37,7 +37,7 @@ function onpopstate() {
37
37
  }
38
38
  }
39
39
 
40
- function request(): Request {
40
+ function request<T>(): Request<T> {
41
41
  let { hash, hostname, href, origin, port, protocol } = new URL( window.location?.href || '' ),
42
42
  path = hash ? hash.slice(1).split('?') : ['/', ''];
43
43
 
@@ -55,9 +55,9 @@ function request(): Request {
55
55
  }
56
56
 
57
57
 
58
- export default <R>(instance?: Router<R>) => {
59
- let router = instance || factory<R>(),
60
- state = reactive( request() );
58
+ export default <T>(instance?: Router<T>) => {
59
+ let router = instance || factory<T>(),
60
+ state = reactive( request<T>() );
61
61
 
62
62
  cache.push(state);
63
63
 
@@ -69,8 +69,8 @@ export default <R>(instance?: Router<R>) => {
69
69
  return {
70
70
  back,
71
71
  forward,
72
- middleware: (...middleware: Middleware<R>[]) => {
73
- let instance = pipeline<Request, Response<R>>(...middleware);
72
+ middleware: (...middleware: Middleware<T>[]) => {
73
+ let instance = pipeline<Request<T>, Response<T>>(...middleware);
74
74
 
75
75
  return () => instance(state);
76
76
  },
@@ -23,7 +23,7 @@ function radixkey(method: string, path: string, subdomain?: string | null) {
23
23
  return ((subdomain ? subdomain + ' ' : '') + method).toUpperCase() + ' ' + normalize(path);
24
24
  }
25
25
 
26
- function set<R>(route: Route<R>, key: keyof Route<R>, value?: unknown) {
26
+ function set<T>(route: Route<T>, key: keyof Route<T>, value?: unknown) {
27
27
  if (!value) {
28
28
  return;
29
29
  }
@@ -44,11 +44,11 @@ function set<R>(route: Route<R>, key: keyof Route<R>, value?: unknown) {
44
44
  }
45
45
 
46
46
 
47
- class Router<R> {
48
- groups: Omit<Options<R>, 'responder'>[] = [];
49
- root: Node<R>;
50
- routes: Record<string, Route<R>> = {};
51
- static: Record<string, Route<R>> = {};
47
+ class Router<T> {
48
+ groups: Omit<Options<T>, 'responder'>[] = [];
49
+ root: Node<T>;
50
+ routes: Record<string, Route<T>> = {};
51
+ static: Record<string, Route<T>> = {};
52
52
  subdomains: string[] | null = null;
53
53
 
54
54
 
@@ -57,7 +57,7 @@ class Router<R> {
57
57
  }
58
58
 
59
59
 
60
- private add(radixkey: string, route: Route<R>) {
60
+ private add(radixkey: string, route: Route<T>) {
61
61
  if (radixkey.indexOf(':') === -1 || this.root.add(radixkey, route).type === STATIC) {
62
62
  if (this.static[radixkey]) {
63
63
  throw new Error(`Routing: static path '${radixkey}' is already in use`);
@@ -69,7 +69,7 @@ class Router<R> {
69
69
  return this;
70
70
  }
71
71
 
72
- private route({ middleware, name, path, responder, subdomain }: Options<R>) {
72
+ private route({ middleware, name, path, responder, subdomain }: Options<T>) {
73
73
  let route = new Route(responder);
74
74
 
75
75
  for (let i = 0, n = this.groups.length; i < n; i++) {
@@ -98,19 +98,19 @@ class Router<R> {
98
98
  }
99
99
 
100
100
 
101
- delete(options: Options<R>) {
101
+ delete(options: Options<T>) {
102
102
  this.on(['DELETE'], options);
103
103
  return this;
104
104
  }
105
105
 
106
- get(options: Options<R>) {
106
+ get(options: Options<T>) {
107
107
  this.on(['GET'], options);
108
108
  return this;
109
109
  }
110
110
 
111
- group(options: Router<R>['groups'][0]) {
111
+ group(options: Router<T>['groups'][0]) {
112
112
  return {
113
- routes: (fn: (router: Router<R>) => void) => {
113
+ routes: (fn: (router: Router<T>) => void) => {
114
114
  this.groups.push(options);
115
115
  fn(this);
116
116
  this.groups.pop();
@@ -118,7 +118,7 @@ class Router<R> {
118
118
  }
119
119
  }
120
120
 
121
- match(method: string, path: string, subdomain?: string | null): ReturnType<Node<R>['find']> {
121
+ match(method: string, path: string, subdomain?: string | null): ReturnType<Node<T>['find']> {
122
122
  let key = radixkey(method, path, subdomain);
123
123
 
124
124
  if (key in this.static) {
@@ -130,7 +130,7 @@ class Router<R> {
130
130
  return this.root.find(key);
131
131
  }
132
132
 
133
- on(methods: string[], options: Options<R>) {
133
+ on(methods: string[], options: Options<T>) {
134
134
  let route = this.route(options);
135
135
 
136
136
  if (route.name) {
@@ -171,12 +171,12 @@ class Router<R> {
171
171
  return this;
172
172
  }
173
173
 
174
- post(options: Options<R>) {
174
+ post(options: Options<T>) {
175
175
  this.on(['POST'], options);
176
176
  return this;
177
177
  }
178
178
 
179
- put(options: Options<R>) {
179
+ put(options: Options<T>) {
180
180
  this.on(['PUT'], options);
181
181
  return this;
182
182
  }
@@ -219,5 +219,5 @@ class Router<R> {
219
219
  }
220
220
 
221
221
 
222
- export default <R>() => new Router<R>();
222
+ export default <T>() => new Router<T>();
223
223
  export { Router, Route };
@@ -2,28 +2,28 @@ import { PLACEHOLDER, STATIC, WILDCARD } from '~/constants';
2
2
  import { Route } from './index';
3
3
 
4
4
 
5
- class Node<R> {
6
- children: Map<string | number, Node<R>> | null = null;
7
- parent: Node<R> | null = null;
5
+ class Node<T> {
6
+ children: Map<string | number, Node<T>> | null = null;
7
+ parent: Node<T> | null = null;
8
8
  path: string | null = null;
9
9
  property: string | null = null;
10
- route: Route<R> | null = null;
10
+ route: Route<T> | null = null;
11
11
  type: number | null = null;
12
12
 
13
13
 
14
- constructor(parent: Node<R>['parent'] = null) {
14
+ constructor(parent: Node<T>['parent'] = null) {
15
15
  this.parent = parent;
16
16
  }
17
17
 
18
18
 
19
- add(path: string, route: Route<R>) {
20
- let node: Node<R> | undefined = this,
19
+ add(path: string, route: Route<T>) {
20
+ let node: Node<T> | undefined = this,
21
21
  segments = path.split('/'),
22
- type: Node<R>['type'] = STATIC,
22
+ type: Node<T>['type'] = STATIC,
23
23
  unnamed = 0;
24
24
 
25
25
  for (let i = 0, n = segments.length; i < n; i++) {
26
- let child: Node<R> | undefined = node.children?.get(segments[i]);
26
+ let child: Node<T> | undefined = node.children?.get(segments[i]);
27
27
 
28
28
  if (!child) {
29
29
  let segment = segments[i],
@@ -33,7 +33,7 @@ class Node<R> {
33
33
  node.children = new Map();
34
34
  }
35
35
 
36
- node.children.set(segment, (child = new Node<R>(node)));
36
+ node.children.set(segment, (child = new Node<T>(node)));
37
37
 
38
38
  // Named property
39
39
  if (symbol === ':') {
@@ -61,12 +61,12 @@ class Node<R> {
61
61
 
62
62
  find(path: string): {
63
63
  parameters?: Record<PropertyKey, unknown>;
64
- route?: Route<R>;
64
+ route?: Route<T>;
65
65
  } {
66
- let node: Node<R> | undefined = this,
66
+ let node: Node<T> | undefined = this,
67
67
  parameters: Record<PropertyKey, unknown> = {},
68
68
  segments = path.split('/'),
69
- wildcard: { node: Node<R>, value: string } | null = null;
69
+ wildcard: { node: Node<T>, value: string } | null = null;
70
70
 
71
71
  for (let i = 0, n = segments.length; i < n; i++) {
72
72
  let segment = segments[i],
@@ -80,7 +80,7 @@ class Node<R> {
80
80
  }
81
81
 
82
82
  // Exact matches take precedence over placeholders
83
- let next: Node<R> | undefined = node.children?.get(segment);
83
+ let next: Node<T> | undefined = node.children?.get(segment);
84
84
 
85
85
  if (next) {
86
86
  node = next;
@@ -112,7 +112,7 @@ class Node<R> {
112
112
  }
113
113
 
114
114
  remove(path: string) {
115
- let node: Node<R> | undefined = this,
115
+ let node: Node<T> | undefined = this,
116
116
  segments = path.split('/');
117
117
 
118
118
  for (let i = 0, n = segments.length; i < n; i++) {
@@ -2,16 +2,16 @@ import { Middleware, Next, Responder } from '~/types';
2
2
  import pipeline from '@esportsplus/pipeline';
3
3
 
4
4
 
5
- class Route<R> {
6
- dispatch: Next<R> | null = null;
5
+ class Route<T> {
6
+ dispatch: Next<T> | null = null;
7
7
  name: string | null = null;
8
8
  path: string | null = null;
9
- responder: Responder<R>;
10
- stack: Middleware<R>[] | null = null;
9
+ responder: Responder<T>;
10
+ stack: Middleware<T>[] | null = null;
11
11
  subdomain: string | null = null;
12
12
 
13
13
 
14
- constructor(responder: Responder<R>) {
14
+ constructor(responder: Responder<T>) {
15
15
  this.responder = responder;
16
16
  }
17
17
 
package/src/types.ts CHANGED
@@ -2,20 +2,20 @@ import { Next as N, Stage } from '@esportsplus/pipeline';
2
2
  import { Route, Router } from './router';
3
3
 
4
4
 
5
- type Middleware<R> = Stage<Request, Response<R>>;
5
+ type Middleware<T> = Stage<Request<T>, Response<T>>;
6
6
 
7
- type Next<R> = N<Request, Response<R>>;
7
+ type Next<T> = N<Request<T>, Response<T>>;
8
8
 
9
- type Options<R> = {
10
- middleware?: Middleware<R>[];
9
+ type Options<T> = {
10
+ middleware?: Middleware<T>[];
11
11
  name?: string;
12
12
  path?: string;
13
- responder: Responder<R>;
13
+ responder: Responder<T>;
14
14
  subdomain?: string;
15
15
  };
16
16
 
17
- type Request = {
18
- data: Record<PropertyKey, unknown>;
17
+ type Request<T> = {
18
+ data: Record<PropertyKey, unknown> & ReturnType<Router<T>['match']>;
19
19
  href: string;
20
20
  hostname: string;
21
21
  method: string;
@@ -27,9 +27,9 @@ type Request = {
27
27
  subdomain?: string;
28
28
  };
29
29
 
30
- type Responder<R> = (request: Request) => Response<R>;
30
+ type Responder<T> = (request: Request<T>) => Response<T>;
31
31
 
32
- type Response<R> = Promise<R> | R;
32
+ type Response<T> = Promise<T> | T;
33
33
 
34
34
 
35
35
  export { Middleware, Next, Options, Request, Responder, Response, Route, Router };