@esportsplus/routing 0.0.22 → 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
@@ -2,10 +2,10 @@
2
2
  "author": "ICJR",
3
3
  "dependencies": {
4
4
  "@esportsplus/pipeline": "^0.0.1",
5
- "@esportsplus/reactivity": "^0.0.30"
5
+ "@esportsplus/reactivity": "^0.1.2"
6
6
  },
7
7
  "devDependencies": {
8
- "@esportsplus/typescript": "^0.0.1"
8
+ "@esportsplus/typescript": "^0.0.3"
9
9
  },
10
10
  "main": "./build/index.js",
11
11
  "name": "@esportsplus/routing",
@@ -17,5 +17,5 @@
17
17
  "prepublishOnly": "npm run build"
18
18
  },
19
19
  "types": "./build/index.d.ts",
20
- "version": "0.0.22"
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 };
package/build/hash.d.ts DELETED
@@ -1,70 +0,0 @@
1
- import { Router } from './types';
2
- declare let cache: {
3
- factory: typeof request;
4
- state: Record<PropertyKey, unknown>;
5
- }[];
6
- declare const back: () => void;
7
- declare const forward: () => void;
8
- declare const listener: {
9
- register: (factory: (typeof cache)[0]['factory'], state: (typeof cache)[0]['state']) => () => void;
10
- remove: (state: (typeof cache)[0]['state']) => void;
11
- };
12
- declare const factory: {
13
- redirect: (router: Router) => (path: string, { state, values }: {
14
- state?: Record<PropertyKey, unknown> | undefined;
15
- values?: unknown[] | undefined;
16
- }) => void;
17
- uri: (router: Router) => (path: string, values?: unknown[]) => string;
18
- };
19
- declare const request: (url?: string) => {
20
- href: string;
21
- hostname: string;
22
- method: string;
23
- origin: string;
24
- path: string;
25
- port: string;
26
- protocol: string;
27
- query: {
28
- [k: string]: string;
29
- };
30
- };
31
- declare const _default: {
32
- back: () => void;
33
- factory: {
34
- redirect: (router: Router) => (path: string, { state, values }: {
35
- state?: Record<PropertyKey, unknown> | undefined;
36
- values?: unknown[] | undefined;
37
- }) => void;
38
- uri: (router: Router) => (path: string, values?: unknown[]) => string;
39
- };
40
- forward: () => void;
41
- listener: {
42
- register: (factory: (url?: string) => {
43
- href: string;
44
- hostname: string;
45
- method: string;
46
- origin: string;
47
- path: string;
48
- port: string;
49
- protocol: string;
50
- query: {
51
- [k: string]: string;
52
- };
53
- }, state: Record<PropertyKey, unknown>) => () => void;
54
- remove: (state: Record<PropertyKey, unknown>) => void;
55
- };
56
- request: (url?: string) => {
57
- href: string;
58
- hostname: string;
59
- method: string;
60
- origin: string;
61
- path: string;
62
- port: string;
63
- protocol: string;
64
- query: {
65
- [k: string]: string;
66
- };
67
- };
68
- };
69
- export default _default;
70
- export { back, factory, forward, listener, request };
package/build/hash.js DELETED
@@ -1,75 +0,0 @@
1
- let cache = [], registered = false;
2
- function update() {
3
- for (let i = 0, n = cache.length; i < n; i++) {
4
- let { factory, state } = cache[i], values = factory();
5
- for (let key in values) {
6
- state[key] = values[key];
7
- }
8
- }
9
- }
10
- const back = () => window.history.back();
11
- const forward = () => window.history.forward();
12
- const listener = {
13
- register: (factory, state) => {
14
- cache.push({ factory, state });
15
- if (!registered) {
16
- registered = true;
17
- update();
18
- window.addEventListener('popstate', update);
19
- }
20
- return () => {
21
- listener.remove(state);
22
- };
23
- },
24
- remove: (state) => {
25
- for (let i = 0, n = cache.length; i < n; i++) {
26
- if (cache[i].state !== state) {
27
- continue;
28
- }
29
- cache[i] = cache[n - 1];
30
- cache.pop();
31
- if (cache.length === 0) {
32
- window.removeEventListener('popstate', update);
33
- }
34
- return;
35
- }
36
- }
37
- };
38
- const factory = {
39
- redirect: (router) => {
40
- return (path, { state, values }) => {
41
- if (path.startsWith('http://') || path.startsWith('https://')) {
42
- return window.location.replace(path);
43
- }
44
- let uri = router.uri(path, values || []);
45
- if (uri[0] === '/') {
46
- uri = '#' + uri;
47
- }
48
- window.history.pushState(state || {}, '', uri);
49
- };
50
- },
51
- uri: (router) => {
52
- return (path, values = []) => {
53
- let uri = router.uri(path, values || []);
54
- if (uri[0] === '/') {
55
- uri = '#' + uri;
56
- }
57
- return uri;
58
- };
59
- }
60
- };
61
- const request = (url = window?.location?.href || '') => {
62
- let { hash, hostname, href, origin, port, protocol } = new URL(url), path = hash?.replace('#/', '/')?.split('?') || ['/', ''];
63
- return {
64
- href,
65
- hostname,
66
- method: 'GET',
67
- origin,
68
- path: path[0],
69
- port,
70
- protocol,
71
- query: Object.fromEntries((new URLSearchParams(path[1])).entries())
72
- };
73
- };
74
- export default { back, factory, forward, listener, request };
75
- export { back, factory, forward, listener, request };
@@ -1,9 +0,0 @@
1
- import { Request } from '../types';
2
- import { Router } from '../router';
3
- declare const _default: <R>(request: Request & {
4
- data: {
5
- parameters?: Record<PropertyKey, unknown> | undefined;
6
- route?: import("../router/route").Route<R> | undefined;
7
- };
8
- }) => import("../types").Response<R>;
9
- export default _default;
@@ -1,7 +0,0 @@
1
- export default (request) => {
2
- let { route } = request.data;
3
- if (route === undefined) {
4
- throw new Error(`Routing: route dispatching failed, route is undefined!`);
5
- }
6
- return route.dispatcher(request);
7
- };
@@ -1,3 +0,0 @@
1
- import { Middleware } from '../types';
2
- declare const _default: (...middleware: Middleware[]) => <T>(request: T) => unknown;
3
- export default _default;
@@ -1,13 +0,0 @@
1
- function error() {
2
- throw new Error('Routing: request middleware did not return a responder');
3
- }
4
- export default (...middleware) => {
5
- let stack = [];
6
- for (let i = 0, n = middleware.length; i < n; i++) {
7
- stack[i] = (request) => middleware[i](request, stack[i + 1] || error);
8
- }
9
- if (!stack.length) {
10
- throw new Error('Routing: request middleware has not been defined');
11
- }
12
- return (request) => stack[0](request);
13
- };
@@ -1,13 +0,0 @@
1
- import dispatch from './dispatch';
2
- import match from './match';
3
- declare const _default: {
4
- dispatch: <R>(request: import("../types").Request & {
5
- data: {
6
- parameters?: Record<PropertyKey, unknown> | undefined;
7
- route?: import("../router/route").Route<R> | undefined;
8
- };
9
- }) => import("../types").Response<R>;
10
- match: <R_1>(router: import("../router").Router<R_1>, subdomain?: string | undefined) => import("../types").Middleware<R_1>;
11
- };
12
- export default _default;
13
- export { dispatch, match };
@@ -1,4 +0,0 @@
1
- import dispatch from './dispatch';
2
- import match from './match';
3
- export default { dispatch, match };
4
- export { dispatch, match };
@@ -1,3 +0,0 @@
1
- import { Middleware, Router } from '../types';
2
- declare const _default: <R>(router: Router<R>, subdomain?: string) => Middleware<R>;
3
- export default _default;
@@ -1,23 +0,0 @@
1
- export default (router, subdomain) => {
2
- return (request, next) => {
3
- let match = subdomain || request.subdomain;
4
- if (match === undefined) {
5
- if (router.subdomains) {
6
- for (let i = 0, n = router.subdomains.length; i < n; i++) {
7
- if (!request.hostname.startsWith(router.subdomains[i])) {
8
- continue;
9
- }
10
- match = router.subdomains[i];
11
- break;
12
- }
13
- }
14
- if (match === undefined) {
15
- match = '';
16
- }
17
- }
18
- let { parameters, route } = router.match(request.method, request.path, match);
19
- request.data.parameters = parameters;
20
- request.data.route = route;
21
- return next(request);
22
- };
23
- };
@@ -1,6 +0,0 @@
1
- declare const normalize: (path: string) => string;
2
- declare const radixkey: (path: string, { method, subdomain }?: {
3
- method?: string | null | undefined;
4
- subdomain?: string | null | undefined;
5
- }) => string;
6
- export { normalize, radixkey };
@@ -1,20 +0,0 @@
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 };
package/build/spa.d.ts DELETED
@@ -1,14 +0,0 @@
1
- import { Middleware, Response, Router } from './types';
2
- declare function back(): void;
3
- declare function forward(): void;
4
- declare const _default: <R>(router: Router<R>) => {
5
- back: typeof back;
6
- forward: typeof forward;
7
- middleware: (...fns: Middleware<R>[]) => () => Response<R>;
8
- redirect: (path: string, { state, values }: {
9
- state?: Record<PropertyKey, unknown> | undefined;
10
- values?: unknown[] | undefined;
11
- }) => void;
12
- uri: (path: string, values?: unknown[]) => string;
13
- };
14
- export default _default;
package/build/spa.js DELETED
@@ -1,63 +0,0 @@
1
- import { reactive } from '@esportsplus/reactivity';
2
- import pipeline from '@esportsplus/pipeline';
3
- let cache = [], registered = false;
4
- function back() {
5
- window.history.back();
6
- }
7
- function forward() {
8
- window.history.forward();
9
- }
10
- function normalize(uri) {
11
- if (uri[0] === '/') {
12
- return '#' + uri;
13
- }
14
- return uri;
15
- }
16
- function request() {
17
- let { hash, hostname, href, origin, port, protocol } = new URL(window.location?.href || ''), path = hash ? hash.slice(1).split('?') : ['/', ''];
18
- return {
19
- data: {},
20
- href,
21
- hostname,
22
- method: 'GET',
23
- origin,
24
- path: path[0],
25
- port,
26
- protocol,
27
- query: path[1] ? Object.fromEntries((new URLSearchParams(path[1])).entries()) : {}
28
- };
29
- }
30
- function event() {
31
- let values = request();
32
- for (let i = 0, n = cache.length; i < n; i++) {
33
- let state = cache[i];
34
- for (let key in values) {
35
- state[key] = values[key];
36
- }
37
- }
38
- }
39
- export default (router) => {
40
- let state = reactive(request());
41
- cache.push(state);
42
- if (!registered) {
43
- registered = true;
44
- window.addEventListener('popstate', event);
45
- }
46
- return {
47
- back,
48
- forward,
49
- middleware: (...fns) => {
50
- let instance = pipeline(...fns);
51
- return () => instance(state);
52
- },
53
- redirect: (path, { state, values }) => {
54
- if (path.startsWith('https://') || path.startsWith('http://')) {
55
- return window.location.replace(path);
56
- }
57
- window.history.pushState((state || {}), '', normalize(router.uri(path, values || [])));
58
- },
59
- uri: (path, values = []) => {
60
- return normalize(router.uri(path, values || []));
61
- }
62
- };
63
- };
@@ -1,4 +0,0 @@
1
- declare const PLACEHOLDER = 0;
2
- declare const STATIC = 1;
3
- declare const WILDCARD = 2;
4
- export { PLACEHOLDER, STATIC, WILDCARD };
package/build/symbols.js DELETED
@@ -1,4 +0,0 @@
1
- const PLACEHOLDER = 0;
2
- const STATIC = 1;
3
- const WILDCARD = 2;
4
- export { PLACEHOLDER, STATIC, WILDCARD };