svelte-firekit 0.0.25 → 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.
- package/README.md +445 -213
- package/dist/components/Collection.svelte +150 -0
- package/dist/components/Collection.svelte.d.ts +27 -0
- package/dist/components/Ddoc.svelte +131 -0
- package/dist/components/Ddoc.svelte.d.ts +28 -0
- package/dist/components/Node.svelte +97 -0
- package/dist/components/Node.svelte.d.ts +23 -0
- package/dist/components/auth-guard.svelte +89 -0
- package/dist/components/auth-guard.svelte.d.ts +26 -0
- package/dist/components/custom-guard.svelte +122 -0
- package/dist/components/custom-guard.svelte.d.ts +31 -0
- package/dist/components/download-url.svelte +92 -0
- package/dist/components/download-url.svelte.d.ts +19 -0
- package/dist/components/firebase-app.svelte +30 -0
- package/dist/components/firebase-app.svelte.d.ts +7 -0
- package/dist/components/node-list.svelte +102 -0
- package/dist/components/node-list.svelte.d.ts +27 -0
- package/dist/components/signed-in.svelte +42 -0
- package/dist/components/signed-in.svelte.d.ts +11 -0
- package/dist/components/signed-out.svelte +42 -0
- package/dist/components/signed-out.svelte.d.ts +11 -0
- package/dist/components/storage-list.svelte +97 -0
- package/dist/components/storage-list.svelte.d.ts +26 -0
- package/dist/components/upload-task.svelte +108 -0
- package/dist/components/upload-task.svelte.d.ts +24 -0
- package/dist/config.js +17 -39
- package/dist/firebase.d.ts +43 -21
- package/dist/firebase.js +121 -35
- package/dist/index.d.ts +21 -13
- package/dist/index.js +27 -15
- package/dist/services/auth.d.ts +397 -0
- package/dist/services/auth.js +882 -0
- package/dist/services/collection.svelte.d.ts +286 -0
- package/dist/services/collection.svelte.js +871 -0
- package/dist/services/document.svelte.d.ts +288 -0
- package/dist/services/document.svelte.js +555 -0
- package/dist/services/mutations.d.ts +336 -0
- package/dist/services/mutations.js +1079 -0
- package/dist/services/presence.svelte.d.ts +141 -0
- package/dist/services/presence.svelte.js +727 -0
- package/dist/{realtime → services}/realtime.svelte.d.ts +3 -1
- package/dist/{realtime → services}/realtime.svelte.js +13 -7
- package/dist/services/storage.svelte.d.ts +257 -0
- package/dist/services/storage.svelte.js +374 -0
- package/dist/services/user.svelte.d.ts +296 -0
- package/dist/services/user.svelte.js +609 -0
- package/dist/types/auth.d.ts +158 -0
- package/dist/types/auth.js +106 -0
- package/dist/types/collection.d.ts +360 -0
- package/dist/types/collection.js +167 -0
- package/dist/types/document.d.ts +342 -0
- package/dist/types/document.js +148 -0
- package/dist/types/firebase.d.ts +44 -0
- package/dist/types/firebase.js +33 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.js +4 -0
- package/dist/types/mutations.d.ts +387 -0
- package/dist/types/mutations.js +205 -0
- package/dist/types/presence.d.ts +282 -0
- package/dist/types/presence.js +80 -0
- package/dist/utils/errors.d.ts +21 -0
- package/dist/utils/errors.js +35 -0
- package/dist/utils/firestore.d.ts +9 -0
- package/dist/utils/firestore.js +33 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +8 -0
- package/dist/utils/providers.d.ts +16 -0
- package/dist/utils/providers.js +30 -0
- package/dist/utils/user.d.ts +8 -0
- package/dist/utils/user.js +29 -0
- package/package.json +64 -64
- package/dist/auth/auth.d.ts +0 -117
- package/dist/auth/auth.js +0 -194
- package/dist/auth/presence.svelte.d.ts +0 -139
- package/dist/auth/presence.svelte.js +0 -373
- package/dist/auth/user.svelte.d.ts +0 -112
- package/dist/auth/user.svelte.js +0 -155
- package/dist/firestore/awaitable-doc.svelte.d.ts +0 -141
- package/dist/firestore/awaitable-doc.svelte.js +0 -183
- package/dist/firestore/batch-mutations.svelte.d.ts +0 -140
- package/dist/firestore/batch-mutations.svelte.js +0 -218
- package/dist/firestore/collection-group.svelte.d.ts +0 -78
- package/dist/firestore/collection-group.svelte.js +0 -120
- package/dist/firestore/collection.svelte.d.ts +0 -96
- package/dist/firestore/collection.svelte.js +0 -137
- package/dist/firestore/doc.svelte.d.ts +0 -90
- package/dist/firestore/doc.svelte.js +0 -131
- package/dist/firestore/document-mutations.svelte.d.ts +0 -164
- package/dist/firestore/document-mutations.svelte.js +0 -273
- package/dist/storage/download-url.svelte.d.ts +0 -83
- package/dist/storage/download-url.svelte.js +0 -114
- package/dist/storage/storage-list.svelte.d.ts +0 -89
- package/dist/storage/storage-list.svelte.js +0 -123
- package/dist/storage/upload-task.svelte.d.ts +0 -94
- package/dist/storage/upload-task.svelte.js +0 -138
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview FirekitDoc - Optimized Firestore document management for Svelte
|
|
3
|
+
* @module FirekitDoc
|
|
4
|
+
* @version 1.0.0
|
|
5
|
+
*/
|
|
6
|
+
import { type DocumentReference, type DocumentData } from 'firebase/firestore';
|
|
7
|
+
import { type DocumentState, type DocumentOptions, DocumentError } from '../types/document.js';
|
|
8
|
+
/**
|
|
9
|
+
* Manages real-time Firestore document subscriptions with reactive state.
|
|
10
|
+
* Uses Svelte 5 runes for optimal reactivity and performance.
|
|
11
|
+
*
|
|
12
|
+
* @class FirekitDoc
|
|
13
|
+
* @template T Document data type
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* interface User {
|
|
18
|
+
* id: string;
|
|
19
|
+
* name: string;
|
|
20
|
+
* email: string;
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* // Create document subscription
|
|
24
|
+
* const userDoc = firekitDoc<User>('users/123', {
|
|
25
|
+
* id: '123',
|
|
26
|
+
* name: 'Loading...',
|
|
27
|
+
* email: ''
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* // Access reactive state in Svelte component
|
|
31
|
+
* $: if (userDoc.loading) {
|
|
32
|
+
* console.log('Loading user...');
|
|
33
|
+
* } else if (userDoc.error) {
|
|
34
|
+
* console.error('Error:', userDoc.error);
|
|
35
|
+
* } else if (userDoc.exists) {
|
|
36
|
+
* console.log('User data:', userDoc.data);
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare class FirekitDoc<T extends DocumentData = DocumentData> {
|
|
41
|
+
/** Document reference */
|
|
42
|
+
private docRef;
|
|
43
|
+
/** Unsubscribe function for real-time listener */
|
|
44
|
+
private unsubscribe;
|
|
45
|
+
/** Configuration options */
|
|
46
|
+
private options;
|
|
47
|
+
/** Current document data - reactive */
|
|
48
|
+
private _data;
|
|
49
|
+
/** Loading state - reactive */
|
|
50
|
+
private _loading;
|
|
51
|
+
/** Error state - reactive */
|
|
52
|
+
private _error;
|
|
53
|
+
/** Document exists state - reactive */
|
|
54
|
+
private _exists;
|
|
55
|
+
/** Computed document state with all reactive properties */
|
|
56
|
+
private _computedState;
|
|
57
|
+
/** Derived: Whether document is in a valid state for operations */
|
|
58
|
+
private _isValid;
|
|
59
|
+
/** Derived: Whether document can be refreshed */
|
|
60
|
+
private _canRefresh;
|
|
61
|
+
/** Derived: Whether document has pending operations */
|
|
62
|
+
private _hasPendingOperations;
|
|
63
|
+
/**
|
|
64
|
+
* Creates a document subscription with real-time updates
|
|
65
|
+
*
|
|
66
|
+
* @param ref Document path or DocumentReference
|
|
67
|
+
* @param startWith Initial data to show while loading
|
|
68
|
+
* @param options Configuration options
|
|
69
|
+
*/
|
|
70
|
+
constructor(ref: string | DocumentReference<T>, startWith?: T, options?: DocumentOptions);
|
|
71
|
+
/**
|
|
72
|
+
* Initialize document reference and subscription
|
|
73
|
+
*/
|
|
74
|
+
private initializeDocument;
|
|
75
|
+
/**
|
|
76
|
+
* Set up real-time document listener
|
|
77
|
+
*/
|
|
78
|
+
private setupRealtimeListener;
|
|
79
|
+
/**
|
|
80
|
+
* Fetch document data once (no real-time updates)
|
|
81
|
+
*/
|
|
82
|
+
private fetchOnce;
|
|
83
|
+
/**
|
|
84
|
+
* Process document snapshot and update state
|
|
85
|
+
*/
|
|
86
|
+
private processSnapshot;
|
|
87
|
+
/**
|
|
88
|
+
* Handle and process errors
|
|
89
|
+
*/
|
|
90
|
+
private handleError;
|
|
91
|
+
/**
|
|
92
|
+
* Map Firestore error codes to DocumentErrorCode
|
|
93
|
+
*/
|
|
94
|
+
private mapFirestoreErrorCode;
|
|
95
|
+
/**
|
|
96
|
+
* Reactive method: Automatically retry if error is retryable
|
|
97
|
+
* @returns Promise that resolves when retry is complete
|
|
98
|
+
*/
|
|
99
|
+
retryIfNeeded(): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Reactive method: Refresh if data is stale
|
|
102
|
+
* @param maxAge Maximum age in milliseconds
|
|
103
|
+
* @returns Promise that resolves when refresh is complete
|
|
104
|
+
*/
|
|
105
|
+
refreshIfStale(maxAge?: number): Promise<void>;
|
|
106
|
+
/**
|
|
107
|
+
* Reactive method: Get fresh data if not ready
|
|
108
|
+
* @returns Promise resolving to document data
|
|
109
|
+
*/
|
|
110
|
+
ensureReady(): Promise<T | null>;
|
|
111
|
+
/**
|
|
112
|
+
* Update state atomically with partial updates
|
|
113
|
+
* @param updates Partial state updates
|
|
114
|
+
*/
|
|
115
|
+
private updateState;
|
|
116
|
+
/**
|
|
117
|
+
* Get human-readable status string
|
|
118
|
+
* @returns Status description
|
|
119
|
+
*/
|
|
120
|
+
private getStatus;
|
|
121
|
+
/**
|
|
122
|
+
* Get current document data
|
|
123
|
+
* @returns Current document data or null
|
|
124
|
+
*/
|
|
125
|
+
get data(): T | null;
|
|
126
|
+
/**
|
|
127
|
+
* Get document ID
|
|
128
|
+
* @returns Document ID or empty string if not available
|
|
129
|
+
*/
|
|
130
|
+
get id(): string;
|
|
131
|
+
/**
|
|
132
|
+
* Get loading state
|
|
133
|
+
* @returns True if document is currently loading
|
|
134
|
+
*/
|
|
135
|
+
get loading(): boolean;
|
|
136
|
+
/**
|
|
137
|
+
* Get error state
|
|
138
|
+
* @returns Current error or null if no error
|
|
139
|
+
*/
|
|
140
|
+
get error(): DocumentError | null;
|
|
141
|
+
/**
|
|
142
|
+
* Check if document exists
|
|
143
|
+
* @returns True if document exists in Firestore
|
|
144
|
+
*/
|
|
145
|
+
get exists(): boolean;
|
|
146
|
+
/**
|
|
147
|
+
* Get document reference
|
|
148
|
+
* @returns Firestore DocumentReference
|
|
149
|
+
* @throws DocumentError if reference is not available
|
|
150
|
+
*/
|
|
151
|
+
get ref(): DocumentReference<T>;
|
|
152
|
+
/**
|
|
153
|
+
* Get current document state summary
|
|
154
|
+
* @returns Complete document state object
|
|
155
|
+
*/
|
|
156
|
+
get state(): DocumentState<T>;
|
|
157
|
+
/**
|
|
158
|
+
* Get enhanced computed state
|
|
159
|
+
* @returns Computed state with additional reactive properties
|
|
160
|
+
*/
|
|
161
|
+
get computedState(): {
|
|
162
|
+
data: T | null;
|
|
163
|
+
loading: boolean;
|
|
164
|
+
error: DocumentError | null;
|
|
165
|
+
exists: boolean;
|
|
166
|
+
id: string;
|
|
167
|
+
isEmpty: boolean;
|
|
168
|
+
isReady: boolean;
|
|
169
|
+
hasData: boolean;
|
|
170
|
+
canRetry: boolean;
|
|
171
|
+
isStale: boolean | null;
|
|
172
|
+
status: string;
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Check if document is in valid state for operations
|
|
176
|
+
* @returns True if document can be operated on
|
|
177
|
+
*/
|
|
178
|
+
get isValid(): boolean;
|
|
179
|
+
/**
|
|
180
|
+
* Check if document can be refreshed
|
|
181
|
+
* @returns True if refresh operation is available
|
|
182
|
+
*/
|
|
183
|
+
get canRefresh(): boolean;
|
|
184
|
+
/**
|
|
185
|
+
* Check if document has pending operations
|
|
186
|
+
* @returns True if there are ongoing operations
|
|
187
|
+
*/
|
|
188
|
+
get hasPendingOperations(): boolean;
|
|
189
|
+
/**
|
|
190
|
+
* Manually refresh document data
|
|
191
|
+
* @returns Promise that resolves when refresh is complete
|
|
192
|
+
*/
|
|
193
|
+
refresh(): Promise<void>;
|
|
194
|
+
/**
|
|
195
|
+
* Get fresh data from server (bypassing cache)
|
|
196
|
+
* @returns Promise resolving to fresh document data
|
|
197
|
+
*/
|
|
198
|
+
getFromServer(): Promise<T | null>;
|
|
199
|
+
/**
|
|
200
|
+
* Switch between realtime and one-time fetch modes
|
|
201
|
+
* @param realtime Whether to enable real-time updates
|
|
202
|
+
*/
|
|
203
|
+
setRealtimeMode(realtime: boolean): void;
|
|
204
|
+
/**
|
|
205
|
+
* Check if document data is stale (for cache management)
|
|
206
|
+
* @param maxAge Maximum age in milliseconds
|
|
207
|
+
* @returns True if data is considered stale
|
|
208
|
+
*/
|
|
209
|
+
isStale(maxAge?: number): boolean;
|
|
210
|
+
/**
|
|
211
|
+
* Clean up resources and unsubscribe from listeners
|
|
212
|
+
*/
|
|
213
|
+
dispose(): void;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Creates a reactive document subscription with real-time updates
|
|
217
|
+
*
|
|
218
|
+
* @template T Document data type
|
|
219
|
+
* @param ref Document path or DocumentReference
|
|
220
|
+
* @param startWith Initial data to show while loading
|
|
221
|
+
* @param options Configuration options
|
|
222
|
+
* @returns FirekitDoc instance with reactive state
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```typescript
|
|
226
|
+
* interface User {
|
|
227
|
+
* id: string;
|
|
228
|
+
* name: string;
|
|
229
|
+
* email: string;
|
|
230
|
+
* }
|
|
231
|
+
*
|
|
232
|
+
* // Real-time document subscription
|
|
233
|
+
* const userDoc = firekitDoc<User>('users/123', {
|
|
234
|
+
* id: '123',
|
|
235
|
+
* name: 'Loading...',
|
|
236
|
+
* email: ''
|
|
237
|
+
* });
|
|
238
|
+
*
|
|
239
|
+
* // One-time fetch
|
|
240
|
+
* const userDoc = firekitDoc<User>('users/123', undefined, {
|
|
241
|
+
* realtime: false
|
|
242
|
+
* });
|
|
243
|
+
*
|
|
244
|
+
* // In Svelte component
|
|
245
|
+
* $: if (userDoc.loading) {
|
|
246
|
+
* // Show loading state
|
|
247
|
+
* } else if (userDoc.error) {
|
|
248
|
+
* // Handle error
|
|
249
|
+
* } else if (userDoc.exists) {
|
|
250
|
+
* // Use userDoc.data
|
|
251
|
+
* }
|
|
252
|
+
*
|
|
253
|
+
* // Use reactive methods
|
|
254
|
+
* $: if (userDoc.computedState.canRetry) {
|
|
255
|
+
* userDoc.retryIfNeeded();
|
|
256
|
+
* }
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
export declare function firekitDoc<T extends DocumentData = DocumentData>(ref: string | DocumentReference<T>, startWith?: T, options?: DocumentOptions): FirekitDoc<T>;
|
|
260
|
+
/**
|
|
261
|
+
* Creates a one-time document fetch (no real-time updates)
|
|
262
|
+
*
|
|
263
|
+
* @template T Document data type
|
|
264
|
+
* @param ref Document path or DocumentReference
|
|
265
|
+
* @param startWith Initial data to show while loading
|
|
266
|
+
* @returns FirekitDoc instance configured for one-time fetch
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```typescript
|
|
270
|
+
* const userDoc = firekitDocOnce<User>('users/123');
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
export declare function firekitDocOnce<T extends DocumentData = DocumentData>(ref: string | DocumentReference<T>, startWith?: T): FirekitDoc<T>;
|
|
274
|
+
/**
|
|
275
|
+
* Creates a document subscription with metadata changes included
|
|
276
|
+
*
|
|
277
|
+
* @template T Document data type
|
|
278
|
+
* @param ref Document path or DocumentReference
|
|
279
|
+
* @param startWith Initial data to show while loading
|
|
280
|
+
* @returns FirekitDoc instance with metadata tracking
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```typescript
|
|
284
|
+
* const userDoc = firekitDocWithMetadata<User>('users/123');
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
export declare function firekitDocWithMetadata<T extends DocumentData = DocumentData>(ref: string | DocumentReference<T>, startWith?: T): FirekitDoc<T>;
|
|
288
|
+
export {};
|