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,6 +1,23 @@
1
1
  import { ref, onDisconnect, onValue, get, set } from 'firebase/database';
2
2
  import { firebaseService } from '../firebase.js';
3
3
  import { browser } from '$app/environment';
4
+ /**
5
+ * Manages real-time user presence tracking with optional geolocation support
6
+ * @class
7
+ * @example
8
+ * ```typescript
9
+ * // Initialize presence tracking
10
+ * await presenceService.initialize(currentUser, {
11
+ * geolocation: { enabled: true, type: 'browser' },
12
+ * sessionTTL: 30 * 60 * 1000
13
+ * });
14
+ *
15
+ * // Listen for presence events
16
+ * presenceService.addEventListener((event) => {
17
+ * console.log(event.type, event.data);
18
+ * });
19
+ * ```
20
+ */
4
21
  class PresenceService {
5
22
  static instance;
6
23
  connectedListener = null;
@@ -35,13 +52,26 @@ class PresenceService {
35
52
  return PresenceService.instance;
36
53
  }
37
54
  // Getters
55
+ /** Get current session data */
38
56
  get currentSession() { return this._currentSession; }
57
+ /** Get all active sessions */
39
58
  get sessions() { return this._sessions; }
59
+ /** Get current presence status */
40
60
  get status() { return this._status; }
61
+ /** Get loading state */
41
62
  get loading() { return this._loading; }
63
+ /** Get error state */
42
64
  get error() { return this._error; }
65
+ /** Check if service is initialized */
43
66
  get isInitialized() { return this.initialized; }
67
+ /** Check if location consent is granted */
44
68
  get hasLocationConsent() { return this.locationConsent; }
69
+ /**
70
+ * Initialize presence tracking
71
+ * @param {any} user Current user object
72
+ * @param {PresenceConfig} config Optional configuration
73
+ * @throws {Error} If initialization fails
74
+ */
45
75
  async initializePresence() {
46
76
  const connectedRef = ref(firebaseService.getDatabaseInstance(), '.info/connected');
47
77
  this.connectedListener = onValue(connectedRef, async (snapshot) => {
@@ -94,6 +124,10 @@ class PresenceService {
94
124
  const browser = browserInfo[1] || 'Unknown Browser';
95
125
  return `${browser}-${platform}`.replace(/[^a-zA-Z0-9-]/g, '');
96
126
  }
127
+ /**
128
+ * Request location tracking consent
129
+ * @returns {Promise<boolean>} Whether consent was granted
130
+ */
97
131
  async requestLocationConsent() {
98
132
  if (!this.config.geolocation?.enabled)
99
133
  return false;
@@ -303,6 +337,11 @@ class PresenceService {
303
337
  this.locationWatcher = null;
304
338
  }
305
339
  }
340
+ /**
341
+ * Add presence event listener
342
+ * @param {Function} callback Event callback function
343
+ * @returns {Function} Cleanup function to remove listener
344
+ */
306
345
  addEventListener(callback) {
307
346
  this.eventListeners.add(callback);
308
347
  return () => this.eventListeners.delete(callback);
@@ -310,6 +349,9 @@ class PresenceService {
310
349
  emitEvent(event) {
311
350
  this.eventListeners.forEach(callback => callback(event));
312
351
  }
352
+ /**
353
+ * Cleanup presence tracking
354
+ */
313
355
  dispose() {
314
356
  this.stopLocationWatcher();
315
357
  if (this.connectedListener) {
@@ -1,25 +1,29 @@
1
1
  import { type User } from "firebase/auth";
2
2
  /**
3
3
  * FirekitUser provides a singleton class for managing Firebase authentication state and user operations.
4
+ * Implements state management using SvelteKit's $state and $derived.
5
+ *
4
6
  * @class
5
7
  * @example
6
8
  * ```typescript
7
- * // Get instance
9
+ * // Get instance and check auth state
8
10
  * const auth = firekitUser;
9
- *
10
- * // Check if user is logged in
11
11
  * if (auth.isLoggedIn) {
12
12
  * console.log(auth.user);
13
13
  * }
14
14
  *
15
15
  * // Update user profile
16
16
  * await auth.updateDisplayName("John Doe");
17
+ * await auth.updatePhotoURL("https://example.com/photo.jpg");
17
18
  * ```
18
19
  */
19
20
  declare class FirekitUser {
20
21
  private static instance;
22
+ /** @private Current user object state */
21
23
  private _user;
24
+ /** @private Authentication initialization state */
22
25
  private _initialized;
26
+ /** @private User login state */
23
27
  private _isLoggedIn;
24
28
  /** Current user's UID */
25
29
  readonly uid: string | undefined;
@@ -31,6 +35,10 @@ declare class FirekitUser {
31
35
  readonly photoURL: string | null | undefined;
32
36
  /** Whether current user's email is verified */
33
37
  readonly emailVerified: boolean | undefined;
38
+ /**
39
+ * Private constructor that initializes auth state listener
40
+ * @private
41
+ */
34
42
  private constructor();
35
43
  /**
36
44
  * Gets the singleton instance of FirekitUser
@@ -57,7 +65,9 @@ declare class FirekitUser {
57
65
  * @param {string} email - The new email address
58
66
  * @throws {Error} If no user is authenticated
59
67
  * @example
68
+ * ```typescript
60
69
  * await firekitUser.updateEmailUser("new@email.com");
70
+ * ```
61
71
  */
62
72
  updateEmailUser(email: string): Promise<void>;
63
73
  /**
@@ -65,7 +75,9 @@ declare class FirekitUser {
65
75
  * @param {string} password - The new password
66
76
  * @throws {Error} If no user is authenticated
67
77
  * @example
78
+ * ```typescript
68
79
  * await firekitUser.updatePassword("newPassword123");
80
+ * ```
69
81
  */
70
82
  updatePassword(password: string): Promise<void>;
71
83
  /**
@@ -73,7 +85,9 @@ declare class FirekitUser {
73
85
  * @param {string} displayName - The new display name
74
86
  * @throws {Error} If no user is authenticated
75
87
  * @example
88
+ * ```typescript
76
89
  * await firekitUser.updateDisplayName("John Doe");
90
+ * ```
77
91
  */
78
92
  updateDisplayName(displayName: string): Promise<void>;
79
93
  /**
@@ -81,12 +95,16 @@ declare class FirekitUser {
81
95
  * @param {string} photoURL - The new photo URL
82
96
  * @throws {Error} If no user is authenticated
83
97
  * @example
98
+ * ```typescript
84
99
  * await firekitUser.updatePhotoURL("https://example.com/photo.jpg");
100
+ * ```
85
101
  */
86
102
  updatePhotoURL(photoURL: string): Promise<void>;
87
103
  }
88
104
  /**
89
- * Singleton instance of FirekitUser
105
+ * Pre-initialized singleton instance of FirekitUser.
106
+ * Use this to access auth state and user operations.
107
+ *
90
108
  * @const
91
109
  * @type {FirekitUser}
92
110
  */
@@ -1,26 +1,33 @@
1
+ /**
2
+ * @module FirekitUser
3
+ */
1
4
  import { firebaseService } from "../firebase.js";
2
5
  import { onAuthStateChanged, updateEmail, updatePassword, updateProfile } from "firebase/auth";
3
6
  /**
4
7
  * FirekitUser provides a singleton class for managing Firebase authentication state and user operations.
8
+ * Implements state management using SvelteKit's $state and $derived.
9
+ *
5
10
  * @class
6
11
  * @example
7
12
  * ```typescript
8
- * // Get instance
13
+ * // Get instance and check auth state
9
14
  * const auth = firekitUser;
10
- *
11
- * // Check if user is logged in
12
15
  * if (auth.isLoggedIn) {
13
16
  * console.log(auth.user);
14
17
  * }
15
18
  *
16
19
  * // Update user profile
17
20
  * await auth.updateDisplayName("John Doe");
21
+ * await auth.updatePhotoURL("https://example.com/photo.jpg");
18
22
  * ```
19
23
  */
20
24
  class FirekitUser {
21
25
  static instance;
26
+ /** @private Current user object state */
22
27
  _user = $state();
28
+ /** @private Authentication initialization state */
23
29
  _initialized = $state();
30
+ /** @private User login state */
24
31
  _isLoggedIn = $state();
25
32
  /** Current user's UID */
26
33
  uid = $derived(this._user?.uid);
@@ -32,6 +39,10 @@ class FirekitUser {
32
39
  photoURL = $derived(this._user?.photoURL);
33
40
  /** Whether current user's email is verified */
34
41
  emailVerified = $derived(this._user?.emailVerified);
42
+ /**
43
+ * Private constructor that initializes auth state listener
44
+ * @private
45
+ */
35
46
  constructor() {
36
47
  const auth = firebaseService.getAuthInstance();
37
48
  onAuthStateChanged(auth, async (user) => {
@@ -82,7 +93,9 @@ class FirekitUser {
82
93
  * @param {string} email - The new email address
83
94
  * @throws {Error} If no user is authenticated
84
95
  * @example
96
+ * ```typescript
85
97
  * await firekitUser.updateEmailUser("new@email.com");
98
+ * ```
86
99
  */
87
100
  async updateEmailUser(email) {
88
101
  if (!this._user)
@@ -94,7 +107,9 @@ class FirekitUser {
94
107
  * @param {string} password - The new password
95
108
  * @throws {Error} If no user is authenticated
96
109
  * @example
110
+ * ```typescript
97
111
  * await firekitUser.updatePassword("newPassword123");
112
+ * ```
98
113
  */
99
114
  async updatePassword(password) {
100
115
  if (!this._user)
@@ -106,7 +121,9 @@ class FirekitUser {
106
121
  * @param {string} displayName - The new display name
107
122
  * @throws {Error} If no user is authenticated
108
123
  * @example
124
+ * ```typescript
109
125
  * await firekitUser.updateDisplayName("John Doe");
126
+ * ```
110
127
  */
111
128
  async updateDisplayName(displayName) {
112
129
  if (!this._user)
@@ -118,7 +135,9 @@ class FirekitUser {
118
135
  * @param {string} photoURL - The new photo URL
119
136
  * @throws {Error} If no user is authenticated
120
137
  * @example
138
+ * ```typescript
121
139
  * await firekitUser.updatePhotoURL("https://example.com/photo.jpg");
140
+ * ```
122
141
  */
123
142
  async updatePhotoURL(photoURL) {
124
143
  if (!this._user)
@@ -127,7 +146,9 @@ class FirekitUser {
127
146
  }
128
147
  }
129
148
  /**
130
- * Singleton instance of FirekitUser
149
+ * Pre-initialized singleton instance of FirekitUser.
150
+ * Use this to access auth state and user operations.
151
+ *
131
152
  * @const
132
153
  * @type {FirekitUser}
133
154
  */
package/dist/config.d.ts CHANGED
@@ -1,2 +1,12 @@
1
1
  import type { FirebaseOptions } from 'firebase/app';
2
+ /**
3
+ * Pre-initialized Firebase configuration instance.
4
+ * Use this to get Firebase configuration options directly.
5
+ *
6
+ * @example
7
+ * import { firebaseConfig } from './firebase-config';
8
+ * import { initializeApp } from 'firebase/app';
9
+ *
10
+ * const app = initializeApp(firebaseConfig);
11
+ */
2
12
  export declare const firebaseConfig: FirebaseOptions;
package/dist/config.js CHANGED
@@ -1,7 +1,42 @@
1
1
  import { PUBLIC_FIREBASE_API_KEY, PUBLIC_FIREBASE_AUTH_DOMAIN, PUBLIC_FIREBASE_PROJECT_ID, PUBLIC_FIREBASE_STORAGE_BUCKET, PUBLIC_FIREBASE_MESSAGING_SENDER_ID, PUBLIC_FIREBASE_APP_ID, PUBLIC_FIREBASE_MEASUREMENT_ID } from '$env/static/public';
2
+ /**
3
+ * @module FirebaseConfig
4
+ */
5
+ /**
6
+ * Required environment variables for Firebase configuration.
7
+ * These must be defined in your .env file or environment.
8
+ * @typedef {Object} FirebaseEnvVars
9
+ * @property {string} PUBLIC_FIREBASE_API_KEY - Firebase API key
10
+ * @property {string} PUBLIC_FIREBASE_AUTH_DOMAIN - Firebase auth domain
11
+ * @property {string} PUBLIC_FIREBASE_PROJECT_ID - Firebase project ID
12
+ * @property {string} PUBLIC_FIREBASE_STORAGE_BUCKET - Firebase storage bucket
13
+ * @property {string} PUBLIC_FIREBASE_MESSAGING_SENDER_ID - Firebase messaging sender ID
14
+ * @property {string} PUBLIC_FIREBASE_APP_ID - Firebase app ID
15
+ * @property {string} PUBLIC_FIREBASE_MEASUREMENT_ID - Firebase measurement ID
16
+ */
17
+ /**
18
+ * Singleton class that manages Firebase configuration.
19
+ * Implements the Singleton pattern to ensure only one Firebase config instance exists.
20
+ *
21
+ * @example
22
+ * // Get Firebase configuration
23
+ * const config = FirebaseConfig.getInstance().getConfig();
24
+ *
25
+ * // Initialize Firebase app
26
+ * const app = initializeApp(config);
27
+ *
28
+ * @throws {Error} If any required Firebase configuration variables are missing
29
+ */
2
30
  class FirebaseConfig {
3
31
  static instance;
4
32
  config;
33
+ /**
34
+ * Private constructor to prevent direct instantiation.
35
+ * Validates all required environment variables are present and creates config.
36
+ *
37
+ * @private
38
+ * @throws {Error} If any required Firebase configuration variables are missing
39
+ */
5
40
  constructor() {
6
41
  const missingVars = this.getMissingFirebaseConfigVars();
7
42
  if (missingVars.length > 0) {
@@ -17,6 +52,12 @@ class FirebaseConfig {
17
52
  measurementId: PUBLIC_FIREBASE_MEASUREMENT_ID
18
53
  };
19
54
  }
55
+ /**
56
+ * Checks for missing required Firebase configuration variables.
57
+ *
58
+ * @private
59
+ * @returns {string[]} Array of missing environment variable names
60
+ */
20
61
  getMissingFirebaseConfigVars() {
21
62
  const requiredVars = {
22
63
  'PUBLIC_FIREBASE_API_KEY': PUBLIC_FIREBASE_API_KEY,
@@ -31,14 +72,36 @@ class FirebaseConfig {
31
72
  .filter(([_, value]) => !value)
32
73
  .map(([key]) => key);
33
74
  }
75
+ /**
76
+ * Gets the singleton instance of FirebaseConfig.
77
+ * Creates a new instance if one doesn't exist.
78
+ *
79
+ * @returns {FirebaseConfig} The singleton FirebaseConfig instance
80
+ * @throws {Error} If any required Firebase configuration variables are missing
81
+ */
34
82
  static getInstance() {
35
83
  if (!FirebaseConfig.instance) {
36
84
  FirebaseConfig.instance = new FirebaseConfig();
37
85
  }
38
86
  return FirebaseConfig.instance;
39
87
  }
88
+ /**
89
+ * Gets the Firebase configuration options.
90
+ *
91
+ * @returns {FirebaseOptions} The Firebase configuration options
92
+ */
40
93
  getConfig() {
41
94
  return this.config;
42
95
  }
43
96
  }
97
+ /**
98
+ * Pre-initialized Firebase configuration instance.
99
+ * Use this to get Firebase configuration options directly.
100
+ *
101
+ * @example
102
+ * import { firebaseConfig } from './firebase-config';
103
+ * import { initializeApp } from 'firebase/app';
104
+ *
105
+ * const app = initializeApp(firebaseConfig);
106
+ */
44
107
  export const firebaseConfig = FirebaseConfig.getInstance().getConfig();
@@ -4,6 +4,17 @@ import { type Auth } from 'firebase/auth';
4
4
  import { type Functions } from 'firebase/functions';
5
5
  import { type Database } from 'firebase/database';
6
6
  import { type FirebaseStorage } from 'firebase/storage';
7
+ /**
8
+ * Singleton service class that manages Firebase service instances.
9
+ * Handles initialization and access to Firebase app and its various services.
10
+ *
11
+ * @example
12
+ * // Get Firestore instance
13
+ * const db = firebaseService.getDbInstance();
14
+ *
15
+ * // Get Auth instance
16
+ * const auth = firebaseService.getAuthInstance();
17
+ */
7
18
  declare class FirebaseService {
8
19
  private static instance;
9
20
  private firebaseApp;
@@ -12,16 +23,74 @@ declare class FirebaseService {
12
23
  private functions;
13
24
  private database;
14
25
  private storage;
26
+ /** Flag to determine if code is running in browser environment */
15
27
  private readonly isBrowser;
28
+ /** @private */
16
29
  private constructor();
30
+ /**
31
+ * Gets the singleton instance of FirebaseService.
32
+ * Creates a new instance if one doesn't exist.
33
+ *
34
+ * @returns {FirebaseService} The singleton FirebaseService instance
35
+ */
17
36
  static getInstance(): FirebaseService;
37
+ /**
38
+ * Initializes or retrieves the Firebase app instance.
39
+ * Also initializes Firestore if running in browser environment.
40
+ *
41
+ * @returns {FirebaseApp} The Firebase app instance
42
+ */
18
43
  getFirebaseApp(): FirebaseApp;
44
+ /**
45
+ * Initializes Firestore with persistent cache and multi-tab support.
46
+ * Only runs in browser environment.
47
+ *
48
+ * @private
49
+ */
19
50
  private initializeFirestoreInstance;
51
+ /**
52
+ * Gets the Firestore instance, initializing it if necessary.
53
+ *
54
+ * @returns {Firestore} The Firestore instance
55
+ */
20
56
  getDbInstance(): Firestore;
57
+ /**
58
+ * Gets the Authentication instance, initializing it if necessary.
59
+ *
60
+ * @returns {Auth} The Authentication instance
61
+ */
21
62
  getAuthInstance(): Auth;
63
+ /**
64
+ * Gets the Cloud Functions instance, initializing it if necessary.
65
+ *
66
+ * @returns {Functions} The Cloud Functions instance
67
+ */
22
68
  getFunctionsInstance(): Functions;
69
+ /**
70
+ * Gets the Realtime Database instance, initializing it if necessary.
71
+ *
72
+ * @returns {Database} The Realtime Database instance
73
+ */
23
74
  getDatabaseInstance(): Database;
75
+ /**
76
+ * Gets the Storage instance, initializing it if necessary.
77
+ *
78
+ * @returns {FirebaseStorage} The Storage instance
79
+ */
24
80
  getStorageInstance(): FirebaseStorage;
25
81
  }
82
+ /**
83
+ * Pre-initialized Firebase service instance.
84
+ * Use this to access Firebase services directly.
85
+ *
86
+ * @example
87
+ * import { firebaseService } from './firebase-service';
88
+ *
89
+ * // Get Firestore
90
+ * const db = firebaseService.getDbInstance();
91
+ *
92
+ * // Get Auth
93
+ * const auth = firebaseService.getAuthInstance();
94
+ */
26
95
  export declare const firebaseService: FirebaseService;
27
96
  export {};
package/dist/firebase.js CHANGED
@@ -1,10 +1,21 @@
1
1
  import { initializeApp, getApps } from 'firebase/app';
2
- import { initializeFirestore, CACHE_SIZE_UNLIMITED, persistentLocalCache, persistentMultipleTabManager, enablePersistentCacheIndexAutoCreation, getPersistentCacheIndexManager, writeBatch } from 'firebase/firestore';
2
+ import { initializeFirestore, CACHE_SIZE_UNLIMITED, persistentLocalCache, persistentMultipleTabManager, enablePersistentCacheIndexAutoCreation, getPersistentCacheIndexManager, } from 'firebase/firestore';
3
3
  import { getAuth } from 'firebase/auth';
4
4
  import { getFunctions } from 'firebase/functions';
5
5
  import { getDatabase } from 'firebase/database';
6
6
  import { getStorage } from 'firebase/storage';
7
7
  import { firebaseConfig } from './config.js';
8
+ /**
9
+ * Singleton service class that manages Firebase service instances.
10
+ * Handles initialization and access to Firebase app and its various services.
11
+ *
12
+ * @example
13
+ * // Get Firestore instance
14
+ * const db = firebaseService.getDbInstance();
15
+ *
16
+ * // Get Auth instance
17
+ * const auth = firebaseService.getAuthInstance();
18
+ */
8
19
  class FirebaseService {
9
20
  static instance;
10
21
  firebaseApp = null;
@@ -13,15 +24,28 @@ class FirebaseService {
13
24
  functions = null;
14
25
  database = null;
15
26
  storage = null;
27
+ /** Flag to determine if code is running in browser environment */
16
28
  isBrowser = typeof window !== 'undefined';
17
- constructor() {
18
- }
29
+ /** @private */
30
+ constructor() { }
31
+ /**
32
+ * Gets the singleton instance of FirebaseService.
33
+ * Creates a new instance if one doesn't exist.
34
+ *
35
+ * @returns {FirebaseService} The singleton FirebaseService instance
36
+ */
19
37
  static getInstance() {
20
38
  if (!FirebaseService.instance) {
21
39
  FirebaseService.instance = new FirebaseService();
22
40
  }
23
41
  return FirebaseService.instance;
24
42
  }
43
+ /**
44
+ * Initializes or retrieves the Firebase app instance.
45
+ * Also initializes Firestore if running in browser environment.
46
+ *
47
+ * @returns {FirebaseApp} The Firebase app instance
48
+ */
25
49
  getFirebaseApp() {
26
50
  if (this.firebaseApp)
27
51
  return this.firebaseApp;
@@ -36,6 +60,12 @@ class FirebaseService {
36
60
  this.initializeFirestoreInstance();
37
61
  return this.firebaseApp;
38
62
  }
63
+ /**
64
+ * Initializes Firestore with persistent cache and multi-tab support.
65
+ * Only runs in browser environment.
66
+ *
67
+ * @private
68
+ */
39
69
  initializeFirestoreInstance() {
40
70
  if (this.db || !this.isBrowser)
41
71
  return;
@@ -54,30 +84,68 @@ class FirebaseService {
54
84
  console.warn('Failed to initialize the Firestore cache index manager');
55
85
  }
56
86
  }
87
+ /**
88
+ * Gets the Firestore instance, initializing it if necessary.
89
+ *
90
+ * @returns {Firestore} The Firestore instance
91
+ */
57
92
  getDbInstance() {
58
93
  if (!this.db)
59
94
  this.getFirebaseApp();
60
95
  return this.db;
61
96
  }
97
+ /**
98
+ * Gets the Authentication instance, initializing it if necessary.
99
+ *
100
+ * @returns {Auth} The Authentication instance
101
+ */
62
102
  getAuthInstance() {
63
103
  if (!this.auth)
64
104
  this.auth = getAuth(this.getFirebaseApp());
65
105
  return this.auth;
66
106
  }
107
+ /**
108
+ * Gets the Cloud Functions instance, initializing it if necessary.
109
+ *
110
+ * @returns {Functions} The Cloud Functions instance
111
+ */
67
112
  getFunctionsInstance() {
68
113
  if (!this.functions)
69
114
  this.functions = getFunctions(this.getFirebaseApp());
70
115
  return this.functions;
71
116
  }
117
+ /**
118
+ * Gets the Realtime Database instance, initializing it if necessary.
119
+ *
120
+ * @returns {Database} The Realtime Database instance
121
+ */
72
122
  getDatabaseInstance() {
73
123
  if (!this.database)
74
124
  this.database = getDatabase(this.getFirebaseApp());
75
125
  return this.database;
76
126
  }
127
+ /**
128
+ * Gets the Storage instance, initializing it if necessary.
129
+ *
130
+ * @returns {FirebaseStorage} The Storage instance
131
+ */
77
132
  getStorageInstance() {
78
133
  if (!this.storage)
79
134
  this.storage = getStorage(this.getFirebaseApp());
80
135
  return this.storage;
81
136
  }
82
137
  }
138
+ /**
139
+ * Pre-initialized Firebase service instance.
140
+ * Use this to access Firebase services directly.
141
+ *
142
+ * @example
143
+ * import { firebaseService } from './firebase-service';
144
+ *
145
+ * // Get Firestore
146
+ * const db = firebaseService.getDbInstance();
147
+ *
148
+ * // Get Auth
149
+ * const auth = firebaseService.getAuthInstance();
150
+ */
83
151
  export const firebaseService = FirebaseService.getInstance();