@donkeylabs/adapter-sveltekit 2.0.0 → 2.0.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@donkeylabs/adapter-sveltekit",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "type": "module",
5
5
  "description": "SvelteKit adapter for @donkeylabs/server - seamless SSR/browser API integration",
6
6
  "main": "./src/index.ts",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "peerDependencies": {
39
39
  "@sveltejs/kit": "^2.0.0",
40
- "@donkeylabs/server": "^2.0.0"
40
+ "@donkeylabs/server": "^2.0.2"
41
41
  },
42
42
  "keywords": [
43
43
  "sveltekit",
@@ -24,6 +24,23 @@ export interface SSESubscription {
24
24
  unsubscribe: () => void;
25
25
  }
26
26
 
27
+ /**
28
+ * SSE options for connection configuration.
29
+ * Compatible with @donkeylabs/server/client SSEOptions.
30
+ */
31
+ export interface SSEOptions {
32
+ /** Called when connection is established */
33
+ onConnect?: () => void;
34
+ /** Called when connection is lost */
35
+ onDisconnect?: () => void;
36
+ /** Called on connection error */
37
+ onError?: (error: Event) => void;
38
+ /** Auto-reconnect on disconnect (default: true) */
39
+ autoReconnect?: boolean;
40
+ /** Reconnect delay in ms (default: 3000) */
41
+ reconnectDelay?: number;
42
+ }
43
+
27
44
  /**
28
45
  * Type-safe SSE connection wrapper.
29
46
  * Provides typed event handlers with automatic JSON parsing.
@@ -91,6 +108,29 @@ export class SSEConnection<TEvents extends Record<string, any> = Record<string,
91
108
  };
92
109
  }
93
110
 
111
+ /**
112
+ * Register a typed event handler that fires only once.
113
+ * @returns Unsubscribe function to remove this specific handler
114
+ */
115
+ once<K extends keyof TEvents>(
116
+ event: K & string,
117
+ handler: (data: TEvents[K]) => void
118
+ ): () => void {
119
+ const wrappedHandler = (data: TEvents[K]) => {
120
+ unsubscribe();
121
+ handler(data);
122
+ };
123
+ const unsubscribe = this.on(event, wrappedHandler);
124
+ return unsubscribe;
125
+ }
126
+
127
+ /**
128
+ * Remove all handlers for an event.
129
+ */
130
+ off<K extends keyof TEvents>(event: K & string): void {
131
+ this.handlers.delete(event);
132
+ }
133
+
94
134
  /**
95
135
  * Register error handler
96
136
  */
@@ -307,6 +347,21 @@ export class UnifiedApiClientBase {
307
347
  return new SSEConnection<TEvents>(url);
308
348
  }
309
349
 
350
+ /**
351
+ * Connect to a specific SSE route endpoint.
352
+ * Alias for sseConnect() - provides compatibility with @donkeylabs/server generated clients.
353
+ * @returns SSE connection with typed event handlers
354
+ */
355
+ protected connectToSSERoute<TEvents extends Record<string, any>>(
356
+ route: string,
357
+ input: Record<string, any> = {},
358
+ _options?: Omit<SSEOptions, "endpoint" | "channels">
359
+ ): SSEConnection<TEvents> {
360
+ // Note: options (onConnect, onDisconnect, etc.) are not used by SSEConnection
361
+ // but we accept them for API compatibility with @donkeylabs/server/client
362
+ return this.sseConnect<Record<string, any>, TEvents>(route, input);
363
+ }
364
+
310
365
  /**
311
366
  * Make a formData request (file uploads with validated fields).
312
367
  */
package/src/index.ts CHANGED
@@ -214,8 +214,8 @@ export function createUnifiedServer(config) {
214
214
  });
215
215
  }
216
216
 
217
- // 2. API routes (POST only)
218
- if (req.method === "POST") {
217
+ // 2. API routes (GET or POST - stream/html/sse use GET, typed uses POST)
218
+ if ((req.method === "GET" || req.method === "POST") && /^\\/[a-zA-Z][a-zA-Z0-9_.]*$/.test(pathname)) {
219
219
  const routeName = pathname.slice(1); // Remove leading /
220
220
 
221
221
  // Check if this is a registered API route