litestar-vite-plugin 0.14.0 → 0.15.0-alpha.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/README.md +64 -108
- package/dist/js/astro.d.ts +157 -0
- package/dist/js/astro.js +70 -0
- package/dist/js/dev-server-index.html +136 -142
- package/dist/js/helpers/csrf.d.ts +76 -0
- package/dist/js/helpers/csrf.js +114 -0
- package/dist/js/helpers/index.d.ts +24 -0
- package/dist/js/helpers/index.js +26 -0
- package/dist/js/helpers/routes.d.ts +140 -0
- package/dist/js/helpers/routes.js +280 -0
- package/dist/js/index.d.ts +86 -1
- package/dist/js/index.js +428 -58
- package/dist/js/inertia-helpers/helpers/csrf.d.ts +76 -0
- package/dist/js/inertia-helpers/helpers/csrf.js +114 -0
- package/dist/js/inertia-helpers/helpers/index.d.ts +24 -0
- package/dist/js/inertia-helpers/helpers/index.js +26 -0
- package/dist/js/inertia-helpers/helpers/routes.d.ts +140 -0
- package/dist/js/inertia-helpers/helpers/routes.js +280 -0
- package/dist/js/inertia-helpers/inertia-helpers/index.d.ts +33 -0
- package/dist/js/inertia-helpers/inertia-helpers/index.js +47 -0
- package/dist/js/install-hint.d.ts +1 -0
- package/dist/js/install-hint.js +21 -0
- package/dist/js/litestar-meta.d.ts +18 -0
- package/dist/js/litestar-meta.js +84 -0
- package/dist/js/nuxt.d.ts +193 -0
- package/dist/js/nuxt.js +197 -0
- package/dist/js/sveltekit.d.ts +158 -0
- package/dist/js/sveltekit.js +168 -0
- package/package.json +25 -5
- package/dist/js/inertia-helpers/index.d.ts +0 -20
- package/dist/js/inertia-helpers/index.js +0 -138
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inertia.js helpers for Litestar applications.
|
|
3
|
+
*
|
|
4
|
+
* This module re-exports common helpers from litestar-vite-plugin/helpers
|
|
5
|
+
* and adds Inertia-specific utilities.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
// Re-export all helpers from the main helpers module
|
|
10
|
+
export {
|
|
11
|
+
// CSRF utilities
|
|
12
|
+
getCsrfToken, csrfHeaders, csrfFetch,
|
|
13
|
+
// Route utilities
|
|
14
|
+
route, getRoutes, toRoute, currentRoute, isRoute, isCurrentRoute, getRelativeUrlPath, } from "../helpers/index.js";
|
|
15
|
+
/**
|
|
16
|
+
* Resolve a page component from a glob import.
|
|
17
|
+
*
|
|
18
|
+
* Used with Inertia.js to dynamically import page components.
|
|
19
|
+
*
|
|
20
|
+
* @param path - Component path or array of paths to try
|
|
21
|
+
* @param pages - Glob import result (e.g., import.meta.glob('./pages/**\/*.vue'))
|
|
22
|
+
* @returns Promise resolving to the component
|
|
23
|
+
* @throws Error if no matching component is found
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { resolvePageComponent } from 'litestar-vite-plugin/inertia-helpers'
|
|
28
|
+
*
|
|
29
|
+
* createInertiaApp({
|
|
30
|
+
* resolve: (name) => resolvePageComponent(
|
|
31
|
+
* `./pages/${name}.vue`,
|
|
32
|
+
* import.meta.glob('./pages/**\/*.vue')
|
|
33
|
+
* ),
|
|
34
|
+
* // ...
|
|
35
|
+
* })
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export async function resolvePageComponent(path, pages) {
|
|
39
|
+
for (const p of Array.isArray(path) ? path : [path]) {
|
|
40
|
+
const page = pages[p];
|
|
41
|
+
if (typeof page === "undefined") {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
return typeof page === "function" ? page() : page;
|
|
45
|
+
}
|
|
46
|
+
throw new Error(`Page not found: ${path}`);
|
|
47
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function resolveInstallHint(pkg?: string): string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
function resolveInstallHint(pkg = "@hey-api/openapi-ts") {
|
|
2
|
+
const runtime = (process.env.LITESTAR_VITE_RUNTIME ?? "").toLowerCase();
|
|
3
|
+
switch (runtime) {
|
|
4
|
+
case "bun":
|
|
5
|
+
return `bun add -d ${pkg}`;
|
|
6
|
+
case "deno":
|
|
7
|
+
return `deno add -d npm:${pkg}`;
|
|
8
|
+
case "pnpm":
|
|
9
|
+
return `pnpm add -D ${pkg}`;
|
|
10
|
+
case "yarn":
|
|
11
|
+
return `yarn add -D ${pkg}`;
|
|
12
|
+
}
|
|
13
|
+
const envInstall = process.env.LITESTAR_VITE_INSTALL_CMD?.trim();
|
|
14
|
+
if (envInstall) {
|
|
15
|
+
return `${envInstall} -D ${pkg}`;
|
|
16
|
+
}
|
|
17
|
+
return `npm install -D ${pkg}`;
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
resolveInstallHint
|
|
21
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ResolvedConfig } from "vite";
|
|
2
|
+
export interface LitestarMeta {
|
|
3
|
+
litestarVersion?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface BackendStatus {
|
|
6
|
+
available: boolean;
|
|
7
|
+
url?: string;
|
|
8
|
+
error?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Check if the Litestar backend is reachable at the given URL.
|
|
12
|
+
*
|
|
13
|
+
* Uses a GET request to the OpenAPI schema endpoint as a lightweight
|
|
14
|
+
* health check, since it's typically enabled and responds quickly.
|
|
15
|
+
* The schema path is read from LITESTAR_OPENAPI_PATH env var (default: "/schema").
|
|
16
|
+
*/
|
|
17
|
+
export declare function checkBackendAvailability(appUrl: string): Promise<BackendStatus>;
|
|
18
|
+
export declare function loadLitestarMeta(resolvedConfig: ResolvedConfig, routesPathHint?: string): Promise<LitestarMeta>;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
async function checkBackendAvailability(appUrl) {
|
|
4
|
+
if (!appUrl || appUrl === "undefined") {
|
|
5
|
+
return {
|
|
6
|
+
available: false,
|
|
7
|
+
error: "APP_URL not configured"
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
try {
|
|
11
|
+
const controller = new AbortController();
|
|
12
|
+
const timeout = setTimeout(() => controller.abort(), 2e3);
|
|
13
|
+
const schemaPath = process.env.LITESTAR_OPENAPI_PATH || "/schema";
|
|
14
|
+
const checkUrl = new URL(schemaPath, appUrl).href;
|
|
15
|
+
const response = await fetch(checkUrl, {
|
|
16
|
+
method: "GET",
|
|
17
|
+
signal: controller.signal
|
|
18
|
+
});
|
|
19
|
+
clearTimeout(timeout);
|
|
20
|
+
return {
|
|
21
|
+
available: response.ok || response.status < 500,
|
|
22
|
+
url: appUrl
|
|
23
|
+
};
|
|
24
|
+
} catch (err) {
|
|
25
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
26
|
+
if (message.includes("ECONNREFUSED") || message.includes("fetch failed")) {
|
|
27
|
+
return {
|
|
28
|
+
available: false,
|
|
29
|
+
url: appUrl,
|
|
30
|
+
error: "Backend not reachable (is Litestar running?)"
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
if (message.includes("aborted")) {
|
|
34
|
+
return {
|
|
35
|
+
available: false,
|
|
36
|
+
url: appUrl,
|
|
37
|
+
error: "Backend connection timeout"
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
available: false,
|
|
42
|
+
url: appUrl,
|
|
43
|
+
error: message
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function readJson(file) {
|
|
48
|
+
try {
|
|
49
|
+
const raw = fs.readFileSync(file, "utf8");
|
|
50
|
+
return JSON.parse(raw);
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function firstExisting(paths) {
|
|
56
|
+
for (const p of paths) {
|
|
57
|
+
if (fs.existsSync(p)) return p;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
async function loadLitestarMeta(resolvedConfig, routesPathHint) {
|
|
62
|
+
const fromEnv = process.env.LITESTAR_VERSION?.trim();
|
|
63
|
+
if (fromEnv) {
|
|
64
|
+
return { litestarVersion: fromEnv };
|
|
65
|
+
}
|
|
66
|
+
const root = resolvedConfig.root ?? process.cwd();
|
|
67
|
+
const candidates = [routesPathHint ? path.resolve(root, routesPathHint) : null, path.resolve(root, "src/generated/routes.json"), path.resolve(root, "routes.json")].filter(
|
|
68
|
+
Boolean
|
|
69
|
+
);
|
|
70
|
+
const match = firstExisting(candidates);
|
|
71
|
+
if (!match) return {};
|
|
72
|
+
const data = readJson(match);
|
|
73
|
+
if (!data) return {};
|
|
74
|
+
const fromData = (key) => {
|
|
75
|
+
const value = data[key];
|
|
76
|
+
return typeof value === "string" ? value : null;
|
|
77
|
+
};
|
|
78
|
+
const litestarVersion = fromData("litestar_version") ?? fromData("litestarVersion") ?? fromData("version");
|
|
79
|
+
return litestarVersion ? { litestarVersion } : {};
|
|
80
|
+
}
|
|
81
|
+
export {
|
|
82
|
+
checkBackendAvailability,
|
|
83
|
+
loadLitestarMeta
|
|
84
|
+
};
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nuxt module for Litestar-Vite.
|
|
3
|
+
*
|
|
4
|
+
* This module provides seamless integration between Nuxt 3+ and Litestar backend.
|
|
5
|
+
* It enables:
|
|
6
|
+
* - API proxy configuration for dev server
|
|
7
|
+
* - Type generation integration (shares @hey-api/openapi-ts output)
|
|
8
|
+
* - Server-side and client-side API access patterns
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // nuxt.config.ts
|
|
13
|
+
* export default defineNuxtConfig({
|
|
14
|
+
* modules: ['litestar-vite-plugin/nuxt'],
|
|
15
|
+
* litestar: {
|
|
16
|
+
* apiProxy: 'http://localhost:8000',
|
|
17
|
+
* apiPrefix: '/api',
|
|
18
|
+
* types: {
|
|
19
|
+
* enabled: true,
|
|
20
|
+
* output: 'types/api',
|
|
21
|
+
* },
|
|
22
|
+
* },
|
|
23
|
+
* });
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @module
|
|
27
|
+
*/
|
|
28
|
+
import type { Plugin } from "vite";
|
|
29
|
+
/**
|
|
30
|
+
* Configuration for TypeScript type generation in Nuxt.
|
|
31
|
+
*/
|
|
32
|
+
export interface NuxtTypesConfig {
|
|
33
|
+
/**
|
|
34
|
+
* Enable type generation.
|
|
35
|
+
*
|
|
36
|
+
* @default false
|
|
37
|
+
*/
|
|
38
|
+
enabled?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Path to output generated TypeScript types.
|
|
41
|
+
* Relative to the Nuxt project root.
|
|
42
|
+
*
|
|
43
|
+
* @default 'types/api'
|
|
44
|
+
*/
|
|
45
|
+
output?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Path where the OpenAPI schema is exported by Litestar.
|
|
48
|
+
*
|
|
49
|
+
* @default 'openapi.json'
|
|
50
|
+
*/
|
|
51
|
+
openapiPath?: string;
|
|
52
|
+
/**
|
|
53
|
+
* Path where route metadata is exported by Litestar.
|
|
54
|
+
*
|
|
55
|
+
* @default 'routes.json'
|
|
56
|
+
*/
|
|
57
|
+
routesPath?: string;
|
|
58
|
+
/**
|
|
59
|
+
* Generate Zod schemas in addition to TypeScript types.
|
|
60
|
+
*
|
|
61
|
+
* @default false
|
|
62
|
+
*/
|
|
63
|
+
generateZod?: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Debounce time in milliseconds for type regeneration.
|
|
66
|
+
*
|
|
67
|
+
* @default 300
|
|
68
|
+
*/
|
|
69
|
+
debounce?: number;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Configuration options for the Litestar Nuxt module.
|
|
73
|
+
*/
|
|
74
|
+
export interface LitestarNuxtConfig {
|
|
75
|
+
/**
|
|
76
|
+
* URL of the Litestar API backend for proxying requests during development.
|
|
77
|
+
*
|
|
78
|
+
* @example 'http://localhost:8000'
|
|
79
|
+
* @default 'http://localhost:8000'
|
|
80
|
+
*/
|
|
81
|
+
apiProxy?: string;
|
|
82
|
+
/**
|
|
83
|
+
* API route prefix to proxy to the Litestar backend.
|
|
84
|
+
* Requests matching this prefix will be forwarded to the apiProxy URL.
|
|
85
|
+
*
|
|
86
|
+
* @example '/api'
|
|
87
|
+
* @default '/api'
|
|
88
|
+
*/
|
|
89
|
+
apiPrefix?: string;
|
|
90
|
+
/**
|
|
91
|
+
* Enable and configure TypeScript type generation.
|
|
92
|
+
*
|
|
93
|
+
* When set to `true`, enables type generation with default settings.
|
|
94
|
+
* When set to a NuxtTypesConfig object, enables type generation with custom settings.
|
|
95
|
+
*
|
|
96
|
+
* @default false
|
|
97
|
+
*/
|
|
98
|
+
types?: boolean | NuxtTypesConfig;
|
|
99
|
+
/**
|
|
100
|
+
* Enable verbose logging for debugging.
|
|
101
|
+
*
|
|
102
|
+
* @default false
|
|
103
|
+
*/
|
|
104
|
+
verbose?: boolean;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Litestar Vite plugins for Nuxt.
|
|
108
|
+
*
|
|
109
|
+
* This function returns an array of Vite plugins that can be added to Nuxt's
|
|
110
|
+
* vite configuration. For the full Nuxt module experience, use the module
|
|
111
|
+
* directly in your nuxt.config.ts.
|
|
112
|
+
*
|
|
113
|
+
* @param userConfig - Configuration options
|
|
114
|
+
* @returns Array of Vite plugins
|
|
115
|
+
*
|
|
116
|
+
* @example Using as Vite plugins in nuxt.config.ts
|
|
117
|
+
* ```typescript
|
|
118
|
+
* import { litestarPlugins } from 'litestar-vite-plugin/nuxt';
|
|
119
|
+
*
|
|
120
|
+
* export default defineNuxtConfig({
|
|
121
|
+
* vite: {
|
|
122
|
+
* plugins: [
|
|
123
|
+
* ...litestarPlugins({
|
|
124
|
+
* apiProxy: 'http://localhost:8000',
|
|
125
|
+
* types: true,
|
|
126
|
+
* }),
|
|
127
|
+
* ],
|
|
128
|
+
* },
|
|
129
|
+
* });
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
export declare function litestarPlugins(userConfig?: LitestarNuxtConfig): Plugin[];
|
|
133
|
+
/**
|
|
134
|
+
* Nuxt module definition for Litestar integration.
|
|
135
|
+
*
|
|
136
|
+
* This is a minimal module interface that can be used with Nuxt's module system.
|
|
137
|
+
* For full type safety, install @nuxt/kit as a dev dependency.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```typescript
|
|
141
|
+
* // nuxt.config.ts
|
|
142
|
+
* export default defineNuxtConfig({
|
|
143
|
+
* modules: ['litestar-vite-plugin/nuxt'],
|
|
144
|
+
* litestar: {
|
|
145
|
+
* apiProxy: 'http://localhost:8000',
|
|
146
|
+
* apiPrefix: '/api',
|
|
147
|
+
* types: {
|
|
148
|
+
* enabled: true,
|
|
149
|
+
* output: 'types/api',
|
|
150
|
+
* },
|
|
151
|
+
* },
|
|
152
|
+
* });
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* @example Using generated types in a composable
|
|
156
|
+
* ```typescript
|
|
157
|
+
* // composables/useApi.ts
|
|
158
|
+
* import type { User } from '~/types/api/types.gen';
|
|
159
|
+
* import { route } from '~/types/api/routes';
|
|
160
|
+
*
|
|
161
|
+
* export async function useUser(id: string) {
|
|
162
|
+
* const { data } = await useFetch<User>(route('users.show', { id }));
|
|
163
|
+
* return data;
|
|
164
|
+
* }
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export declare const litestarModule: {
|
|
168
|
+
meta: {
|
|
169
|
+
name: string;
|
|
170
|
+
configKey: string;
|
|
171
|
+
compatibility: {
|
|
172
|
+
nuxt: string;
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
defaults: {
|
|
176
|
+
apiProxy: string;
|
|
177
|
+
apiPrefix: string;
|
|
178
|
+
types: false;
|
|
179
|
+
verbose: false;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* Setup function for the Nuxt module.
|
|
183
|
+
* This is called by Nuxt when the module is loaded.
|
|
184
|
+
*/
|
|
185
|
+
setup(userOptions: LitestarNuxtConfig, nuxt: {
|
|
186
|
+
options: {
|
|
187
|
+
vite: {
|
|
188
|
+
plugins?: Plugin[];
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
}): void;
|
|
192
|
+
};
|
|
193
|
+
export default litestarModule;
|
package/dist/js/nuxt.js
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { exec } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import colors from "picocolors";
|
|
6
|
+
import { resolveInstallHint } from "./install-hint.js";
|
|
7
|
+
const execAsync = promisify(exec);
|
|
8
|
+
function resolveConfig(config = {}) {
|
|
9
|
+
let typesConfig = false;
|
|
10
|
+
if (config.types === true) {
|
|
11
|
+
typesConfig = {
|
|
12
|
+
enabled: true,
|
|
13
|
+
output: "types/api",
|
|
14
|
+
openapiPath: "openapi.json",
|
|
15
|
+
routesPath: "routes.json",
|
|
16
|
+
generateZod: false,
|
|
17
|
+
debounce: 300
|
|
18
|
+
};
|
|
19
|
+
} else if (typeof config.types === "object" && config.types !== null) {
|
|
20
|
+
typesConfig = {
|
|
21
|
+
enabled: config.types.enabled ?? true,
|
|
22
|
+
output: config.types.output ?? "types/api",
|
|
23
|
+
openapiPath: config.types.openapiPath ?? "openapi.json",
|
|
24
|
+
routesPath: config.types.routesPath ?? "routes.json",
|
|
25
|
+
generateZod: config.types.generateZod ?? false,
|
|
26
|
+
debounce: config.types.debounce ?? 300
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
apiProxy: config.apiProxy ?? "http://localhost:8000",
|
|
31
|
+
apiPrefix: config.apiPrefix ?? "/api",
|
|
32
|
+
types: typesConfig,
|
|
33
|
+
verbose: config.verbose ?? false
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function debounce(func, wait) {
|
|
37
|
+
let timeout = null;
|
|
38
|
+
return (...args) => {
|
|
39
|
+
if (timeout) {
|
|
40
|
+
clearTimeout(timeout);
|
|
41
|
+
}
|
|
42
|
+
timeout = setTimeout(() => func(...args), wait);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function createProxyPlugin(config) {
|
|
46
|
+
return {
|
|
47
|
+
name: "litestar-nuxt-proxy",
|
|
48
|
+
config() {
|
|
49
|
+
return {
|
|
50
|
+
server: {
|
|
51
|
+
proxy: {
|
|
52
|
+
[config.apiPrefix]: {
|
|
53
|
+
target: config.apiProxy,
|
|
54
|
+
changeOrigin: true,
|
|
55
|
+
secure: false
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
configureServer(server) {
|
|
62
|
+
if (config.verbose) {
|
|
63
|
+
server.middlewares.use((req, _res, next) => {
|
|
64
|
+
if (req.url?.startsWith(config.apiPrefix)) {
|
|
65
|
+
console.log(colors.cyan("[litestar-nuxt]"), `Proxying: ${req.method} ${req.url}`);
|
|
66
|
+
}
|
|
67
|
+
next();
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
server.httpServer?.once("listening", () => {
|
|
71
|
+
setTimeout(() => {
|
|
72
|
+
console.log("");
|
|
73
|
+
console.log(` ${colors.cyan("[litestar-nuxt]")} ${colors.green("Integration active")}`);
|
|
74
|
+
console.log(` ${colors.dim("\u251C\u2500")} API Proxy: ${colors.yellow(config.apiProxy)}`);
|
|
75
|
+
console.log(` ${colors.dim("\u251C\u2500")} API Prefix: ${colors.yellow(config.apiPrefix)}`);
|
|
76
|
+
if (config.types !== false && config.types.enabled) {
|
|
77
|
+
console.log(` ${colors.dim("\u2514\u2500")} Types Output: ${colors.yellow(config.types.output)}`);
|
|
78
|
+
} else {
|
|
79
|
+
console.log(` ${colors.dim("\u2514\u2500")} Types: ${colors.dim("disabled")}`);
|
|
80
|
+
}
|
|
81
|
+
console.log("");
|
|
82
|
+
}, 100);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function createTypeGenerationPlugin(typesConfig) {
|
|
88
|
+
let server = null;
|
|
89
|
+
let isGenerating = false;
|
|
90
|
+
async function runTypeGeneration() {
|
|
91
|
+
if (isGenerating) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
isGenerating = true;
|
|
95
|
+
const startTime = Date.now();
|
|
96
|
+
try {
|
|
97
|
+
const openapiPath = path.resolve(process.cwd(), typesConfig.openapiPath);
|
|
98
|
+
if (!fs.existsSync(openapiPath)) {
|
|
99
|
+
console.log(colors.cyan("[litestar-nuxt]"), colors.yellow("OpenAPI schema not found:"), typesConfig.openapiPath);
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
console.log(colors.cyan("[litestar-nuxt]"), colors.dim("Generating TypeScript types..."));
|
|
103
|
+
const args = ["@hey-api/openapi-ts", "-i", typesConfig.openapiPath, "-o", typesConfig.output];
|
|
104
|
+
if (typesConfig.generateZod) {
|
|
105
|
+
args.push("--plugins", "@hey-api/schemas", "@hey-api/types");
|
|
106
|
+
}
|
|
107
|
+
await execAsync(`npx ${args.join(" ")}`, {
|
|
108
|
+
cwd: process.cwd()
|
|
109
|
+
});
|
|
110
|
+
const duration = Date.now() - startTime;
|
|
111
|
+
console.log(colors.cyan("[litestar-nuxt]"), colors.green("Types generated"), colors.dim(`in ${duration}ms`));
|
|
112
|
+
if (server) {
|
|
113
|
+
server.ws.send({
|
|
114
|
+
type: "custom",
|
|
115
|
+
event: "litestar:types-updated",
|
|
116
|
+
data: {
|
|
117
|
+
output: typesConfig.output,
|
|
118
|
+
timestamp: Date.now()
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return true;
|
|
123
|
+
} catch (error) {
|
|
124
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
125
|
+
if (message.includes("not found") || message.includes("ENOENT")) {
|
|
126
|
+
console.log(colors.cyan("[litestar-nuxt]"), colors.yellow("@hey-api/openapi-ts not installed"), "- run:", resolveInstallHint());
|
|
127
|
+
} else {
|
|
128
|
+
console.error(colors.cyan("[litestar-nuxt]"), colors.red("Type generation failed:"), message);
|
|
129
|
+
}
|
|
130
|
+
return false;
|
|
131
|
+
} finally {
|
|
132
|
+
isGenerating = false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const debouncedRunTypeGeneration = debounce(runTypeGeneration, typesConfig.debounce);
|
|
136
|
+
return {
|
|
137
|
+
name: "litestar-nuxt-types",
|
|
138
|
+
enforce: "pre",
|
|
139
|
+
configureServer(devServer) {
|
|
140
|
+
server = devServer;
|
|
141
|
+
console.log(colors.cyan("[litestar-nuxt]"), colors.dim("Watching for schema changes:"), colors.yellow(typesConfig.openapiPath));
|
|
142
|
+
},
|
|
143
|
+
handleHotUpdate({ file }) {
|
|
144
|
+
if (!typesConfig.enabled) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const relativePath = path.relative(process.cwd(), file);
|
|
148
|
+
const openapiPath = typesConfig.openapiPath.replace(/^\.\//, "");
|
|
149
|
+
const routesPath = typesConfig.routesPath.replace(/^\.\//, "");
|
|
150
|
+
if (relativePath === openapiPath || relativePath === routesPath || file.endsWith(openapiPath) || file.endsWith(routesPath)) {
|
|
151
|
+
console.log(colors.cyan("[litestar-nuxt]"), colors.dim("Schema changed:"), colors.yellow(relativePath));
|
|
152
|
+
debouncedRunTypeGeneration();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function litestarPlugins(userConfig = {}) {
|
|
158
|
+
const config = resolveConfig(userConfig);
|
|
159
|
+
const plugins = [createProxyPlugin(config)];
|
|
160
|
+
if (config.types !== false && config.types.enabled) {
|
|
161
|
+
plugins.push(createTypeGenerationPlugin(config.types));
|
|
162
|
+
}
|
|
163
|
+
return plugins;
|
|
164
|
+
}
|
|
165
|
+
const litestarModule = {
|
|
166
|
+
meta: {
|
|
167
|
+
name: "litestar-vite",
|
|
168
|
+
configKey: "litestar",
|
|
169
|
+
compatibility: {
|
|
170
|
+
nuxt: ">=3.0.0"
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
defaults: {
|
|
174
|
+
apiProxy: "http://localhost:8000",
|
|
175
|
+
apiPrefix: "/api",
|
|
176
|
+
types: false,
|
|
177
|
+
verbose: false
|
|
178
|
+
},
|
|
179
|
+
/**
|
|
180
|
+
* Setup function for the Nuxt module.
|
|
181
|
+
* This is called by Nuxt when the module is loaded.
|
|
182
|
+
*/
|
|
183
|
+
setup(userOptions, nuxt) {
|
|
184
|
+
const config = resolveConfig(userOptions);
|
|
185
|
+
const plugins = litestarPlugins(config);
|
|
186
|
+
nuxt.options.vite = nuxt.options.vite || {};
|
|
187
|
+
nuxt.options.vite.plugins = nuxt.options.vite.plugins || [];
|
|
188
|
+
nuxt.options.vite.plugins.push(...plugins);
|
|
189
|
+
console.log(colors.cyan("[litestar-nuxt]"), "Module initialized");
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
var nuxt_default = litestarModule;
|
|
193
|
+
export {
|
|
194
|
+
nuxt_default as default,
|
|
195
|
+
litestarModule,
|
|
196
|
+
litestarPlugins
|
|
197
|
+
};
|