@esportsplus/routing 0.6.7 → 0.7.2

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.
@@ -5,7 +5,7 @@ declare const router: <const Factories extends readonly RouteFactory<any>[]>(...
5
5
  back: typeof back;
6
6
  forward: typeof forward;
7
7
  middleware: {
8
- (...stages: Middleware<InferOutput<Factories[number]>>[]): InferOutput<Factories[number]>;
8
+ (...middleware: Middleware<InferOutput<Factories[number]>>[]): InferOutput<Factories[number]>;
9
9
  dispatch(request: Request<InferOutput<Factories[number]>>): InferOutput<Factories[number]>;
10
10
  match(fallback: Route<InferOutput<Factories[number]>>): (request: Request<InferOutput<Factories[number]>>, next: Next<InferOutput<Factories[number]>>) => InferOutput<Factories[number]>;
11
11
  };
@@ -1,33 +1,40 @@
1
- import * as reactivity_133ba2c86bd54849af03b85c9ce9b48a0 from '@esportsplus/reactivity';
1
+ import * as reactivity_ffa278ace0bb4e959ff55da8933387190 from '@esportsplus/reactivity';
2
2
  import { effect, root } from '@esportsplus/reactivity';
3
3
  import { Router } from './router/index.js';
4
- import pipeline from '@esportsplus/pipeline';
5
4
  import { PACKAGE_NAME } from './constants.js';
6
- class ReactiveObject_133ba2c86bd54849af03b85c9ce9b48a1 extends reactivity_133ba2c86bd54849af03b85c9ce9b48a0.ReactiveObject {
5
+ class ReactiveObject_ffa278ace0bb4e959ff55da8933387191 extends reactivity_ffa278ace0bb4e959ff55da8933387190.ReactiveObject {
7
6
  #parameters;
8
7
  #route;
9
8
  constructor(_p0, _p1) {
10
9
  super(null);
11
- this.#parameters = this[reactivity_133ba2c86bd54849af03b85c9ce9b48a0.SIGNAL](_p0);
12
- this.#route = this[reactivity_133ba2c86bd54849af03b85c9ce9b48a0.SIGNAL](_p1);
10
+ this.#parameters = this[reactivity_ffa278ace0bb4e959ff55da8933387190.SIGNAL](_p0);
11
+ this.#route = this[reactivity_ffa278ace0bb4e959ff55da8933387190.SIGNAL](_p1);
13
12
  }
14
13
  get parameters() {
15
- return reactivity_133ba2c86bd54849af03b85c9ce9b48a0.read(this.#parameters);
14
+ return reactivity_ffa278ace0bb4e959ff55da8933387190.read(this.#parameters);
16
15
  }
17
16
  set parameters(_v0) {
18
- reactivity_133ba2c86bd54849af03b85c9ce9b48a0.write(this.#parameters, _v0);
17
+ reactivity_ffa278ace0bb4e959ff55da8933387190.write(this.#parameters, _v0);
19
18
  }
20
19
  get route() {
21
- return reactivity_133ba2c86bd54849af03b85c9ce9b48a0.read(this.#route);
20
+ return reactivity_ffa278ace0bb4e959ff55da8933387190.read(this.#route);
22
21
  }
23
22
  set route(_v1) {
24
- reactivity_133ba2c86bd54849af03b85c9ce9b48a0.write(this.#route, _v1);
23
+ reactivity_ffa278ace0bb4e959ff55da8933387190.write(this.#route, _v1);
25
24
  }
26
25
  }
27
26
  let cache = [], location = window.location;
28
27
  function back() {
29
28
  window.history.back();
30
29
  }
30
+ function build(stages) {
31
+ let chain = () => { throw new Error(`${PACKAGE_NAME}: final stage did not return a value`); };
32
+ for (let i = stages.length - 1; i >= 0; i--) {
33
+ let next = chain, stage = stages[i];
34
+ chain = (input) => stage(input, next);
35
+ }
36
+ return chain;
37
+ }
31
38
  function forward() {
32
39
  window.history.forward();
33
40
  }
@@ -64,12 +71,12 @@ function match(request, router, subdomain) {
64
71
  return router.match(request.method, request.path, subdomain || '');
65
72
  }
66
73
  function middleware(request, router) {
67
- let middleware = pipeline();
68
- function host(...stages) {
69
- for (let i = 0, n = stages.length; i < n; i++) {
70
- middleware.add(stages[i]);
74
+ let stages = [];
75
+ function host(...middleware) {
76
+ for (let i = 0, n = middleware.length; i < n; i++) {
77
+ stages.push(middleware[i]);
71
78
  }
72
- return middleware.dispatch(request);
79
+ return build(stages)(request);
73
80
  }
74
81
  ;
75
82
  host.dispatch = (request) => {
@@ -77,10 +84,13 @@ function middleware(request, router) {
77
84
  if (route === undefined) {
78
85
  throw new Error(`${PACKAGE_NAME}: route is undefined!`);
79
86
  }
80
- return route.pipeline.dispatch(request);
87
+ if (typeof route.middleware !== 'function') {
88
+ route.middleware = build(route.middleware);
89
+ }
90
+ return route.middleware(request);
81
91
  };
82
92
  host.match = (fallback) => {
83
- let state = new ReactiveObject_133ba2c86bd54849af03b85c9ce9b48a1(undefined, undefined);
93
+ let state = new ReactiveObject_ffa278ace0bb4e959ff55da8933387191(undefined, undefined);
84
94
  if (fallback === undefined) {
85
95
  throw new Error(`${PACKAGE_NAME}: fallback route does not exist`);
86
96
  }
@@ -120,7 +130,7 @@ function onpopstate() {
120
130
  }
121
131
  }
122
132
  const router = (...factories) => {
123
- let instance = factories.reduce((r, factory) => factory(r), new Router()), request = reactivity_133ba2c86bd54849af03b85c9ce9b48a0.reactive(Object.assign(href(), { data: {} }));
133
+ let instance = factories.reduce((r, factory) => factory(r), new Router()), request = reactivity_ffa278ace0bb4e959ff55da8933387190.reactive(Object.assign(href(), { data: {} }));
124
134
  if (cache.push(request) === 1) {
125
135
  window.addEventListener('hashchange', onpopstate);
126
136
  }
@@ -1,6 +1,5 @@
1
1
  import { ON_DELETE, ON_GET, ON_POST, ON_PUT, PACKAGE_NAME } from '../constants.js';
2
2
  import { Node } from './node.js';
3
- import pipeline from '@esportsplus/pipeline';
4
3
  function key(method, subdomain) {
5
4
  return (method + (subdomain ? ' ' + subdomain : '')).toUpperCase();
6
5
  }
@@ -16,21 +15,24 @@ function normalize(path) {
16
15
  return path || '/';
17
16
  }
18
17
  function set(route, options) {
19
- let pipeline = route.pipeline;
20
- for (let key in options) {
21
- let value = options[key];
22
- if (key === 'middleware') {
23
- for (let i = 0, n = value.length; i < n; i++) {
24
- pipeline.add(value[i]);
25
- }
26
- }
27
- else if (key === 'responder') {
28
- pipeline.add(value);
29
- }
30
- else {
31
- route[key] = (route[key] || '') + value;
18
+ let middleware = route.middleware;
19
+ if (options.middleware) {
20
+ for (let i = 0, n = options.middleware.length; i < n; i++) {
21
+ middleware.push(options.middleware[i]);
32
22
  }
33
23
  }
24
+ if ('responder' in options) {
25
+ middleware.push(options.responder);
26
+ }
27
+ if (options.name) {
28
+ route.name = (route.name || '') + options.name;
29
+ }
30
+ if (options.path) {
31
+ route.path = (route.path || '') + options.path;
32
+ }
33
+ if (options.subdomain) {
34
+ route.subdomain = (route.subdomain || '') + options.subdomain;
35
+ }
34
36
  }
35
37
  class Router {
36
38
  bucket = {};
@@ -57,7 +59,7 @@ class Router {
57
59
  let groups = this.groups, route = {
58
60
  name: null,
59
61
  path: null,
60
- pipeline: pipeline(),
62
+ middleware: [],
61
63
  subdomain: null
62
64
  };
63
65
  for (let i = 0, n = groups.length; i < n; i++) {
@@ -1,6 +1,5 @@
1
1
  import { NeverAsync } from '@esportsplus/utilities';
2
2
  import { Router } from './router/index.js';
3
- import pipeline from '@esportsplus/pipeline';
4
3
  type AccumulateRoutes<T extends readonly RouteFactory<any>[]> = T extends readonly [infer F extends RouteFactory<any>, ...infer Rest extends readonly RouteFactory<any>[]] ? ExtractRoutes<F> & AccumulateRoutes<Rest> : {};
5
4
  type ExtractOptionalParams<Path extends string> = Path extends `/${infer Segment}/${infer Rest}` ? (Segment extends `?:${infer Param}` ? Param : never) | ExtractOptionalParams<`/${Rest}`> : Path extends `/${infer Segment}` ? (Segment extends `?:${infer Param}` ? Param : never) : never;
6
5
  type ExtractParamsTuple<Path extends string> = Path extends `${infer _Start}:${infer Param}/${infer Rest}` ? [Param, ...ExtractParamsTuple<`/${Rest}`>] : Path extends `${infer _Start}:${infer Param}` ? [Param] : [];
@@ -38,7 +37,7 @@ type Request<T> = {
38
37
  type Route<T> = {
39
38
  name: Name | null;
40
39
  path: string | null;
41
- pipeline: ReturnType<typeof pipeline<Request<T>, T>>;
40
+ middleware: Middleware<T>[] | Next<T>;
42
41
  subdomain: string | null;
43
42
  };
44
43
  type RouteFactory<T> = (router: Router<T, any>) => Router<T, RouteRegistry>;
package/package.json CHANGED
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "author": "ICJR",
3
3
  "dependencies": {
4
- "@esportsplus/pipeline": "^1.2.2",
5
4
  "@esportsplus/reactivity": "^0.30.2",
6
5
  "@esportsplus/typescript": "^0.29.0",
7
6
  "@esportsplus/utilities": "^0.27.2"
@@ -29,7 +28,7 @@
29
28
  },
30
29
  "type": "module",
31
30
  "types": "./build/index.d.ts",
32
- "version": "0.6.7",
31
+ "version": "0.7.2",
33
32
  "scripts": {
34
33
  "build": "tsc",
35
34
  "build:test": "vite build --config test/vite.config.ts",
@@ -1,7 +1,6 @@
1
1
  import { effect, reactive, root } from '@esportsplus/reactivity';
2
2
  import { AccumulateRoutes, ExtractOptionalParams, ExtractRequiredParams, InferOutput, Middleware, Next, PathParamsObject, Request, Route, RouteFactory, RoutePath } from './types';
3
3
  import { Router } from './router';
4
- import pipeline from '@esportsplus/pipeline';
5
4
  import { PACKAGE_NAME } from './constants';
6
5
 
7
6
 
@@ -13,6 +12,19 @@ function back() {
13
12
  window.history.back();
14
13
  }
15
14
 
15
+ function build<T>(stages: Middleware<T>[]): Next<T> {
16
+ let chain: Next<T> = () => { throw new Error(`${PACKAGE_NAME}: final stage did not return a value`); };
17
+
18
+ for (let i = stages.length - 1; i >= 0; i--) {
19
+ let next = chain,
20
+ stage = stages[i];
21
+
22
+ chain = (input) => stage(input, next);
23
+ }
24
+
25
+ return chain;
26
+ }
27
+
16
28
  function forward() {
17
29
  window.history.forward();
18
30
  }
@@ -62,24 +74,28 @@ function match<T>(request: Request<T>, router: Router<T>, subdomain?: string) {
62
74
  }
63
75
 
64
76
  function middleware<T>(request: Request<T>, router: Router<T>) {
65
- let middleware = pipeline<Request<T>, T>();
77
+ let stages: Middleware<T>[] = [];
66
78
 
67
- function host(...stages: Middleware<T>[]) {
68
- for (let i = 0, n = stages.length; i < n; i++) {
69
- middleware.add( stages[i] );
79
+ function host(...middleware: Middleware<T>[]) {
80
+ for (let i = 0, n = middleware.length; i < n; i++) {
81
+ stages.push( middleware[i] );
70
82
  }
71
83
 
72
- return middleware.dispatch(request) as T;
84
+ return build(stages)(request);
73
85
  };
74
86
 
75
87
  host.dispatch = (request: Request<T>) => {
76
- let { route } = request.data;
88
+ let { route } = request.data as { route: Route<T> | undefined };
77
89
 
78
90
  if (route === undefined) {
79
91
  throw new Error(`${PACKAGE_NAME}: route is undefined!`);
80
92
  }
81
93
 
82
- return route.pipeline.dispatch(request);
94
+ if (typeof route.middleware !== 'function') {
95
+ route.middleware = build(route.middleware);
96
+ }
97
+
98
+ return route.middleware(request);
83
99
  };
84
100
 
85
101
  host.match = (fallback: Route<T>) => {
@@ -133,8 +149,7 @@ function onpopstate() {
133
149
  let state = cache[i];
134
150
 
135
151
  for (let key in values) {
136
- // @ts-ignore
137
- state[key] = values[key];
152
+ (state as Record<string, unknown>)[key] = (values as Record<string, unknown>)[key];
138
153
  }
139
154
  }
140
155
  }
@@ -1,7 +1,6 @@
1
1
  import { ON_DELETE, ON_GET, ON_POST, ON_PUT, PACKAGE_NAME } from '../constants';
2
- import { Route, Name, Options, PathParamsTuple, Request, RouteOptions, RouteRegistry } from '../types';
2
+ import { Middleware, Route, Name, Options, PathParamsTuple, RouteOptions, RouteRegistry } from '../types';
3
3
  import { Node } from './node';
4
- import pipeline from '@esportsplus/pipeline';
5
4
 
6
5
 
7
6
  function key(method: string, subdomain?: string | null) {
@@ -23,24 +22,29 @@ function normalize(path: string) {
23
22
  }
24
23
 
25
24
  function set<T>(route: Route<T>, options: Options<T> | RouteOptions<T>) {
26
- let pipeline = route.pipeline;
25
+ let middleware = route.middleware as Middleware<T>[];
27
26
 
28
- for (let key in options) {
29
- let value = options[key as keyof typeof options] as any;
30
-
31
- if (key === 'middleware') {
32
- for (let i = 0, n = value.length; i < n; i++) {
33
- pipeline.add(value[i]);
34
- }
35
- }
36
- else if (key === 'responder') {
37
- pipeline.add(value);
38
- }
39
- else {
40
- // @ts-ignore
41
- route[key] = (route[key] || '') + value;
27
+ if (options.middleware) {
28
+ for (let i = 0, n = options.middleware.length; i < n; i++) {
29
+ middleware.push(options.middleware[i]);
42
30
  }
43
31
  }
32
+
33
+ if ('responder' in options) {
34
+ middleware.push((options as RouteOptions<T>).responder);
35
+ }
36
+
37
+ if (options.name) {
38
+ route.name = (route.name || '') + options.name;
39
+ }
40
+
41
+ if (options.path) {
42
+ route.path = (route.path || '') + options.path;
43
+ }
44
+
45
+ if (options.subdomain) {
46
+ route.subdomain = (route.subdomain || '') + options.subdomain;
47
+ }
44
48
  }
45
49
 
46
50
 
@@ -76,7 +80,7 @@ class Router<T, TRoutes extends RouteRegistry = {}> {
76
80
  route: Route<T> = {
77
81
  name: null,
78
82
  path: null,
79
- pipeline: pipeline<Request<T>, T>(),
83
+ middleware: [],
80
84
  subdomain: null
81
85
  };
82
86
 
@@ -1,6 +1,5 @@
1
1
  import { NeverAsync } from '@esportsplus/utilities';
2
2
  import { Router } from './router';
3
- import pipeline from '@esportsplus/pipeline';
4
3
 
5
4
 
6
5
  type AccumulateRoutes<T extends readonly RouteFactory<any>[]> =
@@ -77,7 +76,7 @@ type Request<T> = {
77
76
  type Route<T> = {
78
77
  name: Name | null;
79
78
  path: string | null;
80
- pipeline: ReturnType<typeof pipeline<Request<T>, T>>,
79
+ middleware: Middleware<T>[] | Next<T>,
81
80
  subdomain: string | null;
82
81
  };
83
82