litestar-vite-plugin 0.15.0-beta.5 → 0.15.0-beta.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.
@@ -1,151 +0,0 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- async function emitRouteTypes(routesPath, outputDir, options = {}) {
4
- const { globalRoute = false, declareGlobalVars = false } = options;
5
- const contents = await fs.promises.readFile(routesPath, "utf-8");
6
- const json = JSON.parse(contents);
7
- const outDir = path.resolve(process.cwd(), outputDir);
8
- await fs.promises.mkdir(outDir, { recursive: true });
9
- const outFile = path.join(outDir, "routes.ts");
10
- const banner = `// AUTO-GENERATED by litestar-vite. Do not edit.
11
- /* eslint-disable */
12
-
13
- `;
14
- const routesData = json.routes || json;
15
- const routeNames = Object.keys(routesData);
16
- const routeNameType = routeNames.length > 0 ? routeNames.map((n) => `"${n}"`).join(" | ") : "never";
17
- const routeParamTypes = [];
18
- for (const [name, data] of Object.entries(routesData)) {
19
- if (data.parameters && data.parameters.length > 0) {
20
- const params = data.parameters.map((p) => `${p}: string | number`).join("; ");
21
- routeParamTypes.push(` "${name}": { ${params} }`);
22
- } else {
23
- routeParamTypes.push(` "${name}": Record<string, never>`);
24
- }
25
- }
26
- let globalDeclarations = `declare global {
27
- interface Window {
28
- /**
29
- * Fully-typed route metadata injected by Litestar.
30
- */
31
- __LITESTAR_ROUTES__?: typeof routesMeta
32
- /**
33
- * Simple route map (name -> uri) for legacy consumers.
34
- */
35
- routes?: ${declareGlobalVars ? "typeof routes" : "Record<string, string>"}
36
- serverRoutes?: ${declareGlobalVars ? "typeof serverRoutes" : "Record<string, string>"}`;
37
- if (globalRoute) {
38
- globalDeclarations += `
39
- /**
40
- * Global route helper (available when globalRoute=true in TypeGenConfig).
41
- * @see route
42
- */
43
- route?: typeof route`;
44
- }
45
- globalDeclarations += `
46
- }`;
47
- if (declareGlobalVars) {
48
- globalDeclarations += `
49
- // eslint-disable-next-line no-var
50
- var routes: typeof routes | undefined
51
- var serverRoutes: typeof serverRoutes | undefined`;
52
- }
53
- globalDeclarations += `
54
- }`;
55
- const globalRouteRegistration = globalRoute ? `
56
-
57
- // Register route() globally on window for Laravel/Ziggy-style usage
58
- if (typeof window !== "undefined") {
59
- window.route = route
60
- }
61
- ` : "";
62
- const body = `/**
63
- * AUTO-GENERATED by litestar-vite.
64
- *
65
- * Exports:
66
- * - routesMeta: full route metadata
67
- * - routes: name -> uri map
68
- * - serverRoutes: alias of routes for clarity in apps
69
- * - route(): type-safe URL generator
70
- * - hasRoute(): type guard
71
- * - csrf helpers re-exported from litestar-vite-plugin/helpers
72
- *
73
- * @see https://litestar-vite.litestar.dev/
74
- */
75
- export const routesMeta = ${JSON.stringify(json, null, 2)} as const
76
-
77
- /**
78
- * Route name to URI mapping.
79
- */
80
- export const routes = ${JSON.stringify(Object.fromEntries(Object.entries(routesData).map(([name, data]) => [name, data.uri])), null, 2)} as const
81
-
82
- /**
83
- * Alias for server-injected route map (more descriptive for consumers).
84
- */
85
- export const serverRoutes = routes
86
-
87
- /**
88
- * All available route names.
89
- */
90
- export type RouteName = ${routeNameType}
91
-
92
- /**
93
- * Parameter types for each route.
94
- */
95
- export interface RouteParams {
96
- ${routeParamTypes.join("\n")}
97
- }
98
-
99
- /**
100
- * Generate a URL for a named route with type-safe parameters.
101
- *
102
- * @param name - The route name
103
- * @param params - Route parameters (required if route has path parameters)
104
- * @returns The generated URL
105
- *
106
- * @example
107
- * \`\`\`ts
108
- * import { route } from '@/generated/routes'
109
- *
110
- * // Route without parameters
111
- * route('home') // "/"
112
- *
113
- * // Route with parameters
114
- * route('user:detail', { user_id: 123 }) // "/users/123"
115
- * \`\`\`
116
- */
117
- export function route<T extends RouteName>(
118
- name: T,
119
- ...args: RouteParams[T] extends Record<string, never> ? [] : [params: RouteParams[T]]
120
- ): string {
121
- let uri = routes[name] as string
122
- const params = args[0] as Record<string, string | number> | undefined
123
-
124
- if (params) {
125
- for (const [key, value] of Object.entries(params)) {
126
- // Handle both {param} and {param:type} syntax
127
- uri = uri.replace(new RegExp(\`\\\\{\${key}(?::[^}]+)?\\\\}\`, "g"), String(value))
128
- }
129
- }
130
-
131
- return uri
132
- }
133
-
134
- /**
135
- * Check if a route name exists.
136
- */
137
- export function hasRoute(name: string): name is RouteName {
138
- return name in routes
139
- }
140
-
141
- ${globalDeclarations}
142
-
143
- // Re-export helper functions from litestar-vite-plugin
144
- // These work with the routes defined above
145
- export { getCsrfToken, csrfHeaders, csrfFetch } from "litestar-vite-plugin/helpers"
146
- ${globalRouteRegistration}`;
147
- await fs.promises.writeFile(outFile, `${banner}${body}`, "utf-8");
148
- }
149
- export {
150
- emitRouteTypes
151
- };