litestar-vite-plugin 0.6.3 → 0.6.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.
@@ -1,5 +1,6 @@
1
1
  export declare function resolvePageComponent<T>(path: string | string[], pages: Record<string, Promise<T> | (() => Promise<T>)>): Promise<T>;
2
2
  export declare function route(routeName: string, ...args: any[]): string;
3
+ export declare function getRelativeUrlPath(url: string): string;
3
4
  export declare function toRoute(url: string): string | null;
4
5
  export declare function currentRoute(): string | null;
5
6
  export declare function isRoute(url: string, routeName: string): boolean;
@@ -15,7 +15,7 @@ export function route(routeName, ...args) {
15
15
  console.error(`URL '${routeName}' not found in routes.`);
16
16
  return "#"; // Return "#" to indicate failure
17
17
  }
18
- const argTokens = url.match(/{(\w+:?)?\w+}/g);
18
+ const argTokens = url.match(/\{([^}]+):([^}]+)\}/g);
19
19
  if (!argTokens && args.length > 0) {
20
20
  console.error(`Invalid URL lookup: URL '${routeName}' does not expect arguments.`);
21
21
  return "#";
@@ -53,11 +53,22 @@ export function route(routeName, ...args) {
53
53
  }
54
54
  return url;
55
55
  }
56
+ export function getRelativeUrlPath(url) {
57
+ try {
58
+ const urlObject = new URL(url);
59
+ return urlObject.pathname + urlObject.search + urlObject.hash;
60
+ }
61
+ catch (e) {
62
+ // If the URL is invalid or already a relative path, just return it as is
63
+ return url;
64
+ }
65
+ }
56
66
  export function toRoute(url) {
67
+ url = getRelativeUrlPath(url);
68
+ url = url === '/' ? url : url.replace(/\/$/, '');
57
69
  for (const routeName in routes) {
58
70
  const routePattern = routes[routeName];
59
- const regexPattern = routePattern
60
- .replace(/{\w+:(\w+)}/g, (match, paramName, paramType) => {
71
+ const regexPattern = routePattern.replace(/\//g, '\\/').replace(/\{([^}]+):([^}]+)\}/g, (match, paramName, paramType) => {
61
72
  // Create a regex pattern based on the parameter type
62
73
  switch (paramType) {
63
74
  case 'str':
@@ -68,9 +79,8 @@ export function toRoute(url) {
68
79
  default:
69
80
  return '([^/]+)'; // Default to match any non-slash characters
70
81
  }
71
- })
72
- .replace(/\//g, '\\/'); // Escape slashes for regex
73
- const regex = new RegExp('^' + regexPattern);
82
+ });
83
+ const regex = new RegExp(`^${regexPattern}$`);
74
84
  if (regex.test(url)) {
75
85
  return routeName;
76
86
  }
@@ -82,29 +92,30 @@ export function currentRoute() {
82
92
  return toRoute(currentUrl);
83
93
  }
84
94
  export function isRoute(url, routeName) {
85
- try {
86
- const routePattern = routes[routeName];
87
- const regexPattern = routePattern
88
- .replace(/{\w+:(\w+)}/g, (match, paramName, paramType) => {
89
- // Create a regex pattern based on the parameter type
90
- switch (paramType) {
91
- case 'str':
92
- case 'path':
93
- return '([^/]+)'; // Match any non-slash characters
94
- case 'uuid':
95
- return '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'; // Match a UUID pattern
96
- default:
97
- return '([^/]+)'; // Default to match any non-slash characters
95
+ url = getRelativeUrlPath(url);
96
+ url = url === '/' ? url : url.replace(/\/$/, '');
97
+ const routeNameRegex = new RegExp('^' + routeName.replace(/\*/g, '.*') + '$');
98
+ for (const routeName in routes) {
99
+ if (routeNameRegex.test(routeName)) {
100
+ const routePattern = routes[routeName];
101
+ const regexPattern = routePattern.replace(/\//g, '\\/').replace(/\{([^}]+):([^}]+)\}/g, (match, paramName, paramType) => {
102
+ switch (paramType) {
103
+ case 'str':
104
+ case 'path':
105
+ return '([^/]+)';
106
+ case 'uuid':
107
+ return '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
108
+ default:
109
+ return '([^/]+)';
110
+ }
111
+ });
112
+ const regex = new RegExp(`^${regexPattern}$`);
113
+ if (regex.test(url)) {
114
+ return true;
98
115
  }
99
- })
100
- .replace(/\//g, '\\/'); // Escape slashes for regex
101
- const regex = new RegExp('^' + regexPattern);
102
- return regex.test(url);
103
- }
104
- catch (error) {
105
- console.error(error);
106
- return false;
116
+ }
107
117
  }
118
+ return false;
108
119
  }
109
120
  export function isCurrentRoute(routeName) {
110
121
  const currentRouteName = currentRoute();
@@ -112,7 +123,8 @@ export function isCurrentRoute(routeName) {
112
123
  console.error("Could not match current window location to a named route.");
113
124
  return false;
114
125
  }
115
- return currentRouteName == routeName;
126
+ const routeNameRegex = new RegExp('^' + routeName.replace(/\*/g, '.*') + '$');
127
+ return routeNameRegex.test(currentRouteName);
116
128
  }
117
129
  globalThis.routes = globalThis.routes || {};
118
130
  globalThis.route = route;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "litestar-vite-plugin",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "type": "module",
5
5
  "description": "Litestar plugin for Vite.",
6
6
  "keywords": [