litestar-vite-plugin 0.6.3 → 0.6.4

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,10 +92,11 @@ export function currentRoute() {
82
92
  return toRoute(currentUrl);
83
93
  }
84
94
  export function isRoute(url, routeName) {
95
+ url = getRelativeUrlPath(url);
96
+ url = url === '/' ? url : url.replace(/\/$/, '');
85
97
  try {
86
98
  const routePattern = routes[routeName];
87
- const regexPattern = routePattern
88
- .replace(/{\w+:(\w+)}/g, (match, paramName, paramType) => {
99
+ const regexPattern = routePattern.replace(/\//g, '\\/').replace(/\{([^}]+):([^}]+)\}/g, (match, paramName, paramType) => {
89
100
  // Create a regex pattern based on the parameter type
90
101
  switch (paramType) {
91
102
  case 'str':
@@ -96,9 +107,8 @@ export function isRoute(url, routeName) {
96
107
  default:
97
108
  return '([^/]+)'; // Default to match any non-slash characters
98
109
  }
99
- })
100
- .replace(/\//g, '\\/'); // Escape slashes for regex
101
- const regex = new RegExp('^' + regexPattern);
110
+ });
111
+ const regex = new RegExp(`^${regexPattern}$`);
102
112
  return regex.test(url);
103
113
  }
104
114
  catch (error) {
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.4",
4
4
  "type": "module",
5
5
  "description": "Litestar plugin for Vite.",
6
6
  "keywords": [