@esportsplus/routing 0.0.19 → 0.0.21

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