@esportsplus/routing 0.0.4 → 0.0.6

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: {
@@ -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 data = new URL(url), parts = data.host.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)) {
@@ -8,14 +8,13 @@ const parse = (url = window.location.href) => {
8
8
  }
9
9
  return {
10
10
  data: {},
11
- href: data.href,
12
- hostname: data.hostname,
13
- uri: data.hash.replace('#/', '/') || '/',
14
- origin: data.origin,
15
- pathname: data.pathname,
16
- port: data.port,
17
- protocol: data.protocol,
18
- query: Object.fromEntries(data.searchParams.entries()),
11
+ href: href,
12
+ hostname: hostname,
13
+ origin: origin,
14
+ path: path[0],
15
+ port: port,
16
+ protocol: protocol,
17
+ query: Object.fromEntries((new URLSearchParams(path[1])).entries()),
19
18
  subdomain
20
19
  };
21
20
  };
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.4"
18
+ "version": "0.0.6"
26
19
  }
package/src/group.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { Group } from "./types";
2
-
3
-
4
- export default (): Group => {
5
- return {
6
- middleware: [],
7
- name: '',
8
- pattern: '',
9
- subdomain: ''
10
- };
11
- };
1
+ import { Group } from "./types";
2
+
3
+
4
+ export default (): Group => {
5
+ return {
6
+ middleware: [],
7
+ name: '',
8
+ path: '',
9
+ subdomain: ''
10
+ };
11
+ };
package/src/index.ts CHANGED
@@ -1,9 +1,9 @@
1
- import middleware from './middleware';
2
- import redirect from './redirect';
3
- import listener from './listener';
4
- import routes from './routes';
5
- import url from './url';
6
-
7
-
8
- export default { listener, middleware, redirect, routes, url };
9
- export { listener, middleware, redirect, routes, url };
1
+ import middleware from './middleware';
2
+ import redirect from './redirect';
3
+ import listener from './listener';
4
+ import routes from './routes';
5
+ import url from './url';
6
+
7
+
8
+ export default { listener, middleware, redirect, routes, url };
9
+ export { listener, middleware, redirect, routes, url };
@@ -1,17 +1,17 @@
1
- import { factory } from '~/middleware';
2
- import { Request } from '~/types';
3
-
4
-
5
- export default (request: Request) => {
6
- let route = request.data.route;
7
-
8
- if (!route) {
9
- throw new Error(`Route dispatching failed, route was not defined!`);
10
- }
11
-
12
- if (!route.middleware.length) {
13
- return route.responder(request);
14
- }
15
-
16
- return factory(...route.middleware, (request => route.responder(request)))(request);
17
- };
1
+ import { factory } from '~/middleware';
2
+ import { Request } from '~/types';
3
+
4
+
5
+ export default (request: Request) => {
6
+ let route = request.data.route;
7
+
8
+ if (!route) {
9
+ throw new Error(`Route dispatching failed, route was not defined!`);
10
+ }
11
+
12
+ if (!route.middleware.length) {
13
+ return route.responder(request);
14
+ }
15
+
16
+ return factory(...route.middleware, (request => route.responder(request)))(request);
17
+ };
@@ -1,5 +1,5 @@
1
- import dispatch from './dispatch';
2
- import match from './match';
3
-
4
-
5
- export default { dispatch, match };
1
+ import dispatch from './dispatch';
2
+ import match from './match';
3
+
4
+
5
+ export default { dispatch, match };
@@ -1,26 +1,24 @@
1
- import { Routes } from '~/routes';
2
- import { Next, Request } from '~/types';
3
-
4
-
5
- export default (routes: Routes) => {
6
- return (request: Request, next: Next) => {
7
- let name = (routes.subdomains[request.subdomain] || {})[request.uri],
8
- route = routes.static[name];
9
-
10
- // Dynamic routing
11
- if (!route) {
12
- // TODO:
13
- // - Trie based routing
14
- // - Bind variables to request
15
- }
16
-
17
- // Fallback route
18
- if (!route && routes.fallback) {
19
- route = routes.static[routes.fallback];
20
- }
21
-
22
- request.data.route = route;
23
-
24
- return next(request);
25
- };
26
- };
1
+ import { Next, Request, Routes } from '~/types';
2
+
3
+
4
+ export default ({ routes, subdomains }: Routes) => {
5
+ return (request: Request, next: Next) => {
6
+ let name = (subdomains[request.subdomain] || {})[request.path],
7
+ route = routes[name];
8
+
9
+ // Dynamic routing
10
+ if (!route) {
11
+ // TODO:
12
+ // - Trie based routing
13
+ // - Bind variables to request
14
+ }
15
+
16
+ if (!route && routes.fallback) {
17
+ route = routes.fallback;
18
+ }
19
+
20
+ request.data.route = route;
21
+
22
+ return next(request);
23
+ };
24
+ };
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';
2
-
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']>> = {};
10
-
11
-
12
- add(name: string, pattern: string, responder: Responder) {
13
- let route: Route = {
14
- middleware: [],
15
- name: '',
16
- pattern: '',
17
- responder,
18
- subdomain: ''
19
- };
20
-
21
- for (let i = 0, n = this.groups.length; i < n; i++) {
22
- let group = this.groups[i];
23
-
24
- route.name += group.name;
25
- route.pattern += group.pattern;
26
- route.subdomain = group.subdomain + route.subdomain;
27
-
28
- if (group.middleware.length) {
29
- route.middleware.push(...group.middleware);
30
- }
31
- }
32
-
33
- route.name += name;
34
- route.pattern += pattern;
35
-
36
- if (route.pattern[0] !== '/') {
37
- route.pattern = `/${route.pattern}`;
38
- }
39
-
40
- if (route.subdomain === 'www') {
41
- route.subdomain = '';
42
- }
43
-
44
- if (!this.subdomains[route.subdomain]) {
45
- this.subdomains[route.subdomain] = {};
46
- }
47
-
48
- this.static[route.name] = route;
49
- this.subdomains[route.subdomain][route.pattern] = route.name;
50
-
51
- return route;
52
- }
53
-
54
- group(group: any, routes: (routes: Routes) => void) {
55
- this.groups.push(group);
56
- routes(this);
57
- this.groups.pop();
58
- }
59
- }
60
-
61
-
62
- export default new Routes();
63
- export { Routes };
1
+ import { Group, Responder, Routes, Route } from './types';
2
+
3
+
4
+ let groups: Group[] = [],
5
+ routes: Record<Route['name'], Route> = {},
6
+ subdomains: Record<NonNullable<Route['subdomain']>, Record<NonNullable<Route['path']>, Route['name']>> = {};
7
+
8
+
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
+ };
16
+
17
+ if (http) {
18
+ route.path = '';
19
+ route.subdomain = '';
20
+ }
21
+
22
+ for (let i = 0, n = groups.length; i < n; i++) {
23
+ let group = groups[i];
24
+
25
+ if (group.middleware.length) {
26
+ route.middleware.push(...group.middleware);
27
+ }
28
+
29
+ route.name += group.name;
30
+
31
+ if (http) {
32
+ route.path += group.path;
33
+ route.subdomain = group.subdomain + route.subdomain;
34
+ }
35
+ }
36
+
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}`;
44
+ }
45
+
46
+ if (!route.subdomain || route.subdomain === 'www') {
47
+ route.subdomain = '';
48
+ }
49
+
50
+ if (!subdomains[route.subdomain]) {
51
+ subdomains[route.subdomain] = {};
52
+ }
53
+
54
+ subdomains[route.subdomain][route.path] = route.name;
55
+ }
56
+
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
+ };
65
+
66
+
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,6 +1,7 @@
1
- const parse = (url: string = window.location.href) => {
2
- let data = new URL( url ),
3
- parts = data.host.split('.'),
1
+ const parse = (url: string = window?.location?.href || '') => {
2
+ let { hash, host, hostname, href, origin, port, protocol } = new URL( url ),
3
+ parts = host.split('.'),
4
+ path = hash?.replace('#/', '/')?.split('?') || ['/', ''],
4
5
  subdomain = '';
5
6
 
6
7
  if (parts.length > 2) {
@@ -13,14 +14,13 @@ const parse = (url: string = window.location.href) => {
13
14
 
14
15
  return {
15
16
  data: {} as Record<string, any>,
16
- href: data.href,
17
- hostname: data.hostname,
18
- uri: data.hash.replace('#/', '/') || '/',
19
- origin: data.origin,
20
- pathname: data.pathname,
21
- port: data.port,
22
- protocol: data.protocol,
23
- query: Object.fromEntries( data.searchParams.entries() ),
17
+ href: href,
18
+ hostname: hostname,
19
+ origin: origin,
20
+ path: path[0],
21
+ port: port,
22
+ protocol: protocol,
23
+ query: Object.fromEntries( (new URLSearchParams(path[1])).entries() ),
24
24
  subdomain
25
25
  };
26
26
  };
@@ -1,3 +0,0 @@
1
- import { Request } from '../types';
2
- declare const _default: (request: Request) => any;
3
- export default _default;
@@ -1,11 +0,0 @@
1
- import { middleware } from '../request';
2
- export default (request) => {
3
- let route = request.data.route;
4
- if (!route) {
5
- throw new Error(`Route dispatching failed, route was not defined!`);
6
- }
7
- if (!route.middleware.length) {
8
- return route.responder(request);
9
- }
10
- return middleware(...route.middleware, (request => route.responder(request)))(request);
11
- };
@@ -1,4 +0,0 @@
1
- import { Routes } from '../routes';
2
- import { Next, Request } from '../types';
3
- declare const _default: (routes: Routes) => (request: Request, next: Next) => unknown;
4
- export default _default;
@@ -1,12 +0,0 @@
1
- export default (routes) => {
2
- return (request, next) => {
3
- let name = (routes.subdomains[request.subdomain] || {})[request.uri], route = routes.static[name];
4
- if (!route) {
5
- }
6
- if (!route && routes.fallback) {
7
- route = routes.static[routes.fallback];
8
- }
9
- request.data.route = route;
10
- return next(request);
11
- };
12
- };
@@ -1,34 +0,0 @@
1
- import middleware from './middleware';
2
- import parse from './parse';
3
- declare const _default: {
4
- middleware: (...middleware: import("./types").Middleware[]) => (request: {
5
- data: Record<string, any>;
6
- href: string;
7
- hostname: string;
8
- uri: string;
9
- origin: string;
10
- pathname: string;
11
- port: string;
12
- protocol: string;
13
- query: {
14
- [k: string]: string;
15
- };
16
- subdomain: string;
17
- }) => Promise<unknown>;
18
- parse: (url?: string) => {
19
- data: Record<string, any>;
20
- href: string;
21
- hostname: string;
22
- uri: string;
23
- origin: string;
24
- pathname: string;
25
- port: string;
26
- protocol: string;
27
- query: {
28
- [k: string]: string;
29
- };
30
- subdomain: string;
31
- };
32
- };
33
- export default _default;
34
- export { middleware, parse };
@@ -1,4 +0,0 @@
1
- import middleware from './middleware';
2
- import parse from './parse';
3
- export default { middleware, parse };
4
- export { middleware, parse };
@@ -1,3 +0,0 @@
1
- import { Middleware, Request } from './types';
2
- declare const _default: (...middleware: Middleware[]) => (request: Request) => Promise<unknown>;
3
- export default _default;
@@ -1,12 +0,0 @@
1
- export default (...middleware) => {
2
- let i = -1, n = middleware.length, next = (request) => middleware[++i](request, (i < n ? next : () => {
3
- throw new Error('Request middleware did not return a responder');
4
- }));
5
- return async (request) => {
6
- if (!middleware.length) {
7
- throw new Error('Request middleware has not been defined');
8
- }
9
- i = -1;
10
- return await next(request);
11
- };
12
- };
@@ -1,15 +0,0 @@
1
- declare const _default: (url?: string) => {
2
- data: Record<string, any>;
3
- href: string;
4
- hostname: string;
5
- uri: string;
6
- origin: string;
7
- pathname: string;
8
- port: string;
9
- protocol: string;
10
- query: {
11
- [k: string]: string;
12
- };
13
- subdomain: string;
14
- };
15
- export default _default;
@@ -1,18 +0,0 @@
1
- export default (url = window.location.href) => {
2
- let data = new URL(url), subdomain = data.host.split('.')[0] || '';
3
- if (['127', 'www'].includes(`${subdomain}`)) {
4
- subdomain = '';
5
- }
6
- return {
7
- data: {},
8
- href: data.href,
9
- hostname: data.hostname,
10
- uri: data.hash.replace('#/', '/') || '/',
11
- origin: data.origin,
12
- pathname: data.pathname,
13
- port: data.port,
14
- protocol: data.protocol,
15
- query: Object.fromEntries(data.searchParams.entries()),
16
- subdomain
17
- };
18
- };
@@ -1,6 +0,0 @@
1
- import parse from './parse';
2
- type Middleware = (request: Request, next: Next) => unknown;
3
- type Next = (request: Request) => unknown;
4
- type Request = ReturnType<typeof parse>;
5
- type Responder = (request: Request) => Promise<unknown> | unknown;
6
- export { Middleware, Next, Request, Responder };
@@ -1 +0,0 @@
1
- export {};