dynamodb-reactive 0.1.0 → 0.1.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.
@@ -0,0 +1,371 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+
4
+ /**
5
+ * JSON Patch operation (RFC 6902)
6
+ */
7
+ interface JsonPatch {
8
+ op: 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test';
9
+ path: string;
10
+ value?: unknown;
11
+ from?: string;
12
+ }
13
+ /**
14
+ * Messages sent from client to server
15
+ */
16
+ interface SubscribeMessage {
17
+ type: 'subscribe';
18
+ subscriptionId: string;
19
+ path: string;
20
+ input: unknown;
21
+ }
22
+ interface UnsubscribeMessage {
23
+ type: 'unsubscribe';
24
+ subscriptionId: string;
25
+ }
26
+ interface CallMessage {
27
+ type: 'call';
28
+ callId: string;
29
+ path: string;
30
+ input: unknown;
31
+ }
32
+ type ClientMessage = SubscribeMessage | UnsubscribeMessage | CallMessage;
33
+ /**
34
+ * Messages sent from server to client
35
+ */
36
+ interface SnapshotMessage {
37
+ type: 'snapshot';
38
+ subscriptionId: string;
39
+ data: unknown;
40
+ }
41
+ interface PatchMessage {
42
+ type: 'patch';
43
+ subscriptionId: string;
44
+ patches: JsonPatch[];
45
+ }
46
+ interface ResultMessage {
47
+ type: 'result';
48
+ callId: string;
49
+ data: unknown;
50
+ }
51
+ interface ErrorMessage {
52
+ type: 'error';
53
+ message: string;
54
+ subscriptionId?: string;
55
+ callId?: string;
56
+ }
57
+ type ServerMessage = SnapshotMessage | PatchMessage | ResultMessage | ErrorMessage;
58
+ /**
59
+ * Client configuration
60
+ */
61
+ interface ReactiveClientConfig {
62
+ /**
63
+ * WebSocket URL to connect to
64
+ */
65
+ url: string;
66
+ /**
67
+ * Authentication token or getter function
68
+ */
69
+ auth?: string | (() => string | Promise<string>);
70
+ /**
71
+ * Enable automatic reconnection
72
+ * @default true
73
+ */
74
+ autoReconnect?: boolean;
75
+ /**
76
+ * Reconnection delay in milliseconds
77
+ * @default 1000
78
+ */
79
+ reconnectDelay?: number;
80
+ /**
81
+ * Maximum reconnection attempts
82
+ * @default 10
83
+ */
84
+ maxReconnectAttempts?: number;
85
+ /**
86
+ * Callback when connected
87
+ */
88
+ onConnect?: () => void;
89
+ /**
90
+ * Callback when disconnected
91
+ */
92
+ onDisconnect?: () => void;
93
+ /**
94
+ * Callback for errors
95
+ */
96
+ onError?: (error: Error) => void;
97
+ }
98
+ /**
99
+ * Subscription options
100
+ */
101
+ interface SubscriptionOptions<TInput> {
102
+ /**
103
+ * Input for the subscription
104
+ */
105
+ input?: TInput;
106
+ /**
107
+ * Whether to automatically resubscribe on reconnect
108
+ * @default true
109
+ */
110
+ resubscribeOnReconnect?: boolean;
111
+ /**
112
+ * Callback when data is received
113
+ */
114
+ onData?: (data: unknown) => void;
115
+ /**
116
+ * Callback when an error occurs
117
+ */
118
+ onError?: (error: Error) => void;
119
+ }
120
+ /**
121
+ * Active subscription
122
+ */
123
+ interface Subscription<TData> {
124
+ /**
125
+ * Current data
126
+ */
127
+ data: TData | undefined;
128
+ /**
129
+ * Whether the subscription is loading
130
+ */
131
+ loading: boolean;
132
+ /**
133
+ * Error if any
134
+ */
135
+ error: Error | undefined;
136
+ /**
137
+ * Unsubscribe from updates
138
+ */
139
+ unsubscribe: () => void;
140
+ /**
141
+ * Manually refetch the data
142
+ */
143
+ refetch: () => Promise<void>;
144
+ }
145
+ /**
146
+ * Connection state
147
+ */
148
+ type ConnectionState = 'connecting' | 'connected' | 'disconnected' | 'reconnecting';
149
+ /**
150
+ * Type helpers for inferring router types
151
+ */
152
+ type InferInput<T> = T extends {
153
+ input: infer TInput;
154
+ } ? TInput : undefined;
155
+ type InferOutput<T> = T extends {
156
+ output: infer TOutput;
157
+ } ? TOutput : unknown;
158
+
159
+ /**
160
+ * Internal subscription state
161
+ */
162
+ interface SubscriptionState<TData> {
163
+ id: string;
164
+ path: string;
165
+ input: unknown;
166
+ data: TData | undefined;
167
+ loading: boolean;
168
+ error: Error | undefined;
169
+ listeners: Set<() => void>;
170
+ options: SubscriptionOptions<unknown>;
171
+ }
172
+ /**
173
+ * Reactive client for type-safe subscriptions
174
+ */
175
+ declare class ReactiveClient {
176
+ private wsManager;
177
+ private subscriptions;
178
+ private pendingCalls;
179
+ private connectionState;
180
+ private stateListeners;
181
+ constructor(config: ReactiveClientConfig);
182
+ /**
183
+ * Connect to the server
184
+ */
185
+ connect(): Promise<void>;
186
+ /**
187
+ * Disconnect from the server
188
+ */
189
+ disconnect(): void;
190
+ /**
191
+ * Get current connection state
192
+ */
193
+ getConnectionState(): ConnectionState;
194
+ /**
195
+ * Subscribe to connection state changes
196
+ */
197
+ onConnectionStateChange(listener: (state: ConnectionState) => void): () => void;
198
+ /**
199
+ * Subscribe to a procedure
200
+ */
201
+ subscribe<TData>(path: string, options?: SubscriptionOptions<unknown>): Subscription<TData>;
202
+ /**
203
+ * Call a mutation procedure
204
+ */
205
+ call<TInput, TOutput>(path: string, input: TInput): Promise<TOutput>;
206
+ /**
207
+ * Unsubscribe from a subscription
208
+ */
209
+ private unsubscribe;
210
+ /**
211
+ * Refetch a subscription
212
+ */
213
+ private refetch;
214
+ /**
215
+ * Handle incoming server messages
216
+ */
217
+ private handleMessage;
218
+ /**
219
+ * Handle a snapshot message
220
+ */
221
+ private handleSnapshot;
222
+ /**
223
+ * Handle a patch message
224
+ */
225
+ private handlePatch;
226
+ /**
227
+ * Handle a result message
228
+ */
229
+ private handleResult;
230
+ /**
231
+ * Handle an error message
232
+ */
233
+ private handleError;
234
+ /**
235
+ * Resubscribe to all subscriptions after reconnect
236
+ */
237
+ private resubscribeAll;
238
+ /**
239
+ * Notify subscription listeners of state changes
240
+ */
241
+ private notifySubscriptionListeners;
242
+ /**
243
+ * Notify connection state listeners
244
+ */
245
+ private notifyStateListeners;
246
+ /**
247
+ * Add a listener for subscription state changes
248
+ * Used internally by React hooks
249
+ */
250
+ addSubscriptionListener(id: string, listener: () => void): () => void;
251
+ /**
252
+ * Get subscription state
253
+ * Used internally by React hooks
254
+ */
255
+ getSubscriptionState<TData>(id: string): SubscriptionState<TData> | undefined;
256
+ }
257
+ /**
258
+ * Create a type-safe reactive client
259
+ */
260
+ declare function createReactiveClient<TRouter>(config: ReactiveClientConfig): TypedReactiveClient<TRouter>;
261
+ /**
262
+ * Type-safe client proxy type
263
+ */
264
+ type TypedReactiveClient<TRouter> = {
265
+ [K in keyof TRouter]: TRouter[K] extends {
266
+ query: any;
267
+ } ? {
268
+ useSubscription: <TData = unknown>(input?: unknown, options?: Omit<SubscriptionOptions<unknown>, 'input'>) => Subscription<TData>;
269
+ } : TRouter[K] extends {
270
+ mutation: any;
271
+ } ? {
272
+ mutate: <TInput, TOutput>(input: TInput) => Promise<TOutput>;
273
+ } : TypedReactiveClient<TRouter[K]>;
274
+ } & {
275
+ _client: ReactiveClient;
276
+ connect: () => Promise<void>;
277
+ disconnect: () => void;
278
+ };
279
+
280
+ /**
281
+ * Provider component for the reactive client
282
+ *
283
+ * If no config.url or client is provided, WebSocket features are disabled
284
+ * and children are rendered without the reactive context.
285
+ */
286
+ declare function ReactiveClientProvider({ children, config, client: externalClient, }: {
287
+ children: ReactNode;
288
+ config?: ReactiveClientConfig;
289
+ client?: ReactiveClient;
290
+ }): react_jsx_runtime.JSX.Element;
291
+ /**
292
+ * Hook to get the reactive client
293
+ * Returns null if WebSocket is not configured
294
+ */
295
+ declare function useReactiveClient(): ReactiveClient | null;
296
+ /**
297
+ * Hook to get the reactive client, throwing if not available
298
+ */
299
+ declare function useReactiveClientOrThrow(): ReactiveClient;
300
+ /**
301
+ * Hook to get the connection state
302
+ * Returns 'disabled' if WebSocket is not configured
303
+ */
304
+ declare function useConnectionState(): ConnectionState | 'disabled';
305
+ /**
306
+ * Hook for reactive subscriptions
307
+ * Returns disabled state if WebSocket is not configured
308
+ */
309
+ declare function useSubscription<TData>(path: string, options?: SubscriptionOptions<unknown>): {
310
+ data: TData | undefined;
311
+ loading: boolean;
312
+ error: Error | undefined;
313
+ disabled: boolean;
314
+ refetch: () => Promise<void>;
315
+ };
316
+ /**
317
+ * Hook for mutations
318
+ * Throws if called when WebSocket is not configured
319
+ */
320
+ declare function useMutation<TInput, TOutput>(path: string): {
321
+ mutate: (input: TInput) => Promise<TOutput>;
322
+ data: TOutput | undefined;
323
+ loading: boolean;
324
+ error: Error | undefined;
325
+ disabled: boolean;
326
+ reset: () => void;
327
+ };
328
+ /**
329
+ * Create typed React hooks from a router type
330
+ */
331
+ declare function createReactiveHooks(): {
332
+ useSubscription: <TPath extends string, TData = unknown>(path: TPath, options?: SubscriptionOptions<unknown>) => {
333
+ data: TData | undefined;
334
+ loading: boolean;
335
+ error: Error | undefined;
336
+ disabled: boolean;
337
+ refetch: () => Promise<void>;
338
+ };
339
+ useMutation: <TPath extends string, TInput = unknown, TOutput = unknown>(path: TPath) => {
340
+ mutate: (input: TInput) => Promise<TOutput>;
341
+ data: TOutput | undefined;
342
+ loading: boolean;
343
+ error: Error | undefined;
344
+ disabled: boolean;
345
+ reset: () => void;
346
+ };
347
+ useConnectionState: typeof useConnectionState;
348
+ useReactiveClient: typeof useReactiveClient;
349
+ };
350
+ /**
351
+ * Type-safe hook creator for a specific procedure path
352
+ */
353
+ declare function createProcedureHooks<TInput, TOutput>(path: string): {
354
+ useSubscription: (input?: TInput, options?: Omit<SubscriptionOptions<TInput>, "input">) => {
355
+ data: TOutput | undefined;
356
+ loading: boolean;
357
+ error: Error | undefined;
358
+ disabled: boolean;
359
+ refetch: () => Promise<void>;
360
+ };
361
+ useMutation: () => {
362
+ mutate: (input: TInput) => Promise<TOutput>;
363
+ data: TOutput | undefined;
364
+ loading: boolean;
365
+ error: Error | undefined;
366
+ disabled: boolean;
367
+ reset: () => void;
368
+ };
369
+ };
370
+
371
+ export { type ConnectionState as C, type ErrorMessage as E, type InferInput as I, type JsonPatch as J, type PatchMessage as P, type ReactiveClientConfig as R, type ServerMessage as S, type TypedReactiveClient as T, type UnsubscribeMessage as U, type ClientMessage as a, ReactiveClient as b, createReactiveClient as c, type CallMessage as d, type InferOutput as e, type ResultMessage as f, type SnapshotMessage as g, type SubscribeMessage as h, type Subscription as i, type SubscriptionOptions as j, createProcedureHooks as k, createReactiveHooks as l, ReactiveClientProvider as m, useMutation as n, useReactiveClient as o, useReactiveClientOrThrow as p, useSubscription as q, useConnectionState as u };
package/dist/react.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- export * from '@dynamodb-reactive/client/react';
2
- //# sourceMappingURL=react.d.ts.map
1
+ export { m as ReactiveClientProvider, k as createProcedureHooks, l as createReactiveHooks, u as useConnectionState, n as useMutation, o as useReactiveClient, p as useReactiveClientOrThrow, q as useSubscription } from './react-BMZQ8Mth.js';
2
+ import 'react/jsx-runtime';
3
+ import 'react';
package/dist/react.js CHANGED
@@ -1,18 +1,3 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("@dynamodb-reactive/client/react"), exports);
1
+ export { ReactiveClientProvider, createProcedureHooks, createReactiveHooks, useConnectionState, useMutation, useReactiveClient, useReactiveClientOrThrow, useSubscription } from './chunk-KRZQWA2W.js';
2
+ //# sourceMappingURL=react.js.map
18
3
  //# sourceMappingURL=react.js.map
package/dist/react.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"react.js","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kEAAgD"}
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"react.js"}