@uxf/router 11.72.3 → 11.72.5

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 (2) hide show
  1. package/package.json +1 -1
  2. package/router.js +61 -60
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/router",
3
- "version": "11.72.3",
3
+ "version": "11.72.5",
4
4
  "description": "UXF Router",
5
5
  "author": "UXFans <dev@uxf.cz>",
6
6
  "homepage": "https://gitlab.com/uxf-npm/router#readme",
package/router.js CHANGED
@@ -16,6 +16,61 @@ function decodeArgs(args) {
16
16
  return { route: args[0], params: args[1], options: args[2] };
17
17
  }
18
18
  function createRouter(routes, routerOptions) {
19
+ const routeToUrl = (...args) => {
20
+ var _a;
21
+ const { route, params, options } = decodeArgs(args);
22
+ let pathname = typeof routes[route].path === "string"
23
+ ? routes[route].path
24
+ : routes[route].path[(_a = options === null || options === void 0 ? void 0 : options.locale) !== null && _a !== void 0 ? _a : (0, throw_error_1.throwError)("Locale is required")];
25
+ if (!pathname) {
26
+ throw new Error(`Route '${String(route)}' not found.`);
27
+ }
28
+ const restParams = {};
29
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
30
+ Object.keys(params !== null && params !== void 0 ? params : empty_object_1.EMPTY_OBJECT).forEach((key) => {
31
+ const value = params[key];
32
+ const segment = `[${key}]`;
33
+ const catchAllSegments = `[...${key}]`;
34
+ const catchAllSegmentsOptional = `[[...${key}]]`;
35
+ if (pathname.includes(segment)) {
36
+ pathname = pathname.replace(segment, value);
37
+ }
38
+ else if (pathname.includes(catchAllSegmentsOptional)) {
39
+ if ((Array.isArray(value) && value.length === 0) ||
40
+ value === null ||
41
+ value === undefined ||
42
+ value === "") {
43
+ pathname = pathname.replace(`/${catchAllSegmentsOptional}`, "");
44
+ }
45
+ else if (Array.isArray(value)) {
46
+ // catch all segments optional
47
+ pathname = pathname.replace(catchAllSegmentsOptional, value.join("/"));
48
+ }
49
+ else {
50
+ pathname = pathname.replace(catchAllSegmentsOptional, value);
51
+ }
52
+ }
53
+ else if (pathname.includes(catchAllSegments)) {
54
+ // catch all segments
55
+ if (Array.isArray(value)) {
56
+ if (value.length === 0) {
57
+ throw new Error(`Parameter '${key}' can not be empty array for route '${String(route)}'`);
58
+ }
59
+ pathname = pathname.replace(catchAllSegments, value.join("/"));
60
+ }
61
+ else {
62
+ pathname = pathname.replace(catchAllSegments, value);
63
+ }
64
+ }
65
+ else {
66
+ restParams[key] = value;
67
+ }
68
+ });
69
+ if (Object.keys(restParams).length > 0) {
70
+ pathname = `${pathname}?${(0, qs_1.stringify)(restParams, { arrayFormat: "repeat" })}`;
71
+ }
72
+ return (options === null || options === void 0 ? void 0 : options.shouldBeAbsolute) ? `${routerOptions.baseUrl}${pathname}` : pathname;
73
+ };
19
74
  return {
20
75
  route(...args) {
21
76
  var _a;
@@ -28,61 +83,7 @@ function createRouter(routes, routerOptions) {
28
83
  query: params !== null && params !== void 0 ? params : null,
29
84
  };
30
85
  },
31
- routeToUrl(...args) {
32
- var _a;
33
- const { route, params, options } = decodeArgs(args);
34
- let pathname = typeof routes[route].path === "string"
35
- ? routes[route].path
36
- : routes[route].path[(_a = options === null || options === void 0 ? void 0 : options.locale) !== null && _a !== void 0 ? _a : (0, throw_error_1.throwError)("Locale is required")];
37
- if (!pathname) {
38
- throw new Error(`Route '${String(route)}' not found.`);
39
- }
40
- const restParams = {};
41
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
42
- Object.keys(params !== null && params !== void 0 ? params : empty_object_1.EMPTY_OBJECT).forEach((key) => {
43
- const value = params[key];
44
- const segment = `[${key}]`;
45
- const catchAllSegments = `[...${key}]`;
46
- const catchAllSegmentsOptional = `[[...${key}]]`;
47
- if (pathname.includes(segment)) {
48
- pathname = pathname.replace(segment, value);
49
- }
50
- else if (pathname.includes(catchAllSegmentsOptional)) {
51
- if ((Array.isArray(value) && value.length === 0) ||
52
- value === null ||
53
- value === undefined ||
54
- value === "") {
55
- pathname = pathname.replace(`/${catchAllSegmentsOptional}`, "");
56
- }
57
- else if (Array.isArray(value)) {
58
- // catch all segments optional
59
- pathname = pathname.replace(catchAllSegmentsOptional, value.join("/"));
60
- }
61
- else {
62
- pathname = pathname.replace(catchAllSegmentsOptional, value);
63
- }
64
- }
65
- else if (pathname.includes(catchAllSegments)) {
66
- // catch all segments
67
- if (Array.isArray(value)) {
68
- if (value.length === 0) {
69
- throw new Error(`Parameter '${key}' can not be empty array for route '${String(route)}'`);
70
- }
71
- pathname = pathname.replace(catchAllSegments, value.join("/"));
72
- }
73
- else {
74
- pathname = pathname.replace(catchAllSegments, value);
75
- }
76
- }
77
- else {
78
- restParams[key] = value;
79
- }
80
- });
81
- if (Object.keys(restParams).length > 0) {
82
- pathname = `${pathname}?${(0, qs_1.stringify)(restParams, { arrayFormat: "repeat" })}`;
83
- }
84
- return (options === null || options === void 0 ? void 0 : options.shouldBeAbsolute) ? `${routerOptions.baseUrl}${pathname}` : pathname;
85
- },
86
+ routeToUrl,
86
87
  useActiveRoute() {
87
88
  var _a;
88
89
  const router = (0, router_1.useRouter)();
@@ -108,14 +109,14 @@ function createRouter(routes, routerOptions) {
108
109
  if (!schema) {
109
110
  throw new Error(`Route '${String(routeName)}' has no schema.`);
110
111
  }
111
- if (router.isReady) {
112
+ if (!router.isReady) {
112
113
  throw new Error("Router is not ready. Use useQueryParamsStatic instead of useQueryParams.");
113
114
  }
114
115
  return [
115
116
  (0, superstruct_1.mask)(router.query, schema),
116
117
  {
117
- push: (params) => router.push(this.routeToUrl(routeName, params, {})),
118
- replace: (params) => router.replace(this.routeToUrl(routeName, params, {})),
118
+ push: (params) => router.push(routeToUrl(routeName, params, {})),
119
+ replace: (params) => router.replace(routeToUrl(routeName, params, {})),
119
120
  },
120
121
  ];
121
122
  },
@@ -128,8 +129,8 @@ function createRouter(routes, routerOptions) {
128
129
  return [
129
130
  router.isReady ? (0, superstruct_1.mask)(router.query, schema) : null,
130
131
  {
131
- push: (params) => router.push(this.routeToUrl(routeName, params, {})),
132
- replace: (params) => router.replace(this.routeToUrl(routeName, params, {})),
132
+ push: (params) => router.push(routeToUrl(routeName, params, {})),
133
+ replace: (params) => router.replace(routeToUrl(routeName, params, {})),
133
134
  },
134
135
  ];
135
136
  },