@donkeylabs/adapter-sveltekit 2.0.14 → 2.0.16
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/client/index.d.ts +231 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +501 -0
- package/dist/generator/index.d.ts +17 -0
- package/dist/generator/index.d.ts.map +1 -0
- package/dist/generator/index.js +330 -0
- package/dist/hooks/index.d.ts +53 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +96 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.d.ts.map +1 -0
- package/{src/index.ts → dist/index.js} +51 -120
- package/dist/vite.d.ts +71 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +611 -0
- package/package.json +16 -14
- package/src/client/index.ts +0 -659
- package/src/generator/index.ts +0 -401
- package/src/hooks/index.ts +0 -124
- package/src/vite.ts +0 -729
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified API client for @donkeylabs/adapter-sveltekit
|
|
3
|
+
*
|
|
4
|
+
* Auto-detects environment:
|
|
5
|
+
* - SSR: Direct service calls through locals (no HTTP)
|
|
6
|
+
* - Browser: HTTP calls to API routes
|
|
7
|
+
*/
|
|
8
|
+
export interface RequestOptions {
|
|
9
|
+
headers?: Record<string, string>;
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
}
|
|
12
|
+
export interface ClientOptions {
|
|
13
|
+
/** Base URL for HTTP calls. Defaults to empty string (relative URLs). */
|
|
14
|
+
baseUrl?: string;
|
|
15
|
+
/** SvelteKit locals object for SSR direct calls. */
|
|
16
|
+
locals?: any;
|
|
17
|
+
/** Custom fetch function. In SSR, pass event.fetch to handle relative URLs. */
|
|
18
|
+
fetch?: typeof fetch;
|
|
19
|
+
}
|
|
20
|
+
export interface SSESubscription {
|
|
21
|
+
unsubscribe: () => void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* SSE options for connection configuration.
|
|
25
|
+
* Compatible with @donkeylabs/server/client SSEOptions.
|
|
26
|
+
*/
|
|
27
|
+
export interface SSEOptions {
|
|
28
|
+
/** Called when connection is established */
|
|
29
|
+
onConnect?: () => void;
|
|
30
|
+
/** Called when connection is lost */
|
|
31
|
+
onDisconnect?: () => void;
|
|
32
|
+
/** Called on connection error */
|
|
33
|
+
onError?: (error: Event) => void;
|
|
34
|
+
/** Auto-reconnect on disconnect (default: true) */
|
|
35
|
+
autoReconnect?: boolean;
|
|
36
|
+
/** Reconnect delay in ms (default: 3000) */
|
|
37
|
+
reconnectDelay?: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Options for SSE connection behavior.
|
|
41
|
+
*/
|
|
42
|
+
export interface SSEConnectionOptions {
|
|
43
|
+
/** Enable automatic reconnection on disconnect (default: true) */
|
|
44
|
+
autoReconnect?: boolean;
|
|
45
|
+
/** Delay in ms before attempting reconnect (default: 3000) */
|
|
46
|
+
reconnectDelay?: number;
|
|
47
|
+
/** Called when connection is established */
|
|
48
|
+
onConnect?: () => void;
|
|
49
|
+
/** Called when connection is lost */
|
|
50
|
+
onDisconnect?: () => void;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Type-safe SSE connection wrapper.
|
|
54
|
+
* Provides typed event handlers with automatic JSON parsing.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* // With auto-reconnect (default)
|
|
59
|
+
* const connection = api.notifications.subscribe({ userId: "123" });
|
|
60
|
+
*
|
|
61
|
+
* // Disable auto-reconnect for manual control
|
|
62
|
+
* const connection = api.notifications.subscribe({ userId: "123" }, {
|
|
63
|
+
* autoReconnect: false
|
|
64
|
+
* });
|
|
65
|
+
*
|
|
66
|
+
* // Custom reconnect delay
|
|
67
|
+
* const connection = api.notifications.subscribe({ userId: "123" }, {
|
|
68
|
+
* reconnectDelay: 5000 // 5 seconds
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* // Typed event handler - returns unsubscribe function
|
|
72
|
+
* const unsubscribe = connection.on("notification", (data) => {
|
|
73
|
+
* console.log(data.message); // Fully typed!
|
|
74
|
+
* });
|
|
75
|
+
*
|
|
76
|
+
* // Later: unsubscribe from this specific handler
|
|
77
|
+
* unsubscribe();
|
|
78
|
+
*
|
|
79
|
+
* // Close entire connection
|
|
80
|
+
* connection.close();
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare class SSEConnection<TEvents extends Record<string, any> = Record<string, any>> {
|
|
84
|
+
private eventSource;
|
|
85
|
+
private handlers;
|
|
86
|
+
private url;
|
|
87
|
+
private options;
|
|
88
|
+
private reconnectTimeout;
|
|
89
|
+
private closed;
|
|
90
|
+
constructor(url: string, options?: SSEConnectionOptions);
|
|
91
|
+
private createEventSource;
|
|
92
|
+
private scheduleReconnect;
|
|
93
|
+
private dispatchEvent;
|
|
94
|
+
/**
|
|
95
|
+
* Register a typed event handler.
|
|
96
|
+
* @returns Unsubscribe function to remove this specific handler
|
|
97
|
+
*/
|
|
98
|
+
on<K extends keyof TEvents>(event: K & string, handler: (data: TEvents[K]) => void): () => void;
|
|
99
|
+
/**
|
|
100
|
+
* Register a typed event handler that fires only once.
|
|
101
|
+
* @returns Unsubscribe function to remove this specific handler
|
|
102
|
+
*/
|
|
103
|
+
once<K extends keyof TEvents>(event: K & string, handler: (data: TEvents[K]) => void): () => void;
|
|
104
|
+
/**
|
|
105
|
+
* Remove all handlers for an event.
|
|
106
|
+
*/
|
|
107
|
+
off<K extends keyof TEvents>(event: K & string): void;
|
|
108
|
+
/**
|
|
109
|
+
* Register error handler.
|
|
110
|
+
* Note: With autoReconnect enabled, errors trigger automatic reconnection.
|
|
111
|
+
*/
|
|
112
|
+
onError(handler: (event: Event) => void): () => void;
|
|
113
|
+
/**
|
|
114
|
+
* Register open handler (connection established)
|
|
115
|
+
*/
|
|
116
|
+
onOpen(handler: (event: Event) => void): () => void;
|
|
117
|
+
/**
|
|
118
|
+
* Get connection state
|
|
119
|
+
*/
|
|
120
|
+
get readyState(): number;
|
|
121
|
+
/**
|
|
122
|
+
* Check if connected
|
|
123
|
+
*/
|
|
124
|
+
get connected(): boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Check if reconnecting
|
|
127
|
+
*/
|
|
128
|
+
get reconnecting(): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Close the SSE connection and stop reconnection attempts
|
|
131
|
+
*/
|
|
132
|
+
close(): void;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Base class for unified API clients.
|
|
136
|
+
* Extend this class with your generated route methods.
|
|
137
|
+
*/
|
|
138
|
+
export declare class UnifiedApiClientBase {
|
|
139
|
+
protected baseUrl: string;
|
|
140
|
+
protected locals?: any;
|
|
141
|
+
protected isSSR: boolean;
|
|
142
|
+
protected customFetch?: typeof fetch;
|
|
143
|
+
constructor(options?: ClientOptions);
|
|
144
|
+
/**
|
|
145
|
+
* Make a request to an API route.
|
|
146
|
+
* Automatically uses direct calls in SSR (when locals.handleRoute is available), HTTP otherwise.
|
|
147
|
+
*/
|
|
148
|
+
protected request<TInput, TOutput>(route: string, input: TInput, options?: RequestOptions): Promise<TOutput>;
|
|
149
|
+
/**
|
|
150
|
+
* HTTP call to API endpoint (browser or SSR with event.fetch).
|
|
151
|
+
*/
|
|
152
|
+
private httpCall;
|
|
153
|
+
/**
|
|
154
|
+
* Make a raw request (for non-JSON endpoints like streaming).
|
|
155
|
+
* Returns the raw Response object without processing.
|
|
156
|
+
*/
|
|
157
|
+
protected rawRequest(route: string, init?: RequestInit): Promise<Response>;
|
|
158
|
+
/**
|
|
159
|
+
* Make a stream request (validated input, Response output).
|
|
160
|
+
* For streaming, binary data, or custom content-type responses.
|
|
161
|
+
*
|
|
162
|
+
* By default uses POST with JSON body. For browser compatibility
|
|
163
|
+
* (video src, image src, download links), use streamUrl() instead.
|
|
164
|
+
*/
|
|
165
|
+
protected streamRequest<TInput>(route: string, input: TInput, options?: RequestOptions): Promise<Response>;
|
|
166
|
+
/**
|
|
167
|
+
* Get the URL for a stream endpoint (for browser src attributes).
|
|
168
|
+
* Returns a URL with query params that can be used in:
|
|
169
|
+
* - <video src={url}>
|
|
170
|
+
* - <img src={url}>
|
|
171
|
+
* - <a href={url} download>
|
|
172
|
+
* - window.open(url)
|
|
173
|
+
*/
|
|
174
|
+
protected streamUrl<TInput>(route: string, input?: TInput): string;
|
|
175
|
+
/**
|
|
176
|
+
* Fetch a stream via GET with query params.
|
|
177
|
+
* Alternative to streamRequest() for cases where GET is preferred.
|
|
178
|
+
*/
|
|
179
|
+
protected streamGet<TInput>(route: string, input?: TInput, options?: RequestOptions): Promise<Response>;
|
|
180
|
+
/**
|
|
181
|
+
* Connect to an SSE endpoint.
|
|
182
|
+
* Returns a typed SSEConnection for handling server-sent events.
|
|
183
|
+
*/
|
|
184
|
+
protected sseConnect<TInput, TEvents extends Record<string, any> = Record<string, any>>(route: string, input?: TInput, options?: SSEConnectionOptions): SSEConnection<TEvents>;
|
|
185
|
+
/**
|
|
186
|
+
* Connect to a specific SSE route endpoint.
|
|
187
|
+
* Alias for sseConnect() - provides compatibility with @donkeylabs/server generated clients.
|
|
188
|
+
* @returns SSE connection with typed event handlers
|
|
189
|
+
*/
|
|
190
|
+
protected connectToSSERoute<TEvents extends Record<string, any>>(route: string, input?: Record<string, any>, options?: Omit<SSEOptions, "endpoint" | "channels">): SSEConnection<TEvents>;
|
|
191
|
+
/**
|
|
192
|
+
* Make a formData request (file uploads with validated fields).
|
|
193
|
+
*/
|
|
194
|
+
protected formDataRequest<TFields, TOutput>(route: string, fields: TFields, files: File[], options?: RequestOptions): Promise<TOutput>;
|
|
195
|
+
/**
|
|
196
|
+
* Make an HTML request (returns HTML string).
|
|
197
|
+
*/
|
|
198
|
+
protected htmlRequest<TInput>(route: string, input?: TInput, options?: RequestOptions): Promise<string>;
|
|
199
|
+
/**
|
|
200
|
+
* SSE (Server-Sent Events) subscription.
|
|
201
|
+
* Only works in the browser.
|
|
202
|
+
*/
|
|
203
|
+
sse: {
|
|
204
|
+
/**
|
|
205
|
+
* Subscribe to SSE channels.
|
|
206
|
+
* Returns a function to unsubscribe.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* const unsub = api.sse.subscribe(["notifications"], (event, data) => {
|
|
210
|
+
* console.log(event, data);
|
|
211
|
+
* });
|
|
212
|
+
* // Later: unsub();
|
|
213
|
+
*/
|
|
214
|
+
subscribe: (channels: string[], callback: (event: string, data: any) => void, options?: {
|
|
215
|
+
reconnect?: boolean;
|
|
216
|
+
}) => (() => void);
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Create an API client instance.
|
|
221
|
+
* Call with locals and fetch in SSR, without in browser.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* // +page.server.ts (SSR)
|
|
225
|
+
* const api = createApiClient({ locals, fetch });
|
|
226
|
+
*
|
|
227
|
+
* // +page.svelte (browser)
|
|
228
|
+
* const api = createApiClient();
|
|
229
|
+
*/
|
|
230
|
+
export declare function createApiClient<T extends UnifiedApiClientBase>(ClientClass: new (options?: ClientOptions) => T, options?: ClientOptions): T;
|
|
231
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,WAAW,aAAa;IAC5B,yEAAyE;IACzE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,oDAAoD;IACpD,MAAM,CAAC,EAAE,GAAG,CAAC;IACb,+EAA+E;IAC/E,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACtB;A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