litestar-vite-plugin 0.6.4 → 0.6.6

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.
@@ -63,6 +63,9 @@ export function getRelativeUrlPath(url) {
63
63
  return url;
64
64
  }
65
65
  }
66
+ function routePatternToRegex(pattern) {
67
+ return new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
68
+ }
66
69
  export function toRoute(url) {
67
70
  url = getRelativeUrlPath(url);
68
71
  url = url === '/' ? url : url.replace(/\/$/, '');
@@ -94,27 +97,28 @@ export function currentRoute() {
94
97
  export function isRoute(url, routeName) {
95
98
  url = getRelativeUrlPath(url);
96
99
  url = url === '/' ? url : url.replace(/\/$/, '');
97
- try {
98
- const routePattern = routes[routeName];
99
- const regexPattern = routePattern.replace(/\//g, '\\/').replace(/\{([^}]+):([^}]+)\}/g, (match, paramName, paramType) => {
100
- // Create a regex pattern based on the parameter type
101
- switch (paramType) {
102
- case 'str':
103
- case 'path':
104
- return '([^/]+)'; // Match any non-slash characters
105
- case 'uuid':
106
- 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
107
- default:
108
- return '([^/]+)'; // Default to match any non-slash characters
100
+ const routeNameRegex = routePatternToRegex(routeName);
101
+ for (const routeName in routes) {
102
+ if (routeNameRegex.test(routeName)) {
103
+ const routePattern = routes[routeName];
104
+ const regexPattern = routePattern.replace(/\//g, '\\/').replace(/\{([^}]+):([^}]+)\}/g, (match, paramName, paramType) => {
105
+ switch (paramType) {
106
+ case 'str':
107
+ case 'path':
108
+ return '([^/]+)';
109
+ case 'uuid':
110
+ return '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
111
+ default:
112
+ return '([^/]+)';
113
+ }
114
+ });
115
+ const regex = new RegExp(`^${regexPattern}$`);
116
+ if (regex.test(url)) {
117
+ return true;
109
118
  }
110
- });
111
- const regex = new RegExp(`^${regexPattern}$`);
112
- return regex.test(url);
113
- }
114
- catch (error) {
115
- console.error(error);
116
- return false;
119
+ }
117
120
  }
121
+ return false;
118
122
  }
119
123
  export function isCurrentRoute(routeName) {
120
124
  const currentRouteName = currentRoute();
@@ -122,7 +126,8 @@ export function isCurrentRoute(routeName) {
122
126
  console.error("Could not match current window location to a named route.");
123
127
  return false;
124
128
  }
125
- return currentRouteName == routeName;
129
+ const routeNameRegex = routePatternToRegex(routeName);
130
+ return routeNameRegex.test(currentRouteName);
126
131
  }
127
132
  globalThis.routes = globalThis.routes || {};
128
133
  globalThis.route = route;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "litestar-vite-plugin",
3
- "version": "0.6.4",
3
+ "version": "0.6.6",
4
4
  "type": "module",
5
5
  "description": "Litestar plugin for Vite.",
6
6
  "keywords": [