@esportsplus/routing 0.0.10 → 0.0.11

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,24 +1,17 @@
1
+ import factory from '@esportsplus/middleware';
1
2
  import dispatch from './dispatch';
2
- import factory from './factory';
3
3
  import match from './match';
4
4
  declare const _default: {
5
5
  dispatch: (request: {
6
6
  data: {
7
7
  parameters?: Record<PropertyKey, unknown> | undefined;
8
- route?: import("../router").Route | undefined;
8
+ route?: import("../router/route").Route | undefined;
9
9
  };
10
10
  }) => unknown;
11
- factory: (...middleware: import("../types").Middleware[]) => <T>(request: T) => unknown;
12
- match: (router: import("../router").Router, spa?: boolean) => (request: {
13
- data: {
14
- parameters?: Record<PropertyKey, unknown> | undefined;
15
- route?: import("../router").Route | undefined;
16
- };
17
- hostname: string;
18
- method: string;
19
- path: string;
20
- subdomain?: string | undefined;
21
- }, next: import("../types").Next) => unknown;
11
+ factory: <I, R>(...middleware: import("@esportsplus/middleware/build/types").Middleware<I, R>[]) => import("@esportsplus/middleware/build/types").Next<I, R>;
12
+ match: (router: import("../router").Router, { spa }?: {
13
+ spa?: boolean | undefined;
14
+ }) => (request: import("../types").Request, next: import("@esportsplus/middleware/build/types").Next<import("../types").Request, unknown>) => unknown;
22
15
  };
23
16
  export default _default;
24
17
  export { dispatch, factory, match };
@@ -1,5 +1,5 @@
1
+ import factory from '@esportsplus/middleware';
1
2
  import dispatch from './dispatch';
2
- import factory from './factory';
3
3
  import match from './match';
4
4
  export default { dispatch, factory, match };
5
5
  export { dispatch, factory, match };
@@ -1,10 +1,5 @@
1
- import { Next, Router } from '../types';
2
- type Request = {
3
- data: ReturnType<Router['match']>;
4
- hostname: string;
5
- method: string;
6
- path: string;
7
- subdomain?: string;
8
- };
9
- declare const _default: (router: Router, spa?: boolean) => (request: Request, next: Next) => unknown;
1
+ import { Next, Request, Router } from '../types';
2
+ declare const _default: (router: Router, { spa }?: {
3
+ spa?: boolean | undefined;
4
+ }) => (request: Request, next: Next<Request, unknown>) => unknown;
10
5
  export default _default;
@@ -1,4 +1,4 @@
1
- export default (router, spa = false) => {
1
+ export default (router, { spa } = {}) => {
2
2
  let subdomain = null;
3
3
  return (request, next) => {
4
4
  if ((typeof request.subdomain !== 'string' && !spa) || subdomain === null) {
@@ -1,16 +1,6 @@
1
- import { Middleware, Responder, Options } from '../types';
1
+ import { Options } from '../types';
2
2
  import { Node } from './node';
3
- import factory from '../middleware/factory';
4
- declare class Route {
5
- dispatch: ReturnType<typeof factory> | null;
6
- name: string | null;
7
- path: string | null;
8
- responder: Responder;
9
- stack: Middleware[] | null;
10
- subdomain: string | null;
11
- constructor(responder: Responder);
12
- get dispatcher(): <T>(request: T) => unknown;
13
- }
3
+ import { Route } from './route';
14
4
  declare class Router {
15
5
  groups: Omit<Options, 'responder'>[];
16
6
  root: Node;
@@ -1,7 +1,7 @@
1
1
  import { STATIC } from "../symbols";
2
2
  import { Node } from './node';
3
3
  import { normalize, radixkey } from './path';
4
- import factory from '../middleware/factory';
4
+ import { Route } from './route';
5
5
  let { isArray } = Array;
6
6
  function set(route, key, value) {
7
7
  if (!value) {
@@ -21,28 +21,6 @@ function set(route, key, value) {
21
21
  }
22
22
  }
23
23
  }
24
- class Route {
25
- dispatch = null;
26
- name = null;
27
- path = null;
28
- responder;
29
- stack = null;
30
- subdomain = null;
31
- constructor(responder) {
32
- this.responder = responder;
33
- }
34
- get dispatcher() {
35
- if (this.dispatch === null) {
36
- if (!this.stack?.length) {
37
- this.dispatch = (request) => this.responder(request);
38
- }
39
- else {
40
- this.dispatch = factory(...this.stack, (request => this.responder(request)));
41
- }
42
- }
43
- return this.dispatch;
44
- }
45
- }
46
24
  class Router {
47
25
  groups = [];
48
26
  root;
@@ -178,5 +156,5 @@ class Router {
178
156
  return resolved.join('/');
179
157
  }
180
158
  }
181
- export default (...args) => new Router(...args);
159
+ export default () => new Router();
182
160
  export { Router, Route };
@@ -3,12 +3,4 @@ declare const radixkey: (path: string, { method, subdomain }?: {
3
3
  method?: string | null | undefined;
4
4
  subdomain?: string | null | undefined;
5
5
  }) => string;
6
- declare const _default: {
7
- normalize: (path: string) => string;
8
- radixkey: (path: string, { method, subdomain }?: {
9
- method?: string | null | undefined;
10
- subdomain?: string | null | undefined;
11
- }) => string;
12
- };
13
- export default _default;
14
6
  export { normalize, radixkey };
@@ -1,6 +1,6 @@
1
1
  const normalize = (path) => {
2
2
  if (path[0] !== '/') {
3
- path = `/${path}`;
3
+ path = '/' + path;
4
4
  }
5
5
  if (path.endsWith('/')) {
6
6
  path = path.slice(0, -1);
@@ -17,5 +17,4 @@ const radixkey = (path, { method, subdomain } = {}) => {
17
17
  }
18
18
  return prefix.toUpperCase() + normalize(path);
19
19
  };
20
- export default { normalize, radixkey };
21
20
  export { normalize, radixkey };
@@ -0,0 +1,13 @@
1
+ import { Middleware, Responder } from '../types';
2
+ import { factory } from '../middleware';
3
+ declare class Route {
4
+ dispatch: ReturnType<typeof factory> | null;
5
+ name: string | null;
6
+ path: string | null;
7
+ responder: Responder;
8
+ stack: Middleware<unknown, unknown>[] | null;
9
+ subdomain: string | null;
10
+ constructor(responder: Responder);
11
+ get dispatcher(): import("@esportsplus/middleware/build/types").Next<unknown, unknown>;
12
+ }
13
+ export { Route };
@@ -0,0 +1,24 @@
1
+ import { factory } from '../middleware';
2
+ class Route {
3
+ dispatch = null;
4
+ name = null;
5
+ path = null;
6
+ responder;
7
+ stack = null;
8
+ subdomain = null;
9
+ constructor(responder) {
10
+ this.responder = responder;
11
+ }
12
+ get dispatcher() {
13
+ if (this.dispatch === null) {
14
+ if (!this.stack?.length) {
15
+ this.dispatch = (request) => this.responder(request);
16
+ }
17
+ else {
18
+ this.dispatch = factory(...this.stack, (request => this.responder(request)));
19
+ }
20
+ }
21
+ return this.dispatch;
22
+ }
23
+ }
24
+ export { Route };
package/build/types.d.ts CHANGED
@@ -1,12 +1,18 @@
1
+ import { Middleware, Next } from '@esportsplus/middleware';
1
2
  import { Route, Router } from './router';
2
- type Middleware = <T>(request: T, next: Next) => unknown;
3
- type Next = <T>(request: T) => unknown;
4
3
  type Options = {
5
- middleware?: Middleware[];
4
+ middleware?: Middleware<Request, unknown>[];
6
5
  name?: string;
7
6
  path?: string;
8
7
  responder: Responder;
9
8
  subdomain?: string;
10
9
  };
10
+ type Request = {
11
+ data: ReturnType<Router['match']>;
12
+ hostname: string;
13
+ method: string;
14
+ path: string;
15
+ subdomain?: string;
16
+ };
11
17
  type Responder = <T>(request: T) => Promise<unknown> | unknown;
12
- export { Middleware, Next, Options, Responder, Route, Router };
18
+ export { Middleware, Next, Options, Request, Responder, Route, Router };
package/package.json CHANGED
@@ -1,6 +1,5 @@
1
1
  {
2
2
  "author": "ICJR",
3
- "description": "Routing",
4
3
  "devDependencies": {
5
4
  "@esportsplus/rspack": "^0.0.15"
6
5
  },
@@ -14,5 +13,8 @@
14
13
  "prepublishOnly": "npm run build"
15
14
  },
16
15
  "types": "./build/index.d.ts",
17
- "version": "0.0.10"
16
+ "version": "0.0.11",
17
+ "dependencies": {
18
+ "@esportsplus/middleware": "^0.0.4"
19
+ }
18
20
  }
@@ -1,5 +1,5 @@
1
+ import factory from '@esportsplus/middleware';
1
2
  import dispatch from './dispatch';
2
- import factory from './factory';
3
3
  import match from './match';
4
4
 
5
5
 
@@ -1,19 +1,10 @@
1
- import { Next, Router } from '~/types';
1
+ import { Next, Request, Router } from '~/types';
2
2
 
3
3
 
4
- type Request = {
5
- data: ReturnType<Router['match']>;
6
- hostname: string;
7
- method: string;
8
- path: string;
9
- subdomain?: string;
10
- };
11
-
12
-
13
- export default (router: Router, spa = false) => {
4
+ export default (router: Router, { spa }: { spa?: boolean } = {}) => {
14
5
  let subdomain: string | null = null;
15
6
 
16
- return (request: Request, next: Next) => {
7
+ return (request: Request, next: Next<Request, unknown>) => {
17
8
  if ((typeof request.subdomain !== 'string' && !spa) || subdomain === null) {
18
9
  if (router.subdomains) {
19
10
  for (let i = 0, n = router.subdomains.length; i < n; i++) {
@@ -1,8 +1,8 @@
1
1
  import { STATIC } from "~/symbols";
2
- import { Middleware, Responder, Options } from '~/types';
2
+ import { Options } from '~/types';
3
3
  import { Node } from './node';
4
4
  import { normalize, radixkey } from './path';
5
- import factory from '~/middleware/factory';
5
+ import { Route } from './route';
6
6
 
7
7
 
8
8
  let { isArray } = Array;
@@ -29,34 +29,6 @@ function set(route: Route, key: keyof Route, value?: any) {
29
29
  }
30
30
 
31
31
 
32
- class Route {
33
- dispatch: ReturnType<typeof factory> | null = null;
34
- name: string | null = null;
35
- path: string | null = null;
36
- responder: Responder;
37
- stack: Middleware[] | null = null;
38
- subdomain: string | null = null;
39
-
40
-
41
- constructor(responder: Responder) {
42
- this.responder = responder;
43
- }
44
-
45
-
46
- get dispatcher() {
47
- if (this.dispatch === null) {
48
- if (!this.stack?.length) {
49
- this.dispatch = <T>(request: T) => this.responder(request);
50
- }
51
- else {
52
- this.dispatch = factory(...this.stack, (request => this.responder(request)));
53
- }
54
- }
55
-
56
- return this.dispatch;
57
- }
58
- }
59
-
60
32
  class Router {
61
33
  groups: Omit<Options, 'responder'>[] = [];
62
34
  root: Node;
@@ -233,5 +205,5 @@ class Router {
233
205
  }
234
206
 
235
207
 
236
- export default (...args: ConstructorParameters<typeof Router>) => new Router(...args);
208
+ export default () => new Router();
237
209
  export { Router, Route };
@@ -1,6 +1,6 @@
1
1
  const normalize = (path: string) => {
2
2
  if (path[0] !== '/') {
3
- path = `/${path}`;
3
+ path = '/' + path;
4
4
  }
5
5
 
6
6
  if (path.endsWith('/')) {
@@ -25,5 +25,4 @@ const radixkey = (path: string, { method, subdomain }: { method?: string | null;
25
25
  };
26
26
 
27
27
 
28
- export default { normalize, radixkey };
29
28
  export { normalize, radixkey };
@@ -0,0 +1,34 @@
1
+ import { Middleware, Responder } from '~/types';
2
+ import { factory } from '~/middleware';
3
+
4
+
5
+ class Route {
6
+ dispatch: ReturnType<typeof factory> | null = null;
7
+ name: string | null = null;
8
+ path: string | null = null;
9
+ responder: Responder;
10
+ stack: Middleware<unknown, unknown>[] | null = null;
11
+ subdomain: string | null = null;
12
+
13
+
14
+ constructor(responder: Responder) {
15
+ this.responder = responder;
16
+ }
17
+
18
+
19
+ get dispatcher() {
20
+ if (this.dispatch === null) {
21
+ if (!this.stack?.length) {
22
+ this.dispatch = <T>(request: T) => this.responder(request);
23
+ }
24
+ else {
25
+ this.dispatch = factory(...this.stack, (request => this.responder(request)));
26
+ }
27
+ }
28
+
29
+ return this.dispatch;
30
+ }
31
+ }
32
+
33
+
34
+ export { Route };
package/src/types.ts CHANGED
@@ -1,19 +1,24 @@
1
+ import { Middleware, Next } from '@esportsplus/middleware';
1
2
  import { Route, Router } from './router';
2
3
 
3
4
 
4
- type Middleware = <T>(request: T, next: Next) => unknown;
5
-
6
- type Next = <T>(request: T) => unknown;
7
-
8
5
  type Options = {
9
- middleware?: Middleware[];
6
+ middleware?: Middleware<Request, unknown>[];
10
7
  name?: string;
11
8
  path?: string;
12
9
  responder: Responder;
13
10
  subdomain?: string;
14
11
  };
15
12
 
13
+ type Request = {
14
+ data: ReturnType<Router['match']>;
15
+ hostname: string;
16
+ method: string;
17
+ path: string;
18
+ subdomain?: string;
19
+ };
20
+
16
21
  type Responder = <T>(request: T) => Promise<unknown> | unknown;
17
22
 
18
23
 
19
- export { Middleware, Next, Options, Responder, Route, Router };
24
+ export { Middleware, Next, Options, Request, Responder, Route, Router };
@@ -1,22 +0,0 @@
1
- import { Middleware, Next } from '~/types';
2
-
3
-
4
- function error() {
5
- throw new Error('Routing: request middleware did not return a responder');
6
- }
7
-
8
-
9
- // TODO: Use '@esportsplus/middleware'
10
- export default (...middleware: Middleware[]) => {
11
- let stack: Next[] = [];
12
-
13
- for (let i = 0, n = middleware.length; i < n; i++) {
14
- stack[i] = <T>(request: T) => middleware[i](request, stack[i + 1] || error);
15
- }
16
-
17
- if (!stack.length) {
18
- throw new Error('Routing: request middleware has not been defined');
19
- }
20
-
21
- return <T>(request: T) => stack[0](request);
22
- };