litestar-vite-plugin 0.6.2 → 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,7 +1,17 @@
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 getRelativeUrlPath(url: string): string;
4
+ export declare function toRoute(url: string): string | null;
5
+ export declare function currentRoute(): string | null;
6
+ export declare function isRoute(url: string, routeName: string): boolean;
7
+ export declare function isCurrentRoute(routeName: string): boolean;
2
8
  declare global {
3
9
  var routes: {
4
10
  [key: string]: string;
5
11
  };
12
+ function route(routeName: string, ...args: any[]): string;
13
+ function toRoute(url: string): string | null;
14
+ function currentRoute(): string | null;
15
+ function isRoute(url: string, routeName: string): boolean;
16
+ function isCurrentRoute(routeName: string): boolean;
6
17
  }
7
- export declare function route(routeName: string, ...args: any[]): string;
@@ -9,14 +9,13 @@ 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
16
  return "#"; // Return "#" to indicate failure
18
17
  }
19
- const argTokens = url.match(/<(\w+:?)?\w+>/g);
18
+ const argTokens = url.match(/\{([^}]+):([^}]+)\}/g);
20
19
  if (!argTokens && args.length > 0) {
21
20
  console.error(`Invalid URL lookup: URL '${routeName}' does not expect arguments.`);
22
21
  return "#";
@@ -54,3 +53,80 @@ export function route(routeName, ...args) {
54
53
  }
55
54
  return url;
56
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
+ }
66
+ export function toRoute(url) {
67
+ url = getRelativeUrlPath(url);
68
+ url = url === '/' ? url : url.replace(/\/$/, '');
69
+ for (const routeName in routes) {
70
+ const routePattern = routes[routeName];
71
+ const regexPattern = routePattern.replace(/\//g, '\\/').replace(/\{([^}]+):([^}]+)\}/g, (match, paramName, paramType) => {
72
+ // Create a regex pattern based on the parameter type
73
+ switch (paramType) {
74
+ case 'str':
75
+ case 'path':
76
+ return '([^/]+)'; // Match any non-slash characters
77
+ case 'uuid':
78
+ 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
79
+ default:
80
+ return '([^/]+)'; // Default to match any non-slash characters
81
+ }
82
+ });
83
+ const regex = new RegExp(`^${regexPattern}$`);
84
+ if (regex.test(url)) {
85
+ return routeName;
86
+ }
87
+ }
88
+ return null; // No matching route found
89
+ }
90
+ export function currentRoute() {
91
+ const currentUrl = window.location.pathname;
92
+ return toRoute(currentUrl);
93
+ }
94
+ export function isRoute(url, routeName) {
95
+ url = getRelativeUrlPath(url);
96
+ 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
109
+ }
110
+ });
111
+ const regex = new RegExp(`^${regexPattern}$`);
112
+ return regex.test(url);
113
+ }
114
+ catch (error) {
115
+ console.error(error);
116
+ return false;
117
+ }
118
+ }
119
+ export function isCurrentRoute(routeName) {
120
+ const currentRouteName = currentRoute();
121
+ if (!currentRouteName) {
122
+ console.error("Could not match current window location to a named route.");
123
+ return false;
124
+ }
125
+ return currentRouteName == routeName;
126
+ }
127
+ globalThis.routes = globalThis.routes || {};
128
+ globalThis.route = route;
129
+ globalThis.toRoute = toRoute;
130
+ globalThis.currentRoute = currentRoute;
131
+ globalThis.isRoute = isRoute;
132
+ globalThis.isCurrentRoute = isCurrentRoute;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "litestar-vite-plugin",
3
- "version": "0.6.2",
3
+ "version": "0.6.4",
4
4
  "type": "module",
5
5
  "description": "Litestar plugin for Vite.",
6
6
  "keywords": [