Code Generation¶
Code generation utilities for extracting route metadata and generating type-safe route definitions.
Features¶
Extract route metadata from Litestar applications
Generate Ziggy-compatible routes.json
Generate TypeScript route definitions with type safety
Support for path and query parameters with OpenAPI type enrichment
Inertia page props metadata extraction
Public code generation API.
This package provides code generation utilities for:
Unified asset export (
export_integration_assets)Route metadata export (
routes.json+ Ziggy-compatible TS)Inertia page props metadata export
Internal implementation details (OpenAPI integration, TypeScript conversion) are kept in private submodules to keep the public API clean.
- class litestar_vite.codegen.ExportResult[source]¶
Bases:
objectResult of the export operation.
- class litestar_vite.codegen.InertiaPageMetadata[source]¶
Bases:
objectMetadata for a single Inertia page component.
- litestar_vite.codegen.encode_deterministic_json(data: dict[str, Any], *, indent: int = 2, serializer: Callable[[Any], bytes] | None = None) bytes[source]¶
Encode JSON with sorted keys for deterministic output.
This is a wrapper that ensures all nested dict keys are sorted before serialization, producing byte-identical output for the same input data regardless of insertion order.
- litestar_vite.codegen.export_integration_assets(app: Litestar, config: ViteConfig, *, serializer: Callable[[Any], bytes] | None = None) ExportResult[source]¶
Export all integration artifacts with deterministic output.
This is the single source of truth for code generation. Both CLI commands and Plugin startup should call this function to ensure byte-identical output.
The export order is critical: 1. Register Inertia page prop types in OpenAPI schema (mutates schema_dict) 2. Export openapi.json (now includes session prop types) 3. Export routes.json (uses schema for component refs) 4. Export routes.ts (if enabled) 5. Export inertia-pages.json (if enabled)
- litestar_vite.codegen.extract_inertia_pages(app: Litestar, *, openapi_schema: dict[str, Any] | None = None, fallback_type: str = 'unknown', openapi_support: OpenAPISupport | None = None) list[InertiaPageMetadata][source]¶
Extract Inertia page metadata from an application.
When multiple handlers map to the same component, GET handlers are preferred since Inertia pages are typically loaded via GET requests.
- Parameters:
- Returns:
List of InertiaPageMetadata for each discovered page.
- litestar_vite.codegen.extract_route_metadata(app: Litestar, *, only: list[str] | None = None, exclude: list[str] | None = None, openapi_schema: dict[str, Any] | None = None) list[RouteMetadata][source]¶
Extract route metadata from a Litestar application.
Note
openapi_schemais accepted for API compatibility and future enrichment, but parameter typing is currently derived from Litestar’s OpenAPI parameter generation, not the exported schema document.- Returns:
A list of RouteMetadata objects.
- litestar_vite.codegen.generate_inertia_pages_json(app: Litestar, *, openapi_schema: dict[str, Any] | None = None, include_default_auth: bool = True, include_default_flash: bool = True, inertia_config: InertiaConfig | None = None, types_config: TypeGenConfig | None = None) dict[str, Any][source]¶
Generate Inertia pages metadata JSON.
The output is deterministic: all dict keys are sorted alphabetically to produce byte-identical output for the same input data.
A single OpenAPISupport instance is shared across both page extraction and shared props building to ensure consistent schema registration and naming. This eliminates non-determinism from split schema registries.
- Returns:
An Inertia pages metadata payload as a dictionary with sorted keys.
- litestar_vite.codegen.generate_routes_json(app: Litestar, *, only: list[str] | None = None, exclude: list[str] | None = None, include_components: bool = False, openapi_schema: dict[str, Any] | None = None) dict[str, Any][source]¶
Generate Ziggy-compatible routes JSON.
The output is deterministic: routes are sorted by name to produce byte-identical output for the same input data.
- Returns:
A Ziggy-compatible routes payload as a dictionary with sorted keys.
- litestar_vite.codegen.generate_routes_ts(app: Litestar, *, only: list[str] | None = None, exclude: list[str] | None = None, openapi_schema: dict[str, Any] | None = None, global_route: bool = False) str[source]¶
Generate typed routes TypeScript file (Ziggy-style).
The output is deterministic: routes are sorted by name to produce byte-identical output for the same input data.
- Returns:
The generated TypeScript source.
- litestar_vite.codegen.strip_timestamp_for_comparison(content: bytes) bytes[source]¶
Remove generatedAt and other timestamp fields for content comparison.
This allows comparing file content while ignoring fields that change on every generation (like timestamps).
- Parameters:
content¶ – JSON content as bytes.
- Returns:
JSON content with timestamp fields removed, sorted keys.
- litestar_vite.codegen.write_if_changed(path: Path, content: bytes | str, *, normalize_for_comparison: Callable[[bytes], bytes] | None = None, encoding: str = 'utf-8') bool[source]¶
Write content to file only if it differs from the existing content.
Uses hash comparison to avoid unnecessary writes that would trigger file watchers and unnecessary rebuilds. Optionally normalizes content before comparison (e.g., to strip timestamps).
- Parameters:
path¶ – The file path to write to.
content¶ – The content to write (bytes or str).
normalize_for_comparison¶ – Optional callback to normalize content before comparison (e.g., strip timestamps). The file is written with the original content, not the normalized version.
encoding¶ – Encoding for string content.
- Returns:
True if file was written (content changed), False if skipped (unchanged).