svelte-firekit 0.0.21 → 0.0.22

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.
@@ -1,18 +1,56 @@
1
+ /**
2
+ * @module FirekitRealtimeDB
3
+ */
1
4
  import { ref, onValue, push, set, update, remove } from "firebase/database";
2
5
  import { firebaseService } from "../firebase.js";
3
6
  import { browser } from "$app/environment";
7
+ /**
8
+ * Manages real-time Firebase Realtime Database subscriptions with reactive state
9
+ * @class
10
+ * @template T Data type
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * interface ChatMessage {
15
+ * text: string;
16
+ * userId: string;
17
+ * timestamp: number;
18
+ * }
19
+ *
20
+ * // Create regular reference
21
+ * const chatRef = firekitRealtimeDB<ChatMessage>('chats/123');
22
+ *
23
+ * // Create list reference
24
+ * const messagesList = firekitRealtimeList<ChatMessage>('messages');
25
+ * ```
26
+ */
4
27
  class FirekitRealtimeDB {
28
+ /** Current data */
5
29
  _data = $state(null);
30
+ /** Loading state */
6
31
  _loading = $state(true);
32
+ /** Error state */
7
33
  _error = $state(null);
34
+ /** Database reference */
8
35
  dbRef = null;
36
+ /** Subscription cleanup function */
9
37
  unsubscribe = null;
38
+ /**
39
+ * Creates a Realtime Database subscription
40
+ * @param {string} path Database path
41
+ * @param {T} [startWith] Initial data before fetch completes
42
+ */
10
43
  constructor(path, startWith) {
11
44
  this._data = startWith ?? null;
12
45
  if (browser) {
13
46
  this.initializeRealtimeDB(path);
14
47
  }
15
48
  }
49
+ /**
50
+ * Initializes database subscription
51
+ * @private
52
+ * @param {string} path Database path
53
+ */
16
54
  initializeRealtimeDB(path) {
17
55
  try {
18
56
  const database = firebaseService.getDatabaseInstance();
@@ -31,7 +69,20 @@ class FirekitRealtimeDB {
31
69
  this._loading = false;
32
70
  }
33
71
  }
34
- // Push new data to the list
72
+ /**
73
+ * Pushes new data to list
74
+ * @param {T} data Data to push
75
+ * @returns {Promise<string | null>} New item key or null if failed
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * const key = await chatRef.push({
80
+ * text: 'Hello',
81
+ * userId: '123',
82
+ * timestamp: Date.now()
83
+ * });
84
+ * ```
85
+ */
35
86
  async push(data) {
36
87
  if (!this.dbRef)
37
88
  return null;
@@ -39,41 +90,76 @@ class FirekitRealtimeDB {
39
90
  await set(newRef, data);
40
91
  return newRef.key;
41
92
  }
42
- // Set data at the reference
93
+ /**
94
+ * Sets data at reference
95
+ * @param {T} data Data to set
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * await chatRef.set({
100
+ * text: 'Updated message',
101
+ * userId: '123',
102
+ * timestamp: Date.now()
103
+ * });
104
+ * ```
105
+ */
43
106
  async set(data) {
44
107
  if (!this.dbRef)
45
108
  return;
46
109
  await set(this.dbRef, data);
47
110
  }
48
- // Update data at the reference
111
+ /**
112
+ * Updates data at reference
113
+ * @param {Partial<T>} data Data to update
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * await chatRef.update({
118
+ * text: 'Edited message'
119
+ * });
120
+ * ```
121
+ */
49
122
  async update(data) {
50
123
  if (!this.dbRef)
51
124
  return;
52
125
  await update(this.dbRef, data);
53
126
  }
54
- // Remove data at the reference
127
+ /**
128
+ * Removes data at reference
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * await chatRef.remove();
133
+ * ```
134
+ */
55
135
  async remove() {
56
136
  if (!this.dbRef)
57
137
  return;
58
138
  await remove(this.dbRef);
59
139
  }
60
- // Getters for reactive state
140
+ /** Gets current data */
61
141
  get data() {
62
142
  return this._data;
63
143
  }
144
+ /** Gets loading state */
64
145
  get loading() {
65
146
  return this._loading;
66
147
  }
148
+ /** Gets error state */
67
149
  get error() {
68
150
  return this._error;
69
151
  }
152
+ /**
153
+ * Gets database reference
154
+ * @throws {Error} If reference is not available
155
+ */
70
156
  get ref() {
71
157
  if (!this.dbRef) {
72
158
  throw new Error("Database reference is not available");
73
159
  }
74
160
  return this.dbRef;
75
161
  }
76
- // Cleanup subscription
162
+ /** Cleanup subscription */
77
163
  dispose() {
78
164
  if (this.unsubscribe) {
79
165
  this.unsubscribe();
@@ -82,22 +168,35 @@ class FirekitRealtimeDB {
82
168
  }
83
169
  /**
84
170
  * Creates a reactive Realtime Database reference
85
- * @param path Database path
86
- * @param startWith Optional initial data
87
- * @returns FirekitRealtimeDB instance
171
+ * @template T Data type
172
+ * @param {string} path Database path
173
+ * @param {T} [startWith] Initial data
174
+ * @returns {FirekitRealtimeDB<T>} Database subscription instance
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * const chatRef = firekitRealtimeDB<ChatMessage>('chats/123');
179
+ * ```
88
180
  */
89
181
  export function firekitRealtimeDB(path, startWith) {
90
182
  return new FirekitRealtimeDB(path, startWith);
91
183
  }
92
184
  /**
93
185
  * Creates a reactive Realtime Database list reference
94
- * Automatically converts the data into an array format
95
- * @param path Database path
96
- * @param startWith Optional initial array data
97
- * @returns FirekitRealtimeDB instance with array data
186
+ * Automatically converts data to array format with IDs
187
+ *
188
+ * @template T List item type
189
+ * @param {string} path Database path
190
+ * @param {T[]} [startWith=[]] Initial array data
191
+ * @returns {FirekitRealtimeDB} Database subscription instance with array support
192
+ *
193
+ * @example
194
+ * ```typescript
195
+ * const messagesList = firekitRealtimeList<ChatMessage>('messages');
196
+ * console.log(messagesList.list); // Array of messages with IDs
197
+ * ```
98
198
  */
99
199
  export function firekitRealtimeList(path, startWith = []) {
100
- // Convert initial array to Record format
101
200
  const startWithRecord = startWith.reduce((acc, item, index) => {
102
201
  acc[`key${index}`] = item;
103
202
  return acc;
@@ -1,14 +1,83 @@
1
+ /**
2
+ * @module FirekitDownloadUrl
3
+ */
4
+ /**
5
+ * Manages Firebase Storage download URL fetching with reactive state
6
+ * @class
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // Get download URL for image
11
+ * const imageUrl = firekitDownloadUrl('images/photo.jpg');
12
+ *
13
+ * // Access reactive state
14
+ * if (imageUrl.loading) {
15
+ * console.log('Loading URL...');
16
+ * } else if (imageUrl.url) {
17
+ * console.log('Download URL:', imageUrl.url);
18
+ * }
19
+ * ```
20
+ */
1
21
  declare class FirekitDownloadUrl {
22
+ /** Current download URL */
2
23
  private _url;
24
+ /** Loading state */
3
25
  private _loading;
26
+ /** Error state */
4
27
  private _error;
28
+ /** Storage reference */
5
29
  private storageRef;
30
+ /**
31
+ * Creates a download URL fetcher
32
+ * @param {string} path Storage path to file
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const url = new FirekitDownloadUrl('documents/file.pdf');
37
+ * ```
38
+ */
6
39
  constructor(path: string);
40
+ /**
41
+ * Initializes download URL fetching
42
+ * @private
43
+ * @param {string} path Storage path
44
+ */
7
45
  private initializeDownload;
46
+ /** Gets current download URL */
8
47
  get url(): string | null;
48
+ /** Gets loading state */
9
49
  get loading(): boolean;
50
+ /** Gets error state */
10
51
  get error(): Error | null;
52
+ /**
53
+ * Refreshes download URL
54
+ * Useful when file content has changed
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * // Refresh URL after file update
59
+ * await uploadNewVersion();
60
+ * imageUrl.refresh();
61
+ * ```
62
+ */
11
63
  refresh(): void;
12
64
  }
65
+ /**
66
+ * Creates a download URL fetcher
67
+ * @param {string} path Storage path to file
68
+ * @returns {FirekitDownloadUrl} Download URL fetcher instance
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const imageUrl = firekitDownloadUrl('images/profile.jpg');
73
+ *
74
+ * // Use in template
75
+ * {#if imageUrl.loading}
76
+ * <p>Loading...</p>
77
+ * {:else if imageUrl.url}
78
+ * <img src={imageUrl.url} alt="Profile" />
79
+ * {/if}
80
+ * ```
81
+ */
13
82
  export declare function firekitDownloadUrl(path: string): FirekitDownloadUrl;
14
83
  export {};
@@ -1,16 +1,54 @@
1
+ /**
2
+ * @module FirekitDownloadUrl
3
+ */
1
4
  import { ref, getDownloadURL } from "firebase/storage";
2
5
  import { browser } from "$app/environment";
3
6
  import { firebaseService } from "../firebase.js";
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
+ */
4
24
  class FirekitDownloadUrl {
25
+ /** Current download URL */
5
26
  _url = $state(null);
27
+ /** Loading state */
6
28
  _loading = $state(true);
29
+ /** Error state */
7
30
  _error = $state(null);
31
+ /** Storage reference */
8
32
  storageRef = null;
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
+ */
9
42
  constructor(path) {
10
43
  if (browser) {
11
44
  this.initializeDownload(path);
12
45
  }
13
46
  }
47
+ /**
48
+ * Initializes download URL fetching
49
+ * @private
50
+ * @param {string} path Storage path
51
+ */
14
52
  async initializeDownload(path) {
15
53
  try {
16
54
  const storage = firebaseService.getStorageInstance();
@@ -23,15 +61,29 @@ class FirekitDownloadUrl {
23
61
  this._loading = false;
24
62
  }
25
63
  }
64
+ /** Gets current download URL */
26
65
  get url() {
27
66
  return this._url;
28
67
  }
68
+ /** Gets loading state */
29
69
  get loading() {
30
70
  return this._loading;
31
71
  }
72
+ /** Gets error state */
32
73
  get error() {
33
74
  return this._error;
34
75
  }
76
+ /**
77
+ * Refreshes download URL
78
+ * Useful when file content has changed
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * // Refresh URL after file update
83
+ * await uploadNewVersion();
84
+ * imageUrl.refresh();
85
+ * ```
86
+ */
35
87
  refresh() {
36
88
  if (this.storageRef) {
37
89
  this._loading = true;
@@ -40,6 +92,23 @@ class FirekitDownloadUrl {
40
92
  }
41
93
  }
42
94
  }
95
+ /**
96
+ * Creates a download URL fetcher
97
+ * @param {string} path Storage path to file
98
+ * @returns {FirekitDownloadUrl} Download URL fetcher instance
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * const imageUrl = firekitDownloadUrl('images/profile.jpg');
103
+ *
104
+ * // Use in template
105
+ * {#if imageUrl.loading}
106
+ * <p>Loading...</p>
107
+ * {:else if imageUrl.url}
108
+ * <img src={imageUrl.url} alt="Profile" />
109
+ * {/if}
110
+ * ```
111
+ */
43
112
  export function firekitDownloadUrl(path) {
44
113
  return new FirekitDownloadUrl(path);
45
114
  }
@@ -1,17 +1,89 @@
1
+ /**
2
+ * @module FirekitStorageList
3
+ */
1
4
  import { type StorageReference } from "firebase/storage";
5
+ /**
6
+ * Manages Firebase Storage directory listing with reactive state
7
+ * @class
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * // List contents of images directory
12
+ * const imagesList = firekitStorageList('images');
13
+ *
14
+ * // Access items and folders
15
+ * console.log('Files:', imagesList.items);
16
+ * console.log('Folders:', imagesList.prefixes);
17
+ * ```
18
+ */
2
19
  declare class FirekitStorageList {
20
+ /** List of files in directory */
3
21
  private _items;
22
+ /** List of subdirectories */
4
23
  private _prefixes;
24
+ /** Loading state */
5
25
  private _loading;
26
+ /** Error state */
6
27
  private _error;
28
+ /** Storage reference */
7
29
  private storageRef;
30
+ /**
31
+ * Creates a storage directory lister
32
+ * @param {string} path Storage directory path
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const list = new FirekitStorageList('uploads/2024');
37
+ * ```
38
+ */
8
39
  constructor(path: string);
40
+ /**
41
+ * Initializes directory listing
42
+ * @private
43
+ * @param {string} path Storage directory path
44
+ */
9
45
  private initializeList;
46
+ /** Gets list of files */
10
47
  get items(): StorageReference[];
48
+ /** Gets list of subdirectories */
11
49
  get prefixes(): StorageReference[];
50
+ /** Gets loading state */
12
51
  get loading(): boolean;
52
+ /** Gets error state */
13
53
  get error(): Error | null;
54
+ /**
55
+ * Refreshes directory listing
56
+ * Useful when directory contents have changed
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * // Refresh after upload
61
+ * await uploadFile('images/new.jpg');
62
+ * imagesList.refresh();
63
+ * ```
64
+ */
14
65
  refresh(): void;
15
66
  }
67
+ /**
68
+ * Creates a storage directory lister
69
+ * @param {string} path Storage directory path
70
+ * @returns {FirekitStorageList} Storage list instance
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * const documents = firekitStorageList('documents');
75
+ *
76
+ * // Use in template
77
+ * {#if documents.loading}
78
+ * <p>Loading...</p>
79
+ * {:else}
80
+ * <ul>
81
+ * {#each documents.items as item}
82
+ * <li>{item.name}</li>
83
+ * {/each}
84
+ * </ul>
85
+ * {/if}
86
+ * ```
87
+ */
16
88
  export declare function firekitStorageList(path: string): FirekitStorageList;
17
89
  export {};
@@ -1,17 +1,53 @@
1
+ /**
2
+ * @module FirekitStorageList
3
+ */
1
4
  import { ref, listAll } from "firebase/storage";
2
5
  import { browser } from "$app/environment";
3
6
  import { firebaseService } from "../firebase.js";
7
+ /**
8
+ * Manages Firebase Storage directory listing with reactive state
9
+ * @class
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * // List contents of images directory
14
+ * const imagesList = firekitStorageList('images');
15
+ *
16
+ * // Access items and folders
17
+ * console.log('Files:', imagesList.items);
18
+ * console.log('Folders:', imagesList.prefixes);
19
+ * ```
20
+ */
4
21
  class FirekitStorageList {
22
+ /** List of files in directory */
5
23
  _items = $state([]);
24
+ /** List of subdirectories */
6
25
  _prefixes = $state([]);
26
+ /** Loading state */
7
27
  _loading = $state(true);
28
+ /** Error state */
8
29
  _error = $state(null);
30
+ /** Storage reference */
9
31
  storageRef = null;
32
+ /**
33
+ * Creates a storage directory lister
34
+ * @param {string} path Storage directory path
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * const list = new FirekitStorageList('uploads/2024');
39
+ * ```
40
+ */
10
41
  constructor(path) {
11
42
  if (browser) {
12
43
  this.initializeList(path);
13
44
  }
14
45
  }
46
+ /**
47
+ * Initializes directory listing
48
+ * @private
49
+ * @param {string} path Storage directory path
50
+ */
15
51
  async initializeList(path) {
16
52
  try {
17
53
  const storage = firebaseService.getStorageInstance();
@@ -26,18 +62,33 @@ class FirekitStorageList {
26
62
  this._loading = false;
27
63
  }
28
64
  }
65
+ /** Gets list of files */
29
66
  get items() {
30
67
  return this._items;
31
68
  }
69
+ /** Gets list of subdirectories */
32
70
  get prefixes() {
33
71
  return this._prefixes;
34
72
  }
73
+ /** Gets loading state */
35
74
  get loading() {
36
75
  return this._loading;
37
76
  }
77
+ /** Gets error state */
38
78
  get error() {
39
79
  return this._error;
40
80
  }
81
+ /**
82
+ * Refreshes directory listing
83
+ * Useful when directory contents have changed
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * // Refresh after upload
88
+ * await uploadFile('images/new.jpg');
89
+ * imagesList.refresh();
90
+ * ```
91
+ */
41
92
  refresh() {
42
93
  if (this.storageRef) {
43
94
  this._loading = true;
@@ -46,6 +97,27 @@ class FirekitStorageList {
46
97
  }
47
98
  }
48
99
  }
100
+ /**
101
+ * Creates a storage directory lister
102
+ * @param {string} path Storage directory path
103
+ * @returns {FirekitStorageList} Storage list instance
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * const documents = firekitStorageList('documents');
108
+ *
109
+ * // Use in template
110
+ * {#if documents.loading}
111
+ * <p>Loading...</p>
112
+ * {:else}
113
+ * <ul>
114
+ * {#each documents.items as item}
115
+ * <li>{item.name}</li>
116
+ * {/each}
117
+ * </ul>
118
+ * {/if}
119
+ * ```
120
+ */
49
121
  export function firekitStorageList(path) {
50
122
  return new FirekitStorageList(path);
51
123
  }
@@ -1,23 +1,94 @@
1
+ /**
2
+ * @module FirekitUploadTask
3
+ */
1
4
  import { type UploadTaskSnapshot } from "firebase/storage";
5
+ /**
6
+ * Manages Firebase Storage upload operations with reactive state and progress tracking
7
+ * @class
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * // Create upload task
12
+ * const upload = firekitUploadTask('images/photo.jpg', file);
13
+ *
14
+ * // Monitor progress
15
+ * console.log(`Upload progress: ${upload.progress}%`);
16
+ *
17
+ * // Control upload
18
+ * upload.pause();
19
+ * upload.resume();
20
+ * upload.cancel();
21
+ * ```
22
+ */
2
23
  declare class FirekitUploadTask {
24
+ /** Upload progress percentage */
3
25
  private _progress;
26
+ /** Error state */
4
27
  private _error;
28
+ /** Current upload snapshot */
5
29
  private _snapshot;
30
+ /** Download URL of uploaded file */
6
31
  private _downloadURL;
32
+ /** Upload completion state */
7
33
  private _completed;
34
+ /** Upload task reference */
8
35
  private uploadTask;
36
+ /** Storage reference */
9
37
  private storageRef;
38
+ /** Derived download URL */
10
39
  readonly URLdownload: string | null;
40
+ /**
41
+ * Creates an upload task
42
+ * @param {string} path Storage path for upload
43
+ * @param {File} file File to upload
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const task = new FirekitUploadTask('documents/report.pdf', file);
48
+ * ```
49
+ */
11
50
  constructor(path: string, file: File);
51
+ /**
52
+ * Initializes file upload
53
+ * @private
54
+ * @param {string} path Storage path
55
+ * @param {File} file File to upload
56
+ */
12
57
  private initializeUpload;
58
+ /** Pauses upload */
13
59
  pause(): void;
60
+ /** Resumes upload */
14
61
  resume(): void;
62
+ /** Cancels upload */
15
63
  cancel(): void;
64
+ /** Gets upload progress percentage */
16
65
  get progress(): number;
66
+ /** Gets error state */
17
67
  get error(): Error | null;
68
+ /** Gets current upload snapshot */
18
69
  get snapshot(): UploadTaskSnapshot | null;
70
+ /** Gets download URL */
19
71
  get downloadURL(): string | null;
72
+ /** Gets completion state */
20
73
  get completed(): boolean;
21
74
  }
75
+ /**
76
+ * Creates an upload task
77
+ * @param {string} path Storage path for upload
78
+ * @param {File} file File to upload
79
+ * @returns {FirekitUploadTask} Upload task instance
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * const uploadTask = firekitUploadTask('images/profile.jpg', imageFile);
84
+ *
85
+ * // Template usage
86
+ * {#if !uploadTask.completed}
87
+ * <progress value={uploadTask.progress} max="100" />
88
+ * {:else}
89
+ * <img src={uploadTask.downloadURL} alt="Uploaded file" />
90
+ * {/if}
91
+ * ```
92
+ */
22
93
  export declare function firekitUploadTask(path: string, file: File): FirekitUploadTask;
23
94
  export {};