@esportsplus/routing 0.0.22 → 0.0.23

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.
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.23"
21
21
  }
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 };