valyrian.js 7.2.11 → 8.0.0

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.
Files changed (165) hide show
  1. package/README.md +6 -6
  2. package/dist/flux-store/index.d.ts +32 -0
  3. package/dist/flux-store/index.d.ts.map +1 -0
  4. package/dist/flux-store/index.js +258 -0
  5. package/dist/flux-store/index.js.map +7 -0
  6. package/dist/flux-store/index.min.js +1 -0
  7. package/dist/flux-store/index.min.js.map +1 -0
  8. package/dist/flux-store/index.mjs +237 -0
  9. package/dist/flux-store/index.mjs.map +7 -0
  10. package/dist/hooks/index.d.ts.map +1 -1
  11. package/dist/hooks/index.js +42 -75
  12. package/dist/hooks/index.js.map +2 -2
  13. package/dist/hooks/index.min.js +1 -0
  14. package/dist/hooks/index.min.js.map +1 -0
  15. package/dist/hooks/index.mjs +43 -76
  16. package/dist/hooks/index.mjs.map +2 -2
  17. package/dist/index.d.ts +52 -54
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +397 -328
  20. package/dist/index.js.map +3 -3
  21. package/dist/index.min.js +1 -1
  22. package/dist/index.min.js.map +1 -1
  23. package/dist/index.mjs +397 -328
  24. package/dist/index.mjs.map +3 -3
  25. package/dist/native-store/index.d.ts +14 -0
  26. package/dist/native-store/index.d.ts.map +1 -0
  27. package/dist/native-store/index.js +103 -0
  28. package/dist/native-store/index.js.map +7 -0
  29. package/dist/native-store/index.min.js +1 -0
  30. package/dist/native-store/index.min.js.map +1 -0
  31. package/dist/native-store/index.mjs +82 -0
  32. package/dist/native-store/index.mjs.map +7 -0
  33. package/dist/node/index.d.ts.map +1 -1
  34. package/dist/node/index.js +223 -86
  35. package/dist/node/index.js.map +4 -4
  36. package/dist/node/index.mjs +223 -86
  37. package/dist/node/index.mjs.map +4 -4
  38. package/dist/node/node.sw.js +152 -0
  39. package/dist/node/utils/inline.d.ts.map +1 -1
  40. package/dist/node/utils/node.sw.js +152 -0
  41. package/dist/node/utils/session-storage.d.ts +22 -0
  42. package/dist/node/utils/session-storage.d.ts.map +1 -0
  43. package/dist/node/utils/sw.d.ts.map +1 -1
  44. package/dist/node/utils/tree-adapter.d.ts +9 -0
  45. package/dist/node/utils/tree-adapter.d.ts.map +1 -1
  46. package/dist/pulse-store/index.d.ts +13 -0
  47. package/dist/pulse-store/index.d.ts.map +1 -0
  48. package/dist/pulse-store/index.js +143 -0
  49. package/dist/pulse-store/index.js.map +7 -0
  50. package/dist/pulse-store/index.min.js +1 -0
  51. package/dist/pulse-store/index.min.js.map +1 -0
  52. package/dist/pulse-store/index.mjs +122 -0
  53. package/dist/pulse-store/index.mjs.map +7 -0
  54. package/dist/request/index.d.ts.map +1 -1
  55. package/dist/request/index.js +68 -89
  56. package/dist/request/index.js.map +2 -2
  57. package/dist/request/index.min.js +1 -0
  58. package/dist/request/index.min.js.map +1 -0
  59. package/dist/request/index.mjs +68 -89
  60. package/dist/request/index.mjs.map +2 -2
  61. package/dist/router/index.d.ts +32 -31
  62. package/dist/router/index.d.ts.map +1 -1
  63. package/dist/router/index.js +256 -104
  64. package/dist/router/index.js.map +3 -3
  65. package/dist/router/index.min.js +1 -0
  66. package/dist/router/index.min.js.map +1 -0
  67. package/dist/router/index.mjs +256 -104
  68. package/dist/router/index.mjs.map +3 -3
  69. package/dist/signals/index.d.ts +6 -0
  70. package/dist/signals/index.d.ts.map +1 -0
  71. package/dist/signals/index.js +92 -0
  72. package/dist/signals/index.js.map +7 -0
  73. package/dist/signals/index.min.js +1 -0
  74. package/dist/signals/index.min.js.map +1 -0
  75. package/dist/signals/index.mjs +71 -0
  76. package/dist/signals/index.mjs.map +7 -0
  77. package/dist/suspense/index.d.ts +6 -0
  78. package/dist/suspense/index.d.ts.map +1 -0
  79. package/dist/suspense/index.js +67 -0
  80. package/dist/suspense/index.js.map +7 -0
  81. package/dist/suspense/index.min.js +1 -0
  82. package/dist/suspense/index.min.js.map +1 -0
  83. package/dist/suspense/index.mjs +46 -0
  84. package/dist/suspense/index.mjs.map +7 -0
  85. package/dist/sw/index.min.js +1 -0
  86. package/dist/sw/index.min.js.map +1 -0
  87. package/dist/translate/index.d.ts +19 -0
  88. package/dist/translate/index.d.ts.map +1 -0
  89. package/dist/translate/index.js +150 -0
  90. package/dist/translate/index.js.map +7 -0
  91. package/dist/translate/index.min.js +1 -0
  92. package/dist/translate/index.min.js.map +1 -0
  93. package/dist/translate/index.mjs +129 -0
  94. package/dist/translate/index.mjs.map +7 -0
  95. package/dist/tsconfig.tsbuildinfo +1 -1
  96. package/dist/utils/deep-freeze.d.ts +3 -0
  97. package/dist/utils/deep-freeze.d.ts.map +1 -0
  98. package/dist/utils/getter-setter.d.ts +3 -0
  99. package/dist/utils/getter-setter.d.ts.map +1 -0
  100. package/dist/utils/has-changed.d.ts +2 -0
  101. package/dist/utils/has-changed.d.ts.map +1 -0
  102. package/dist/utils/index.d.ts +4 -0
  103. package/dist/utils/index.d.ts.map +1 -0
  104. package/dist/utils/index.js +138 -0
  105. package/dist/utils/index.js.map +7 -0
  106. package/dist/utils/index.min.js +1 -0
  107. package/dist/utils/index.min.js.map +1 -0
  108. package/dist/utils/index.mjs +115 -0
  109. package/dist/utils/index.mjs.map +7 -0
  110. package/lib/flux-store/index.ts +301 -0
  111. package/lib/hooks/index.ts +52 -101
  112. package/lib/index.ts +479 -719
  113. package/lib/native-store/index.ts +106 -0
  114. package/lib/node/index.ts +5 -3
  115. package/lib/node/utils/icons.ts +5 -5
  116. package/lib/node/utils/inline.ts +17 -17
  117. package/lib/node/utils/node.sw.js +152 -0
  118. package/lib/node/utils/session-storage.ts +117 -0
  119. package/lib/node/utils/sw.ts +35 -11
  120. package/lib/node/utils/tree-adapter.ts +99 -52
  121. package/lib/pulse-store/index.ts +181 -0
  122. package/lib/request/index.ts +86 -116
  123. package/lib/router/index.ts +358 -170
  124. package/lib/signals/index.ts +98 -0
  125. package/lib/suspense/index.ts +57 -0
  126. package/lib/translate/index.ts +156 -0
  127. package/lib/utils/deep-freeze.ts +54 -0
  128. package/lib/utils/getter-setter.ts +40 -0
  129. package/lib/utils/has-changed.ts +43 -0
  130. package/lib/utils/index.ts +3 -0
  131. package/package.json +38 -50
  132. package/tsconfig.json +1 -1
  133. package/dist/dataset/index.d.ts +0 -24
  134. package/dist/dataset/index.d.ts.map +0 -1
  135. package/dist/dataset/index.js +0 -178
  136. package/dist/dataset/index.js.map +0 -7
  137. package/dist/dataset/index.mjs +0 -157
  138. package/dist/dataset/index.mjs.map +0 -7
  139. package/dist/node/node.sw.tpl +0 -133
  140. package/dist/node/utils/node.sw.tpl +0 -133
  141. package/dist/proxy-signal/index.d.ts +0 -23
  142. package/dist/proxy-signal/index.d.ts.map +0 -1
  143. package/dist/proxy-signal/index.js +0 -138
  144. package/dist/proxy-signal/index.js.map +0 -7
  145. package/dist/proxy-signal/index.mjs +0 -117
  146. package/dist/proxy-signal/index.mjs.map +0 -7
  147. package/dist/signal/index.d.ts +0 -20
  148. package/dist/signal/index.d.ts.map +0 -1
  149. package/dist/signal/index.js +0 -95
  150. package/dist/signal/index.js.map +0 -7
  151. package/dist/signal/index.mjs +0 -74
  152. package/dist/signal/index.mjs.map +0 -7
  153. package/dist/store/index.d.ts +0 -16
  154. package/dist/store/index.d.ts.map +0 -1
  155. package/dist/store/index.js +0 -93
  156. package/dist/store/index.js.map +0 -7
  157. package/dist/store/index.mjs +0 -72
  158. package/dist/store/index.mjs.map +0 -7
  159. package/lib/dataset/index.ts +0 -193
  160. package/lib/index.d.ts +0 -0
  161. package/lib/interfaces.ts.bak +0 -141
  162. package/lib/node/utils/node.sw.tpl +0 -133
  163. package/lib/proxy-signal/index.ts +0 -187
  164. package/lib/signal/index.ts +0 -161
  165. package/lib/store/index.ts +0 -101
@@ -21,83 +21,85 @@ function getPathWithoutLastSlash(path) {
21
21
  }
22
22
  return pathWithoutLastSlash;
23
23
  }
24
- var addPath = ({
25
- router,
26
- method,
27
- path,
28
- middlewares
29
- }) => {
30
- if (!method || !path || !Array.isArray(middlewares) || middlewares.length === 0) {
31
- throw new Error(`Invalid route input: ${method} ${path} ${middlewares}`);
32
- }
33
- let realpath = path.replace(/(\S)(\/+)$/, "$1");
34
- let params = (realpath.match(/:(\w+)?/gi) || []).map((param) => param.slice(1));
35
- let regexpPath = "^" + realpath.replace(/:(\w+)/gi, "([^\\/\\s]+)") + "$";
36
- router.paths.push({
37
- method,
38
- path: realpath,
39
- middlewares: flat(middlewares),
40
- params,
41
- regexp: new RegExp(regexpPath, "i")
42
- });
43
- };
44
24
  function parseQuery(queryParts) {
45
- let parts = queryParts ? queryParts.split("&") : [];
46
- let query = {};
47
- for (let nameValue of parts) {
48
- let [name, value] = nameValue.split("=", 2);
49
- query[name] = value || "";
25
+ const parts = queryParts ? queryParts.split("&") : [];
26
+ const query = {};
27
+ for (const nameValue of parts) {
28
+ const [name, value] = nameValue.split("=", 2);
29
+ query[name] = isNaN(Number(value)) === false ? Number(value) : value === "true" ? true : value === "false" ? false : value;
50
30
  }
51
31
  return query;
52
32
  }
53
- function searchMiddlewares(router, path) {
54
- let middlewares = [];
55
- let params = {};
56
- let matches = [];
57
- for (let item of router.paths) {
58
- let match = item.regexp.exec(path);
59
- if (Array.isArray(match)) {
60
- middlewares.push(...item.middlewares);
61
- match.shift();
62
- for (let [index, key] of item.params.entries()) {
63
- params[key] = match[index];
64
- }
65
- matches.push(...match);
66
- if (item.method === "add") {
67
- router.path = getPathWithoutPrefix(item.path, router.pathPrefix);
68
- break;
33
+ var RouteTree = class {
34
+ root = { segment: "", children: /* @__PURE__ */ new Map(), isDynamic: false };
35
+ addRoute(path, middlewares) {
36
+ const segments = path === "/" ? [path] : path.split("/").filter(Boolean);
37
+ let currentNode = this.root;
38
+ for (const segment of segments) {
39
+ const isDynamic = segment.startsWith(":");
40
+ const key = isDynamic ? ":" : segment;
41
+ if (!currentNode.children.has(key)) {
42
+ currentNode.children.set(key, {
43
+ segment,
44
+ children: /* @__PURE__ */ new Map(),
45
+ isDynamic,
46
+ paramKey: isDynamic ? segment.slice(1) : void 0
47
+ });
69
48
  }
49
+ currentNode = currentNode.children.get(key);
70
50
  }
51
+ currentNode.middlewares = middlewares;
71
52
  }
72
- router.params = params;
73
- router.matches = matches;
74
- return middlewares;
75
- }
76
- async function searchComponent(router, middlewares) {
77
- const request = {
78
- params: router.params,
79
- query: router.query,
80
- url: router.url,
81
- path: router.path,
82
- matches: router.matches,
83
- redirect: (path, parentComponent) => {
84
- router.go(path, parentComponent);
85
- return false;
86
- }
87
- };
88
- let response;
89
- for (const middleware of middlewares) {
90
- response = await middleware(request, response);
91
- if (response !== void 0 && (isComponent(response) || isVnodeComponent(response))) {
92
- return response;
93
- }
94
- if (response === false) {
95
- return false;
53
+ // Search for a route in the tree
54
+ // eslint-disable-next-line sonarjs/cognitive-complexity
55
+ findRoute(path) {
56
+ const pathWithoutLastSlash = getPathWithoutLastSlash(path);
57
+ const segments = pathWithoutLastSlash === "/" ? [pathWithoutLastSlash] : pathWithoutLastSlash.split("/").filter(Boolean);
58
+ let currentNode = this.root;
59
+ const params = {};
60
+ const wildcardMiddlewares = [];
61
+ const segmentsLength = segments.length;
62
+ for (let i = 0; i < segmentsLength; i++) {
63
+ if (!currentNode) {
64
+ break;
65
+ }
66
+ const segment = segments[i];
67
+ let found = false;
68
+ for (const [key, child] of currentNode.children) {
69
+ if (key === segment) {
70
+ currentNode = child;
71
+ found = true;
72
+ break;
73
+ }
74
+ if (segment !== ".*" && key === ":") {
75
+ currentNode = child;
76
+ params[child.paramKey] = segment;
77
+ found = true;
78
+ break;
79
+ }
80
+ if (key === ".*" && !found) {
81
+ wildcardMiddlewares.push(...child.middlewares || []);
82
+ }
83
+ }
84
+ if (!found) {
85
+ if (currentNode.children.has(".*")) {
86
+ return { middlewares: wildcardMiddlewares, params };
87
+ }
88
+ return null;
89
+ }
96
90
  }
91
+ const allMiddlewares = [...wildcardMiddlewares, ...currentNode.middlewares || []];
92
+ if (allMiddlewares.length === 0) {
93
+ return null;
94
+ }
95
+ return { middlewares: allMiddlewares, params };
97
96
  }
98
- }
97
+ };
98
+ var RouterError = class RouterError2 extends Error {
99
+ status = 500;
100
+ };
99
101
  var Router = class _Router {
100
- paths = [];
102
+ routeTree = new RouteTree();
101
103
  container = null;
102
104
  query = {};
103
105
  options = {};
@@ -106,55 +108,93 @@ var Router = class _Router {
106
108
  params = {};
107
109
  matches = [];
108
110
  pathPrefix = "";
111
+ errorHandlers = /* @__PURE__ */ new Map();
109
112
  constructor(pathPrefix = "") {
110
113
  this.pathPrefix = pathPrefix;
111
114
  }
112
- add(path, ...middlewares) {
113
- let pathWithoutLastSlash = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);
114
- addPath({ router: this, method: "add", path: pathWithoutLastSlash, middlewares });
115
- return this;
116
- }
117
- use(...middlewares) {
118
- let path = getPathWithoutLastSlash(
119
- `${this.pathPrefix}${typeof middlewares[0] === "string" ? middlewares.shift() : "/"}`
115
+ add(...args) {
116
+ const flatArgs = flat(args);
117
+ const path = getPathWithoutLastSlash(
118
+ `${this.pathPrefix}${typeof flatArgs[0] === "string" ? flatArgs.shift() : "/.*"}`
120
119
  );
121
- for (const item of middlewares) {
122
- if (item instanceof _Router) {
123
- const subrouter = item;
124
- for (const subpath of subrouter.paths) {
125
- addPath({
126
- router: this,
127
- method: subpath.method,
128
- path: `${path}${subpath.path}`.replace(/^\/\//, "/"),
129
- middlewares: subpath.middlewares
130
- });
131
- }
132
- continue;
120
+ if (flatArgs.length === 1 && flatArgs[0] instanceof _Router) {
121
+ const subrouter = flatArgs[0];
122
+ for (const subroute of subrouter.routes()) {
123
+ const subroutePath = `${path}${subroute}`;
124
+ this.routeTree.addRoute(subroutePath, subrouter.routeTree.findRoute(subroute).middlewares || []);
125
+ }
126
+ } else {
127
+ if (flatArgs.some((item) => item instanceof _Router)) {
128
+ throw new RouterError("You cannot add middlewares when adding a subrouter.");
133
129
  }
134
- if (typeof item === "function") {
135
- addPath({ router: this, method: "use", path: `${path}.*`, middlewares: [item] });
130
+ if (flatArgs.some((item) => typeof item !== "function")) {
131
+ throw new RouterError("All middlewares must be functions.");
136
132
  }
133
+ this.routeTree.addRoute(path, flatArgs);
137
134
  }
138
135
  return this;
139
136
  }
137
+ catch(...args) {
138
+ const condition = typeof args[0] === "number" || typeof args[0] === "string" || args[0].name.includes("Error") ? args.shift() : "generic";
139
+ if (typeof condition !== "number" && typeof condition !== "string" && !condition.name.includes("Error")) {
140
+ throw new RouterError("The condition must be a number, string or an instance of Error.");
141
+ }
142
+ if (args.some((item) => typeof item !== "function")) {
143
+ throw new RouterError("All middlewares must be functions.");
144
+ }
145
+ let handlers = this.errorHandlers.get(condition);
146
+ if (!handlers) {
147
+ handlers = [];
148
+ this.errorHandlers.set(condition, handlers);
149
+ }
150
+ handlers.push(...args);
151
+ return this;
152
+ }
140
153
  routes() {
141
- return this.paths.filter((path) => path.method === "add").map((path) => path.path);
154
+ return this.getAllRoutes(this.routeTree.root, "");
142
155
  }
143
- async go(path, parentComponent, preventPushState = false) {
156
+ // eslint-disable-next-line sonarjs/cognitive-complexity
157
+ async go(path, parentComponent) {
144
158
  if (!path) {
145
- throw new Error("router.url.required");
159
+ return this.handleError(new RouterError("The URL is empty."), parentComponent);
160
+ }
161
+ if (/%[^0-9A-Fa-f]{2}/.test(path)) {
162
+ return this.handleError(new RouterError(`The URL ${path} is malformed.`));
146
163
  }
147
- let constructedPath = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);
164
+ const constructedPath = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);
148
165
  const parts = constructedPath.split("?", 2);
149
166
  this.url = constructedPath;
150
167
  this.query = parseQuery(parts[1]);
151
- const middlewares = searchMiddlewares(this, parts[0].replace(/(.+)\/$/, "$1").split("#")[0]);
152
- let component = await searchComponent(this, middlewares);
168
+ const finalPath = parts[0].replace(/(.+)\/$/, "$1").split("#")[0];
169
+ this.path = path;
170
+ let route = this.routeTree.findRoute(finalPath);
171
+ if (!route || !route.middlewares) {
172
+ const finalPathParts = finalPath.split("/");
173
+ while (finalPathParts.length > 0) {
174
+ finalPathParts.pop();
175
+ const wildcardRoute = this.routeTree.findRoute(finalPathParts.join("/") + "/.*");
176
+ if (wildcardRoute) {
177
+ route = wildcardRoute;
178
+ break;
179
+ }
180
+ }
181
+ if (!route || !route.middlewares) {
182
+ const error = new RouterError(`The URL ${constructedPath} was not found in the router's registered paths.`);
183
+ error.status = 404;
184
+ return this.handleError(error, parentComponent);
185
+ }
186
+ }
187
+ const { middlewares, params } = route;
188
+ this.params = params;
189
+ let component = await this.searchComponent(middlewares, parentComponent);
153
190
  if (component === false) {
154
191
  return;
155
192
  }
156
193
  if (!component) {
157
- throw new Error(`The url ${constructedPath} requested wasn't found`);
194
+ return this.handleError(
195
+ new RouterError(`The URL ${constructedPath} did not return a valid component.`),
196
+ parentComponent
197
+ );
158
198
  }
159
199
  if (isComponent(parentComponent) || isVnodeComponent(parentComponent)) {
160
200
  const childComponent = isVnodeComponent(component) ? component : v(component, {});
@@ -165,7 +205,7 @@ var Router = class _Router {
165
205
  component = v(parentComponent, {}, childComponent);
166
206
  }
167
207
  }
168
- if (!isNodeJs && !preventPushState) {
208
+ if (!isNodeJs && window.location.pathname + window.location.search !== constructedPath) {
169
209
  window.history.pushState(null, "", constructedPath);
170
210
  }
171
211
  if (this.container) {
@@ -174,17 +214,128 @@ var Router = class _Router {
174
214
  }
175
215
  getOnClickHandler(url) {
176
216
  return (e) => {
217
+ if (e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.defaultPrevented) {
218
+ return;
219
+ }
177
220
  if (typeof url === "string" && url.length > 0) {
178
221
  this.go(url);
179
222
  }
180
223
  e.preventDefault();
181
224
  };
182
225
  }
226
+ getAllRoutes(node, prefix) {
227
+ const routes = [];
228
+ for (const [key, child] of node.children) {
229
+ const newPrefix = `${prefix}/${child.isDynamic ? `:${child.paramKey}` : key}`.replace(/\/$/, "");
230
+ if (child.middlewares) {
231
+ routes.push(newPrefix);
232
+ }
233
+ routes.push(...this.getAllRoutes(child, newPrefix));
234
+ }
235
+ return routes;
236
+ }
237
+ createRequest() {
238
+ return {
239
+ params: this.params,
240
+ query: this.query,
241
+ url: this.url,
242
+ path: this.path,
243
+ matches: this.matches,
244
+ redirect: (path) => this.go(path)
245
+ };
246
+ }
247
+ getErrorConditionMiddlewares(error) {
248
+ for (const [condition, middlewares] of this.errorHandlers) {
249
+ if (typeof condition !== "number" && typeof condition !== "string" && error instanceof condition && error.name === condition.name) {
250
+ return middlewares;
251
+ }
252
+ }
253
+ for (const [condition, middlewares] of this.errorHandlers) {
254
+ if (typeof condition === "number" && (error.status === condition || error.code === condition)) {
255
+ return middlewares;
256
+ }
257
+ }
258
+ for (const [condition, middlewares] of this.errorHandlers) {
259
+ if (typeof condition === "string" && (error.name === condition || error.message.includes(condition))) {
260
+ return middlewares;
261
+ }
262
+ }
263
+ return this.errorHandlers.get("generic") || false;
264
+ }
265
+ // eslint-disable-next-line sonarjs/cognitive-complexity
266
+ async handleError(error, parentComponent) {
267
+ const request = this.createRequest();
268
+ let component = null;
269
+ const middlewares = this.getErrorConditionMiddlewares(error);
270
+ if (middlewares === false) {
271
+ throw error;
272
+ }
273
+ let response;
274
+ try {
275
+ for (const middleware of middlewares) {
276
+ response = await middleware(request, error);
277
+ if (response !== void 0 && (isComponent(response) || isVnodeComponent(response))) {
278
+ component = response;
279
+ break;
280
+ }
281
+ if (response === false) {
282
+ return;
283
+ }
284
+ }
285
+ } catch (err) {
286
+ err.cause = error;
287
+ let errorCauseCount = 0;
288
+ while (err.cause) {
289
+ errorCauseCount++;
290
+ }
291
+ if (errorCauseCount > 20) {
292
+ throw new RouterError("Too many error causes. Possible circular error handling.");
293
+ }
294
+ return this.handleError(err, parentComponent);
295
+ }
296
+ if (component) {
297
+ if (isComponent(parentComponent) || isVnodeComponent(parentComponent)) {
298
+ const childComponent = isVnodeComponent(component) ? component : v(component, {});
299
+ if (isVnodeComponent(parentComponent)) {
300
+ parentComponent.children.push(childComponent);
301
+ component = parentComponent;
302
+ } else {
303
+ component = v(parentComponent, {}, childComponent);
304
+ }
305
+ }
306
+ if (!isNodeJs && window.location.pathname + window.location.search !== this.url) {
307
+ window.history.pushState(null, "", this.url);
308
+ }
309
+ if (this.container) {
310
+ return mount(this.container, component);
311
+ }
312
+ }
313
+ throw error;
314
+ }
315
+ async searchComponent(middlewares, parentComponent) {
316
+ const request = this.createRequest();
317
+ let response;
318
+ for (const middleware of middlewares) {
319
+ try {
320
+ response = await middleware(request);
321
+ } catch (error) {
322
+ return this.handleError(error, parentComponent);
323
+ }
324
+ if (response !== void 0 && (isComponent(response) || isVnodeComponent(response))) {
325
+ return response;
326
+ }
327
+ if (response === false) {
328
+ return false;
329
+ }
330
+ }
331
+ return response;
332
+ }
183
333
  };
184
334
  var localRedirect;
185
- function redirect(url, parentComponent, preventPushState = false) {
335
+ async function redirect(url, parentComponent, preventPushState = false) {
186
336
  if (!localRedirect) {
187
- throw new Error("router.redirect.not.found");
337
+ console.warn("Redirect function is not initialized. Please mount the router first.");
338
+ return;
188
339
  }
189
340
  return localRedirect(url, parentComponent, preventPushState);
190
341
  }
@@ -193,20 +344,21 @@ function mountRouter(elementContainer, router) {
193
344
  localRedirect = router.go.bind(router);
194
345
  if (!isNodeJs) {
195
346
  let onPopStateGoToRoute2 = function() {
196
- let pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);
197
- router.go(pathWithoutPrefix, void 0, true);
347
+ const pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);
348
+ router.go(pathWithoutPrefix);
198
349
  };
199
350
  var onPopStateGoToRoute = onPopStateGoToRoute2;
200
351
  window.addEventListener("popstate", onPopStateGoToRoute2, false);
201
352
  onPopStateGoToRoute2();
202
353
  }
203
- directive("route", (url, vnode, oldnode) => {
204
- setAttribute("href", url, vnode, oldnode);
205
- setAttribute("onclick", router.getOnClickHandler(url), vnode, oldnode);
354
+ directive("route", (url, vnode) => {
355
+ setAttribute("href", url, vnode);
356
+ setAttribute("onclick", router.getOnClickHandler(url), vnode);
206
357
  });
207
358
  }
208
359
  export {
209
360
  Router,
361
+ RouterError,
210
362
  mountRouter,
211
363
  redirect
212
364
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../lib/router/index.ts"],
4
- "sourcesContent": ["/* eslint-disable no-use-before-define */\nimport {\n Component,\n POJOComponent,\n VnodeComponentInterface,\n VnodeWithDom,\n directive,\n isComponent,\n isNodeJs,\n isVnodeComponent,\n mount,\n setAttribute,\n v\n} from \"valyrian.js\";\n\ninterface Request {\n params: Record<string, any>;\n query: Record<string, any>;\n url: string;\n path: string;\n matches: string[];\n // eslint-disable-next-line no-unused-vars\n redirect: (path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface) => false;\n}\n\ninterface Middleware {\n // eslint-disable-next-line no-unused-vars\n (req: Request, res?: any):\n | Promise<any | Component | POJOComponent | VnodeComponentInterface>\n | any\n | Component\n | POJOComponent\n | VnodeComponentInterface;\n}\n\ninterface Middlewares extends Array<Middleware> {}\n\ninterface Path {\n method: string;\n path: string;\n middlewares: Middlewares;\n params: string[];\n regexp: RegExp;\n}\n\ninterface RouterInterface {\n paths: Path[];\n container: Element | string | null;\n query: Record<string, string | number>;\n options: Record<string, any>;\n url: string;\n path: string;\n params: Record<string, string | number | any>;\n matches: string[];\n pathPrefix: string;\n // eslint-disable-next-line no-unused-vars\n add(method: string, ...args: Middlewares): Router;\n // eslint-disable-next-line no-unused-vars\n use(...args: (string | Middleware | Router)[]): Router;\n\n routes(): string[];\n // eslint-disable-next-line no-unused-vars\n go(path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface): Promise<string | void>;\n}\n\ninterface RedirectFunction {\n // eslint-disable-next-line no-unused-vars\n (\n path: string,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface,\n preventPushState?: boolean\n ): Promise<string | void>;\n}\n\nfunction flat(array: any) {\n return Array.isArray(array) ? array.flat(Infinity) : [array];\n}\n\nfunction getPathWithoutPrefix(path: string, prefix: string) {\n return getPathWithoutLastSlash(path.replace(new RegExp(`^${prefix}`), \"\"));\n}\n\nfunction getPathWithoutLastSlash(path: string) {\n let pathWithoutLastSlash = path.replace(/\\/$/, \"\");\n if (pathWithoutLastSlash === \"\") {\n pathWithoutLastSlash = \"/\";\n }\n return pathWithoutLastSlash;\n}\n\nconst addPath = ({\n router,\n method,\n path,\n middlewares\n}: {\n router: Router;\n method: string;\n path: string;\n middlewares: Middleware[];\n}): void => {\n if (!method || !path || !Array.isArray(middlewares) || middlewares.length === 0) {\n throw new Error(`Invalid route input: ${method} ${path} ${middlewares}`);\n }\n\n // Trim trailing slashes from the path\n let realpath = path.replace(/(\\S)(\\/+)$/, \"$1\");\n\n // Find the express-like params in the path\n let params = (realpath.match(/:(\\w+)?/gi) || [])\n // Set the names of the params found\n .map((param) => param.slice(1));\n\n // Generate a regular expression to match the path\n let regexpPath = \"^\" + realpath.replace(/:(\\w+)/gi, \"([^\\\\/\\\\s]+)\") + \"$\";\n\n router.paths.push({\n method,\n path: realpath,\n middlewares: flat(middlewares),\n params,\n regexp: new RegExp(regexpPath, \"i\")\n });\n};\n\n// Parse a query string into an object\nfunction parseQuery(queryParts?: string): Record<string, string> {\n // Split the query string into an array of name-value pairs\n let parts = queryParts ? queryParts.split(\"&\") : [];\n let query: Record<string, string> = {};\n\n // Iterate over the name-value pairs and add them to the query object\n for (let nameValue of parts) {\n let [name, value] = nameValue.split(\"=\", 2);\n query[name] = value || \"\";\n }\n\n return query;\n}\n\n// Search for middlewares that match a given path\nfunction searchMiddlewares(router: RouterInterface, path: string): Middlewares {\n let middlewares: Middlewares = [];\n let params: Record<string, any> = {};\n let matches = [];\n\n // Search for middlewares\n for (let item of router.paths) {\n let match = item.regexp.exec(path);\n\n // If we found middlewares\n if (Array.isArray(match)) {\n middlewares.push(...item.middlewares);\n match.shift();\n\n // Parse params\n for (let [index, key] of item.params.entries()) {\n params[key] = match[index];\n }\n\n // Add remaining matches to the array\n matches.push(...match);\n\n if (item.method === \"add\") {\n router.path = getPathWithoutPrefix(item.path, router.pathPrefix);\n break;\n }\n }\n }\n\n router.params = params;\n router.matches = matches;\n\n return middlewares;\n}\n\nasync function searchComponent(\n router: RouterInterface,\n middlewares: Middlewares\n): Promise<Component | VnodeComponentInterface | false | void> {\n // Define request object with default values\n const request: Request = {\n params: router.params,\n query: router.query,\n url: router.url,\n path: router.path,\n matches: router.matches,\n redirect: (path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface) => {\n router.go(path, parentComponent);\n // Return false to stop the middleware chain\n return false;\n }\n };\n\n // Initialize response variable\n let response;\n\n // Iterate through middlewares\n for (const middleware of middlewares) {\n // Invoke middleware and update response\n response = await middleware(request, response);\n\n // Return response if it's a valid component\n if (response !== undefined && (isComponent(response) || isVnodeComponent(response))) {\n return response;\n }\n\n // Return false if response is explicitly false to stop the middleware chain\n if (response === false) {\n return false;\n }\n }\n}\n\nexport class Router implements RouterInterface {\n paths: Path[] = [];\n container: Element | string | null = null;\n query: Record<string, string | number> = {};\n options: Record<string, any> = {};\n url: string = \"\";\n path: string = \"\";\n params: Record<string, string | number | any> = {};\n matches: string[] = [];\n pathPrefix: string = \"\";\n\n constructor(pathPrefix: string = \"\") {\n this.pathPrefix = pathPrefix;\n }\n\n add(path: string, ...middlewares: Middlewares): Router {\n let pathWithoutLastSlash = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);\n addPath({ router: this, method: \"add\", path: pathWithoutLastSlash, middlewares });\n return this;\n }\n\n use(...middlewares: Middlewares | Router[] | string[]): Router {\n let path = getPathWithoutLastSlash(\n `${this.pathPrefix}${typeof middlewares[0] === \"string\" ? middlewares.shift() : \"/\"}`\n );\n\n for (const item of middlewares) {\n if (item instanceof Router) {\n const subrouter = item as Router;\n for (const subpath of subrouter.paths) {\n addPath({\n router: this,\n method: subpath.method,\n path: `${path}${subpath.path}`.replace(/^\\/\\//, \"/\"),\n middlewares: subpath.middlewares\n });\n }\n continue;\n }\n\n if (typeof item === \"function\") {\n addPath({ router: this, method: \"use\", path: `${path}.*`, middlewares: [item as Middleware] });\n }\n }\n\n return this;\n }\n\n routes(): string[] {\n return this.paths.filter((path) => path.method === \"add\").map((path) => path.path);\n }\n\n async go(\n path: string,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface,\n preventPushState = false\n ): Promise<string | void> {\n if (!path) {\n throw new Error(\"router.url.required\");\n }\n\n let constructedPath = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);\n const parts = constructedPath.split(\"?\", 2);\n this.url = constructedPath;\n this.query = parseQuery(parts[1]);\n\n const middlewares = searchMiddlewares(this as RouterInterface, parts[0].replace(/(.+)\\/$/, \"$1\").split(\"#\")[0]);\n let component = await searchComponent(this as RouterInterface, middlewares);\n\n if (component === false) {\n return;\n }\n\n if (!component) {\n throw new Error(`The url ${constructedPath} requested wasn't found`);\n }\n\n if (isComponent(parentComponent) || isVnodeComponent(parentComponent)) {\n const childComponent = isVnodeComponent(component) ? component : v(component as Component, {});\n if (isVnodeComponent(parentComponent)) {\n parentComponent.children.push(childComponent);\n component = parentComponent;\n } else {\n component = v(parentComponent, {}, childComponent) as VnodeComponentInterface;\n }\n }\n\n if (!isNodeJs && !preventPushState) {\n window.history.pushState(null, \"\", constructedPath);\n }\n\n if (this.container) {\n return mount(this.container, component);\n }\n }\n\n getOnClickHandler(url: string) {\n return (e: MouseEvent) => {\n if (typeof url === \"string\" && url.length > 0) {\n this.go(url);\n }\n e.preventDefault();\n };\n }\n}\n\nlet localRedirect: RedirectFunction;\n\nexport function redirect(\n url: string,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface,\n preventPushState = false\n): Promise<string | void> {\n if (!localRedirect) {\n throw new Error(\"router.redirect.not.found\");\n }\n return localRedirect(url, parentComponent, preventPushState);\n}\n\nexport function mountRouter(elementContainer: string | any, router: Router): void {\n router.container = elementContainer;\n localRedirect = router.go.bind(router);\n\n if (!isNodeJs) {\n function onPopStateGoToRoute(): void {\n let pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);\n (router as unknown as Router).go(pathWithoutPrefix, undefined, true);\n }\n window.addEventListener(\"popstate\", onPopStateGoToRoute, false);\n onPopStateGoToRoute();\n }\n\n directive(\"route\", (url: string, vnode: VnodeWithDom, oldnode?: VnodeWithDom): void => {\n setAttribute(\"href\", url, vnode, oldnode);\n setAttribute(\"onclick\", router.getOnClickHandler(url), vnode, oldnode);\n });\n}\n"],
5
- "mappings": ";AACA;AAAA,EAKE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AA6DP,SAAS,KAAK,OAAY;AACxB,SAAO,MAAM,QAAQ,KAAK,IAAI,MAAM,KAAK,QAAQ,IAAI,CAAC,KAAK;AAC7D;AAEA,SAAS,qBAAqB,MAAc,QAAgB;AAC1D,SAAO,wBAAwB,KAAK,QAAQ,IAAI,OAAO,IAAI,MAAM,EAAE,GAAG,EAAE,CAAC;AAC3E;AAEA,SAAS,wBAAwB,MAAc;AAC7C,MAAI,uBAAuB,KAAK,QAAQ,OAAO,EAAE;AACjD,MAAI,yBAAyB,IAAI;AAC/B,2BAAuB;AAAA,EACzB;AACA,SAAO;AACT;AAEA,IAAM,UAAU,CAAC;AAAA,EACf;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAKY;AACV,MAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,MAAM,QAAQ,WAAW,KAAK,YAAY,WAAW,GAAG;AAC/E,UAAM,IAAI,MAAM,wBAAwB,MAAM,IAAI,IAAI,IAAI,WAAW,EAAE;AAAA,EACzE;AAGA,MAAI,WAAW,KAAK,QAAQ,cAAc,IAAI;AAG9C,MAAI,UAAU,SAAS,MAAM,WAAW,KAAK,CAAC,GAE3C,IAAI,CAAC,UAAU,MAAM,MAAM,CAAC,CAAC;AAGhC,MAAI,aAAa,MAAM,SAAS,QAAQ,YAAY,cAAc,IAAI;AAEtE,SAAO,MAAM,KAAK;AAA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WAAW;AAClB,aAAO,MAAM,KAAK,WAAW,SAAS;AAAA,IACxC;AAAA,EACF;AAAA,EAEA,kBAAkB,KAAa;AAC7B,WAAO,CAAC,MAAkB;AACxB,UAAI,OAAO,QAAQ,YAAY,IAAI,SAAS,GAAG;AAC7C,aAAK,GAAG,GAAG;AAAA,MACb;AACA,QAAE,eAAe;AAAA,IACnB;AAAA,EACF;AACF;AAEA,IAAI;AAEG,SAAS,SACd,KACA,iBACA,mBAAmB,OACK;AACxB,MAAI,CAAC,eAAe;AAClB,UAAM,IAAI,MAAM,2BAA2B;AAAA,EAC7C;AACA,SAAO,cAAc,KAAK,iBAAiB,gBAAgB;AAC7D;AAEO,SAAS,YAAY,kBAAgC,QAAsB;AAChF,SAAO,YAAY;AACnB,kBAAgB,OAAO,GAAG,KAAK,MAAM;AAErC,MAAI,CAAC,UAAU;AACb,QAASA,uBAAT,WAAqC;AACnC,UAAI,oBAAoB,qBAAqB,SAAS,SAAS,UAAU,OAAO,UAAU;AAC1F,MAAC,OAA6B,GAAG,mBAAmB,QAAW,IAAI;AAAA,IACrE;AAHS,8BAAAA;AAIT,WAAO,iBAAiB,YAAYA,sBAAqB,KAAK;AAC9D,IAAAA,qBAAoB;AAAA,EACtB;AAEA,YAAU,SAAS,CAAC,KAAa,OAAqB,YAAiC;AACrF,iBAAa,QAAQ,KAAK,OAAO,OAAO;AACxC,iBAAa,WAAW,OAAO,kBAAkB,GAAG,GAAG,OAAO,OAAO;AAAA,EACvE,CAAC;AACH;",
6
- "names": ["onPopStateGoToRoute"]
4
+ "sourcesContent": ["/* eslint-disable no-console */\n/* eslint-disable no-use-before-define */\nimport {\n Component,\n POJOComponent,\n VnodeComponentInterface,\n VnodeWithDom,\n directive,\n isComponent,\n isNodeJs,\n isVnodeComponent,\n mount,\n setAttribute,\n v\n} from \"valyrian.js\";\n\ninterface Request {\n params: Record<string, any>;\n query: Record<string, any>;\n url: string;\n path: string;\n matches: string[];\n // eslint-disable-next-line no-unused-vars\n redirect: (path: string) => Promise<string | void>;\n}\n\ninterface Middleware {\n // eslint-disable-next-line no-unused-vars\n (req: Request, err?: any):\n | Promise<any | Component | POJOComponent | VnodeComponentInterface>\n | any\n | Component\n | POJOComponent\n | VnodeComponentInterface;\n}\n\ninterface Middlewares extends Array<Middleware> {}\n\ninterface RedirectFunction {\n (\n // eslint-disable-next-line no-unused-vars\n path: string,\n // eslint-disable-next-line no-unused-vars\n parentComponent?: Component | POJOComponent | VnodeComponentInterface,\n // eslint-disable-next-line no-unused-vars\n preventPushState?: boolean\n ): Promise<string | void>;\n}\n\nfunction flat(array: any) {\n return Array.isArray(array) ? array.flat(Infinity) : [array];\n}\n\nfunction getPathWithoutPrefix(path: string, prefix: string) {\n return getPathWithoutLastSlash(path.replace(new RegExp(`^${prefix}`), \"\"));\n}\n\nfunction getPathWithoutLastSlash(path: string) {\n let pathWithoutLastSlash = path.replace(/\\/$/, \"\");\n if (pathWithoutLastSlash === \"\") {\n pathWithoutLastSlash = \"/\";\n }\n return pathWithoutLastSlash;\n}\n\n// Parse a query string into an object\nfunction parseQuery(queryParts?: string): Record<string, any> {\n const parts = queryParts ? queryParts.split(\"&\") : [];\n const query: Record<string, any> = {};\n\n for (const nameValue of parts) {\n const [name, value] = nameValue.split(\"=\", 2);\n query[name] =\n isNaN(Number(value)) === false ? Number(value) : value === \"true\" ? true : value === \"false\" ? false : value;\n }\n\n return query;\n}\n\ninterface RouteNode {\n segment: string;\n children: Map<string, RouteNode>;\n middlewares?: Middlewares;\n paramKey?: string;\n isDynamic: boolean;\n}\n\nclass RouteTree {\n root: RouteNode = { segment: \"\", children: new Map(), isDynamic: false };\n\n addRoute(path: string, middlewares: Middlewares) {\n const segments = path === \"/\" ? [path] : path.split(\"/\").filter(Boolean); // Divide the path into segments\n let currentNode = this.root;\n\n for (const segment of segments) {\n const isDynamic = segment.startsWith(\":\");\n const key = isDynamic ? \":\" : segment; // If the segment is dynamic, use \":\" as key\n\n if (!currentNode.children.has(key)) {\n currentNode.children.set(key, {\n segment: segment,\n children: new Map(),\n isDynamic: isDynamic,\n paramKey: isDynamic ? segment.slice(1) : undefined\n });\n }\n\n currentNode = currentNode.children.get(key)!;\n }\n\n currentNode.middlewares = middlewares; // Assign the middlewares to the last node\n }\n\n // Search for a route in the tree\n // eslint-disable-next-line sonarjs/cognitive-complexity\n findRoute(path: string): { middlewares?: Middlewares; params: Record<string, string> } | null {\n const pathWithoutLastSlash = getPathWithoutLastSlash(path);\n const segments =\n pathWithoutLastSlash === \"/\" ? [pathWithoutLastSlash] : pathWithoutLastSlash.split(\"/\").filter(Boolean);\n let currentNode: RouteNode | null = this.root;\n const params: Record<string, string> = {};\n\n const wildcardMiddlewares: Middlewares = []; // Middlewares for wildcard routes\n const segmentsLength = segments.length;\n\n for (let i = 0; i < segmentsLength; i++) {\n if (!currentNode) {\n break;\n }\n\n const segment = segments[i];\n\n let found = false;\n\n for (const [key, child] of currentNode.children) {\n if (key === segment) {\n currentNode = child;\n found = true;\n break;\n }\n\n if (segment !== \".*\" && key === \":\") {\n currentNode = child;\n params[child.paramKey!] = segment;\n found = true;\n break;\n }\n\n if (key === \".*\" && !found) {\n wildcardMiddlewares.push(...(child.middlewares || []));\n }\n }\n\n if (!found) {\n if (currentNode.children.has(\".*\")) {\n return { middlewares: wildcardMiddlewares, params };\n }\n return null;\n }\n }\n\n // Add the wildcard middlewares to the current node middlewares\n const allMiddlewares = [...wildcardMiddlewares, ...(currentNode.middlewares || [])];\n\n // If there are no middlewares, return null\n if (allMiddlewares.length === 0) {\n return null;\n }\n\n // If there are middlewares, return them\n return { middlewares: allMiddlewares, params };\n }\n}\n\nexport const RouterError = class RouterError extends Error {\n status: number | undefined = 500;\n};\n\nexport class Router {\n private routeTree = new RouteTree();\n container: Element | string | null = null;\n query: Record<string, string | number> = {};\n options: Record<string, any> = {};\n url: string = \"\";\n path: string = \"\";\n params: Record<string, string | number | any> = {};\n matches: string[] = [];\n pathPrefix: string = \"\";\n\n private errorHandlers: Map<number | string | Error | \"generic\", Middlewares> = new Map();\n\n constructor(pathPrefix: string = \"\") {\n this.pathPrefix = pathPrefix;\n }\n\n add(...args: (string | Middleware | Router)[]): Router {\n const flatArgs = flat(args);\n const path = getPathWithoutLastSlash(\n `${this.pathPrefix}${typeof flatArgs[0] === \"string\" ? flatArgs.shift() : \"/.*\"}`\n );\n\n // If the first argument is a Router, add all its routes\n if (flatArgs.length === 1 && flatArgs[0] instanceof Router) {\n const subrouter = flatArgs[0] as Router;\n for (const subroute of subrouter.routes()) {\n const subroutePath = `${path}${subroute}`;\n this.routeTree.addRoute(subroutePath, subrouter.routeTree.findRoute(subroute)!.middlewares || []);\n }\n } else {\n // Verify that no middlewares are added when a subrouter is added\n if (flatArgs.some((item) => item instanceof Router)) {\n throw new RouterError(\"You cannot add middlewares when adding a subrouter.\");\n }\n\n // Verify that all middlewares are functions\n if (flatArgs.some((item) => typeof item !== \"function\")) {\n throw new RouterError(\"All middlewares must be functions.\");\n }\n\n this.routeTree.addRoute(path, flatArgs as Middlewares);\n }\n\n return this;\n }\n\n catch(...args: (number | string | Error | Middleware)[]): Router {\n const condition =\n typeof args[0] === \"number\" || typeof args[0] === \"string\" || args[0].name.includes(\"Error\")\n ? (args.shift() as number | string | Error)\n : \"generic\";\n\n if (typeof condition !== \"number\" && typeof condition !== \"string\" && !condition.name.includes(\"Error\")) {\n throw new RouterError(\"The condition must be a number, string or an instance of Error.\");\n }\n\n // Verify that all middlewares are functions\n if (args.some((item) => typeof item !== \"function\")) {\n throw new RouterError(\"All middlewares must be functions.\");\n }\n\n let handlers = this.errorHandlers.get(condition);\n if (!handlers) {\n handlers = [];\n this.errorHandlers.set(condition, handlers);\n }\n\n handlers.push(...(args as Middlewares));\n return this;\n }\n\n routes(): string[] {\n return this.getAllRoutes(this.routeTree.root, \"\");\n }\n\n // eslint-disable-next-line sonarjs/cognitive-complexity\n async go(\n path: string,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface\n ): Promise<string | void> {\n if (!path) {\n return this.handleError(new RouterError(\"The URL is empty.\"), parentComponent);\n }\n\n if (/%[^0-9A-Fa-f]{2}/.test(path)) {\n return this.handleError(new RouterError(`The URL ${path} is malformed.`));\n }\n\n const constructedPath = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);\n const parts = constructedPath.split(\"?\", 2);\n this.url = constructedPath;\n this.query = parseQuery(parts[1]);\n\n const finalPath = parts[0].replace(/(.+)\\/$/, \"$1\").split(\"#\")[0];\n this.path = path;\n\n let route = this.routeTree.findRoute(finalPath);\n\n if (!route || !route.middlewares) {\n // If the route is not found, search for a wildcard route\n const finalPathParts = finalPath.split(\"/\"); // Divide the path into segments\n\n while (finalPathParts.length > 0) {\n finalPathParts.pop(); // Remove the last segment\n const wildcardRoute = this.routeTree.findRoute(finalPathParts.join(\"/\") + \"/.*\"); // Search for a wildcard route\n if (wildcardRoute) {\n route = wildcardRoute;\n break;\n }\n }\n\n // If no route is found, return a 404 error\n if (!route || !route.middlewares) {\n const error = new RouterError(`The URL ${constructedPath} was not found in the router's registered paths.`);\n (error as any).status = 404;\n return this.handleError(error, parentComponent);\n }\n }\n\n const { middlewares, params } = route;\n this.params = params;\n\n let component = await this.searchComponent(middlewares, parentComponent);\n\n if (component === false) {\n return;\n }\n\n if (!component) {\n return this.handleError(\n new RouterError(`The URL ${constructedPath} did not return a valid component.`),\n parentComponent\n );\n }\n\n if (isComponent(parentComponent) || isVnodeComponent(parentComponent)) {\n const childComponent = isVnodeComponent(component) ? component : v(component as Component, {});\n if (isVnodeComponent(parentComponent)) {\n parentComponent.children.push(childComponent);\n component = parentComponent;\n } else {\n component = v(parentComponent, {}, childComponent) as VnodeComponentInterface;\n }\n }\n\n if (!isNodeJs && window.location.pathname + window.location.search !== constructedPath) {\n window.history.pushState(null, \"\", constructedPath);\n }\n\n if (this.container) {\n return mount(this.container, component);\n }\n }\n\n getOnClickHandler(url: string) {\n return (e: MouseEvent) => {\n if (e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.defaultPrevented) {\n return;\n }\n\n if (typeof url === \"string\" && url.length > 0) {\n this.go(url);\n }\n e.preventDefault();\n };\n }\n\n private getAllRoutes(node: RouteNode, prefix: string): string[] {\n const routes: string[] = [];\n\n for (const [key, child] of node.children) {\n // eslint-disable-next-line sonarjs/no-nested-template-literals\n const newPrefix = `${prefix}/${child.isDynamic ? `:${child.paramKey}` : key}`.replace(/\\/$/, \"\");\n if (child.middlewares) {\n routes.push(newPrefix);\n }\n routes.push(...this.getAllRoutes(child, newPrefix));\n }\n\n return routes;\n }\n\n private createRequest(): Request {\n return {\n params: this.params,\n query: this.query,\n url: this.url,\n path: this.path,\n matches: this.matches,\n redirect: (path: string) => this.go(path)\n };\n }\n\n private getErrorConditionMiddlewares(error: any): Middlewares | false {\n // Search first for class and name errors\n for (const [condition, middlewares] of this.errorHandlers) {\n if (\n typeof condition !== \"number\" &&\n typeof condition !== \"string\" &&\n error instanceof (condition as any) &&\n error.name === condition.name\n ) {\n return middlewares;\n }\n }\n\n // then for code errors\n for (const [condition, middlewares] of this.errorHandlers) {\n if (typeof condition === \"number\" && (error.status === condition || error.code === condition)) {\n return middlewares;\n }\n }\n\n // and then for message errors\n for (const [condition, middlewares] of this.errorHandlers) {\n if (typeof condition === \"string\" && (error.name === condition || error.message.includes(condition))) {\n return middlewares;\n }\n }\n\n // If no specific error handler is found, return the generic one\n return this.errorHandlers.get(\"generic\") || false;\n }\n\n // eslint-disable-next-line sonarjs/cognitive-complexity\n private async handleError(\n error: Error,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface\n ): Promise<void | string> {\n const request: Request = this.createRequest();\n let component = null;\n const middlewares = this.getErrorConditionMiddlewares(error);\n\n // If no error handler is found, throw the error\n if (middlewares === false) {\n throw error;\n }\n\n let response;\n try {\n for (const middleware of middlewares) {\n response = await middleware(request, error);\n\n // If the response is a component or vnode, return it for rendering\n if (response !== undefined && (isComponent(response) || isVnodeComponent(response))) {\n component = response;\n break;\n }\n\n // If the response is false, stop the middleware chain\n if (response === false) {\n return;\n }\n }\n } catch (err) {\n // If an error occurs during the error handling, we handle it recursively\n (err as Error).cause = error;\n\n let errorCauseCount = 0;\n while ((err as Error).cause) {\n errorCauseCount++;\n }\n\n if (errorCauseCount > 20) {\n throw new RouterError(\"Too many error causes. Possible circular error handling.\");\n }\n\n return this.handleError(err as Error, parentComponent);\n }\n\n if (component) {\n // If there is an error middleware that returns a component, we return it\n if (isComponent(parentComponent) || isVnodeComponent(parentComponent)) {\n const childComponent = isVnodeComponent(component) ? component : v(component as Component, {});\n if (isVnodeComponent(parentComponent)) {\n parentComponent.children.push(childComponent);\n component = parentComponent;\n } else {\n component = v(parentComponent, {}, childComponent) as VnodeComponentInterface;\n }\n }\n\n // If we are in the browser, we update the URL\n if (!isNodeJs && window.location.pathname + window.location.search !== this.url) {\n window.history.pushState(null, \"\", this.url);\n }\n\n // If there is a container, we mount the component\n if (this.container) {\n return mount(this.container, component);\n }\n }\n\n // If there is no component to render, we throw the error\n throw error;\n }\n\n private async searchComponent(\n middlewares: Middlewares,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface\n ) {\n const request: Request = this.createRequest();\n\n let response;\n\n for (const middleware of middlewares) {\n try {\n response = await middleware(request);\n } catch (error) {\n return this.handleError(error as Error, parentComponent);\n }\n\n // If the response is a component or vnode, return it for rendering\n if (response !== undefined && (isComponent(response) || isVnodeComponent(response))) {\n return response;\n }\n\n // If the response is false, stop the middleware chain\n if (response === false) {\n return false;\n }\n }\n\n return response;\n }\n}\n\nlet localRedirect: RedirectFunction;\n\nexport async function redirect(\n url: string,\n parentComponent?: Component | POJOComponent | VnodeComponentInterface,\n preventPushState = false\n): Promise<string | void> {\n if (!localRedirect) {\n // eslint-disable-next-line no-console\n console.warn(\"Redirect function is not initialized. Please mount the router first.\");\n return;\n }\n return localRedirect(url, parentComponent, preventPushState);\n}\n\nexport function mountRouter(elementContainer: string | any, router: Router): void {\n router.container = elementContainer;\n localRedirect = router.go.bind(router);\n\n if (!isNodeJs) {\n function onPopStateGoToRoute(): void {\n const pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);\n (router as unknown as Router).go(pathWithoutPrefix);\n }\n window.addEventListener(\"popstate\", onPopStateGoToRoute, false);\n onPopStateGoToRoute();\n }\n\n directive(\"route\", (url: string, vnode: VnodeWithDom): void => {\n setAttribute(\"href\", url, vnode);\n setAttribute(\"onclick\", router.getOnClickHandler(url), vnode);\n });\n}\n"],
5
+ "mappings": ";AAEA;AAAA,EAKE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAmCP,SAAS,KAAK,OAAY;AACxB,SAAO,MAAM,QAAQ,KAAK,IAAI,MAAM,KAAK,QAAQ,IAAI,CAAC,KAAK;AAC7D;AAEA,SAAS,qBAAqB,MAAc,QAAgB;AAC1D,SAAO,wBAAwB,KAAK,QAAQ,IAAI,OAAO,IAAI,MAAM,EAAE,GAAG,EAAE,CAAC;AAC3E;AAEA,SAAS,wBAAwB,MAAc;AAC7C,MAAI,uBAAuB,KAAK,QAAQ,OAAO,EAAE;AACjD,MAAI,yBAAyB,IAAI;AAC/B,2BAAuB;AAAA,EACzB;AACA,SAAO;AACT;AAGA,SAAS,WAAW,YAA0C;AAC5D,QAAM,QAAQ,aAAa,WAAW,MAAM,GAAG,IAAI,CAAC;AACpD,QAAM,QAA6B,CAAC;AAEpC,aAAW,aAAa,OAAO;AAC7B,UAAM,CAAC,MAAM,KAAK,IAAI,UAAU,MAAM,KAAK,CAAC;AAC5C,UAAM,IAAI,IACR,MAAM,OAAO,KAAK,CAAC,MAAM,QAAQ,OAAO,KAAK,IAAI,UAAU,SAAS,OAAO,UAAU,UAAU,QAAQ;AAAA,EAC3G;AAEA,SAAO;AACT;AAUA,IAAM,YAAN,MAAgB;AAAA,EACd,OAAkB,EAAE,SAAS,IAAI,UAAU,oBAAI,IAAI,GAAG,WAAW,MAAM;AAAA,EAEvE,SAAS,MAAc,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,WAAO,iBAAiB,YAAYA,sBAAqB,KAAK;AAC9D,IAAAA,qBAAoB;AAAA,EACtB;AAEA,YAAU,SAAS,CAAC,KAAa,UAA8B;AAC7D,iBAAa,QAAQ,KAAK,KAAK;AAC/B,iBAAa,WAAW,OAAO,kBAAkB,GAAG,GAAG,KAAK;AAAA,EAC9D,CAAC;AACH;",
6
+ "names": ["RouterError", "onPopStateGoToRoute"]
7
7
  }
@@ -0,0 +1,6 @@
1
+ export type Signal<T> = [() => T, (newValue: T | ((current: T) => T)) => void, () => void];
2
+ export declare function createSignal<T>(initialValue: T): Signal<T>;
3
+ export declare function createEffect(effect: Function): void;
4
+ export type SignalStore<T> = [() => T, (path: string, newValue: T | ((current: T) => T)) => void];
5
+ export declare function createSignalStore<T>(initialState: T): SignalStore<T>;
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/signals/index.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,IAAI,EAAE,MAAM,IAAI,CAAC,CAAC;AAE3F,wBAAgB,YAAY,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAkD1D;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,QAAQ,QAW5C;AAMD,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC;AAElG,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAkBpE"}