@esportsplus/routing 0.0.5 → 0.0.7

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/build/group.js CHANGED
@@ -2,7 +2,7 @@ export default () => {
2
2
  return {
3
3
  middleware: [],
4
4
  name: '',
5
- pattern: '',
5
+ path: '',
6
6
  subdomain: ''
7
7
  };
8
8
  };
package/build/index.d.ts CHANGED
@@ -8,9 +8,8 @@ declare const _default: {
8
8
  data: Record<string, any>;
9
9
  href: string;
10
10
  hostname: string;
11
- uri: string;
12
11
  origin: string;
13
- pathname: string;
12
+ path: string;
14
13
  port: string;
15
14
  protocol: string;
16
15
  query: {
@@ -24,9 +23,8 @@ declare const _default: {
24
23
  data: Record<string, any>;
25
24
  href: string;
26
25
  hostname: string;
27
- uri: string;
28
26
  origin: string;
29
- pathname: string;
27
+ path: string;
30
28
  port: string;
31
29
  protocol: string;
32
30
  query: {
@@ -34,13 +32,12 @@ declare const _default: {
34
32
  };
35
33
  subdomain: string;
36
34
  }) => any;
37
- match: (routes: import("./routes").Routes) => (request: {
35
+ match: ({ routes, subdomains }: import("./types").Routes) => (request: {
38
36
  data: Record<string, any>;
39
37
  href: string;
40
38
  hostname: string;
41
- uri: string;
42
39
  origin: string;
43
- pathname: string;
40
+ path: string;
44
41
  port: string;
45
42
  protocol: string;
46
43
  query: {
@@ -53,9 +50,8 @@ declare const _default: {
53
50
  data: Record<string, any>;
54
51
  href: string;
55
52
  hostname: string;
56
- uri: string;
57
53
  origin: string;
58
- pathname: string;
54
+ path: string;
59
55
  port: string;
60
56
  protocol: string;
61
57
  query: {
@@ -65,15 +61,23 @@ declare const _default: {
65
61
  }) => Promise<unknown>;
66
62
  };
67
63
  redirect: (key: string, _?: Record<string, any>) => void;
68
- routes: import("./routes").Routes;
64
+ routes: {
65
+ add: ({ name, path, responder }: {
66
+ name: string;
67
+ path?: string | undefined;
68
+ responder: import("./types").Responder;
69
+ }) => import("./types").Route;
70
+ group: (group: any, fn: (routes: import("./types").Routes) => void) => void;
71
+ routes: Record<string, import("./types").Route>;
72
+ subdomains: Record<string, Record<string, string>>;
73
+ };
69
74
  url: {
70
75
  parse: (url?: string) => {
71
76
  data: Record<string, any>;
72
77
  href: string;
73
78
  hostname: string;
74
- uri: string;
75
79
  origin: string;
76
- pathname: string;
80
+ path: string;
77
81
  port: string;
78
82
  protocol: string;
79
83
  query: {
@@ -85,3 +89,4 @@ declare const _default: {
85
89
  };
86
90
  export default _default;
87
91
  export { listener, middleware, redirect, routes, url };
92
+ export * from './types';
package/build/index.js CHANGED
@@ -5,3 +5,4 @@ import routes from './routes';
5
5
  import url from './url';
6
6
  export default { listener, middleware, redirect, routes, url };
7
7
  export { listener, middleware, redirect, routes, url };
8
+ export * from './types';
@@ -3,9 +3,8 @@ declare const _default: {
3
3
  data: Record<string, any>;
4
4
  href: string;
5
5
  hostname: string;
6
- uri: string;
7
6
  origin: string;
8
- pathname: string;
7
+ path: string;
9
8
  port: string;
10
9
  protocol: string;
11
10
  query: {
@@ -13,13 +12,12 @@ declare const _default: {
13
12
  };
14
13
  subdomain: string;
15
14
  }) => any;
16
- match: (routes: import("../../routes").Routes) => (request: {
15
+ match: ({ routes, subdomains }: import("../../types").Routes) => (request: {
17
16
  data: Record<string, any>;
18
17
  href: string;
19
18
  hostname: string;
20
- uri: string;
21
19
  origin: string;
22
- pathname: string;
20
+ path: string;
23
21
  port: string;
24
22
  protocol: string;
25
23
  query: {
@@ -1,4 +1,3 @@
1
- import { Routes } from '../../routes';
2
- import { Next, Request } from '../../types';
3
- declare const _default: (routes: Routes) => (request: Request, next: Next) => unknown;
1
+ import { Next, Request, Routes } from '../../types';
2
+ declare const _default: ({ routes, subdomains }: Routes) => (request: Request, next: Next) => unknown;
4
3
  export default _default;
@@ -1,10 +1,10 @@
1
- export default (routes) => {
1
+ export default ({ routes, subdomains }) => {
2
2
  return (request, next) => {
3
- let name = (routes.subdomains[request.subdomain] || {})[request.uri], route = routes.static[name];
3
+ let name = (subdomains[request.subdomain] || {})[request.path], route = routes[name];
4
4
  if (!route) {
5
5
  }
6
6
  if (!route && routes.fallback) {
7
- route = routes.static[routes.fallback];
7
+ route = routes.fallback;
8
8
  }
9
9
  request.data.route = route;
10
10
  return next(request);
@@ -6,9 +6,8 @@ declare const _default: {
6
6
  data: Record<string, any>;
7
7
  href: string;
8
8
  hostname: string;
9
- uri: string;
10
9
  origin: string;
11
- pathname: string;
10
+ path: string;
12
11
  port: string;
13
12
  protocol: string;
14
13
  query: {
@@ -16,13 +15,12 @@ declare const _default: {
16
15
  };
17
16
  subdomain: string;
18
17
  }) => any;
19
- match: (routes: import("../routes").Routes) => (request: {
18
+ match: ({ routes, subdomains }: import("../types").Routes) => (request: {
20
19
  data: Record<string, any>;
21
20
  href: string;
22
21
  hostname: string;
23
- uri: string;
24
22
  origin: string;
25
- pathname: string;
23
+ path: string;
26
24
  port: string;
27
25
  protocol: string;
28
26
  query: {
@@ -35,9 +33,8 @@ declare const _default: {
35
33
  data: Record<string, any>;
36
34
  href: string;
37
35
  hostname: string;
38
- uri: string;
39
36
  origin: string;
40
- pathname: string;
37
+ path: string;
41
38
  port: string;
42
39
  protocol: string;
43
40
  query: {
package/build/redirect.js CHANGED
@@ -1,10 +1,10 @@
1
- import routes from './routes';
1
+ import { routes } from './routes';
2
2
  export default (key, _ = {}) => {
3
3
  if (key.startsWith('http://') || key.startsWith('https://')) {
4
4
  window.location.replace(key);
5
5
  }
6
- if (!routes.static[key]) {
6
+ if (!routes[key]) {
7
7
  throw new Error(`Route '${key}' does not exist`);
8
8
  }
9
- window.location.hash = `#${routes.static[key].pattern}`;
9
+ window.location.hash = `#${routes[key].path || ''}`;
10
10
  };
package/build/routes.d.ts CHANGED
@@ -1,13 +1,20 @@
1
- import { Group, Responder, Route } from './types';
2
- declare class Routes {
3
- dynamic: Record<Route['name'], Route>;
4
- fallback?: string;
5
- groups: Group[];
6
- static: Record<Route['name'], Route>;
7
- subdomains: Record<Route['subdomain'], Record<Route['pattern'], Route['name']>>;
8
- add(name: string, pattern: string, responder: Responder): Route;
9
- group(group: any, routes: (routes: Routes) => void): void;
10
- }
11
- declare const _default: Routes;
1
+ import { Responder, Routes, Route } from './types';
2
+ declare let routes: Record<Route['name'], Route>, subdomains: Record<NonNullable<Route['subdomain']>, Record<NonNullable<Route['path']>, Route['name']>>;
3
+ declare const add: ({ name, path, responder }: {
4
+ name: string;
5
+ path?: string | undefined;
6
+ responder: Responder;
7
+ }) => Route;
8
+ declare const group: (group: any, fn: (routes: Routes) => void) => void;
9
+ declare const _default: {
10
+ add: ({ name, path, responder }: {
11
+ name: string;
12
+ path?: string | undefined;
13
+ responder: Responder;
14
+ }) => Route;
15
+ group: (group: any, fn: (routes: Routes) => void) => void;
16
+ routes: Record<string, Route>;
17
+ subdomains: Record<string, Record<string, string>>;
18
+ };
12
19
  export default _default;
13
- export { Routes };
20
+ export { add, group, routes, subdomains };
package/build/routes.js CHANGED
@@ -1,46 +1,45 @@
1
- class Routes {
2
- dynamic = {};
3
- fallback;
4
- groups = [];
5
- static = {};
6
- subdomains = {};
7
- add(name, pattern, responder) {
8
- let route = {
9
- middleware: [],
10
- name: '',
11
- pattern: '',
12
- responder,
13
- subdomain: ''
14
- };
15
- for (let i = 0, n = this.groups.length; i < n; i++) {
16
- let group = this.groups[i];
17
- route.name += group.name;
18
- route.pattern += group.pattern;
1
+ let groups = [], routes = {}, subdomains = {};
2
+ const add = ({ name, path, responder }) => {
3
+ let http = path !== undefined, route = {
4
+ middleware: [],
5
+ name: '',
6
+ responder
7
+ };
8
+ if (http) {
9
+ route.path = '';
10
+ route.subdomain = '';
11
+ }
12
+ for (let i = 0, n = groups.length; i < n; i++) {
13
+ let group = groups[i];
14
+ if (group.middleware.length) {
15
+ route.middleware.push(...group.middleware);
16
+ }
17
+ route.name += group.name;
18
+ if (http) {
19
+ route.path += group.path;
19
20
  route.subdomain = group.subdomain + route.subdomain;
20
- if (group.middleware.length) {
21
- route.middleware.push(...group.middleware);
22
- }
23
21
  }
24
- route.name += name;
25
- route.pattern += pattern;
26
- if (route.pattern[0] !== '/') {
27
- route.pattern = `/${route.pattern}`;
22
+ }
23
+ routes[route.name += name] = route;
24
+ if (http) {
25
+ route.path = `${route.path}${path}`;
26
+ if (route.path[0] !== '/') {
27
+ route.path = `/${route.path}`;
28
28
  }
29
- if (route.subdomain === 'www') {
29
+ if (!route.subdomain || route.subdomain === 'www') {
30
30
  route.subdomain = '';
31
31
  }
32
- if (!this.subdomains[route.subdomain]) {
33
- this.subdomains[route.subdomain] = {};
32
+ if (!subdomains[route.subdomain]) {
33
+ subdomains[route.subdomain] = {};
34
34
  }
35
- this.static[route.name] = route;
36
- this.subdomains[route.subdomain][route.pattern] = route.name;
37
- return route;
38
- }
39
- group(group, routes) {
40
- this.groups.push(group);
41
- routes(this);
42
- this.groups.pop();
35
+ subdomains[route.subdomain][route.path] = route.name;
43
36
  }
44
- }
45
- export default new Routes();
46
- export { Routes };
37
+ return route;
38
+ };
39
+ const group = (group, fn) => {
40
+ groups.push(group);
41
+ fn({ add, group, routes, subdomains });
42
+ groups.pop();
43
+ };
44
+ export default { add, group, routes, subdomains };
45
+ export { add, group, routes, subdomains };
package/build/types.d.ts CHANGED
@@ -1,20 +1,26 @@
1
- import { Routes } from './routes';
2
1
  import { parse } from './url';
2
+ import routes from './routes';
3
3
  type Group = {
4
4
  middleware: Middleware[];
5
5
  name: string;
6
- pattern: string;
6
+ path: string;
7
7
  subdomain: string;
8
8
  };
9
9
  type Middleware = (request: Request, next: Next) => unknown;
10
10
  type Next = (request: Request) => unknown;
11
11
  type Request = ReturnType<typeof parse>;
12
12
  type Responder = (request: Request) => Promise<unknown> | unknown;
13
+ type Routes = {
14
+ add: typeof routes.add;
15
+ group: typeof routes.group;
16
+ routes: typeof routes.routes;
17
+ subdomains: typeof routes.subdomains;
18
+ };
13
19
  type Route = {
14
20
  middleware: Middleware[];
15
21
  name: string;
16
- pattern: string;
22
+ path?: string;
17
23
  responder: Responder;
18
- subdomain: string;
24
+ subdomain?: string;
19
25
  };
20
26
  export { Group, Middleware, Next, Request, Responder, Route, Routes };
package/build/types.js CHANGED
@@ -1,2 +1 @@
1
- import { Routes } from './routes';
2
- export { Routes };
1
+ export {};
package/build/url.d.ts CHANGED
@@ -2,9 +2,8 @@ declare const parse: (url?: string) => {
2
2
  data: Record<string, any>;
3
3
  href: string;
4
4
  hostname: string;
5
- uri: string;
6
5
  origin: string;
7
- pathname: string;
6
+ path: string;
8
7
  port: string;
9
8
  protocol: string;
10
9
  query: {
@@ -17,9 +16,8 @@ declare const _default: {
17
16
  data: Record<string, any>;
18
17
  href: string;
19
18
  hostname: string;
20
- uri: string;
21
19
  origin: string;
22
- pathname: string;
20
+ path: string;
23
21
  port: string;
24
22
  protocol: string;
25
23
  query: {
package/build/url.js CHANGED
@@ -1,5 +1,5 @@
1
- const parse = (url = window.location.href) => {
2
- let { hash, host, hostname, href, origin, pathname, port, protocol } = new URL(url), parts = host.split('.'), path = hash?.replace('#/', '/')?.split('?') || ['/', ''], subdomain = '';
1
+ const parse = (url = window?.location?.href || '') => {
2
+ let { hash, host, hostname, href, origin, port, protocol } = new URL(url), parts = host.split('.'), path = hash?.replace('#/', '/')?.split('?') || ['/', ''], subdomain = '';
3
3
  if (parts.length > 2) {
4
4
  subdomain = parts[0];
5
5
  if (['127', 'www'].includes(subdomain)) {
@@ -10,9 +10,8 @@ const parse = (url = window.location.href) => {
10
10
  data: {},
11
11
  href: href,
12
12
  hostname: hostname,
13
- uri: path[0],
14
13
  origin: origin,
15
- pathname: pathname,
14
+ path: path[0],
16
15
  port: port,
17
16
  protocol: protocol,
18
17
  query: Object.fromEntries((new URLSearchParams(path[1])).entries()),
package/package.json CHANGED
@@ -2,8 +2,8 @@
2
2
  "author": "ICJR",
3
3
  "description": "Routing",
4
4
  "devDependencies": {
5
- "tsc-alias": "^1.8.1",
6
- "typescript": "^4.9.3"
5
+ "tsc-alias": "^1.8.2",
6
+ "typescript": "^4.9.4"
7
7
  },
8
8
  "main": "./build/index.js",
9
9
  "name": "@esportsplus/routing",
@@ -15,12 +15,5 @@
15
15
  "prepublishOnly": "npm run build"
16
16
  },
17
17
  "types": "./build/index.d.ts",
18
- "typesVersions": {
19
- "*": {
20
- "types": [
21
- "build/types"
22
- ]
23
- }
24
- },
25
- "version": "0.0.5"
18
+ "version": "0.0.7"
26
19
  }
package/src/group.ts CHANGED
@@ -5,7 +5,7 @@ export default (): Group => {
5
5
  return {
6
6
  middleware: [],
7
7
  name: '',
8
- pattern: '',
8
+ path: '',
9
9
  subdomain: ''
10
10
  };
11
11
  };
package/src/index.ts CHANGED
@@ -7,3 +7,4 @@ import url from './url';
7
7
 
8
8
  export default { listener, middleware, redirect, routes, url };
9
9
  export { listener, middleware, redirect, routes, url };
10
+ export * from './types';
@@ -1,11 +1,10 @@
1
- import { Routes } from '~/routes';
2
- import { Next, Request } from '~/types';
1
+ import { Next, Request, Routes } from '~/types';
3
2
 
4
3
 
5
- export default (routes: Routes) => {
4
+ export default ({ routes, subdomains }: Routes) => {
6
5
  return (request: Request, next: Next) => {
7
- let name = (routes.subdomains[request.subdomain] || {})[request.uri],
8
- route = routes.static[name];
6
+ let name = (subdomains[request.subdomain] || {})[request.path],
7
+ route = routes[name];
9
8
 
10
9
  // Dynamic routing
11
10
  if (!route) {
@@ -14,9 +13,8 @@ export default (routes: Routes) => {
14
13
  // - Bind variables to request
15
14
  }
16
15
 
17
- // Fallback route
18
16
  if (!route && routes.fallback) {
19
- route = routes.static[routes.fallback];
17
+ route = routes.fallback;
20
18
  }
21
19
 
22
20
  request.data.route = route;
package/src/redirect.ts CHANGED
@@ -1,4 +1,4 @@
1
- import routes from './routes';
1
+ import { routes } from './routes';
2
2
 
3
3
 
4
4
  export default (key: string, _: Record<string, any> = {}) => {
@@ -8,9 +8,9 @@ export default (key: string, _: Record<string, any> = {}) => {
8
8
  }
9
9
 
10
10
  // Internal route based redirect
11
- if (!routes.static[key]) {
11
+ if (!routes[key]) {
12
12
  throw new Error(`Route '${key}' does not exist`);
13
13
  }
14
14
 
15
- window.location.hash = `#${routes.static[key].pattern}`;
15
+ window.location.hash = `#${routes[key].path || ''}`;
16
16
  };
package/src/routes.ts CHANGED
@@ -1,63 +1,68 @@
1
- import { Group, Responder, Route } from './types';
1
+ import { Group, Responder, Routes, Route } from './types';
2
2
 
3
3
 
4
- class Routes {
5
- dynamic: Record<Route['name'], Route> = {};
6
- fallback?: string;
7
- groups: Group[] = [];
8
- static: Record<Route['name'], Route> = {};
9
- subdomains: Record<Route['subdomain'], Record<Route['pattern'], Route['name']>> = {};
4
+ let groups: Group[] = [],
5
+ routes: Record<Route['name'], Route> = {},
6
+ subdomains: Record<NonNullable<Route['subdomain']>, Record<NonNullable<Route['path']>, Route['name']>> = {};
10
7
 
11
8
 
12
- add(name: string, pattern: string, responder: Responder) {
13
- let route: Route = {
14
- middleware: [],
15
- name: '',
16
- pattern: '',
17
- responder,
18
- subdomain: ''
19
- };
9
+ const add = ({ name, path, responder }: { name: string, path?: string, responder: Responder }) => {
10
+ let http = path !== undefined,
11
+ route: Route = {
12
+ middleware: [],
13
+ name: '',
14
+ responder
15
+ };
20
16
 
21
- for (let i = 0, n = this.groups.length; i < n; i++) {
22
- let group = this.groups[i];
17
+ if (http) {
18
+ route.path = '';
19
+ route.subdomain = '';
20
+ }
23
21
 
24
- route.name += group.name;
25
- route.pattern += group.pattern;
26
- route.subdomain = group.subdomain + route.subdomain;
22
+ for (let i = 0, n = groups.length; i < n; i++) {
23
+ let group = groups[i];
27
24
 
28
- if (group.middleware.length) {
29
- route.middleware.push(...group.middleware);
30
- }
25
+ if (group.middleware.length) {
26
+ route.middleware.push(...group.middleware);
31
27
  }
32
28
 
33
- route.name += name;
34
- route.pattern += pattern;
29
+ route.name += group.name;
35
30
 
36
- if (route.pattern[0] !== '/') {
37
- route.pattern = `/${route.pattern}`;
31
+ if (http) {
32
+ route.path += group.path;
33
+ route.subdomain = group.subdomain + route.subdomain;
38
34
  }
35
+ }
39
36
 
40
- if (route.subdomain === 'www') {
41
- route.subdomain = '';
37
+ routes[ route.name += name ] = route;
38
+
39
+ if (http) {
40
+ route.path = `${route.path}${path}`;
41
+
42
+ if (route.path[0] !== '/') {
43
+ route.path = `/${route.path}`;
42
44
  }
43
45
 
44
- if (!this.subdomains[route.subdomain]) {
45
- this.subdomains[route.subdomain] = {};
46
+ if (!route.subdomain || route.subdomain === 'www') {
47
+ route.subdomain = '';
46
48
  }
47
49
 
48
- this.static[route.name] = route;
49
- this.subdomains[route.subdomain][route.pattern] = route.name;
50
+ if (!subdomains[route.subdomain]) {
51
+ subdomains[route.subdomain] = {};
52
+ }
50
53
 
51
- return route;
54
+ subdomains[route.subdomain][route.path] = route.name;
52
55
  }
53
56
 
54
- group(group: any, routes: (routes: Routes) => void) {
55
- this.groups.push(group);
56
- routes(this);
57
- this.groups.pop();
58
- }
59
- }
57
+ return route;
58
+ };
59
+
60
+ const group = (group: any, fn: (routes: Routes) => void) => {
61
+ groups.push(group);
62
+ fn({ add, group, routes, subdomains });
63
+ groups.pop();
64
+ };
60
65
 
61
66
 
62
- export default new Routes();
63
- export { Routes };
67
+ export default { add, group, routes, subdomains };
68
+ export { add, group, routes, subdomains };
package/src/types.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { Routes } from './routes';
2
1
  import { parse } from './url';
2
+ import routes from './routes';
3
3
 
4
4
 
5
5
  type Group = {
6
6
  middleware: Middleware[];
7
7
  name: string;
8
- pattern: string;
8
+ path: string;
9
9
  subdomain: string;
10
10
  };
11
11
 
@@ -17,12 +17,19 @@ type Request = ReturnType<typeof parse>;
17
17
 
18
18
  type Responder = (request: Request) => Promise<unknown> | unknown;
19
19
 
20
+ type Routes = {
21
+ add: typeof routes.add;
22
+ group: typeof routes.group;
23
+ routes: typeof routes.routes;
24
+ subdomains: typeof routes.subdomains;
25
+ };
26
+
20
27
  type Route = {
21
28
  middleware: Middleware[];
22
29
  name: string;
23
- pattern: string;
30
+ path?: string;
24
31
  responder: Responder;
25
- subdomain: string;
32
+ subdomain?: string;
26
33
  };
27
34
 
28
35
 
package/src/url.ts CHANGED
@@ -1,5 +1,5 @@
1
- const parse = (url: string = window.location.href) => {
2
- let { hash, host, hostname, href, origin, pathname, port, protocol } = new URL( url ),
1
+ const parse = (url: string = window?.location?.href || '') => {
2
+ let { hash, host, hostname, href, origin, port, protocol } = new URL( url ),
3
3
  parts = host.split('.'),
4
4
  path = hash?.replace('#/', '/')?.split('?') || ['/', ''],
5
5
  subdomain = '';
@@ -16,9 +16,8 @@ const parse = (url: string = window.location.href) => {
16
16
  data: {} as Record<string, any>,
17
17
  href: href,
18
18
  hostname: hostname,
19
- uri: path[0],
20
19
  origin: origin,
21
- pathname: pathname,
20
+ path: path[0],
22
21
  port: port,
23
22
  protocol: protocol,
24
23
  query: Object.fromEntries( (new URLSearchParams(path[1])).entries() ),