litestar-vite-plugin 0.15.0-beta.5 → 0.15.0-rc.1
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.
- package/dist/js/astro.d.ts +27 -3
- package/dist/js/astro.js +55 -28
- package/dist/js/helpers/htmx.js +48 -15
- package/dist/js/helpers/index.d.ts +1 -1
- package/dist/js/helpers/index.js +1 -1
- package/dist/js/index.d.ts +26 -49
- package/dist/js/index.js +38 -374
- package/dist/js/inertia-helpers/index.d.ts +3 -4
- package/dist/js/inertia-helpers/index.js +3 -8
- package/dist/js/nuxt.d.ts +29 -32
- package/dist/js/nuxt.js +59 -35
- package/dist/js/shared/bridge-schema.d.ts +51 -0
- package/dist/js/shared/bridge-schema.js +178 -0
- package/dist/js/shared/emit-page-props-types.d.ts +4 -0
- package/dist/js/shared/emit-page-props-types.js +268 -0
- package/dist/js/shared/format-path.d.ts +0 -9
- package/dist/js/shared/format-path.js +1 -5
- package/dist/js/shared/typegen-plugin.d.ts +35 -0
- package/dist/js/shared/typegen-plugin.js +178 -0
- package/dist/js/sveltekit.d.ts +30 -6
- package/dist/js/sveltekit.js +35 -26
- package/package.json +3 -5
- package/dist/js/shared/create-type-gen-plugin.d.ts +0 -99
- package/dist/js/shared/create-type-gen-plugin.js +0 -110
- package/dist/js/shared/emit-route-types.d.ts +0 -41
- package/dist/js/shared/emit-route-types.js +0 -151
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared route type generation utility.
|
|
3
|
-
*
|
|
4
|
-
* Generates TypeScript types from routes.json metadata for type-safe routing.
|
|
5
|
-
* Used by the main litestar plugin and framework integrations (Astro, Nuxt, SvelteKit).
|
|
6
|
-
*
|
|
7
|
-
* @module
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Options for emitRouteTypes.
|
|
11
|
-
*/
|
|
12
|
-
export interface EmitRouteTypesOptions {
|
|
13
|
-
/**
|
|
14
|
-
* Whether to register route() on window for global access.
|
|
15
|
-
* Only used by the main plugin.
|
|
16
|
-
* @default false
|
|
17
|
-
*/
|
|
18
|
-
globalRoute?: boolean;
|
|
19
|
-
/**
|
|
20
|
-
* Whether to declare global `var routes` and `var serverRoutes`.
|
|
21
|
-
* Used by framework integrations for SSR compatibility.
|
|
22
|
-
* @default false
|
|
23
|
-
*/
|
|
24
|
-
declareGlobalVars?: boolean;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Generate TypeScript route types from routes.json metadata.
|
|
28
|
-
*
|
|
29
|
-
* Creates a routes.ts file with:
|
|
30
|
-
* - routesMeta: full route metadata
|
|
31
|
-
* - routes: name -> uri map
|
|
32
|
-
* - serverRoutes: alias of routes
|
|
33
|
-
* - route(): type-safe URL generator
|
|
34
|
-
* - hasRoute(): type guard
|
|
35
|
-
* - CSRF helpers re-exported from litestar-vite-plugin/helpers
|
|
36
|
-
*
|
|
37
|
-
* @param routesPath - Path to routes.json file
|
|
38
|
-
* @param outputDir - Output directory for routes.ts
|
|
39
|
-
* @param options - Generation options
|
|
40
|
-
*/
|
|
41
|
-
export declare function emitRouteTypes(routesPath: string, outputDir: string, options?: EmitRouteTypesOptions): Promise<void>;
|
|
@@ -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
|
-
};
|