@esportsplus/routing 0.0.19 → 0.0.20

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,3 +1,9 @@
1
- import { Request, Responder } from '../types';
2
- declare const _default: <R>(request: Request<R>) => import("../types").Response<R>;
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>;
3
9
  export default _default;
@@ -1,6 +1,6 @@
1
1
  export default (request) => {
2
2
  let { route } = request.data;
3
- if (!route) {
3
+ if (route === undefined) {
4
4
  throw new Error(`Routing: route dispatching failed, route is undefined!`);
5
5
  }
6
6
  return route.dispatcher(request);
@@ -1,11 +1,13 @@
1
- import { Middleware, Request, Response } from '../types';
2
1
  import dispatch from './dispatch';
3
2
  import match from './match';
4
- declare const factory: <R>(...fns: Middleware<R>[]) => import("@esportsplus/middleware/build/types").Next<Request<R>, Response<R>>;
5
3
  declare const _default: {
6
- dispatch: <R>(request: Request<R>) => Response<R>;
7
- factory: <R_1>(...fns: Middleware<R_1>[]) => import("@esportsplus/middleware/build/types").Next<Request<R_1>, Response<R_1>>;
8
- match: <R_2>(router: import("../router").Router<R_2>, subdomain?: string | undefined) => Middleware<R_2>;
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>;
9
11
  };
10
12
  export default _default;
11
- export { dispatch, factory, match };
13
+ export { dispatch, match };
@@ -1,8 +1,4 @@
1
- import middleware from '@esportsplus/middleware';
2
1
  import dispatch from './dispatch';
3
2
  import match from './match';
4
- const factory = (...fns) => {
5
- return middleware(...fns);
6
- };
7
- export default { dispatch, factory, match };
8
- export { dispatch, factory, match };
3
+ export default { dispatch, match };
4
+ export { dispatch, match };
@@ -1,4 +1,4 @@
1
- import { factory } from '../middleware';
1
+ import middleware from '@esportsplus/middleware';
2
2
  class Route {
3
3
  dispatch = null;
4
4
  name = null;
@@ -15,7 +15,7 @@ class Route {
15
15
  this.dispatch = (request) => this.responder(request);
16
16
  }
17
17
  else {
18
- this.dispatch = factory(...this.stack, (request => this.responder(request)));
18
+ this.dispatch = middleware(...this.stack, (request => this.responder(request)));
19
19
  }
20
20
  }
21
21
  return this.dispatch;
package/build/spa.d.ts CHANGED
@@ -1,14 +1,14 @@
1
- import { Request, Router } from './types';
1
+ import { Middleware, Response, Router } from './types';
2
2
  declare function back(): void;
3
3
  declare function forward(): void;
4
4
  declare const _default: <R>(router: Router<R>) => {
5
5
  back: typeof back;
6
6
  forward: typeof forward;
7
+ middleware: (...fns: Middleware<R>[]) => () => Response<R>;
7
8
  redirect: (path: string, { state, values }: {
8
9
  state?: Record<PropertyKey, unknown> | undefined;
9
10
  values?: unknown[] | undefined;
10
11
  }) => void;
11
- request: Request<R>;
12
12
  uri: (path: string, values?: unknown[]) => string;
13
13
  };
14
14
  export default _default;
package/build/spa.js CHANGED
@@ -1,3 +1,6 @@
1
+ import { reactive } from '@esportsplus/reactivity';
2
+ import middleware from '@esportsplus/middleware';
3
+ let cache = [], registered = false;
1
4
  function back() {
2
5
  window.history.back();
3
6
  }
@@ -11,7 +14,7 @@ function normalize(uri) {
11
14
  return uri;
12
15
  }
13
16
  function request() {
14
- let { hash, hostname, href, origin, port, protocol } = new URL(window?.location?.href || ''), path = hash?.replace('#/', '/')?.split('?') || ['/', ''];
17
+ let { hash, hostname, href, origin, port, protocol } = new URL(window.location?.href || ''), path = hash ? hash.slice(1).split('?') : ['/', ''];
15
18
  return {
16
19
  data: {},
17
20
  href,
@@ -21,27 +24,38 @@ function request() {
21
24
  path: path[0],
22
25
  port,
23
26
  protocol,
24
- query: Object.fromEntries((new URLSearchParams(path[1])).entries())
27
+ query: path[1] ? Object.fromEntries((new URLSearchParams(path[1])).entries()) : {}
25
28
  };
26
29
  }
27
- export default (router) => {
28
- let state = request();
29
- window.addEventListener('popstate', () => {
30
- let values = request();
30
+ function event() {
31
+ let values = request();
32
+ for (let i = 0, n = cache.length; i < n; i++) {
33
+ let state = cache[i];
31
34
  for (let key in values) {
32
35
  state[key] = values[key];
33
36
  }
34
- });
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
+ }
35
46
  return {
36
47
  back,
37
48
  forward,
49
+ middleware: (...fns) => {
50
+ let pipeline = middleware(...fns);
51
+ return () => pipeline(state);
52
+ },
38
53
  redirect: (path, { state, values }) => {
39
54
  if (path.startsWith('https://') || path.startsWith('http://')) {
40
55
  return window.location.replace(path);
41
56
  }
42
57
  window.history.pushState((state || {}), '', normalize(router.uri(path, values || [])));
43
58
  },
44
- request: state,
45
59
  uri: (path, values = []) => {
46
60
  return normalize(router.uri(path, values || []));
47
61
  }
package/build/types.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Middleware as M, Next as N } from '@esportsplus/middleware';
2
2
  import { Route, Router } from './router';
3
- type Middleware<R> = M<Request<R>, Response<R>>;
4
- type Next<R> = N<Request<R>, Response<R>>;
3
+ type Middleware<R> = M<Request, Response<R>>;
4
+ type Next<R> = N<Request, Response<R>>;
5
5
  type Options<R> = {
6
6
  middleware?: Middleware<R>[];
7
7
  name?: string;
@@ -9,8 +9,8 @@ type Options<R> = {
9
9
  responder: Responder<R>;
10
10
  subdomain?: string;
11
11
  };
12
- type Request<R> = {
13
- data: ReturnType<Router<R>['match']> & Record<PropertyKey, unknown>;
12
+ type Request = {
13
+ data: Record<PropertyKey, unknown>;
14
14
  href: string;
15
15
  hostname: string;
16
16
  method: string;
@@ -21,6 +21,6 @@ type Request<R> = {
21
21
  query: Record<string, unknown>;
22
22
  subdomain?: string;
23
23
  };
24
- type Responder<R> = (request: Request<R>) => Response<R>;
24
+ type Responder<R> = (request: Request) => Response<R>;
25
25
  type Response<R> = Promise<R> | R;
26
26
  export { Middleware, Next, Options, Request, Responder, Response, Route, Router };
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "author": "ICJR",
3
3
  "dependencies": {
4
- "@esportsplus/middleware": "^0.0.5"
4
+ "@esportsplus/middleware": "^0.0.5",
5
+ "@esportsplus/reactivity": "^0.0.30"
5
6
  },
6
7
  "devDependencies": {
7
8
  "@esportsplus/typescript": "^0.0.1"
@@ -16,5 +17,5 @@
16
17
  "prepublishOnly": "npm run build"
17
18
  },
18
19
  "types": "./build/index.d.ts",
19
- "version": "0.0.19"
20
+ "version": "0.0.20"
20
21
  }
@@ -1,10 +1,11 @@
1
- import { Request, Responder } from '~/types';
1
+ import { Request } from '~/types';
2
+ import { Router } from '~/router';
2
3
 
3
4
 
4
- export default <R>(request: Request<R>): ReturnType<Responder<R>> => {
5
+ export default <R>(request: Request & { data: ReturnType<Router<R>['match']> }) => {
5
6
  let { route } = request.data;
6
7
 
7
- if (!route) {
8
+ if (route === undefined) {
8
9
  throw new Error(`Routing: route dispatching failed, route is undefined!`);
9
10
  }
10
11
 
@@ -1,13 +1,6 @@
1
- import { Middleware, Request, Response } from '~/types';
2
- import middleware from '@esportsplus/middleware';
3
1
  import dispatch from './dispatch';
4
2
  import match from './match';
5
3
 
6
4
 
7
- const factory = <R>(...fns: Middleware<R>[]) => {
8
- return middleware<Request<R>, Response<R>>(...fns);
9
- };
10
-
11
-
12
- export default { dispatch, factory, match };
13
- export { dispatch, factory, match };
5
+ export default { dispatch, match };
6
+ export { dispatch, match };
@@ -1,5 +1,5 @@
1
1
  import { Middleware, Next, Responder } from '~/types';
2
- import { factory } from '~/middleware';
2
+ import middleware from '@esportsplus/middleware';
3
3
 
4
4
 
5
5
  class Route<R> {
@@ -22,7 +22,7 @@ class Route<R> {
22
22
  this.dispatch = (request) => this.responder(request);
23
23
  }
24
24
  else {
25
- this.dispatch = factory(...this.stack, (request => this.responder(request)));
25
+ this.dispatch = middleware(...this.stack, (request => this.responder(request)));
26
26
  }
27
27
  }
28
28
 
package/src/spa.ts CHANGED
@@ -1,4 +1,10 @@
1
- import { Request, Router } from './types';
1
+ import { reactive } from '@esportsplus/reactivity';
2
+ import { Middleware, Request, Response, Router } from './types';
3
+ import middleware from '@esportsplus/middleware';
4
+
5
+
6
+ let cache: Request[] = [],
7
+ registered = false;
2
8
 
3
9
 
4
10
  function back() {
@@ -17,9 +23,9 @@ function normalize(uri: string) {
17
23
  return uri;
18
24
  }
19
25
 
20
- function request<R>(): Request<R> {
21
- let { hash, hostname, href, origin, port, protocol } = new URL( window?.location?.href || '' ),
22
- path = hash?.replace('#/', '/')?.split('?') || ['/', ''];
26
+ function request(): Request {
27
+ let { hash, hostname, href, origin, port, protocol } = new URL( window.location?.href || '' ),
28
+ path = hash ? hash.slice(1).split('?') : ['/', ''];
23
29
 
24
30
  return {
25
31
  data: {},
@@ -30,26 +36,42 @@ function request<R>(): Request<R> {
30
36
  path: path[0],
31
37
  port,
32
38
  protocol,
33
- query: Object.fromEntries( (new URLSearchParams(path[1])).entries() )
39
+ query: path[1] ? Object.fromEntries( (new URLSearchParams(path[1])).entries() ) : {}
34
40
  };
35
41
  }
36
42
 
43
+ function event() {
44
+ let values = request();
37
45
 
38
- export default <R>(router: Router<R>) => {
39
- let state = request<R>();
40
-
41
- window.addEventListener('popstate', () => {
42
- let values = request<R>();
46
+ for (let i = 0, n = cache.length; i < n; i++) {
47
+ let state = cache[i];
43
48
 
44
49
  for (let key in values) {
45
50
  // @ts-ignore
46
51
  state[key] = values[key];
47
52
  }
48
- });
53
+ }
54
+ }
55
+
56
+
57
+ export default <R>(router: Router<R>) => {
58
+ let state = reactive( request() );
59
+
60
+ cache.push(state);
61
+
62
+ if (!registered) {
63
+ registered = true;
64
+ window.addEventListener('popstate', event);
65
+ }
49
66
 
50
67
  return {
51
68
  back,
52
69
  forward,
70
+ middleware: (...fns: Middleware<R>[]) => {
71
+ let pipeline = middleware<Request, Response<R>>(...fns);
72
+
73
+ return () => pipeline(state);
74
+ },
53
75
  redirect: (path: string, { state, values }: { state?: Record<PropertyKey, unknown>; values?: unknown[] }) => {
54
76
  if (path.startsWith('https://') || path.startsWith('http://')) {
55
77
  return window.location.replace(path);
@@ -57,7 +79,6 @@ export default <R>(router: Router<R>) => {
57
79
 
58
80
  window.history.pushState( (state || {}), '', normalize(router.uri(path, values || [])) );
59
81
  },
60
- request: state,
61
82
  uri: (path: string, values: unknown[] = []) => {
62
83
  return normalize( router.uri(path, values || []) );
63
84
  }
package/src/types.ts CHANGED
@@ -2,9 +2,9 @@ import { Middleware as M, Next as N } from '@esportsplus/middleware';
2
2
  import { Route, Router } from './router';
3
3
 
4
4
 
5
- type Middleware<R> = M<Request<R>, Response<R>>;
5
+ type Middleware<R> = M<Request, Response<R>>;
6
6
 
7
- type Next<R> = N<Request<R>, Response<R>>;
7
+ type Next<R> = N<Request, Response<R>>;
8
8
 
9
9
  type Options<R> = {
10
10
  middleware?: Middleware<R>[];
@@ -14,8 +14,8 @@ type Options<R> = {
14
14
  subdomain?: string;
15
15
  };
16
16
 
17
- type Request<R> = {
18
- data: ReturnType<Router<R>['match']> & Record<PropertyKey, unknown>;
17
+ type Request = {
18
+ data: Record<PropertyKey, unknown>;
19
19
  href: string;
20
20
  hostname: string;
21
21
  method: string;
@@ -27,7 +27,7 @@ type Request<R> = {
27
27
  subdomain?: string;
28
28
  };
29
29
 
30
- type Responder<R> = (request: Request<R>) => Response<R>;
30
+ type Responder<R> = (request: Request) => Response<R>;
31
31
 
32
32
  type Response<R> = Promise<R> | R;
33
33