litestar-vite-plugin 0.6.1 → 0.6.3

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,7 +1,16 @@
1
1
  export declare function resolvePageComponent<T>(path: string | string[], pages: Record<string, Promise<T> | (() => Promise<T>)>): Promise<T>;
2
+ export declare function route(routeName: string, ...args: any[]): string;
3
+ export declare function toRoute(url: string): string | null;
4
+ export declare function currentRoute(): string | null;
5
+ export declare function isRoute(url: string, routeName: string): boolean;
6
+ export declare function isCurrentRoute(routeName: string): boolean;
2
7
  declare global {
3
8
  var routes: {
4
9
  [key: string]: string;
5
10
  };
11
+ function route(routeName: string, ...args: any[]): string;
12
+ function toRoute(url: string): string | null;
13
+ function currentRoute(): string | null;
14
+ function isRoute(url: string, routeName: string): boolean;
15
+ function isCurrentRoute(routeName: string): boolean;
6
16
  }
7
- export declare function route(routeName: string, ...args: any[]): string | null;
@@ -9,17 +9,16 @@ export async function resolvePageComponent(path, pages) {
9
9
  }
10
10
  throw new Error(`Page not found: ${path}`);
11
11
  }
12
- globalThis.routes = globalThis.routes || {};
13
12
  export function route(routeName, ...args) {
14
13
  let url = globalThis.routes[routeName];
15
14
  if (!url) {
16
15
  console.error(`URL '${routeName}' not found in routes.`);
17
- return null; // Return null to indicate failure
16
+ return "#"; // Return "#" to indicate failure
18
17
  }
19
- const argTokens = url.match(/<(\w+:?)?\w+>/g);
18
+ const argTokens = url.match(/{(\w+:?)?\w+}/g);
20
19
  if (!argTokens && args.length > 0) {
21
20
  console.error(`Invalid URL lookup: URL '${routeName}' does not expect arguments.`);
22
- return null;
21
+ return "#";
23
22
  }
24
23
  try {
25
24
  if (typeof args[0] === "object" && !Array.isArray(args[0])) {
@@ -50,7 +49,74 @@ export function route(routeName, ...args) {
50
49
  }
51
50
  catch (error) {
52
51
  console.error(error.message);
53
- return null; // Return null to indicate failure
52
+ return "#";
54
53
  }
55
54
  return url;
56
55
  }
56
+ export function toRoute(url) {
57
+ for (const routeName in routes) {
58
+ const routePattern = routes[routeName];
59
+ const regexPattern = routePattern
60
+ .replace(/{\w+:(\w+)}/g, (match, paramName, paramType) => {
61
+ // Create a regex pattern based on the parameter type
62
+ switch (paramType) {
63
+ case 'str':
64
+ case 'path':
65
+ return '([^/]+)'; // Match any non-slash characters
66
+ case 'uuid':
67
+ 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
68
+ default:
69
+ return '([^/]+)'; // Default to match any non-slash characters
70
+ }
71
+ })
72
+ .replace(/\//g, '\\/'); // Escape slashes for regex
73
+ const regex = new RegExp('^' + regexPattern);
74
+ if (regex.test(url)) {
75
+ return routeName;
76
+ }
77
+ }
78
+ return null; // No matching route found
79
+ }
80
+ export function currentRoute() {
81
+ const currentUrl = window.location.pathname;
82
+ return toRoute(currentUrl);
83
+ }
84
+ 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
98
+ }
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;
107
+ }
108
+ }
109
+ export function isCurrentRoute(routeName) {
110
+ const currentRouteName = currentRoute();
111
+ if (!currentRouteName) {
112
+ console.error("Could not match current window location to a named route.");
113
+ return false;
114
+ }
115
+ return currentRouteName == routeName;
116
+ }
117
+ globalThis.routes = globalThis.routes || {};
118
+ globalThis.route = route;
119
+ globalThis.toRoute = toRoute;
120
+ globalThis.currentRoute = currentRoute;
121
+ globalThis.isRoute = isRoute;
122
+ globalThis.isCurrentRoute = isCurrentRoute;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "litestar-vite-plugin",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "type": "module",
5
5
  "description": "Litestar plugin for Vite.",
6
6
  "keywords": [