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
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* @fileoverview FirekitRealtimeDB - Optimized Realtime Database management for Svelte applications
|
|
2
3
|
* @module FirekitRealtimeDB
|
|
4
|
+
* @version 1.0.0
|
|
3
5
|
*/
|
|
4
|
-
import { type DatabaseReference } from
|
|
6
|
+
import { type DatabaseReference } from 'firebase/database';
|
|
5
7
|
/**
|
|
6
8
|
* Manages real-time Firebase Realtime Database subscriptions with reactive state
|
|
7
9
|
* @class
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* @fileoverview FirekitRealtimeDB - Optimized Realtime Database management for Svelte applications
|
|
2
3
|
* @module FirekitRealtimeDB
|
|
4
|
+
* @version 1.0.0
|
|
3
5
|
*/
|
|
4
|
-
import { ref, onValue, push, set, update, remove } from
|
|
5
|
-
import { firebaseService } from
|
|
6
|
-
import { browser } from
|
|
6
|
+
import { ref, onValue, push, set, update, remove } from 'firebase/database';
|
|
7
|
+
import { firebaseService } from '../firebase.js';
|
|
8
|
+
import { browser } from '$app/environment';
|
|
7
9
|
/**
|
|
8
10
|
* Manages real-time Firebase Realtime Database subscriptions with reactive state
|
|
9
11
|
* @class
|
|
@@ -54,6 +56,9 @@ class FirekitRealtimeDB {
|
|
|
54
56
|
initializeRealtimeDB(path) {
|
|
55
57
|
try {
|
|
56
58
|
const database = firebaseService.getDatabaseInstance();
|
|
59
|
+
if (!database) {
|
|
60
|
+
throw new Error('Database instance not available');
|
|
61
|
+
}
|
|
57
62
|
this.dbRef = ref(database, path);
|
|
58
63
|
this.unsubscribe = onValue(this.dbRef, (snapshot) => {
|
|
59
64
|
this._data = snapshot.val();
|
|
@@ -155,7 +160,7 @@ class FirekitRealtimeDB {
|
|
|
155
160
|
*/
|
|
156
161
|
get ref() {
|
|
157
162
|
if (!this.dbRef) {
|
|
158
|
-
throw new Error(
|
|
163
|
+
throw new Error('Database reference is not available');
|
|
159
164
|
}
|
|
160
165
|
return this.dbRef;
|
|
161
166
|
}
|
|
@@ -163,6 +168,7 @@ class FirekitRealtimeDB {
|
|
|
163
168
|
dispose() {
|
|
164
169
|
if (this.unsubscribe) {
|
|
165
170
|
this.unsubscribe();
|
|
171
|
+
this.unsubscribe = null;
|
|
166
172
|
}
|
|
167
173
|
}
|
|
168
174
|
}
|
|
@@ -201,15 +207,15 @@ export function firekitRealtimeList(path, startWith = []) {
|
|
|
201
207
|
acc[`key${index}`] = item;
|
|
202
208
|
return acc;
|
|
203
209
|
}, {});
|
|
204
|
-
return new class extends FirekitRealtimeDB {
|
|
210
|
+
return new (class extends FirekitRealtimeDB {
|
|
205
211
|
_list = $derived(this.data
|
|
206
212
|
? Object.entries(this.data).map(([key, value]) => ({
|
|
207
213
|
id: key,
|
|
208
|
-
...value
|
|
214
|
+
...value
|
|
209
215
|
}))
|
|
210
216
|
: []);
|
|
211
217
|
get list() {
|
|
212
218
|
return this._list;
|
|
213
219
|
}
|
|
214
|
-
}(path, startWithRecord);
|
|
220
|
+
})(path, startWithRecord);
|
|
215
221
|
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview FirekitStorage - Optimized Firebase Storage management for Svelte applications
|
|
3
|
+
* @module FirekitStorage
|
|
4
|
+
* @version 1.0.0
|
|
5
|
+
*/
|
|
6
|
+
import { type StorageReference, type UploadTaskSnapshot } from 'firebase/storage';
|
|
7
|
+
/**
|
|
8
|
+
* Manages Firebase Storage download URL fetching with reactive state
|
|
9
|
+
* @class
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Get download URL for image
|
|
14
|
+
* const imageUrl = firekitDownloadUrl('images/photo.jpg');
|
|
15
|
+
*
|
|
16
|
+
* // Access reactive state
|
|
17
|
+
* if (imageUrl.loading) {
|
|
18
|
+
* console.log('Loading URL...');
|
|
19
|
+
* } else if (imageUrl.url) {
|
|
20
|
+
* console.log('Download URL:', imageUrl.url);
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare class FirekitDownloadUrl {
|
|
25
|
+
/** Current download URL */
|
|
26
|
+
private _url;
|
|
27
|
+
/** Loading state */
|
|
28
|
+
private _loading;
|
|
29
|
+
/** Error state */
|
|
30
|
+
private _error;
|
|
31
|
+
/** Storage reference */
|
|
32
|
+
private storageRef;
|
|
33
|
+
/**
|
|
34
|
+
* Creates a download URL fetcher
|
|
35
|
+
* @param {string} path Storage path to file
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const url = new FirekitDownloadUrl('documents/file.pdf');
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
constructor(path: string);
|
|
43
|
+
/**
|
|
44
|
+
* Initializes download URL fetching
|
|
45
|
+
* @private
|
|
46
|
+
* @param {string} path Storage path
|
|
47
|
+
*/
|
|
48
|
+
private initializeDownload;
|
|
49
|
+
/** Gets current download URL */
|
|
50
|
+
get url(): string | null;
|
|
51
|
+
/** Gets loading state */
|
|
52
|
+
get loading(): boolean;
|
|
53
|
+
/** Gets error state */
|
|
54
|
+
get error(): Error | null;
|
|
55
|
+
/**
|
|
56
|
+
* Refreshes download URL
|
|
57
|
+
* Useful when file content has changed
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* // Refresh URL after file update
|
|
62
|
+
* await uploadNewVersion();
|
|
63
|
+
* imageUrl.refresh();
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
refresh(): void;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Manages Firebase Storage directory listing with reactive state
|
|
70
|
+
* @class
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // List contents of images directory
|
|
75
|
+
* const imagesList = firekitStorageList('images');
|
|
76
|
+
*
|
|
77
|
+
* // Access items and folders
|
|
78
|
+
* console.log('Files:', imagesList.items);
|
|
79
|
+
* console.log('Folders:', imagesList.prefixes);
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare class FirekitStorageList {
|
|
83
|
+
/** List of files in directory */
|
|
84
|
+
private _items;
|
|
85
|
+
/** List of subdirectories */
|
|
86
|
+
private _prefixes;
|
|
87
|
+
/** Loading state */
|
|
88
|
+
private _loading;
|
|
89
|
+
/** Error state */
|
|
90
|
+
private _error;
|
|
91
|
+
/** Storage reference */
|
|
92
|
+
private storageRef;
|
|
93
|
+
/**
|
|
94
|
+
* Creates a storage directory lister
|
|
95
|
+
* @param {string} path Storage directory path
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const list = new FirekitStorageList('uploads/2024');
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
constructor(path: string);
|
|
103
|
+
/**
|
|
104
|
+
* Initializes directory listing
|
|
105
|
+
* @private
|
|
106
|
+
* @param {string} path Storage directory path
|
|
107
|
+
*/
|
|
108
|
+
private initializeList;
|
|
109
|
+
/** Gets list of files */
|
|
110
|
+
get items(): StorageReference[];
|
|
111
|
+
/** Gets list of subdirectories */
|
|
112
|
+
get prefixes(): StorageReference[];
|
|
113
|
+
/** Gets loading state */
|
|
114
|
+
get loading(): boolean;
|
|
115
|
+
/** Gets error state */
|
|
116
|
+
get error(): Error | null;
|
|
117
|
+
/**
|
|
118
|
+
* Refreshes directory listing
|
|
119
|
+
* Useful when directory contents have changed
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* // Refresh after upload
|
|
124
|
+
* await uploadFile('images/new.jpg');
|
|
125
|
+
* imagesList.refresh();
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
refresh(): void;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Manages Firebase Storage upload operations with reactive state and progress tracking
|
|
132
|
+
* @class
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* // Create upload task
|
|
137
|
+
* const upload = firekitUploadTask('images/photo.jpg', file);
|
|
138
|
+
*
|
|
139
|
+
* // Monitor progress
|
|
140
|
+
* console.log(`Upload progress: ${upload.progress}%`);
|
|
141
|
+
*
|
|
142
|
+
* // Control upload
|
|
143
|
+
* upload.pause();
|
|
144
|
+
* upload.resume();
|
|
145
|
+
* upload.cancel();
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
declare class FirekitUploadTask {
|
|
149
|
+
/** Upload progress percentage */
|
|
150
|
+
private _progress;
|
|
151
|
+
/** Error state */
|
|
152
|
+
private _error;
|
|
153
|
+
/** Current upload snapshot */
|
|
154
|
+
private _snapshot;
|
|
155
|
+
/** Download URL of uploaded file */
|
|
156
|
+
private _downloadURL;
|
|
157
|
+
/** Upload completion state */
|
|
158
|
+
private _completed;
|
|
159
|
+
/** Upload task reference */
|
|
160
|
+
private uploadTask;
|
|
161
|
+
/** Storage reference */
|
|
162
|
+
private storageRef;
|
|
163
|
+
/**
|
|
164
|
+
* Creates an upload task
|
|
165
|
+
* @param {string} path Storage path for upload
|
|
166
|
+
* @param {File} file File to upload
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* const task = new FirekitUploadTask('documents/report.pdf', file);
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
constructor(path: string, file: File);
|
|
174
|
+
/**
|
|
175
|
+
* Initializes file upload
|
|
176
|
+
* @private
|
|
177
|
+
* @param {string} path Storage path
|
|
178
|
+
* @param {File} file File to upload
|
|
179
|
+
*/
|
|
180
|
+
private initializeUpload;
|
|
181
|
+
/** Pauses upload */
|
|
182
|
+
pause(): void;
|
|
183
|
+
/** Resumes upload */
|
|
184
|
+
resume(): void;
|
|
185
|
+
/** Cancels upload */
|
|
186
|
+
cancel(): void;
|
|
187
|
+
/** Gets upload progress percentage */
|
|
188
|
+
get progress(): number;
|
|
189
|
+
/** Gets error state */
|
|
190
|
+
get error(): Error | null;
|
|
191
|
+
/** Gets current upload snapshot */
|
|
192
|
+
get snapshot(): UploadTaskSnapshot | null;
|
|
193
|
+
/** Gets download URL */
|
|
194
|
+
get downloadURL(): string | null;
|
|
195
|
+
/** Gets completion state */
|
|
196
|
+
get completed(): boolean;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Creates a download URL fetcher
|
|
200
|
+
* @param {string} path Storage path to file
|
|
201
|
+
* @returns {FirekitDownloadUrl} Download URL fetcher instance
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```typescript
|
|
205
|
+
* const imageUrl = firekitDownloadUrl('images/profile.jpg');
|
|
206
|
+
*
|
|
207
|
+
* // Use in template
|
|
208
|
+
* {#if imageUrl.loading}
|
|
209
|
+
* <p>Loading...</p>
|
|
210
|
+
* {:else if imageUrl.url}
|
|
211
|
+
* <img src={imageUrl.url} alt="Profile" />
|
|
212
|
+
* {/if}
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
export declare function firekitDownloadUrl(path: string): FirekitDownloadUrl;
|
|
216
|
+
/**
|
|
217
|
+
* Creates a storage directory lister
|
|
218
|
+
* @param {string} path Storage directory path
|
|
219
|
+
* @returns {FirekitStorageList} Storage list instance
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```typescript
|
|
223
|
+
* const documents = firekitStorageList('documents');
|
|
224
|
+
*
|
|
225
|
+
* // Use in template
|
|
226
|
+
* {#if documents.loading}
|
|
227
|
+
* <p>Loading...</p>
|
|
228
|
+
* {:else}
|
|
229
|
+
* <ul>
|
|
230
|
+
* {#each documents.items as item}
|
|
231
|
+
* <li>{item.name}</li>
|
|
232
|
+
* {/each}
|
|
233
|
+
* </ul>
|
|
234
|
+
* {/if}
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
export declare function firekitStorageList(path: string): FirekitStorageList;
|
|
238
|
+
/**
|
|
239
|
+
* Creates an upload task
|
|
240
|
+
* @param {string} path Storage path for upload
|
|
241
|
+
* @param {File} file File to upload
|
|
242
|
+
* @returns {FirekitUploadTask} Upload task instance
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```typescript
|
|
246
|
+
* const uploadTask = firekitUploadTask('images/profile.jpg', imageFile);
|
|
247
|
+
*
|
|
248
|
+
* // Template usage
|
|
249
|
+
* {#if !uploadTask.completed}
|
|
250
|
+
* <progress value={uploadTask.progress} max="100" />
|
|
251
|
+
* {:else}
|
|
252
|
+
* <img src={uploadTask.downloadURL} alt="Uploaded file" />
|
|
253
|
+
* {/if}
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
export declare function firekitUploadTask(path: string, file: File): FirekitUploadTask;
|
|
257
|
+
export {};
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview FirekitStorage - Optimized Firebase Storage management for Svelte applications
|
|
3
|
+
* @module FirekitStorage
|
|
4
|
+
* @version 1.0.0
|
|
5
|
+
*/
|
|
6
|
+
import { ref, getDownloadURL, listAll, uploadBytesResumable } from 'firebase/storage';
|
|
7
|
+
import { browser } from '$app/environment';
|
|
8
|
+
import { firebaseService } from '../firebase.js';
|
|
9
|
+
/**
|
|
10
|
+
* Manages Firebase Storage download URL fetching with reactive state
|
|
11
|
+
* @class
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Get download URL for image
|
|
16
|
+
* const imageUrl = firekitDownloadUrl('images/photo.jpg');
|
|
17
|
+
*
|
|
18
|
+
* // Access reactive state
|
|
19
|
+
* if (imageUrl.loading) {
|
|
20
|
+
* console.log('Loading URL...');
|
|
21
|
+
* } else if (imageUrl.url) {
|
|
22
|
+
* console.log('Download URL:', imageUrl.url);
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
class FirekitDownloadUrl {
|
|
27
|
+
/** Current download URL */
|
|
28
|
+
_url = $state(null);
|
|
29
|
+
/** Loading state */
|
|
30
|
+
_loading = $state(true);
|
|
31
|
+
/** Error state */
|
|
32
|
+
_error = $state(null);
|
|
33
|
+
/** Storage reference */
|
|
34
|
+
storageRef = null;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a download URL fetcher
|
|
37
|
+
* @param {string} path Storage path to file
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const url = new FirekitDownloadUrl('documents/file.pdf');
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
constructor(path) {
|
|
45
|
+
if (browser) {
|
|
46
|
+
this.initializeDownload(path);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Initializes download URL fetching
|
|
51
|
+
* @private
|
|
52
|
+
* @param {string} path Storage path
|
|
53
|
+
*/
|
|
54
|
+
async initializeDownload(path) {
|
|
55
|
+
try {
|
|
56
|
+
const storage = firebaseService.getStorageInstance();
|
|
57
|
+
if (!storage) {
|
|
58
|
+
throw new Error('Storage instance not available');
|
|
59
|
+
}
|
|
60
|
+
this.storageRef = ref(storage, path);
|
|
61
|
+
this._url = await getDownloadURL(this.storageRef);
|
|
62
|
+
this._loading = false;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
this._error = error;
|
|
66
|
+
this._loading = false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Gets current download URL */
|
|
70
|
+
get url() {
|
|
71
|
+
return this._url;
|
|
72
|
+
}
|
|
73
|
+
/** Gets loading state */
|
|
74
|
+
get loading() {
|
|
75
|
+
return this._loading;
|
|
76
|
+
}
|
|
77
|
+
/** Gets error state */
|
|
78
|
+
get error() {
|
|
79
|
+
return this._error;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Refreshes download URL
|
|
83
|
+
* Useful when file content has changed
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* // Refresh URL after file update
|
|
88
|
+
* await uploadNewVersion();
|
|
89
|
+
* imageUrl.refresh();
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
refresh() {
|
|
93
|
+
if (this.storageRef) {
|
|
94
|
+
this._loading = true;
|
|
95
|
+
this._error = null;
|
|
96
|
+
this.initializeDownload(this.storageRef.fullPath);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Manages Firebase Storage directory listing with reactive state
|
|
102
|
+
* @class
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* // List contents of images directory
|
|
107
|
+
* const imagesList = firekitStorageList('images');
|
|
108
|
+
*
|
|
109
|
+
* // Access items and folders
|
|
110
|
+
* console.log('Files:', imagesList.items);
|
|
111
|
+
* console.log('Folders:', imagesList.prefixes);
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
class FirekitStorageList {
|
|
115
|
+
/** List of files in directory */
|
|
116
|
+
_items = $state([]);
|
|
117
|
+
/** List of subdirectories */
|
|
118
|
+
_prefixes = $state([]);
|
|
119
|
+
/** Loading state */
|
|
120
|
+
_loading = $state(true);
|
|
121
|
+
/** Error state */
|
|
122
|
+
_error = $state(null);
|
|
123
|
+
/** Storage reference */
|
|
124
|
+
storageRef = null;
|
|
125
|
+
/**
|
|
126
|
+
* Creates a storage directory lister
|
|
127
|
+
* @param {string} path Storage directory path
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* const list = new FirekitStorageList('uploads/2024');
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
constructor(path) {
|
|
135
|
+
if (browser) {
|
|
136
|
+
this.initializeList(path);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Initializes directory listing
|
|
141
|
+
* @private
|
|
142
|
+
* @param {string} path Storage directory path
|
|
143
|
+
*/
|
|
144
|
+
async initializeList(path) {
|
|
145
|
+
try {
|
|
146
|
+
const storage = firebaseService.getStorageInstance();
|
|
147
|
+
if (!storage) {
|
|
148
|
+
throw new Error('Storage instance not available');
|
|
149
|
+
}
|
|
150
|
+
this.storageRef = ref(storage, path);
|
|
151
|
+
const result = await listAll(this.storageRef);
|
|
152
|
+
this._items = result.items;
|
|
153
|
+
this._prefixes = result.prefixes;
|
|
154
|
+
this._loading = false;
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
this._error = error;
|
|
158
|
+
this._loading = false;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/** Gets list of files */
|
|
162
|
+
get items() {
|
|
163
|
+
return this._items;
|
|
164
|
+
}
|
|
165
|
+
/** Gets list of subdirectories */
|
|
166
|
+
get prefixes() {
|
|
167
|
+
return this._prefixes;
|
|
168
|
+
}
|
|
169
|
+
/** Gets loading state */
|
|
170
|
+
get loading() {
|
|
171
|
+
return this._loading;
|
|
172
|
+
}
|
|
173
|
+
/** Gets error state */
|
|
174
|
+
get error() {
|
|
175
|
+
return this._error;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Refreshes directory listing
|
|
179
|
+
* Useful when directory contents have changed
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* // Refresh after upload
|
|
184
|
+
* await uploadFile('images/new.jpg');
|
|
185
|
+
* imagesList.refresh();
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
refresh() {
|
|
189
|
+
if (this.storageRef) {
|
|
190
|
+
this._loading = true;
|
|
191
|
+
this._error = null;
|
|
192
|
+
this.initializeList(this.storageRef.fullPath);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Manages Firebase Storage upload operations with reactive state and progress tracking
|
|
198
|
+
* @class
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* // Create upload task
|
|
203
|
+
* const upload = firekitUploadTask('images/photo.jpg', file);
|
|
204
|
+
*
|
|
205
|
+
* // Monitor progress
|
|
206
|
+
* console.log(`Upload progress: ${upload.progress}%`);
|
|
207
|
+
*
|
|
208
|
+
* // Control upload
|
|
209
|
+
* upload.pause();
|
|
210
|
+
* upload.resume();
|
|
211
|
+
* upload.cancel();
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
class FirekitUploadTask {
|
|
215
|
+
/** Upload progress percentage */
|
|
216
|
+
_progress = $state(0);
|
|
217
|
+
/** Error state */
|
|
218
|
+
_error = $state(null);
|
|
219
|
+
/** Current upload snapshot */
|
|
220
|
+
_snapshot = $state(null);
|
|
221
|
+
/** Download URL of uploaded file */
|
|
222
|
+
_downloadURL = $state(null);
|
|
223
|
+
/** Upload completion state */
|
|
224
|
+
_completed = $state(false);
|
|
225
|
+
/** Upload task reference */
|
|
226
|
+
uploadTask = null;
|
|
227
|
+
/** Storage reference */
|
|
228
|
+
storageRef = null;
|
|
229
|
+
/**
|
|
230
|
+
* Creates an upload task
|
|
231
|
+
* @param {string} path Storage path for upload
|
|
232
|
+
* @param {File} file File to upload
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* ```typescript
|
|
236
|
+
* const task = new FirekitUploadTask('documents/report.pdf', file);
|
|
237
|
+
* ```
|
|
238
|
+
*/
|
|
239
|
+
constructor(path, file) {
|
|
240
|
+
if (browser) {
|
|
241
|
+
this.initializeUpload(path, file);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Initializes file upload
|
|
246
|
+
* @private
|
|
247
|
+
* @param {string} path Storage path
|
|
248
|
+
* @param {File} file File to upload
|
|
249
|
+
*/
|
|
250
|
+
initializeUpload(path, file) {
|
|
251
|
+
try {
|
|
252
|
+
const storage = firebaseService.getStorageInstance();
|
|
253
|
+
if (!storage) {
|
|
254
|
+
throw new Error('Storage instance not available');
|
|
255
|
+
}
|
|
256
|
+
this.storageRef = ref(storage, path);
|
|
257
|
+
this.uploadTask = uploadBytesResumable(this.storageRef, file);
|
|
258
|
+
this.uploadTask.on('state_changed', (snapshot) => {
|
|
259
|
+
this._snapshot = snapshot;
|
|
260
|
+
this._progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
|
|
261
|
+
}, (error) => {
|
|
262
|
+
this._error = error;
|
|
263
|
+
}, async () => {
|
|
264
|
+
if (this.storageRef) {
|
|
265
|
+
this._downloadURL = await getDownloadURL(this.storageRef);
|
|
266
|
+
this._completed = true;
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
this._error = error;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
/** Pauses upload */
|
|
275
|
+
pause() {
|
|
276
|
+
this.uploadTask?.pause();
|
|
277
|
+
}
|
|
278
|
+
/** Resumes upload */
|
|
279
|
+
resume() {
|
|
280
|
+
this.uploadTask?.resume();
|
|
281
|
+
}
|
|
282
|
+
/** Cancels upload */
|
|
283
|
+
cancel() {
|
|
284
|
+
this.uploadTask?.cancel();
|
|
285
|
+
}
|
|
286
|
+
/** Gets upload progress percentage */
|
|
287
|
+
get progress() {
|
|
288
|
+
return this._progress;
|
|
289
|
+
}
|
|
290
|
+
/** Gets error state */
|
|
291
|
+
get error() {
|
|
292
|
+
return this._error;
|
|
293
|
+
}
|
|
294
|
+
/** Gets current upload snapshot */
|
|
295
|
+
get snapshot() {
|
|
296
|
+
return this._snapshot;
|
|
297
|
+
}
|
|
298
|
+
/** Gets download URL */
|
|
299
|
+
get downloadURL() {
|
|
300
|
+
return this._downloadURL;
|
|
301
|
+
}
|
|
302
|
+
/** Gets completion state */
|
|
303
|
+
get completed() {
|
|
304
|
+
return this._completed;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
// ========================================
|
|
308
|
+
// FACTORY FUNCTIONS
|
|
309
|
+
// ========================================
|
|
310
|
+
/**
|
|
311
|
+
* Creates a download URL fetcher
|
|
312
|
+
* @param {string} path Storage path to file
|
|
313
|
+
* @returns {FirekitDownloadUrl} Download URL fetcher instance
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```typescript
|
|
317
|
+
* const imageUrl = firekitDownloadUrl('images/profile.jpg');
|
|
318
|
+
*
|
|
319
|
+
* // Use in template
|
|
320
|
+
* {#if imageUrl.loading}
|
|
321
|
+
* <p>Loading...</p>
|
|
322
|
+
* {:else if imageUrl.url}
|
|
323
|
+
* <img src={imageUrl.url} alt="Profile" />
|
|
324
|
+
* {/if}
|
|
325
|
+
* ```
|
|
326
|
+
*/
|
|
327
|
+
export function firekitDownloadUrl(path) {
|
|
328
|
+
return new FirekitDownloadUrl(path);
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Creates a storage directory lister
|
|
332
|
+
* @param {string} path Storage directory path
|
|
333
|
+
* @returns {FirekitStorageList} Storage list instance
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* ```typescript
|
|
337
|
+
* const documents = firekitStorageList('documents');
|
|
338
|
+
*
|
|
339
|
+
* // Use in template
|
|
340
|
+
* {#if documents.loading}
|
|
341
|
+
* <p>Loading...</p>
|
|
342
|
+
* {:else}
|
|
343
|
+
* <ul>
|
|
344
|
+
* {#each documents.items as item}
|
|
345
|
+
* <li>{item.name}</li>
|
|
346
|
+
* {/each}
|
|
347
|
+
* </ul>
|
|
348
|
+
* {/if}
|
|
349
|
+
* ```
|
|
350
|
+
*/
|
|
351
|
+
export function firekitStorageList(path) {
|
|
352
|
+
return new FirekitStorageList(path);
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Creates an upload task
|
|
356
|
+
* @param {string} path Storage path for upload
|
|
357
|
+
* @param {File} file File to upload
|
|
358
|
+
* @returns {FirekitUploadTask} Upload task instance
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* ```typescript
|
|
362
|
+
* const uploadTask = firekitUploadTask('images/profile.jpg', imageFile);
|
|
363
|
+
*
|
|
364
|
+
* // Template usage
|
|
365
|
+
* {#if !uploadTask.completed}
|
|
366
|
+
* <progress value={uploadTask.progress} max="100" />
|
|
367
|
+
* {:else}
|
|
368
|
+
* <img src={uploadTask.downloadURL} alt="Uploaded file" />
|
|
369
|
+
* {/if}
|
|
370
|
+
* ```
|
|
371
|
+
*/
|
|
372
|
+
export function firekitUploadTask(path, file) {
|
|
373
|
+
return new FirekitUploadTask(path, file);
|
|
374
|
+
}
|