litestar-vite-plugin 0.27.0 → 0.29.0

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.
Files changed (49) hide show
  1. package/README.md +37 -18
  2. package/dist/js/astro.d.ts +12 -121
  3. package/dist/js/astro.js +8 -55
  4. package/dist/js/helpers/channels.d.ts +31 -0
  5. package/dist/js/helpers/channels.js +37 -0
  6. package/dist/js/helpers/csrf.d.ts +3 -1
  7. package/dist/js/helpers/csrf.js +26 -12
  8. package/dist/js/helpers/expression.d.ts +3 -0
  9. package/dist/js/helpers/expression.js +524 -0
  10. package/dist/js/helpers/htmx.d.ts +1 -1
  11. package/dist/js/helpers/htmx.js +33 -140
  12. package/dist/js/helpers/index.d.ts +5 -1
  13. package/dist/js/helpers/index.js +9 -1
  14. package/dist/js/helpers/queues.d.ts +61 -0
  15. package/dist/js/helpers/queues.js +117 -0
  16. package/dist/js/helpers/stream-element.d.ts +79 -0
  17. package/dist/js/helpers/stream-element.js +229 -0
  18. package/dist/js/helpers/stream.d.ts +76 -0
  19. package/dist/js/helpers/stream.js +242 -0
  20. package/dist/js/index.d.ts +4 -106
  21. package/dist/js/index.js +4 -0
  22. package/dist/js/install-hint.js +8 -15
  23. package/dist/js/nuxt.d.ts +12 -128
  24. package/dist/js/nuxt.js +17 -74
  25. package/dist/js/react/index.d.ts +28 -0
  26. package/dist/js/react/index.js +54 -0
  27. package/dist/js/shared/bridge-schema.d.ts +10 -7
  28. package/dist/js/shared/bridge-schema.js +6 -0
  29. package/dist/js/shared/emit-page-props-types.js +22 -6
  30. package/dist/js/shared/emit-static-props-types.js +6 -2
  31. package/dist/js/shared/integration-config.d.ts +75 -0
  32. package/dist/js/shared/integration-config.js +59 -0
  33. package/dist/js/shared/logger.d.ts +0 -4
  34. package/dist/js/shared/logger.js +1 -2
  35. package/dist/js/shared/managed-shutdown.d.ts +8 -0
  36. package/dist/js/shared/managed-shutdown.js +26 -0
  37. package/dist/js/shared/network.d.ts +4 -3
  38. package/dist/js/shared/typegen-core.d.ts +2 -9
  39. package/dist/js/shared/typegen-core.js +0 -1
  40. package/dist/js/shared/typegen-plugin.d.ts +84 -0
  41. package/dist/js/shared/vite-compat.d.ts +0 -4
  42. package/dist/js/shared/vite-compat.js +1 -3
  43. package/dist/js/svelte/index.d.ts +27 -0
  44. package/dist/js/svelte/index.js +51 -0
  45. package/dist/js/sveltekit.d.ts +12 -128
  46. package/dist/js/sveltekit.js +6 -55
  47. package/dist/js/vue/index.d.ts +29 -0
  48. package/dist/js/vue/index.js +57 -0
  49. package/package.json +47 -4
@@ -13,10 +13,6 @@ export interface LoggingConfig {
13
13
  suppressViteBanner: boolean;
14
14
  timestamps: boolean;
15
15
  }
16
- /**
17
- * Default logging configuration.
18
- */
19
- export declare const defaultLoggingConfig: LoggingConfig;
20
16
  /**
21
17
  * Logger instance with configurable behavior.
22
18
  */
@@ -54,6 +54,5 @@ function createLogger(config) {
54
54
  };
55
55
  }
56
56
  export {
57
- createLogger,
58
- defaultLoggingConfig
57
+ createLogger
59
58
  };
@@ -0,0 +1,8 @@
1
+ import type { ViteDevServer } from "vite";
2
+ /**
3
+ * Close a Python-managed Vite sidecar when its stdin pipe reaches EOF.
4
+ *
5
+ * @param server - Vite server owned by the Python parent process.
6
+ * @param markShuttingDown - Optional callback for integration-specific shutdown state.
7
+ */
8
+ export declare function installManagedShutdown(server: ViteDevServer, markShuttingDown?: () => void): void;
@@ -0,0 +1,26 @@
1
+ const installedServers = /* @__PURE__ */ new WeakSet();
2
+ function installManagedShutdown(server, markShuttingDown) {
3
+ if (process.env.LITESTAR_VITE_MANAGED !== "1" || installedServers.has(server)) {
4
+ return;
5
+ }
6
+ installedServers.add(server);
7
+ let shuttingDown = false;
8
+ const shutdown = async () => {
9
+ if (shuttingDown) {
10
+ return;
11
+ }
12
+ shuttingDown = true;
13
+ markShuttingDown?.();
14
+ try {
15
+ await server.close();
16
+ } finally {
17
+ process.exit(0);
18
+ }
19
+ };
20
+ process.stdin.on("end", shutdown);
21
+ process.stdin.on("close", shutdown);
22
+ process.stdin.resume();
23
+ }
24
+ export {
25
+ installManagedShutdown
26
+ };
@@ -25,9 +25,10 @@ export declare function normalizeHost(host: string): string;
25
25
  * Resolve the Litestar dev server port for HMR routing.
26
26
  *
27
27
  * Framework integrations (Astro/Nuxt/SvelteKit) need this port to set
28
- * `vite.server.hmr.clientPort` so the browser opens the HMR WebSocket against
29
- * Litestar NOT the framework dev server's portpreserving the
30
- * single-port-via-ASGI contract.
28
+ * `vite.server.ws.clientPort` on Vite 8.1+ (`vite.server.hmr.clientPort` on
29
+ * Vite 7 / 8.0) so the browser opens the HMR WebSocket against Litestar NOT
30
+ * the framework dev server's port — preserving the single-port-via-ASGI
31
+ * contract.
31
32
  *
32
33
  * Resolution order:
33
34
  * 1. `bridge.litestarPort` (preferred; written by Python ≥0.23.0).
@@ -53,7 +53,7 @@ export interface TypeGenLogger {
53
53
  warn(message: string): void;
54
54
  error(message: string): void;
55
55
  }
56
- export interface TypeGenCacheHooks {
56
+ interface TypeGenCacheHooks {
57
57
  shouldRunOpenApiTs(openapiPath: string, configPath: string | null, options: {
58
58
  generateSdk: boolean;
59
59
  generateZod: boolean;
@@ -89,14 +89,6 @@ export declare function resolveDefaultSdkClientPlugin(options: {
89
89
  * Find user's openapi-ts config file.
90
90
  */
91
91
  export declare function findOpenApiTsConfig(projectRoot: string): string | null;
92
- /**
93
- * Build the list of plugins for @hey-api/openapi-ts.
94
- */
95
- export declare function buildHeyApiPlugins(config: {
96
- generateSdk: boolean;
97
- generateZod: boolean;
98
- sdkClientPlugin: string;
99
- }): string[];
100
92
  /**
101
93
  * Resolve the locally installed @hey-api/openapi-ts executable.
102
94
  */
@@ -125,3 +117,4 @@ export declare function runHeyApiGeneration(config: TypeGenCoreConfig, configPat
125
117
  * @returns Result with generated files and timing info
126
118
  */
127
119
  export declare function runTypeGeneration(config: TypeGenCoreConfig, options?: RunTypeGenOptions): Promise<TypeGenResult>;
120
+ export {};
@@ -201,7 +201,6 @@ async function runTypeGeneration(config, options = {}) {
201
201
  return result;
202
202
  }
203
203
  export {
204
- buildHeyApiPlugins,
205
204
  findOpenApiTsConfig,
206
205
  resolveDefaultSdkClientPlugin,
207
206
  resolveHeyApiBin,
@@ -28,20 +28,104 @@ export interface TypeGenPluginOptions {
28
28
  /** Whether .litestar.json was present (used for buildStart warnings) */
29
29
  hasPythonConfig?: boolean;
30
30
  }
31
+ /**
32
+ * Configuration for TypeScript type generation.
33
+ *
34
+ * This is the single source of truth for the `types` option across the core Vite plugin and
35
+ * every framework integration (Astro, Nuxt, SvelteKit), which re-export it under their own
36
+ * names. Paths default relative to `output`, whose default is framework-specific.
37
+ *
38
+ * Type generation works as follows:
39
+ * 1. Python's Litestar exports openapi.json and routes.json on startup (and reload)
40
+ * 2. The Vite plugin watches these files for changes
41
+ * 3. When they change, it runs @hey-api/openapi-ts to generate TypeScript types
42
+ * 4. An HMR event is sent to notify the client
43
+ */
31
44
  export interface TypesConfigShape {
45
+ /**
46
+ * Enable type generation.
47
+ *
48
+ * @default false
49
+ */
32
50
  enabled?: boolean;
51
+ /** Directory for generated TypeScript output. Framework-specific default. */
33
52
  output?: string;
53
+ /**
54
+ * Path where the OpenAPI schema is exported by Litestar. The plugin watches this file
55
+ * and runs @hey-api/openapi-ts when it changes.
56
+ *
57
+ * @default `${output}/openapi.json`
58
+ */
34
59
  openapiPath?: string;
60
+ /**
61
+ * Path where route metadata is exported by Litestar. Watched for route helper generation.
62
+ *
63
+ * @default `${output}/routes.json`
64
+ */
35
65
  routesPath?: string;
66
+ /**
67
+ * Path where Inertia page props metadata is exported by Litestar.
68
+ *
69
+ * @default `${output}/inertia-pages.json`
70
+ */
36
71
  pagePropsPath?: string;
72
+ /**
73
+ * Path for the generated `schemas.ts` helper file.
74
+ *
75
+ * @default `${output}/schemas.ts`
76
+ */
37
77
  schemasTsPath?: string;
78
+ /**
79
+ * Generate Zod schemas in addition to TypeScript types.
80
+ *
81
+ * @default false
82
+ */
38
83
  generateZod?: boolean;
84
+ /**
85
+ * Generate a typed SDK client in addition to types.
86
+ *
87
+ * @default true
88
+ */
39
89
  generateSdk?: boolean;
90
+ /**
91
+ * Generate typed `routes.ts` from routes.json metadata. Mirrors Python
92
+ * `TypeGenConfig.generate_routes`.
93
+ *
94
+ * @default true
95
+ */
40
96
  generateRoutes?: boolean;
97
+ /**
98
+ * Generate Inertia page props types from inertia-pages.json metadata. Mirrors Python
99
+ * `TypeGenConfig.generate_page_props`.
100
+ *
101
+ * @default true
102
+ */
41
103
  generatePageProps?: boolean;
104
+ /**
105
+ * Generate `schemas.ts` with ergonomic form/response type helpers, such as
106
+ * `FormInput<'api:login'>` and `FormResponse<'api:login', 201>`.
107
+ *
108
+ * @default true
109
+ */
42
110
  generateSchemas?: boolean;
111
+ /**
112
+ * Register the generated `route()` function on `window`, similar to Laravel's Ziggy,
113
+ * so it can be used without an import.
114
+ *
115
+ * @default false
116
+ */
43
117
  globalRoute?: boolean;
118
+ /**
119
+ * Fail Vite when type generation fails. Defaults to `true` during `vite build` and
120
+ * `false` during `vite serve`.
121
+ */
44
122
  failOnError?: boolean;
123
+ /**
124
+ * Debounce window in milliseconds for regeneration, so a burst of file writes triggers
125
+ * a single run.
126
+ *
127
+ * @default 300
128
+ */
45
129
  debounce?: number;
46
130
  }
47
131
  export interface ResolveTypesConfigOptions {
@@ -1,7 +1,3 @@
1
- /** Parsed major version of the running Vite instance. */
2
- export declare const viteMajor: number;
3
- /** Parsed minor version of the running Vite instance. */
4
- export declare const viteMinor: number;
5
1
  /**
6
2
  * Whether the running Vite version is 8+, which uses Rolldown and exposes
7
3
  * `build.rolldownOptions` in place of Vite 7's `build.rollupOptions`.
@@ -26,7 +26,5 @@ export {
26
26
  hmrServerConfig,
27
27
  isVite81Plus,
28
28
  isVite8Plus,
29
- resolveUserBuildInput,
30
- viteMajor,
31
- viteMinor
29
+ resolveUserBuildInput
32
30
  };
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Svelte stores for generic and Litestar Queues event streams.
3
+ *
4
+ * @module
5
+ */
6
+ import { type EventStreamOptions, type QueueEventStreamOptions, type StreamGap } from "litestar-vite-plugin/helpers";
7
+ import { type Readable } from "svelte/store";
8
+ export interface EventStreamStoreState<TFrame> {
9
+ healthy: boolean;
10
+ lastEvent: TFrame | null;
11
+ lastGap: StreamGap | null;
12
+ events: TFrame[];
13
+ }
14
+ export type SvelteEventStreamOptions<TFrame> = EventStreamOptions<TFrame> & {
15
+ bufferSize?: number;
16
+ };
17
+ export type SvelteQueueEventStreamOptions<TFrame> = QueueEventStreamOptions<TFrame> & {
18
+ bufferSize?: number;
19
+ };
20
+ /**
21
+ * Create a readable store backed by a generic event stream.
22
+ */
23
+ export declare function createEventStreamStore<TFrame = unknown>(options: SvelteEventStreamOptions<TFrame>): Readable<EventStreamStoreState<TFrame>>;
24
+ /**
25
+ * Create a readable store backed by a Litestar Queues event stream.
26
+ */
27
+ export declare function createQueueEventStreamStore<TFrame = unknown>(options: SvelteQueueEventStreamOptions<TFrame>): Readable<EventStreamStoreState<TFrame>>;
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Svelte stores for generic and Litestar Queues event streams.
3
+ *
4
+ * @module
5
+ */
6
+ import { createEventStream, createQueueEventStream } from "litestar-vite-plugin/helpers";
7
+ import { readable } from "svelte/store";
8
+ function initialState() {
9
+ return { events: [], healthy: false, lastEvent: null, lastGap: null };
10
+ }
11
+ function createStreamStore(factory, options) {
12
+ return readable(initialState(), (set) => {
13
+ let state = initialState();
14
+ const update = (next) => {
15
+ state = { ...state, ...next };
16
+ set(state);
17
+ };
18
+ const { bufferSize: _bufferSize, ...streamOptions } = options;
19
+ const stream = factory({
20
+ ...streamOptions,
21
+ onEvent: (frame) => {
22
+ options.onEvent(frame);
23
+ const bufferSize = Math.max(0, options.bufferSize ?? 100);
24
+ const events = bufferSize === 0 ? [] : [...state.events, frame].slice(-bufferSize);
25
+ update({ events, lastEvent: frame });
26
+ },
27
+ onGap: (gap) => {
28
+ options.onGap?.(gap);
29
+ update({ lastGap: gap });
30
+ },
31
+ onHealthChange: (healthy) => {
32
+ options.onHealthChange?.(healthy);
33
+ update({ healthy });
34
+ },
35
+ });
36
+ stream.connect();
37
+ return () => stream.dispose();
38
+ });
39
+ }
40
+ /**
41
+ * Create a readable store backed by a generic event stream.
42
+ */
43
+ export function createEventStreamStore(options) {
44
+ return createStreamStore(createEventStream, options);
45
+ }
46
+ /**
47
+ * Create a readable store backed by a Litestar Queues event stream.
48
+ */
49
+ export function createQueueEventStreamStore(options) {
50
+ return createStreamStore(createQueueEventStream, options);
51
+ }
@@ -27,138 +27,22 @@
27
27
  *
28
28
  * @module
29
29
  */
30
+ import { type LitestarIntegrationConfig } from "./shared/integration-config.js";
31
+ import { type TypesConfigShape } from "./shared/typegen-plugin.js";
30
32
  /**
31
- * Configuration for TypeScript type generation in SvelteKit.
33
+ * Configuration for TypeScript type generation.
34
+ *
35
+ * Alias of the shared {@link TypesConfigShape} so every integration accepts an identical
36
+ * `types` option. Retained as a named export for backwards compatibility.
32
37
  */
33
- export interface SvelteKitTypesConfig {
34
- /**
35
- * Enable type generation.
36
- *
37
- * @default false
38
- */
39
- enabled?: boolean;
40
- /**
41
- * Path to output generated TypeScript types.
42
- * Relative to the SvelteKit project root.
43
- *
44
- * @default 'src/lib/generated'
45
- */
46
- output?: string;
47
- /**
48
- * Path where the OpenAPI schema is exported by Litestar.
49
- *
50
- * @default `${output}/openapi.json`
51
- */
52
- openapiPath?: string;
53
- /**
54
- * Path where route metadata is exported by Litestar.
55
- *
56
- * @default `${output}/routes.json`
57
- */
58
- routesPath?: string;
59
- /**
60
- * Optional path for the generated schemas.ts helper file.
61
- *
62
- * @default `${output}/schemas.ts`
63
- */
64
- schemasTsPath?: string;
65
- /**
66
- * Path where Inertia page props metadata is exported by Litestar.
67
- *
68
- * @default `${output}/inertia-pages.json`
69
- */
70
- pagePropsPath?: string;
71
- /**
72
- * Generate Zod schemas in addition to TypeScript types.
73
- *
74
- * @default false
75
- */
76
- generateZod?: boolean;
77
- /**
78
- * Generate SDK client functions for API calls.
79
- *
80
- * @default true
81
- */
82
- generateSdk?: boolean;
83
- /**
84
- * Generate typed routes.ts from routes.json metadata.
85
- *
86
- * @default true
87
- */
88
- generateRoutes?: boolean;
89
- /**
90
- * Generate Inertia page props types from inertia-pages.json metadata.
91
- *
92
- * @default true
93
- */
94
- generatePageProps?: boolean;
95
- /**
96
- * Generate schemas.ts with ergonomic form/response type helpers.
97
- *
98
- * @default true
99
- */
100
- generateSchemas?: boolean;
101
- /**
102
- * Register route() globally on window object.
103
- *
104
- * @default false
105
- */
106
- globalRoute?: boolean;
107
- /**
108
- * Fail Vite when type generation fails.
109
- *
110
- * Defaults to true during build and false during dev.
111
- */
112
- failOnError?: boolean;
113
- /**
114
- * Debounce time in milliseconds for type regeneration.
115
- *
116
- * @default 300
117
- */
118
- debounce?: number;
119
- }
38
+ export type SvelteKitTypesConfig = TypesConfigShape;
120
39
  /**
121
- * Configuration options for the Litestar SvelteKit integration.
40
+ * Configuration options for the Litestar integration.
41
+ *
42
+ * Alias of the shared {@link LitestarIntegrationConfig}. Retained as a named export for
43
+ * backwards compatibility.
122
44
  */
123
- export interface LitestarSvelteKitConfig {
124
- /**
125
- * URL of the Litestar API backend for proxying requests during development.
126
- *
127
- * @example 'http://localhost:8000'
128
- * @default 'http://localhost:8000'
129
- */
130
- apiProxy?: string;
131
- /**
132
- * API route prefix to proxy to the Litestar backend.
133
- * Requests matching this prefix will be forwarded to the apiProxy URL.
134
- *
135
- * @example '/api'
136
- * @default '/api'
137
- */
138
- apiPrefix?: string;
139
- /**
140
- * Enable and configure TypeScript type generation.
141
- *
142
- * When set to `true`, enables type generation with default settings.
143
- * When set to a SvelteKitTypesConfig object, enables type generation with custom settings.
144
- *
145
- * @default false
146
- */
147
- types?: boolean | SvelteKitTypesConfig;
148
- /**
149
- * Enable verbose logging for debugging.
150
- *
151
- * @default false
152
- */
153
- verbose?: boolean;
154
- /**
155
- * JavaScript runtime executor for package commands.
156
- * Used when running tools like @hey-api/openapi-ts.
157
- *
158
- * @default undefined (uses LITESTAR_VITE_RUNTIME env or 'node')
159
- */
160
- executor?: "node" | "bun" | "deno" | "yarn" | "pnpm";
161
- }
45
+ export type LitestarSvelteKitConfig = LitestarIntegrationConfig;
162
46
  /**
163
47
  * Litestar integration plugin for SvelteKit.
164
48
  *
@@ -1,63 +1,13 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import colors from "picocolors";
4
- import { readBridgeConfig } from "./shared/bridge-schema.js";
5
- import { normalizeHost, resolveHotFilePath, resolveLitestarPort } from "./shared/network.js";
6
- import { createLitestarTypeGenPlugin, resolveTypesConfig } from "./shared/typegen-plugin.js";
4
+ import { resolveIntegrationConfig } from "./shared/integration-config.js";
5
+ import { installManagedShutdown } from "./shared/managed-shutdown.js";
6
+ import { normalizeHost } from "./shared/network.js";
7
+ import { createLitestarTypeGenPlugin } from "./shared/typegen-plugin.js";
7
8
  import { hmrServerConfig } from "./shared/vite-compat.js";
8
9
  function resolveConfig(config = {}) {
9
- let hotFile;
10
- let proxyMode = "vite";
11
- let port;
12
- let pythonTypesConfig;
13
- let hasPythonConfig = false;
14
- const envPort = process.env.VITE_PORT;
15
- if (envPort) {
16
- port = Number.parseInt(envPort, 10);
17
- if (Number.isNaN(port)) {
18
- port = void 0;
19
- }
20
- }
21
- let pythonExecutor;
22
- let assetUrl;
23
- let litestarPort;
24
- const runtime = readBridgeConfig();
25
- if (runtime) {
26
- hasPythonConfig = true;
27
- const hot = runtime.hotFile;
28
- hotFile = resolveHotFilePath(runtime.bundleDir, hot);
29
- proxyMode = runtime.proxyMode;
30
- port = runtime.port;
31
- pythonExecutor = runtime.executor;
32
- assetUrl = runtime.assetUrl;
33
- if (runtime.types) {
34
- pythonTypesConfig = runtime.types;
35
- }
36
- }
37
- const resolvedLitestarPort = resolveLitestarPort(runtime?.litestarPort, runtime?.appUrl);
38
- if (resolvedLitestarPort !== null) {
39
- litestarPort = resolvedLitestarPort;
40
- }
41
- const typesConfig = resolveTypesConfig({
42
- requested: config.types,
43
- pythonConfig: pythonTypesConfig ?? void 0,
44
- defaultOutput: "src/lib/generated",
45
- mergePythonWhenTrue: true,
46
- mergePythonForObject: true
47
- });
48
- return {
49
- apiProxy: config.apiProxy ?? "http://localhost:8000",
50
- apiPrefix: config.apiPrefix ?? "/api",
51
- types: typesConfig,
52
- verbose: config.verbose ?? false,
53
- hotFile,
54
- proxyMode,
55
- port,
56
- litestarPort,
57
- assetUrl,
58
- executor: config.executor ?? pythonExecutor,
59
- hasPythonConfig
60
- };
10
+ return resolveIntegrationConfig(config, "src/lib/generated");
61
11
  }
62
12
  function litestarSvelteKit(userConfig = {}) {
63
13
  const config = resolveConfig(userConfig);
@@ -97,6 +47,7 @@ function litestarSvelteKit(userConfig = {}) {
97
47
  };
98
48
  },
99
49
  configureServer(server) {
50
+ installManagedShutdown(server);
100
51
  if (config.verbose) {
101
52
  server.middlewares.use((req, _res, next) => {
102
53
  if (req.url?.startsWith(config.apiPrefix)) {
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Vue bindings for generic and Litestar Queues event streams.
3
+ *
4
+ * @module
5
+ */
6
+ import { type EventStreamOptions, type QueueEventStreamOptions, type StreamGap } from "litestar-vite-plugin/helpers";
7
+ import { type MaybeRefOrGetter, type Ref, type ShallowRef } from "vue";
8
+ export interface VueEventStreamState<TFrame> {
9
+ healthy: Ref<boolean>;
10
+ lastEvent: ShallowRef<TFrame | null>;
11
+ lastGap: ShallowRef<StreamGap | null>;
12
+ events: ShallowRef<TFrame[]>;
13
+ }
14
+ export type VueEventStreamOptions<TFrame> = EventStreamOptions<TFrame> & {
15
+ key: MaybeRefOrGetter<string>;
16
+ bufferSize?: number;
17
+ };
18
+ export type VueQueueEventStreamOptions<TFrame> = QueueEventStreamOptions<TFrame> & {
19
+ key: MaybeRefOrGetter<string>;
20
+ bufferSize?: number;
21
+ };
22
+ /**
23
+ * Subscribe a Vue scope to a generic event stream.
24
+ */
25
+ export declare function useEventStream<TFrame = unknown>(options: VueEventStreamOptions<TFrame>): VueEventStreamState<TFrame>;
26
+ /**
27
+ * Subscribe a Vue scope to a Litestar Queues event stream.
28
+ */
29
+ export declare function useQueueEventStream<TFrame = unknown>(options: VueQueueEventStreamOptions<TFrame>): VueEventStreamState<TFrame>;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Vue bindings for generic and Litestar Queues event streams.
3
+ *
4
+ * @module
5
+ */
6
+ import { createEventStream, createQueueEventStream } from "litestar-vite-plugin/helpers";
7
+ import { onMounted, onScopeDispose, ref, shallowRef, toValue, watch } from "vue";
8
+ function useStream(factory, options) {
9
+ const healthy = ref(false);
10
+ const lastEvent = shallowRef(null);
11
+ const lastGap = shallowRef(null);
12
+ const events = shallowRef([]);
13
+ let stream = null;
14
+ const stop = () => {
15
+ stream?.dispose();
16
+ stream = null;
17
+ };
18
+ const start = () => {
19
+ stop();
20
+ healthy.value = false;
21
+ const { bufferSize: _bufferSize, key: _key, ...streamOptions } = options;
22
+ stream = factory({
23
+ ...streamOptions,
24
+ onEvent: (frame) => {
25
+ options.onEvent(frame);
26
+ lastEvent.value = frame;
27
+ const bufferSize = Math.max(0, options.bufferSize ?? 100);
28
+ events.value = bufferSize === 0 ? [] : [...events.value, frame].slice(-bufferSize);
29
+ },
30
+ onGap: (gap) => {
31
+ options.onGap?.(gap);
32
+ lastGap.value = gap;
33
+ },
34
+ onHealthChange: (value) => {
35
+ options.onHealthChange?.(value);
36
+ healthy.value = value;
37
+ },
38
+ });
39
+ stream.connect();
40
+ };
41
+ onMounted(start);
42
+ watch(() => [toValue(options.key), options.transport ?? "websocket"], () => start());
43
+ onScopeDispose(stop);
44
+ return { events, healthy, lastEvent, lastGap };
45
+ }
46
+ /**
47
+ * Subscribe a Vue scope to a generic event stream.
48
+ */
49
+ export function useEventStream(options) {
50
+ return useStream(createEventStream, options);
51
+ }
52
+ /**
53
+ * Subscribe a Vue scope to a Litestar Queues event stream.
54
+ */
55
+ export function useQueueEventStream(options) {
56
+ return useStream(createQueueEventStream, options);
57
+ }