react-query-firebase 1.3.1 → 1.3.3

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,12 +1,30 @@
1
1
  /**
2
- * Custom hook to manage an ID token for the current user.
3
- * This hook provides an `idToken` value and a `refresh` function to manually refresh the token.
4
- *
5
- * @returns {Object} An object containing:
6
- * @returns {string} idToken - The current ID token for the user.
7
- * @returns {Function} refresh - A function to refresh the ID token.
2
+ * @inline
8
3
  */
9
- export declare const useIdToken: () => {
4
+ export type UseIdTokenResult = {
10
5
  idToken: string;
11
- refresh: () => Promise<void>;
6
+ refresh: () => Promise<string | undefined>;
12
7
  };
8
+ /**
9
+ * A hook to manage the ID token.
10
+ * It monitors changes to the ID token and provides the token itself along with a refresh method to update the token when needed.
11
+ *
12
+ * @group Hook
13
+ *
14
+ * @returns {UseIdTokenResult}
15
+ *
16
+ * @example
17
+ * ```jsx
18
+ * export const MyComponent = () => {
19
+ * const result = useIdToken();
20
+ * useEffect(() => {
21
+ * const timeout = setTimeout(() => {
22
+ * result.refresh();
23
+ * }, 5000);
24
+ * return () => clearTimeout();
25
+ * }, [])
26
+ * console.log(resilt.idToken);
27
+ * };
28
+ * ```
29
+ */
30
+ export declare const useIdToken: () => UseIdTokenResult;
@@ -3,12 +3,26 @@ import { useCurrentUser } from "./useCurrentUser";
3
3
  import { onIdTokenChanged } from "firebase/auth";
4
4
  import { useAuth } from "./useAuth";
5
5
  /**
6
- * Custom hook to manage an ID token for the current user.
7
- * This hook provides an `idToken` value and a `refresh` function to manually refresh the token.
6
+ * A hook to manage the ID token.
7
+ * It monitors changes to the ID token and provides the token itself along with a refresh method to update the token when needed.
8
8
  *
9
- * @returns {Object} An object containing:
10
- * @returns {string} idToken - The current ID token for the user.
11
- * @returns {Function} refresh - A function to refresh the ID token.
9
+ * @group Hook
10
+ *
11
+ * @returns {UseIdTokenResult}
12
+ *
13
+ * @example
14
+ * ```jsx
15
+ * export const MyComponent = () => {
16
+ * const result = useIdToken();
17
+ * useEffect(() => {
18
+ * const timeout = setTimeout(() => {
19
+ * result.refresh();
20
+ * }, 5000);
21
+ * return () => clearTimeout();
22
+ * }, [])
23
+ * console.log(resilt.idToken);
24
+ * };
25
+ * ```
12
26
  */
13
27
  export const useIdToken = () => {
14
28
  const auth = useAuth();
@@ -27,7 +41,7 @@ export const useIdToken = () => {
27
41
  return;
28
42
  }
29
43
  const idToken = await currentUser.getIdToken(true);
30
- setIdToken(idToken);
44
+ return idToken;
31
45
  }, [currentUser, idToken]);
32
46
  useEffect(() => {
33
47
  callback();
@@ -1,6 +1,7 @@
1
1
  import React, { PropsWithChildren } from "react";
2
2
  import { ConsentSettings } from "firebase/analytics";
3
3
  import { RemoteConfigSettings } from "firebase/remote-config";
4
+ import { FirestoreSettings } from "firebase/firestore";
4
5
  import { FirebaseOptions } from "firebase/app";
5
6
  /**
6
7
  * @inline
@@ -71,6 +72,13 @@ export type FirebaseContextProviderProps = PropsWithChildren & {
71
72
  * }
72
73
  */
73
74
  consentSettings?: ConsentSettings;
75
+ /**
76
+ * Specifies custom configurations for your Cloud Firestore instance.
77
+ * You must set these before invoking any other methods.
78
+ * {@link https://firebase.google.com/docs/reference/js/firestore_.firestoresettings}
79
+ * @defaultValue {}
80
+ */
81
+ firestoreSettings?: FirestoreSettings;
74
82
  /**
75
83
  * Flag indicating whether Firebase Firestore should be enabled.
76
84
  * @defaultValue `true`
@@ -3,7 +3,7 @@ import { FirebaseContext } from "./FirebaseContext";
3
3
  import { connectAuthEmulator, getAuth } from "firebase/auth";
4
4
  import { getAnalytics, setAnalyticsCollectionEnabled, setConsent } from "firebase/analytics";
5
5
  import { getRemoteConfig } from "firebase/remote-config";
6
- import { connectFirestoreEmulator, getFirestore } from "firebase/firestore";
6
+ import { connectFirestoreEmulator, initializeFirestore } from "firebase/firestore";
7
7
  import { initializeApp } from "firebase/app";
8
8
  /**
9
9
  * FirebaseContextProvider component configures and provides Firebase services to its children.
@@ -28,7 +28,7 @@ import { initializeApp } from "firebase/app";
28
28
  * };
29
29
  * ```
30
30
  */
31
- export const FirebaseContextProvider = ({ emulators, options, children, authEnabled = true, firestoreEnabled = true, analyticsEnabled = true, consentSettings = {}, remoteConfigEnabled = true, remoteConfigSettings, remoteConfigDefaults = {} }) => {
31
+ export const FirebaseContextProvider = ({ emulators, options, children, authEnabled = true, firestoreEnabled = true, analyticsEnabled = true, consentSettings = {}, remoteConfigEnabled = true, remoteConfigSettings, remoteConfigDefaults = {}, firestoreSettings }) => {
32
32
  const firebase = useMemo(() => {
33
33
  return initializeApp(options);
34
34
  }, [options]);
@@ -44,39 +44,53 @@ export const FirebaseContextProvider = ({ emulators, options, children, authEnab
44
44
  ...consentSettings
45
45
  });
46
46
  }, [consentSettings]);
47
- const contextValue = useMemo(() => {
48
- const value = {};
47
+ const firestore = useMemo(() => {
49
48
  if (firestoreEnabled) {
50
- const firestore = getFirestore(firebase);
49
+ const localFirestore = initializeFirestore(firebase, firestoreSettings || {});
51
50
  if (emulators?.firestore?.host && emulators?.firestore?.port) {
52
- connectFirestoreEmulator(firestore, emulators.firestore.host, emulators.firestore.port);
51
+ connectFirestoreEmulator(localFirestore, emulators.firestore.host, emulators.firestore.port);
53
52
  }
54
- value.firestore = firestore;
53
+ return localFirestore;
55
54
  }
55
+ return null;
56
+ }, [firestoreSettings, emulators?.firestore, firestoreEnabled]);
57
+ const auth = useMemo(() => {
56
58
  if (authEnabled) {
57
- const auth = getAuth(firebase);
59
+ const localAuth = getAuth(firebase);
58
60
  if (emulators?.auth?.host) {
59
- connectAuthEmulator(auth, emulators?.auth?.host, {
61
+ connectAuthEmulator(localAuth, emulators?.auth?.host, {
60
62
  disableWarnings: true
61
63
  });
62
64
  }
63
- value.auth = auth;
65
+ return localAuth;
64
66
  }
67
+ return null;
68
+ }, [emulators?.auth, authEnabled]);
69
+ const analytics = useMemo(() => {
65
70
  if (analyticsEnabled && options.measurementId && typeof window !== "undefined") {
66
- const analytics = getAnalytics(firebase);
67
- value.analytics = analytics;
71
+ return getAnalytics(firebase);
68
72
  }
73
+ return null;
74
+ }, [analyticsEnabled, options.measurementId]);
75
+ const remoteConfig = useMemo(() => {
69
76
  if (remoteConfigEnabled && typeof window !== "undefined") {
70
- const remoteConfig = getRemoteConfig(firebase);
77
+ const localRemoteConfig = getRemoteConfig(firebase);
71
78
  if (remoteConfigSettings) {
72
- remoteConfig.settings.fetchTimeoutMillis = remoteConfigSettings.fetchTimeoutMillis;
73
- remoteConfig.settings.minimumFetchIntervalMillis = remoteConfigSettings.minimumFetchIntervalMillis;
74
- remoteConfig.defaultConfig = remoteConfigDefaults;
79
+ localRemoteConfig.settings.fetchTimeoutMillis = remoteConfigSettings.fetchTimeoutMillis;
80
+ localRemoteConfig.settings.minimumFetchIntervalMillis = remoteConfigSettings.minimumFetchIntervalMillis;
81
+ localRemoteConfig.defaultConfig = remoteConfigDefaults;
75
82
  }
76
- value.remoteConfig = remoteConfig;
83
+ return localRemoteConfig;
77
84
  }
78
- return { firebase, ...value };
79
- }, [firebase]);
85
+ return null;
86
+ }, [remoteConfigEnabled]);
87
+ const contextValue = useMemo(() => ({
88
+ firebase,
89
+ auth,
90
+ analytics,
91
+ firestore,
92
+ remoteConfig
93
+ }), [firebase, auth, analytics, firestore, remoteConfig]);
80
94
  useEffect(() => {
81
95
  if (contextValue.analytics) {
82
96
  setAnalyticsCollectionEnabled(contextValue.analytics, consentSettings?.analytics_storage === "granted");
package/package.json CHANGED
@@ -60,5 +60,5 @@
60
60
  "docs:build": "vitepress build docs",
61
61
  "docs:preview": "vitepress preview docs"
62
62
  },
63
- "version": "1.3.1"
63
+ "version": "1.3.3"
64
64
  }
@@ -4,14 +4,36 @@ import { onIdTokenChanged } from "firebase/auth";
4
4
  import { useAuth } from "./useAuth";
5
5
 
6
6
  /**
7
- * Custom hook to manage an ID token for the current user.
8
- * This hook provides an `idToken` value and a `refresh` function to manually refresh the token.
7
+ * @inline
8
+ */
9
+ export type UseIdTokenResult = {
10
+ idToken: string;
11
+ refresh: () => Promise<string | undefined>;
12
+ };
13
+
14
+ /**
15
+ * A hook to manage the ID token.
16
+ * It monitors changes to the ID token and provides the token itself along with a refresh method to update the token when needed.
9
17
  *
10
- * @returns {Object} An object containing:
11
- * @returns {string} idToken - The current ID token for the user.
12
- * @returns {Function} refresh - A function to refresh the ID token.
18
+ * @group Hook
19
+ *
20
+ * @returns {UseIdTokenResult}
21
+ *
22
+ * @example
23
+ * ```jsx
24
+ * export const MyComponent = () => {
25
+ * const result = useIdToken();
26
+ * useEffect(() => {
27
+ * const timeout = setTimeout(() => {
28
+ * result.refresh();
29
+ * }, 5000);
30
+ * return () => clearTimeout();
31
+ * }, [])
32
+ * console.log(resilt.idToken);
33
+ * };
34
+ * ```
13
35
  */
14
- export const useIdToken = () => {
36
+ export const useIdToken = (): UseIdTokenResult => {
15
37
  const auth = useAuth();
16
38
 
17
39
  const currentUser = useCurrentUser();
@@ -33,7 +55,7 @@ export const useIdToken = () => {
33
55
  }
34
56
 
35
57
  const idToken = await currentUser.getIdToken(true);
36
- setIdToken(idToken);
58
+ return idToken;
37
59
  }, [currentUser, idToken]);
38
60
 
39
61
  useEffect(() => {
@@ -3,7 +3,7 @@ import { FirebaseContext } from "./FirebaseContext";
3
3
  import { connectAuthEmulator, getAuth } from "firebase/auth";
4
4
  import { ConsentSettings, getAnalytics, setAnalyticsCollectionEnabled, setConsent } from "firebase/analytics";
5
5
  import { getRemoteConfig, RemoteConfigSettings } from "firebase/remote-config";
6
- import { connectFirestoreEmulator, getFirestore } from "firebase/firestore";
6
+ import { connectFirestoreEmulator, FirestoreSettings, initializeFirestore } from "firebase/firestore";
7
7
  import { FirebaseOptions, initializeApp } from "firebase/app";
8
8
 
9
9
  /**
@@ -78,6 +78,13 @@ export type FirebaseContextProviderProps = PropsWithChildren & {
78
78
  * }
79
79
  */
80
80
  consentSettings?: ConsentSettings;
81
+ /**
82
+ * Specifies custom configurations for your Cloud Firestore instance.
83
+ * You must set these before invoking any other methods.
84
+ * {@link https://firebase.google.com/docs/reference/js/firestore_.firestoresettings}
85
+ * @defaultValue {}
86
+ */
87
+ firestoreSettings?: FirestoreSettings;
81
88
  /**
82
89
  * Flag indicating whether Firebase Firestore should be enabled.
83
90
  * @defaultValue `true`
@@ -132,7 +139,8 @@ export const FirebaseContextProvider: React.FC<FirebaseContextProviderProps> = (
132
139
  consentSettings = {},
133
140
  remoteConfigEnabled = true,
134
141
  remoteConfigSettings,
135
- remoteConfigDefaults = {}
142
+ remoteConfigDefaults = {},
143
+ firestoreSettings
136
144
  }) => {
137
145
  const firebase = useMemo(() => {
138
146
  return initializeApp(options);
@@ -151,46 +159,63 @@ export const FirebaseContextProvider: React.FC<FirebaseContextProviderProps> = (
151
159
  });
152
160
  }, [consentSettings]);
153
161
 
154
- const contextValue = useMemo(() => {
155
- const value: Partial<React.ContextType<typeof FirebaseContext>> = {};
156
-
162
+ const firestore = useMemo(() => {
157
163
  if (firestoreEnabled) {
158
- const firestore = getFirestore(firebase);
164
+ const localFirestore = initializeFirestore(firebase, firestoreSettings || {});
159
165
 
160
166
  if (emulators?.firestore?.host && emulators?.firestore?.port) {
161
- connectFirestoreEmulator(firestore, emulators.firestore.host, emulators.firestore.port);
167
+ connectFirestoreEmulator(localFirestore, emulators.firestore.host, emulators.firestore.port);
162
168
  }
163
169
 
164
- value.firestore = firestore;
170
+ return localFirestore;
165
171
  }
166
172
 
173
+ return null;
174
+ }, [firestoreSettings, emulators?.firestore, firestoreEnabled]);
175
+
176
+ const auth = useMemo(() => {
167
177
  if (authEnabled) {
168
- const auth = getAuth(firebase);
178
+ const localAuth = getAuth(firebase);
169
179
  if (emulators?.auth?.host) {
170
- connectAuthEmulator(auth, emulators?.auth?.host, {
180
+ connectAuthEmulator(localAuth, emulators?.auth?.host, {
171
181
  disableWarnings: true
172
182
  });
173
183
  }
174
- value.auth = auth;
184
+ return localAuth;
175
185
  }
186
+ return null;
187
+ }, [emulators?.auth, authEnabled]);
176
188
 
189
+ const analytics = useMemo(() => {
177
190
  if (analyticsEnabled && options.measurementId && typeof window !== "undefined") {
178
- const analytics = getAnalytics(firebase);
179
- value.analytics = analytics;
191
+ return getAnalytics(firebase);
180
192
  }
193
+ return null;
194
+ }, [analyticsEnabled, options.measurementId]);
181
195
 
196
+ const remoteConfig = useMemo(() => {
182
197
  if (remoteConfigEnabled && typeof window !== "undefined") {
183
- const remoteConfig = getRemoteConfig(firebase);
198
+ const localRemoteConfig = getRemoteConfig(firebase);
184
199
  if (remoteConfigSettings) {
185
- remoteConfig.settings.fetchTimeoutMillis = remoteConfigSettings.fetchTimeoutMillis;
186
- remoteConfig.settings.minimumFetchIntervalMillis = remoteConfigSettings.minimumFetchIntervalMillis;
187
- remoteConfig.defaultConfig = remoteConfigDefaults;
200
+ localRemoteConfig.settings.fetchTimeoutMillis = remoteConfigSettings.fetchTimeoutMillis;
201
+ localRemoteConfig.settings.minimumFetchIntervalMillis = remoteConfigSettings.minimumFetchIntervalMillis;
202
+ localRemoteConfig.defaultConfig = remoteConfigDefaults;
188
203
  }
189
- value.remoteConfig = remoteConfig;
204
+ return localRemoteConfig;
190
205
  }
191
-
192
- return { firebase, ...value };
193
- }, [firebase]);
206
+ return null;
207
+ }, [remoteConfigEnabled]);
208
+
209
+ const contextValue = useMemo(
210
+ () => ({
211
+ firebase,
212
+ auth,
213
+ analytics,
214
+ firestore,
215
+ remoteConfig
216
+ }),
217
+ [firebase, auth, analytics, firestore, remoteConfig]
218
+ );
194
219
 
195
220
  useEffect(() => {
196
221
  if (contextValue.analytics) {