dynamodb-reactive 0.1.22 → 0.2.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.
Files changed (46) hide show
  1. package/README.md +378 -153
  2. package/dist/client-r_tnzwqi.d.ts +224 -0
  3. package/dist/client.cjs +1023 -0
  4. package/dist/client.cjs.map +1 -0
  5. package/dist/client.d.cts +6 -0
  6. package/dist/client.d.ts +4 -70
  7. package/dist/client.js +1013 -1
  8. package/dist/client.js.map +1 -1
  9. package/dist/core.cjs +232 -0
  10. package/dist/core.cjs.map +1 -0
  11. package/dist/core.d.cts +4 -0
  12. package/dist/core.d.ts +3 -3
  13. package/dist/core.js +222 -1
  14. package/dist/core.js.map +1 -1
  15. package/dist/index.cjs +232 -0
  16. package/dist/index.cjs.map +1 -0
  17. package/dist/index.d.cts +157 -0
  18. package/dist/index.d.ts +103 -56
  19. package/dist/index.js +222 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/infra.cjs +623 -0
  22. package/dist/infra.cjs.map +1 -0
  23. package/dist/infra.d.cts +119 -0
  24. package/dist/infra.d.ts +50 -102
  25. package/dist/infra.js +371 -144
  26. package/dist/infra.js.map +1 -1
  27. package/dist/react.cjs +1250 -0
  28. package/dist/react.cjs.map +1 -0
  29. package/dist/react.d.cts +68 -0
  30. package/dist/react.d.ts +68 -3
  31. package/dist/react.js +1237 -1
  32. package/dist/react.js.map +1 -1
  33. package/dist/server.cjs +2351 -0
  34. package/dist/server.cjs.map +1 -0
  35. package/dist/server.d.cts +364 -0
  36. package/dist/server.d.ts +58 -313
  37. package/dist/server.js +1855 -1208
  38. package/dist/server.js.map +1 -1
  39. package/dist/{table-tN3D4Tm-.d.ts → table-BijJ73pX.d.ts} +2 -34
  40. package/dist/{types-ISMJfyIv.d.ts → types-LMLk8JVC.d.ts} +56 -30
  41. package/package.json +79 -41
  42. package/dist/chunk-2KC44Q5I.js +0 -1103
  43. package/dist/chunk-2KC44Q5I.js.map +0 -1
  44. package/dist/chunk-ZAJA4KBE.js +0 -150
  45. package/dist/chunk-ZAJA4KBE.js.map +0 -1
  46. package/dist/react-D_NLHuuO.d.ts +0 -556
@@ -1,556 +0,0 @@
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
- interface InitMessage {
33
- type: 'init';
34
- }
35
- interface PingMessage {
36
- type: 'ping';
37
- }
38
- type ClientMessage = InitMessage | PingMessage | SubscribeMessage | UnsubscribeMessage | CallMessage;
39
- /**
40
- * Messages sent from server to client
41
- */
42
- interface ConnectedMessage {
43
- type: 'connected';
44
- connectionId: string;
45
- }
46
- interface SnapshotMessage {
47
- type: 'snapshot';
48
- subscriptionId: string;
49
- data: unknown;
50
- }
51
- interface PatchMessage {
52
- type: 'patch';
53
- subscriptionId: string;
54
- patches: JsonPatch[];
55
- }
56
- interface ResultMessage {
57
- type: 'result';
58
- callId: string;
59
- data: unknown;
60
- }
61
- interface ErrorMessage {
62
- type: 'error';
63
- message: string;
64
- subscriptionId?: string;
65
- callId?: string;
66
- }
67
- interface PongMessage {
68
- type: 'pong';
69
- }
70
- type ServerMessage = ConnectedMessage | PongMessage | SnapshotMessage | PatchMessage | ResultMessage | ErrorMessage;
71
- /**
72
- * Client configuration
73
- */
74
- interface ReactiveClientConfig {
75
- /**
76
- * WebSocket URL to connect to
77
- */
78
- url: string;
79
- /**
80
- * HTTP URL for subscribe/call requests.
81
- * Defaults to '/api/reactive' for Next.js apps.
82
- */
83
- httpUrl?: string;
84
- /**
85
- * Authentication token or getter function
86
- */
87
- auth?: string | (() => string | Promise<string>);
88
- /**
89
- * Custom headers to send with HTTP requests.
90
- * Can be a static object or a getter function for dynamic headers.
91
- * Useful for passing auth tokens or user context to the server.
92
- */
93
- headers?: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
94
- /**
95
- * Enable automatic reconnection
96
- * @default true
97
- */
98
- autoReconnect?: boolean;
99
- /**
100
- * Reconnection delay in milliseconds
101
- * @default 1000
102
- */
103
- reconnectDelay?: number;
104
- /**
105
- * Maximum reconnection attempts
106
- * @default 10
107
- */
108
- maxReconnectAttempts?: number;
109
- /**
110
- * Callback when connected
111
- */
112
- onConnect?: () => void;
113
- /**
114
- * Callback when disconnected
115
- */
116
- onDisconnect?: () => void;
117
- /**
118
- * Callback for errors
119
- */
120
- onError?: (error: Error) => void;
121
- }
122
- /**
123
- * Subscription options
124
- */
125
- interface SubscriptionOptions<TInput> {
126
- /**
127
- * Input for the subscription
128
- */
129
- input?: TInput;
130
- /**
131
- * Whether to automatically resubscribe on reconnect
132
- * @default true
133
- */
134
- resubscribeOnReconnect?: boolean;
135
- /**
136
- * Callback when data is received
137
- */
138
- onData?: (data: unknown) => void;
139
- /**
140
- * Callback when an error occurs
141
- */
142
- onError?: (error: Error) => void;
143
- }
144
- /**
145
- * Active subscription
146
- */
147
- interface Subscription<TData> {
148
- /**
149
- * Subscription ID (used for registering listeners)
150
- */
151
- id: string;
152
- /**
153
- * Current data
154
- */
155
- data: TData | undefined;
156
- /**
157
- * Whether the subscription is loading
158
- */
159
- loading: boolean;
160
- /**
161
- * Error if any
162
- */
163
- error: Error | undefined;
164
- /**
165
- * Unsubscribe from updates
166
- */
167
- unsubscribe: () => void;
168
- /**
169
- * Manually refetch the data
170
- */
171
- refetch: () => Promise<void>;
172
- }
173
- /**
174
- * Connection state
175
- */
176
- type ConnectionState = 'connecting' | 'connected' | 'disconnected' | 'reconnecting';
177
- /**
178
- * Type helpers for inferring router types
179
- */
180
- type InferInput<T> = T extends {
181
- input: infer TInput;
182
- } ? TInput : undefined;
183
- type InferOutput<T> = T extends {
184
- output: infer TOutput;
185
- } ? TOutput : unknown;
186
- /**
187
- * Mutation options for optimistic updates
188
- */
189
- interface MutationOptions<TInput = unknown, TOutput = unknown> {
190
- /**
191
- * Subscription path to optimistically update (e.g., 'todos.list')
192
- * If not provided, auto-detected from mutation path (e.g., 'todos.toggle' -> 'todos.list')
193
- */
194
- invalidates?: string;
195
- /**
196
- * How to apply the mutation result to subscription data (applied INSTANTLY before HTTP)
197
- * - 'merge': Find item by input.id and merge input fields into it
198
- * - 'remove': Remove item by input.id
199
- * - Custom function for complex cases
200
- */
201
- optimisticUpdate?: 'merge' | 'remove' | ((currentData: unknown[], input: TInput, result?: TOutput) => unknown[]);
202
- /**
203
- * For create operations (no input.id), provide the optimistic item to add.
204
- * Can be the item object or a function that receives input and current data.
205
- */
206
- optimisticData?: TOutput | ((input: TInput, currentData: unknown[]) => TOutput | undefined);
207
- }
208
-
209
- /**
210
- * Internal subscription state
211
- */
212
- interface SubscriptionState<TData> {
213
- id: string;
214
- path: string;
215
- input: unknown;
216
- data: TData | undefined;
217
- loading: boolean;
218
- error: Error | undefined;
219
- listeners: Set<() => void>;
220
- options: SubscriptionOptions<unknown>;
221
- }
222
- /**
223
- * Reactive client for type-safe subscriptions
224
- */
225
- declare class ReactiveClient {
226
- private wsManager;
227
- private httpUrl;
228
- private authGetter?;
229
- private headersGetter?;
230
- private subscriptions;
231
- private pendingCalls;
232
- private connectionState;
233
- private stateListeners;
234
- private optimisticUpdates;
235
- constructor(config: ReactiveClientConfig);
236
- /**
237
- * Connect to the server
238
- */
239
- connect(): Promise<void>;
240
- /**
241
- * Disconnect from the server
242
- */
243
- disconnect(): void;
244
- /**
245
- * Force reconnect - closes existing connection and reconnects
246
- * Useful for recovering from stale connections
247
- */
248
- forceReconnect(): void;
249
- /**
250
- * Update the headers getter for HTTP requests.
251
- * Useful for dynamically setting auth or user context headers.
252
- */
253
- setHeaders(headers: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>)): void;
254
- /**
255
- * Get current connection state
256
- */
257
- getConnectionState(): ConnectionState;
258
- /**
259
- * Subscribe to connection state changes
260
- */
261
- onConnectionStateChange(listener: (state: ConnectionState) => void): () => void;
262
- /**
263
- * Subscribe to a procedure via HTTP.
264
- * Snapshot is returned via HTTP, patches come via WebSocket.
265
- */
266
- subscribe<TData>(path: string, options?: SubscriptionOptions<unknown>): Subscription<TData>;
267
- /**
268
- * Send subscribe request via HTTP
269
- */
270
- private sendSubscribeRequest;
271
- /**
272
- * Call a mutation procedure via HTTP with optimistic update support.
273
- * All optimistic updates are applied INSTANTLY (before HTTP request).
274
- */
275
- call<TInput, TOutput>(path: string, input: TInput, options?: MutationOptions<TInput, TOutput>): Promise<TOutput>;
276
- /**
277
- * Unsubscribe from a subscription
278
- */
279
- private unsubscribe;
280
- /**
281
- * Refetch a subscription via HTTP
282
- */
283
- private refetch;
284
- /**
285
- * Handle incoming server messages.
286
- * Note: 'connected' messages are handled by WebSocketManager before reaching here.
287
- */
288
- private handleMessage;
289
- /**
290
- * Handle a snapshot message
291
- */
292
- private handleSnapshot;
293
- /**
294
- * Handle a patch message
295
- */
296
- private handlePatch;
297
- /**
298
- * Handle a result message
299
- */
300
- private handleResult;
301
- /**
302
- * Handle an error message
303
- */
304
- private handleError;
305
- /**
306
- * Resubscribe to all subscriptions after reconnect via HTTP
307
- */
308
- private resubscribeAll;
309
- /**
310
- * Notify subscription listeners of state changes
311
- */
312
- private notifySubscriptionListeners;
313
- /**
314
- * Notify connection state listeners
315
- */
316
- private notifyStateListeners;
317
- /**
318
- * Add a listener for subscription state changes
319
- * Used internally by React hooks
320
- */
321
- addSubscriptionListener(id: string, listener: () => void): () => void;
322
- /**
323
- * Get subscription state
324
- * Used internally by React hooks
325
- */
326
- getSubscriptionState<TData>(id: string): SubscriptionState<TData> | undefined;
327
- /**
328
- * Find subscription by path
329
- */
330
- private findSubscriptionByPath;
331
- /**
332
- * Apply an optimistic update to a subscription's data.
333
- * Stores the original data for rollback (on mutation error) or as base for
334
- * incoming server patches. Safe to call after external API calls — the next
335
- * server snapshot/patch will reconcile automatically.
336
- */
337
- applyOptimisticUpdate(subscriptionId: string, updater: (data: unknown) => unknown): void;
338
- /**
339
- * Rollback an optimistic update
340
- */
341
- private rollbackOptimisticUpdate;
342
- /**
343
- * Clear optimistic state (called when real data arrives)
344
- */
345
- private clearOptimisticUpdate;
346
- }
347
- /**
348
- * Create a type-safe reactive client
349
- */
350
- declare function createReactiveClient<TRouter>(config: ReactiveClientConfig): TypedReactiveClient<TRouter>;
351
- /**
352
- * Type-safe client proxy type
353
- */
354
- type TypedReactiveClient<TRouter> = {
355
- [K in keyof TRouter]: TRouter[K] extends {
356
- query: unknown;
357
- } ? {
358
- useSubscription: <TData = unknown>(input?: unknown, options?: Omit<SubscriptionOptions<unknown>, 'input'>) => Subscription<TData>;
359
- } : TRouter[K] extends {
360
- mutation: unknown;
361
- } ? {
362
- mutate: <TInput, TOutput>(input: TInput) => Promise<TOutput>;
363
- } : TypedReactiveClient<TRouter[K]>;
364
- } & {
365
- _client: ReactiveClient;
366
- connect: () => Promise<void>;
367
- disconnect: () => void;
368
- };
369
-
370
- /**
371
- * Provider component for the reactive client
372
- *
373
- * If no config.url or client is provided, WebSocket features are disabled
374
- * and children are rendered without the reactive context.
375
- */
376
- declare function ReactiveClientProvider({ children, config, client: externalClient, }: {
377
- children: ReactNode;
378
- config?: ReactiveClientConfig;
379
- client?: ReactiveClient;
380
- }): react_jsx_runtime.JSX.Element;
381
- /**
382
- * Hook to get the reactive client
383
- * Returns null if WebSocket is not configured
384
- */
385
- declare function useReactiveClient(): ReactiveClient | null;
386
- /**
387
- * Hook to get the reactive client, throwing if not available
388
- */
389
- declare function useReactiveClientOrThrow(): ReactiveClient;
390
- /**
391
- * Hook to get the connection state
392
- * Returns 'disabled' if WebSocket is not configured
393
- */
394
- declare function useConnectionState(): ConnectionState | 'disabled';
395
- /**
396
- * Hook for reactive subscriptions
397
- * Returns disabled state if WebSocket is not configured
398
- */
399
- declare function useSubscription<TData>(path: string, options?: SubscriptionOptions<unknown>): {
400
- data: TData | undefined;
401
- loading: boolean;
402
- error: Error | undefined;
403
- disabled: boolean;
404
- refetch: () => Promise<void>;
405
- setData: (updater: (data: TData) => TData) => void;
406
- };
407
- /**
408
- * Hook for mutations with automatic optimistic updates
409
- *
410
- * Auto-detection:
411
- * - `invalidates`: Auto-detected from path (e.g., 'todos.toggle' -> 'todos.list')
412
- * - `optimisticUpdate`: Auto-detected from path ('*.delete' -> 'remove', others -> 'merge')
413
- *
414
- * You can override these with explicit options.
415
- */
416
- declare function useMutation<TInput, TOutput>(path: string, options?: MutationOptions<TInput, TOutput>): {
417
- mutate: (input: TInput) => Promise<TOutput>;
418
- data: TOutput | undefined;
419
- loading: boolean;
420
- error: Error | undefined;
421
- disabled: boolean;
422
- reset: () => void;
423
- };
424
- /**
425
- * Type helper for extracting procedure input type from ProcedureDefinition
426
- * Uses Zod's internal _output property to extract the parsed type
427
- * (Zod schemas have _input/_output as actual properties for type inference)
428
- */
429
- type ProcedureInput<T> = T extends {
430
- inputSchema?: {
431
- _output: infer I;
432
- };
433
- } ? I : undefined;
434
- /**
435
- * Type helper for extracting procedure output type from ProcedureDefinition
436
- * Extracts from resolver return type using any[] for flexible argument matching
437
- */
438
- type ProcedureOutput<T> = T extends {
439
- resolver: (...args: any[]) => infer TReturn;
440
- } ? Awaited<TReturn> : unknown;
441
- /**
442
- * Typed client hooks for a router procedure
443
- * Provides both useQuery and useMutation - use whichever matches your procedure type
444
- */
445
- type TypedProcedureHooks<TInput, TOutput> = {
446
- useQuery: (input: TInput, options?: Omit<SubscriptionOptions<TInput>, 'input'>) => {
447
- data: TOutput | undefined;
448
- loading: boolean;
449
- error: Error | undefined;
450
- disabled: boolean;
451
- refetch: () => Promise<void>;
452
- setData: (updater: (data: TOutput) => TOutput) => void;
453
- };
454
- useMutation: (options?: MutationOptions<TInput, TOutput>) => {
455
- mutate: (input: TInput) => Promise<TOutput>;
456
- data: TOutput | undefined;
457
- loading: boolean;
458
- error: Error | undefined;
459
- disabled: boolean;
460
- reset: () => void;
461
- };
462
- };
463
- /**
464
- * Extract router definition from Router class or use T directly
465
- */
466
- type ExtractRouterDefinition<T> = T extends {
467
- definition: infer TDef;
468
- } ? TDef : T;
469
- /**
470
- * Recursive type for typed client proxy
471
- * Checks for 'resolver' property to identify procedures (all procedures have it)
472
- */
473
- type TypedClientProxyRecursive<T> = T extends {
474
- resolver: unknown;
475
- } ? TypedProcedureHooks<ProcedureInput<T>, ProcedureOutput<T>> : {
476
- [K in keyof T]: TypedClientProxyRecursive<T[K]>;
477
- };
478
- /**
479
- * Client utilities available at the root level
480
- */
481
- interface ClientUtilities {
482
- /**
483
- * Set custom headers to be sent with HTTP requests.
484
- * Useful for auth context (e.g., user tokens, session info).
485
- */
486
- setHeaders: (headers: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>)) => void;
487
- }
488
- /**
489
- * Typed client proxy combining router procedures and client utilities
490
- */
491
- type TypedClientProxy<T> = TypedClientProxyRecursive<ExtractRouterDefinition<T>> & ClientUtilities;
492
- /**
493
- * Create a typed client proxy for use in React components.
494
- *
495
- * @example
496
- * ```tsx
497
- * const client = useClient<AppRouter>();
498
- *
499
- * // Queries (subscriptions)
500
- * const { data, loading } = client.todos.list.useQuery({});
501
- *
502
- * // Mutations
503
- * const { mutate, loading } = client.todos.toggle.useMutation();
504
- * await mutate({ id: '123' });
505
- *
506
- * // Set custom headers for HTTP requests (e.g., for auth context)
507
- * client.setHeaders({ 'Authorization': 'Bearer token' });
508
- * ```
509
- */
510
- declare function useClient<TRouter>(): TypedClientProxy<TRouter>;
511
- /**
512
- * @deprecated Use useClient<TRouter>() instead for typed proxy access
513
- */
514
- declare function createReactiveHooks(): {
515
- useSubscription: <TPath extends string, TData = unknown>(path: TPath, options?: SubscriptionOptions<unknown>) => {
516
- data: TData | undefined;
517
- loading: boolean;
518
- error: Error | undefined;
519
- disabled: boolean;
520
- refetch: () => Promise<void>;
521
- setData: (updater: (data: TData) => TData) => void;
522
- };
523
- useMutation: <TPath extends string, TInput = unknown, TOutput = unknown>(path: TPath, options?: MutationOptions<TInput, TOutput>) => {
524
- mutate: (input: TInput) => Promise<TOutput>;
525
- data: TOutput | undefined;
526
- loading: boolean;
527
- error: Error | undefined;
528
- disabled: boolean;
529
- reset: () => void;
530
- };
531
- useConnectionState: typeof useConnectionState;
532
- useReactiveClient: typeof useReactiveClient;
533
- };
534
- /**
535
- * @deprecated Use useClient<TRouter>() instead for typed proxy access
536
- */
537
- declare function createProcedureHooks<TInput, TOutput>(path: string): {
538
- useSubscription: (input?: TInput, options?: Omit<SubscriptionOptions<TInput>, "input">) => {
539
- data: TOutput | undefined;
540
- loading: boolean;
541
- error: Error | undefined;
542
- disabled: boolean;
543
- refetch: () => Promise<void>;
544
- setData: (updater: (data: TOutput) => TOutput) => void;
545
- };
546
- useMutation: (options?: MutationOptions<TInput, TOutput>) => {
547
- mutate: (input: TInput) => Promise<TOutput>;
548
- data: TOutput | undefined;
549
- loading: boolean;
550
- error: Error | undefined;
551
- disabled: boolean;
552
- reset: () => void;
553
- };
554
- };
555
-
556
- export { type ConnectionState as C, type ErrorMessage as E, type InferInput as I, type JsonPatch as J, type MutationOptions as M, 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, useConnectionState as n, useMutation as o, useReactiveClient as p, useReactiveClientOrThrow as q, useSubscription as r, useClient as u };