svelte-firekit 0.0.21 → 0.0.23

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/dist/index.js CHANGED
@@ -4,12 +4,13 @@ export { firebaseService } from './firebase.js';
4
4
  // auth services
5
5
  export { firekitUser } from './auth/user.svelte.js';
6
6
  export { firekitAuth } from './auth/auth.js';
7
- export { presenceService } from './auth/presence.svelte';
7
+ export { presenceService } from './auth/presence.svelte.js';
8
8
  // firestore services
9
9
  export { firekitAwaitableDoc } from './firestore/awaitable-doc.svelte.js';
10
- export { firekitDocMutations } from './firestore/document-mutations.svelte';
10
+ export { firekitDocMutations } from './firestore/document-mutations.svelte.js';
11
11
  export { firekitCollection } from './firestore/collection.svelte.js';
12
12
  export { firekitDoc } from './firestore/doc.svelte.js';
13
+ export { firekitCollectionGroup } from './firestore/collection-group.svelte.js';
13
14
  // realtime services
14
15
  export { firekitRealtimeDB } from './realtime/realtime.svelte.js';
15
16
  // Storage services
@@ -0,0 +1,145 @@
1
+ /**
2
+ * @module FirekitRealtimeDB
3
+ */
4
+ import { type DatabaseReference } from "firebase/database";
5
+ /**
6
+ * Manages real-time Firebase Realtime Database subscriptions with reactive state
7
+ * @class
8
+ * @template T Data type
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * interface ChatMessage {
13
+ * text: string;
14
+ * userId: string;
15
+ * timestamp: number;
16
+ * }
17
+ *
18
+ * // Create regular reference
19
+ * const chatRef = firekitRealtimeDB<ChatMessage>('chats/123');
20
+ *
21
+ * // Create list reference
22
+ * const messagesList = firekitRealtimeList<ChatMessage>('messages');
23
+ * ```
24
+ */
25
+ declare class FirekitRealtimeDB<T> {
26
+ /** Current data */
27
+ private _data;
28
+ /** Loading state */
29
+ private _loading;
30
+ /** Error state */
31
+ private _error;
32
+ /** Database reference */
33
+ private dbRef;
34
+ /** Subscription cleanup function */
35
+ private unsubscribe;
36
+ /**
37
+ * Creates a Realtime Database subscription
38
+ * @param {string} path Database path
39
+ * @param {T} [startWith] Initial data before fetch completes
40
+ */
41
+ constructor(path: string, startWith?: T);
42
+ /**
43
+ * Initializes database subscription
44
+ * @private
45
+ * @param {string} path Database path
46
+ */
47
+ private initializeRealtimeDB;
48
+ /**
49
+ * Pushes new data to list
50
+ * @param {T} data Data to push
51
+ * @returns {Promise<string | null>} New item key or null if failed
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * const key = await chatRef.push({
56
+ * text: 'Hello',
57
+ * userId: '123',
58
+ * timestamp: Date.now()
59
+ * });
60
+ * ```
61
+ */
62
+ push(data: T): Promise<string | null>;
63
+ /**
64
+ * Sets data at reference
65
+ * @param {T} data Data to set
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * await chatRef.set({
70
+ * text: 'Updated message',
71
+ * userId: '123',
72
+ * timestamp: Date.now()
73
+ * });
74
+ * ```
75
+ */
76
+ set(data: T): Promise<void>;
77
+ /**
78
+ * Updates data at reference
79
+ * @param {Partial<T>} data Data to update
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * await chatRef.update({
84
+ * text: 'Edited message'
85
+ * });
86
+ * ```
87
+ */
88
+ update(data: Partial<T>): Promise<void>;
89
+ /**
90
+ * Removes data at reference
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * await chatRef.remove();
95
+ * ```
96
+ */
97
+ remove(): Promise<void>;
98
+ /** Gets current data */
99
+ get data(): T | null;
100
+ /** Gets loading state */
101
+ get loading(): boolean;
102
+ /** Gets error state */
103
+ get error(): Error | null;
104
+ /**
105
+ * Gets database reference
106
+ * @throws {Error} If reference is not available
107
+ */
108
+ get ref(): DatabaseReference;
109
+ /** Cleanup subscription */
110
+ dispose(): void;
111
+ }
112
+ /**
113
+ * Creates a reactive Realtime Database reference
114
+ * @template T Data type
115
+ * @param {string} path Database path
116
+ * @param {T} [startWith] Initial data
117
+ * @returns {FirekitRealtimeDB<T>} Database subscription instance
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * const chatRef = firekitRealtimeDB<ChatMessage>('chats/123');
122
+ * ```
123
+ */
124
+ export declare function firekitRealtimeDB<T>(path: string, startWith?: T): FirekitRealtimeDB<T>;
125
+ /**
126
+ * Creates a reactive Realtime Database list reference
127
+ * Automatically converts data to array format with IDs
128
+ *
129
+ * @template T List item type
130
+ * @param {string} path Database path
131
+ * @param {T[]} [startWith=[]] Initial array data
132
+ * @returns {FirekitRealtimeDB} Database subscription instance with array support
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * const messagesList = firekitRealtimeList<ChatMessage>('messages');
137
+ * console.log(messagesList.list); // Array of messages with IDs
138
+ * ```
139
+ */
140
+ export declare function firekitRealtimeList<T>(path: string, startWith?: T[]): FirekitRealtimeDB<Record<string, T>> & {
141
+ list: Array<T & {
142
+ id: string;
143
+ }>;
144
+ };
145
+ export {};
@@ -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 {};