react-query-firebase 1.0.2 → 1.0.4

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.
Files changed (150) hide show
  1. package/README.md +27 -3
  2. package/dist/src/analytics/index.d.ts +1 -0
  3. package/dist/src/analytics/index.js +1 -0
  4. package/dist/src/analytics/useAnalytics.d.ts +5 -0
  5. package/dist/src/analytics/useAnalytics.js +10 -0
  6. package/dist/src/analytics/useLogEvent.d.ts +7 -0
  7. package/dist/src/analytics/useLogEvent.js +8 -1
  8. package/dist/src/auth/index.d.ts +2 -0
  9. package/dist/src/auth/index.js +2 -0
  10. package/dist/src/auth/useAuth.d.ts +5 -0
  11. package/dist/src/auth/useAuth.js +10 -0
  12. package/dist/src/auth/useAuthStateReady.d.ts +5 -0
  13. package/dist/src/auth/useAuthStateReady.js +6 -1
  14. package/dist/src/auth/useCreateUserWitEmailAndPasswordMutation.d.ts +7 -0
  15. package/dist/src/auth/useCreateUserWitEmailAndPasswordMutation.js +8 -1
  16. package/dist/src/auth/useCurrentUser.d.ts +5 -0
  17. package/dist/src/auth/useCurrentUser.js +6 -1
  18. package/dist/src/auth/useIdToken.d.ts +12 -0
  19. package/dist/src/auth/useIdToken.js +36 -0
  20. package/dist/src/auth/useReauthenticateWitCredentialMutation.d.ts +8 -0
  21. package/dist/src/auth/useReauthenticateWitCredentialMutation.js +8 -0
  22. package/dist/src/auth/useReauthenticateWitRedirectMutation.d.ts +8 -0
  23. package/dist/src/auth/useReauthenticateWitRedirectMutation.js +8 -0
  24. package/dist/src/auth/useSendEmailVerificationMutation.d.ts +6 -0
  25. package/dist/src/auth/useSendEmailVerificationMutation.js +6 -0
  26. package/dist/src/auth/useSignInWitEmailAndPasswordMutation.d.ts +7 -0
  27. package/dist/src/auth/useSignInWitEmailAndPasswordMutation.js +8 -1
  28. package/dist/src/auth/useSignInWitRedirectMutation.d.ts +6 -0
  29. package/dist/src/auth/useSignInWitRedirectMutation.js +7 -1
  30. package/dist/src/auth/useSignOutMutation.d.ts +7 -0
  31. package/dist/src/auth/useSignOutMutation.js +8 -1
  32. package/dist/src/auth/useUpdateProfileMutation.d.ts +7 -0
  33. package/dist/src/auth/useUpdateProfileMutation.js +7 -0
  34. package/dist/src/{FirebaseContext.d.ts → context/FirebaseContext.d.ts} +8 -2
  35. package/dist/src/context/FirebaseContext.js +8 -0
  36. package/dist/src/context/FirebaseContextProvider.d.ts +105 -0
  37. package/dist/src/{FirebaseContextProvider.js → context/FirebaseContextProvider.js} +24 -1
  38. package/dist/src/context/index.d.ts +1 -0
  39. package/dist/src/context/index.js +1 -0
  40. package/dist/src/firestore/index.d.ts +1 -0
  41. package/dist/src/firestore/index.js +1 -0
  42. package/dist/src/firestore/useAddDocMutation.d.ts +11 -0
  43. package/dist/src/firestore/useAddDocMutation.js +11 -0
  44. package/dist/src/firestore/useBatchWrite.d.ts +7 -0
  45. package/dist/src/firestore/useBatchWrite.js +8 -1
  46. package/dist/src/firestore/useCollectionReference.d.ts +11 -0
  47. package/dist/src/firestore/useCollectionReference.js +12 -1
  48. package/dist/src/firestore/useCompositeFilter.d.ts +10 -0
  49. package/dist/src/firestore/useCompositeFilter.js +17 -0
  50. package/dist/src/firestore/useCountQuery.d.ts +13 -0
  51. package/dist/src/firestore/useCountQuery.js +13 -0
  52. package/dist/src/firestore/useDeleteDocMutation.d.ts +7 -0
  53. package/dist/src/firestore/useDeleteDocMutation.js +7 -0
  54. package/dist/src/firestore/useDocReference.d.ts +12 -0
  55. package/dist/src/firestore/useDocReference.js +25 -1
  56. package/dist/src/firestore/useDocReferences.d.ts +16 -0
  57. package/dist/src/firestore/useDocReferences.js +17 -1
  58. package/dist/src/firestore/useFirestore.d.ts +7 -0
  59. package/dist/src/firestore/useFirestore.js +12 -0
  60. package/dist/src/firestore/useGetDocData.d.ts +8 -0
  61. package/dist/src/firestore/useGetDocData.js +9 -1
  62. package/dist/src/firestore/useInfiniteQuery.d.ts +7 -0
  63. package/dist/src/firestore/useInfiniteQuery.js +7 -0
  64. package/dist/src/firestore/useQuery.d.ts +14 -0
  65. package/dist/src/firestore/useQuery.js +14 -0
  66. package/dist/src/firestore/useRunTransaction.d.ts +7 -0
  67. package/dist/src/firestore/useRunTransaction.js +8 -1
  68. package/dist/src/firestore/useSetDocMutation.d.ts +9 -0
  69. package/dist/src/firestore/useSetDocMutation.js +9 -0
  70. package/dist/src/firestore/useUpdateDocMutation.d.ts +19 -0
  71. package/dist/src/firestore/useUpdateDocMutation.js +19 -0
  72. package/dist/src/firestore/utils/getDocData.d.ts +14 -0
  73. package/dist/src/firestore/utils/getDocData.js +14 -0
  74. package/dist/src/firestore/utils/getDocRef.d.ts +11 -0
  75. package/dist/src/firestore/utils/getDocRef.js +11 -0
  76. package/dist/src/firestore/utils/getDocSnap.d.ts +16 -0
  77. package/dist/src/firestore/utils/getDocSnap.js +16 -0
  78. package/dist/src/index.d.ts +1 -6
  79. package/dist/src/index.js +1 -6
  80. package/dist/src/remoteConfig/index.d.ts +1 -0
  81. package/dist/src/remoteConfig/index.js +1 -0
  82. package/dist/src/remoteConfig/useFetchAndActivate.d.ts +7 -0
  83. package/dist/src/remoteConfig/useFetchAndActivate.js +13 -4
  84. package/dist/src/remoteConfig/useGetValue.d.ts +6 -0
  85. package/dist/src/remoteConfig/useGetValue.js +7 -1
  86. package/dist/src/remoteConfig/useRemoteConfig.d.ts +5 -0
  87. package/dist/src/remoteConfig/useRemoteConfig.js +10 -0
  88. package/package.json +13 -5
  89. package/src/analytics/index.ts +1 -0
  90. package/src/analytics/useAnalytics.ts +12 -0
  91. package/src/analytics/useLogEvent.ts +8 -1
  92. package/src/auth/index.ts +2 -0
  93. package/src/auth/useAuth.ts +12 -0
  94. package/src/auth/useAuthStateReady.ts +6 -1
  95. package/src/auth/useCreateUserWitEmailAndPasswordMutation.ts +8 -1
  96. package/src/auth/useCurrentUser.ts +6 -1
  97. package/src/auth/useIdToken.ts +8 -0
  98. package/src/auth/useReauthenticateWitCredentialMutation.ts +8 -0
  99. package/src/auth/useReauthenticateWitRedirectMutation.ts +8 -0
  100. package/src/auth/useSendEmailVerificationMutation.ts +6 -0
  101. package/src/auth/useSignInWitEmailAndPasswordMutation.ts +8 -1
  102. package/src/auth/useSignInWitRedirectMutation.ts +7 -1
  103. package/src/auth/useSignOutMutation.ts +8 -1
  104. package/src/auth/useUpdateProfileMutation.ts +7 -0
  105. package/src/{FirebaseContext.ts → context/FirebaseContext.ts} +9 -2
  106. package/src/context/FirebaseContextProvider.tsx +173 -0
  107. package/src/context/index.ts +1 -0
  108. package/src/firestore/index.ts +1 -0
  109. package/src/firestore/useAddDocMutation.ts +11 -0
  110. package/src/firestore/useBatchWrite.ts +8 -1
  111. package/src/firestore/useCollectionReference.ts +12 -1
  112. package/src/firestore/useCompositeFilter.ts +18 -0
  113. package/src/firestore/useCountQuery.ts +14 -0
  114. package/src/firestore/useDeleteDocMutation.ts +7 -0
  115. package/src/firestore/useDocReference.ts +25 -1
  116. package/src/firestore/useDocReferences.ts +17 -1
  117. package/src/firestore/useFirestore.ts +13 -0
  118. package/src/firestore/useGetDocData.ts +9 -1
  119. package/src/firestore/useInfiniteQuery.ts +7 -0
  120. package/src/firestore/useQuery.ts +14 -0
  121. package/src/firestore/useRunTransaction.ts +8 -1
  122. package/src/firestore/useSetDocMutation.ts +9 -0
  123. package/src/firestore/useUpdateDocMutation.ts +19 -0
  124. package/src/firestore/utils/getDocData.ts +14 -0
  125. package/src/firestore/utils/getDocRef.ts +11 -0
  126. package/src/firestore/utils/getDocSnap.ts +16 -0
  127. package/src/index.ts +1 -6
  128. package/src/remoteConfig/index.ts +1 -0
  129. package/src/remoteConfig/useFetchAndActivate.ts +13 -4
  130. package/src/remoteConfig/useGetValue.ts +7 -1
  131. package/src/remoteConfig/useRemoteConfig.ts +11 -0
  132. package/dist/src/FirebaseContext.js +0 -3
  133. package/dist/src/FirebaseContextProvider.d.ts +0 -28
  134. package/dist/src/useAnalytics.d.ts +0 -1
  135. package/dist/src/useAnalytics.js +0 -6
  136. package/dist/src/useAuth.d.ts +0 -1
  137. package/dist/src/useAuth.js +0 -6
  138. package/dist/src/useFirebase.d.ts +0 -1
  139. package/dist/src/useFirebase.js +0 -6
  140. package/dist/src/useFirestore.d.ts +0 -1
  141. package/dist/src/useFirestore.js +0 -6
  142. package/dist/src/useRemoteConfig.d.ts +0 -1
  143. package/dist/src/useRemoteConfig.js +0 -6
  144. package/src/FirebaseContextProvider.tsx +0 -91
  145. package/src/firestore/useUpdateDocMutation copy.ts +0 -51
  146. package/src/useAnalytics.ts +0 -8
  147. package/src/useAuth.ts +0 -8
  148. package/src/useFirebase.ts +0 -8
  149. package/src/useFirestore.ts +0 -7
  150. package/src/useRemoteConfig.ts +0 -7
@@ -0,0 +1,173 @@
1
+ import React, { PropsWithChildren, useMemo } from "react";
2
+ import { FirebaseContext } from "./FirebaseContext";
3
+ import { connectAuthEmulator, getAuth } from "firebase/auth";
4
+ import { getAnalytics } from "firebase/analytics";
5
+ import { getRemoteConfig, RemoteConfigSettings } from "firebase/remote-config";
6
+ import { connectFirestoreEmulator, getFirestore } from "firebase/firestore";
7
+ import { FirebaseOptions, initializeApp } from "firebase/app";
8
+
9
+ /**
10
+ * @inline
11
+ */
12
+ export type FirebaseContextProviderFirestoreEmulatorConfig = {
13
+ /**
14
+ * Host to connect to Firebase Firestore Emulator
15
+ */
16
+ host: string;
17
+ /**
18
+ * Port to connect to Firebase Firestore Emulator
19
+ */
20
+ port: number;
21
+ };
22
+
23
+ /**
24
+ * @inline
25
+ */
26
+ export type FirebaseContextProviderAuthEmulatorConfig = {
27
+ /**
28
+ * Host to connect to Firebase Auth Emulator
29
+ */
30
+ host: string;
31
+ };
32
+
33
+ /**
34
+ * @inline
35
+ */
36
+ export type FirebaseContextProviderEmulators = {
37
+ /**
38
+ * Defines configuration for Firebase Firestore emulator. Optional.
39
+ */
40
+ firestore?: FirebaseContextProviderFirestoreEmulatorConfig;
41
+ /**
42
+ * Defines configuration for Firebase Auth emulator. Optional
43
+ */
44
+ auth?: FirebaseContextProviderAuthEmulatorConfig;
45
+ };
46
+
47
+ /**
48
+ * @inline
49
+ */
50
+ export type FirebaseContextProviderProps = PropsWithChildren & {
51
+ /**
52
+ * Defines configuration for firebase emulators
53
+ */
54
+ emulators?: FirebaseContextProviderEmulators;
55
+ /**
56
+ * Configuration options for Firebase initialization. {@link https://firebase.google.com/docs/web/setup#config-object | Learn about the Firebase config object}
57
+ */
58
+ options: FirebaseOptions;
59
+ /**
60
+ * Flag indicating whether Firebase Auth should be enabled.
61
+ */
62
+ authEnabled?: boolean;
63
+ /**
64
+ * Flag indicating whether Firebase Analytics should be enabled.
65
+ * @defaultValue `true`
66
+ */
67
+ analyticsEnabled?: boolean;
68
+ /**
69
+ * Flag indicating whether Firebase Firestore should be enabled.
70
+ * @defaultValue `true`
71
+ */
72
+ firestoreEnabled?: boolean;
73
+ /**
74
+ * Configuration options for Firebase Remote Config Settings. {@link https://firebase.google.com/docs/reference/js/remote-config.remoteconfigsettings | Learn about the Firebase Remote COnfig Settings object}
75
+ * @defaultValue `true`
76
+ */
77
+ remoteConfigSettings?: RemoteConfigSettings;
78
+ /**
79
+ * Configuration options for Firebase Remote Config Defaults.
80
+ */
81
+ remoteConfigDefaults?: { [key: string]: string | number | boolean };
82
+ /**
83
+ * Flag indicating whether Firebase Remote Config should be enabled.
84
+ * @defaultValue `true`
85
+ */
86
+ remoteConfigEnabled?: boolean;
87
+ };
88
+
89
+ /**
90
+ * FirebaseContextProvider component configures and provides Firebase services to its children.
91
+ * Initializes Firebase app and enables optional Firebase services such as Firestore, Auth, Analytics,
92
+ * and Remote Config based on the provided configuration and parameters.
93
+ *
94
+ * @group Component
95
+ *
96
+ * @param {FirebaseContextProviderProps} props
97
+ *
98
+ * @returns {FirebaseContextProvider<FirebaseContextProviderProps>}
99
+ *
100
+ * @example
101
+ * ```jsx
102
+ * const firebaseConfig = {};
103
+ * export const App = () => {
104
+ * return (
105
+ * <FirebaseContextProvider options={firebaseConfig}>
106
+ * <ChildComponent />
107
+ * </FirebaseContextProvider>
108
+ * );
109
+ * };
110
+ * ```
111
+ */
112
+ export const FirebaseContextProvider: React.FC<FirebaseContextProviderProps> = ({
113
+ emulators,
114
+ options,
115
+ children,
116
+ authEnabled = true,
117
+ firestoreEnabled = true,
118
+ analyticsEnabled = true,
119
+ remoteConfigEnabled = true,
120
+ remoteConfigSettings,
121
+ remoteConfigDefaults = {}
122
+ }) => {
123
+ const firebase = useMemo(() => {
124
+ return initializeApp(options);
125
+ }, [options]);
126
+
127
+ const contextValue = useMemo(() => {
128
+ const value: Partial<React.ContextType<typeof FirebaseContext>> = {};
129
+
130
+ if (firestoreEnabled) {
131
+ const firestore = getFirestore(firebase);
132
+
133
+ if (emulators?.firestore?.host && emulators?.firestore?.port) {
134
+ connectFirestoreEmulator(firestore, emulators.firestore.host, emulators.firestore.port);
135
+ }
136
+
137
+ value.firestore = firestore;
138
+ }
139
+
140
+ if (authEnabled) {
141
+ const auth = getAuth(firebase);
142
+ if (emulators?.auth?.host) {
143
+ connectAuthEmulator(auth, emulators?.auth?.host, {
144
+ disableWarnings: true
145
+ });
146
+ }
147
+ value.auth = auth;
148
+ }
149
+
150
+ if (analyticsEnabled && options.measurementId && typeof window !== "undefined") {
151
+ const analytics = getAnalytics(firebase);
152
+ value.analytics = analytics;
153
+ }
154
+
155
+ if (remoteConfigEnabled && typeof window !== "undefined") {
156
+ const remoteConfig = getRemoteConfig(firebase);
157
+ if (remoteConfigSettings) {
158
+ remoteConfig.settings.fetchTimeoutMillis = remoteConfigSettings.fetchTimeoutMillis;
159
+ remoteConfig.settings.minimumFetchIntervalMillis = remoteConfigSettings.minimumFetchIntervalMillis;
160
+ remoteConfig.defaultConfig = remoteConfigDefaults;
161
+ }
162
+ value.remoteConfig = remoteConfig;
163
+ }
164
+
165
+ return { firebase, ...value };
166
+ }, [firebase]);
167
+
168
+ return (
169
+ <FirebaseContext.Provider value={contextValue as React.ContextType<typeof FirebaseContext>}>
170
+ {children}
171
+ </FirebaseContext.Provider>
172
+ );
173
+ };
@@ -0,0 +1 @@
1
+ export * from "./FirebaseContextProvider";
@@ -1,3 +1,4 @@
1
+ export * from "./useFirestore";
1
2
  export * from "./useAddDocMutation";
2
3
  export * from "./useBatchWrite";
3
4
  export * from "./useCollectionReference";
@@ -28,6 +28,17 @@ export type UseAddDocMutationOptions<
28
28
  >;
29
29
  };
30
30
 
31
+ /**
32
+ * Provides a mutation hook to add a document to a Firestore collection utilizing React Query's `useMutation`.
33
+ * It handles addition and optional conversion of the document data in Firestore.
34
+ *
35
+ * @param {Object} options - Options for the mutation hook
36
+ * @param {FirebaseFirestore.CollectionReference<AppModelType>} options.reference - Firestore collection reference where the document should be added.
37
+ * @param {FirebaseFirestore.FirestoreDataConverter<DbModelType>} [options.converter] - Optional data converter for reading and writing Firestore documents.
38
+ * @param {UseMutationOptions<AppModelType, Error, { data: DbModelType }, TContext>} [options.options={}] - Optional configuration for the mutation.
39
+ *
40
+ * @returns {UseMutationResult<AppModelType, Error, { data: DbModelType }, TContext>} The mutation hook result containing status, error, and data of the mutation process.
41
+ */
31
42
  export const useAddDocMutation = <
32
43
  AppModelType extends DocumentData = DocumentData,
33
44
  DbModelType extends DocumentData = DocumentData,
@@ -2,7 +2,7 @@ import { useMutation, UseMutationOptions } from "@tanstack/react-query";
2
2
  import { writeBatch, WriteBatch } from "firebase/firestore";
3
3
 
4
4
  import { FirebaseError } from "firebase/app";
5
- import { useFirestore } from "../useFirestore";
5
+ import { useFirestore } from "./useFirestore";
6
6
 
7
7
  export type UseBatchWriteVariables = (batch: WriteBatch) => Promise<void> | void;
8
8
 
@@ -10,6 +10,13 @@ export type UseBatchWriteOptions<TContext = unknown> = {
10
10
  options?: Omit<UseMutationOptions<void, FirebaseError, UseBatchWriteVariables, TContext>, "mutationFn">;
11
11
  };
12
12
 
13
+ /**
14
+ * Custom hook to perform batch write operations using Firestore.
15
+ * Utilizes a mutation to carry out the batch write transaction.
16
+ * @template TContext - The type of context that can be passed into the hook, defaults to unknown.
17
+ * @param {Object} options - The configuration options for the mutation operation.
18
+ * @returns {Object} Returns an object composed of elements returned by useMutation, including properties such as status, and functions to trigger and control the mutation process.
19
+ */
13
20
  export const useBatchWrite = <TContext = unknown>({ options = {} }: UseBatchWriteOptions<TContext> = {}) => {
14
21
  const db = useFirestore();
15
22
 
@@ -1,7 +1,7 @@
1
1
  import { collection, CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
2
2
 
3
3
  import { useMemo } from "react";
4
- import { useFirestore } from "../useFirestore";
4
+ import { useFirestore } from "./useFirestore";
5
5
 
6
6
  export type UseCollectionReferenceOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
7
7
  reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
@@ -9,6 +9,17 @@ export type UseCollectionReferenceOptions<AppModelType, DbModelType extends Docu
9
9
  pathSegments?: string[];
10
10
  };
11
11
 
12
+ /**
13
+ * Creates a reference to a Firestore collection based on the provided path, reference, and path segments.
14
+ *
15
+ * This hook utilizes useMemo for optimization, ensuring the collection reference is recalculated only when its dependencies change.
16
+ *
17
+ * @param {UseCollectionReferenceOptions<AppModelType, DbModelType>} options - The options including path, reference, and pathSegments to construct the Firestore collection reference.
18
+ * @param {string} options.path - The base path for the collection.
19
+ * @param {FirestoreReference} options.reference - An optional Firestore reference object that should be of type "collection".
20
+ * @param {string[]} options.pathSegments - Additional path segments to append to the base path.
21
+ * @returns {CollectionReference} A Firestore collection reference constructed using the specified path, reference, and path segments.
22
+ */
12
23
  export const useCollectionReference = <AppModelType, DbModelType extends DocumentData = DocumentData>({
13
24
  path,
14
25
  reference,
@@ -29,6 +29,14 @@ export type UseCompositeFilter<DbModelType extends CompositeFilterDocumentData =
29
29
  query?: CompositeFilter<DbModelType>;
30
30
  };
31
31
 
32
+ /**
33
+ * Constructs a composite query filter based on the provided query structure.
34
+ * It recursively builds query constraints using logical "or" or "and" operators.
35
+ *
36
+ * @param {QueryElement<DbModelType>} query - The query element or structure to be evaluated and transformed into filter constraints.
37
+ * @returns {QueryFilterConstraint | null} A constructed query filter constraint based on the input query, or null if no valid constraints can be derived.
38
+ */
39
+
32
40
  const buildCompositeQuery = <DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData>(
33
41
  query: QueryElement<DbModelType>
34
42
  ): QueryFilterConstraint | null => {
@@ -51,6 +59,16 @@ const buildCompositeQuery = <DbModelType extends CompositeFilterDocumentData = C
51
59
  return null;
52
60
  };
53
61
 
62
+ /**
63
+ * A custom hook that generates a composite filter for database queries, using the provided query configuration.
64
+ * It applies either an 'OR' or 'AND' logical operation based on the type specified in the query.
65
+ *
66
+ * @param {Object} query - The query configuration object that contains subqueries and a type for logical combination.
67
+ * @param {string} query.type - The type of composite operation ('or'/'and').
68
+ * @param {Array} query.children - An array of subqueries that will be processed to form the composite filter.
69
+ *
70
+ * @returns {(Function|undefined)} A composite query filter constraint function formed by combining subqueries or undefined if there are no valid constraints.
71
+ */
54
72
  export const useCompositeFilter = <DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData>({
55
73
  query
56
74
  }: UseCompositeFilter<DbModelType>) => {
@@ -25,6 +25,20 @@ type UseCountQueryOptions<
25
25
  compositeFilter?: QueryCompositeFilterConstraint;
26
26
  };
27
27
 
28
+ /**
29
+ * Executes a query with specified constraints and returns the count of matched documents.
30
+ *
31
+ * This function utilizes React Query to asynchronously fetch the count of documents from a server database
32
+ * that match the provided query constraints and an optional composite filter.
33
+ *
34
+ * @param {UseCountQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
35
+ * @param {AppModelType extends DocumentData = DocumentData} [options.options] - Additional options for the React Query.
36
+ * @param {unknown} [options.query] - Reference to the query object to be executed.
37
+ * @param {unknown[]} [options.queryConstraints=[]] - An array of constraints to apply to the query.
38
+ * @param {unknown} [options.compositeFilter] - An optional composite filter to apply to the query.
39
+ * @returns {UseQueryResult<number>} An object containing the number of documents that match the query.
40
+ */
41
+
28
42
  export const useCountQuery = <
29
43
  AppModelType extends DocumentData = DocumentData,
30
44
  DbModelType extends DocumentData = DocumentData
@@ -17,6 +17,13 @@ export type UseDeleteDocMutationOptions<
17
17
  options?: Omit<UseMutationOptions<void, FirebaseError, void, TContext>, "mutationFn" | "mutationKey">;
18
18
  };
19
19
 
20
+ /**
21
+ * A custom hook that provides a mutation function to delete a document from the database.
22
+ * @param {UseDeleteDocMutationOptions<AppModelType, DbModelType, TContext>} options - An object containing the reference to the document and additional options for the mutation.
23
+ * @param {FirestoreReference<AppModelType, DbModelType>} options.reference - The reference to the document that needs to be deleted.
24
+ * @param {object} options.options - Additional options for the mutation, if any (default is an empty object).
25
+ * @returns {UseMutationResult} An object returned by the `useMutation` hook which includes properties and methods to control the mutation's execution and track its state.
26
+ */
20
27
  export const useDeleteDocMutation = <
21
28
  AppModelType extends DocumentData = DocumentData,
22
29
  DbModelType extends DocumentData = DocumentData,
@@ -1,7 +1,7 @@
1
1
  import { doc, CollectionReference, DocumentData, DocumentReference, Firestore } from "firebase/firestore";
2
2
 
3
3
  import { useEffect, useRef } from "react";
4
- import { useFirestore } from "../useFirestore";
4
+ import { useFirestore } from "./useFirestore";
5
5
 
6
6
  export type UseDocReferenceOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
7
7
  reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
@@ -9,6 +9,18 @@ export type UseDocReferenceOptions<AppModelType, DbModelType extends DocumentDat
9
9
  pathSegments?: string[];
10
10
  };
11
11
 
12
+ /**
13
+ * Generates a document reference for a specified path or reference in Firestore.
14
+ * If a reference is not provided, it constructs a document reference using the Firestore instance, path, and path segments.
15
+ *
16
+ * @param {Firestore} db - The Firestore database instance used to create the document reference.
17
+ * @param {UseDocReferenceOptions<AppModelType, DbModelType>} options - An object containing the path, path segments, and optional reference.
18
+ * @param {string} options.path - The path to the document in the Firestore database.
19
+ * @param {string[]} [options.pathSegments] - Optional additional segments to join with the path.
20
+ * @param {DocumentReference | CollectionReference} [options.reference] - Optional Firestore reference object that influences how the document reference is constructed.
21
+ *
22
+ * @returns {DocumentReference<AppModelType, DbModelType> | null} A Firestore document reference if the path is specified; otherwise, returns null if path is not provided.
23
+ */
12
24
  const getDocReference = <AppModelType, DbModelType extends DocumentData = DocumentData>(
13
25
  db: Firestore,
14
26
  { path, pathSegments, reference }: UseDocReferenceOptions<AppModelType, DbModelType>
@@ -26,6 +38,18 @@ const getDocReference = <AppModelType, DbModelType extends DocumentData = Docume
26
38
  ) as DocumentReference<AppModelType, DbModelType>;
27
39
  };
28
40
 
41
+ /**
42
+ * Custom hook to generate and manage a Firestore document reference.
43
+ * This hook facilitates the retrieval of a Firestore document reference based on given options such as path and reference data.
44
+ * The reference is updated whenever the associated path, reference, or path segments change.
45
+ *
46
+ * @param {UseDocReferenceOptions<AppModelType, DbModelType>} options - Configuration options for setting up the document reference.
47
+ * @param {string} options.path - The path to the Firestore document.
48
+ * @param {DbModelType} options.reference - Reference data for the document, providing additional context or specifics.
49
+ * @param {string[]} options.pathSegments - Parts of the path to construct the full document path dynamically.
50
+ *
51
+ * @returns {DocumentReference<AppModelType, DbModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
52
+ */
29
53
  export const useDocReference = <AppModelType, DbModelType extends DocumentData = DocumentData>({
30
54
  path,
31
55
  reference,
@@ -1,7 +1,7 @@
1
1
  import { CollectionReference, doc, DocumentData, DocumentReference } from "firebase/firestore";
2
2
 
3
3
  import { useMemo } from "react";
4
- import { useFirestore } from "../useFirestore";
4
+ import { useFirestore } from "./useFirestore";
5
5
 
6
6
  export type UseDocReferencesOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
7
7
  reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
@@ -9,6 +9,22 @@ export type UseDocReferencesOptions<AppModelType, DbModelType extends DocumentDa
9
9
  pathSegments?: string[];
10
10
  };
11
11
 
12
+ /**
13
+ * A custom hook to generate document references for Firebase Firestore documents using given
14
+ * reference options.
15
+ *
16
+ * The hook accepts an array of options, where each option may contain a path,
17
+ * an existing reference, or path segments, and returns an array of generated
18
+ * Firestore document references based on those options. The references can be used
19
+ * to interact with the Firestore database documents. It uses memoization to optimize
20
+ * reference generation.
21
+ *
22
+ * @param {UseDocReferencesOptions<AppModelType, DbModelType>[]} references - An array of options
23
+ * specifying how to generate document references. Each option may include a `path`
24
+ * as a string, a `reference` as a Firestore reference, and `pathSegments` as an array of strings.
25
+ * @returns {DocumentReference<AppModelType, DbModelType>[]} An array of Firestore document references
26
+ * generated from the options provided.
27
+ */
12
28
  export const useDocReferences = <AppModelType, DbModelType extends DocumentData = DocumentData>(
13
29
  references: UseDocReferencesOptions<AppModelType, DbModelType>[]
14
30
  ) => {
@@ -0,0 +1,13 @@
1
+ import { useContext } from "react";
2
+ import { FirebaseContext } from "../context/FirebaseContext";
3
+
4
+ /**
5
+ * Provides a hook to access the Firestore instance from the Firebase context.
6
+ * It extracts the Firestore object from the Firebase context, allowing components to interact with Firestore services.
7
+ *
8
+ * @returns {firestore.Firestore} The Firestore instance from the Firebase context.
9
+ */
10
+ export const useFirestore = () => {
11
+ const { firestore } = useContext(FirebaseContext);
12
+ return firestore;
13
+ };
@@ -1,6 +1,6 @@
1
1
  import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
2
2
 
3
- import { useFirestore } from "../useFirestore";
3
+ import { useFirestore } from "./useFirestore";
4
4
  import { getDocData } from "./utils/getDocData";
5
5
  import { useQuery, UseQueryOptions } from "@tanstack/react-query";
6
6
 
@@ -15,6 +15,14 @@ type UseGetDocOptions<
15
15
  reference: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
16
16
  };
17
17
 
18
+ /**
19
+ * Custom React Hook to retrieve document data from Firestore using specified parameters.
20
+ * @param {Object} options - The options for configuring the Firestore query.
21
+ * @param {string} reference - The reference to the document in Firestore.
22
+ * @param {string} path - The path to the document in Firestore.
23
+ * @param {Array<string>} pathSegments - Segments of the path to document in Firestore.
24
+ * @returns {Object} Result of the query containing document data and query status.
25
+ */
18
26
  export const useGetDocData = <
19
27
  AppModelType extends DocumentData = DocumentData,
20
28
  DbModelType extends DocumentData = DocumentData
@@ -52,6 +52,13 @@ type UseInfiniteQueryOptions<
52
52
  converter?: FirestoreDataConverter<AppModelType, DbModelType>;
53
53
  };
54
54
 
55
+ /**
56
+ * Custom hook that creates an infinite query using Firestore, allowing for query constraints, composite filters, and converters.
57
+ * It fetches data in pages and can load more as required.
58
+ *
59
+ * @param {UseInfiniteQueryOptions<AppModelType, DbModelType>} options - Configuration options for the infinite query, including Firestore query reference, query constraints, composite filter, and data converter.
60
+ * @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} Result object containing the infinite data and methods for fetching more pages.
61
+ */
55
62
  export const useInfiniteQuery = <
56
63
  AppModelType extends DocumentData = DocumentData,
57
64
  DbModelType extends DocumentData = DocumentData
@@ -27,6 +27,20 @@ type UseQueryOptions<
27
27
  converter?: FirestoreDataConverter<AppModelType, DbModelType>;
28
28
  };
29
29
 
30
+ /**
31
+ * Executes a query on a Firestore-like data source and returns the resulting documents as an array.
32
+ *
33
+ * This hook utilizes an abstraction over React Query to asynchronously fetch data based on the provided query
34
+ * reference and constraints. It supports optional filtering, conversion, and additional query constraints.
35
+ *
36
+ * @param {UseQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
37
+ * @param {DocumentReference<AppModelType>} queryReference - The reference to the query to be executed.
38
+ * @param {QueryConstraint[]} queryConstraints - Additional constraints to fine-tune the query.
39
+ * @param {QueryConstraint} compositeFilter - Optional composite filter to apply to the query.
40
+ * @param {FirestoreDataConverter<AppModelType>} converter - Optional data converter for transforming snapshots.
41
+ *
42
+ * @returns {UseQueryResult<AppModelType[]>} Result containing an array of documents that match the query criteria.
43
+ */
30
44
  export const useQuery = <
31
45
  AppModelType extends DocumentData = DocumentData,
32
46
  DbModelType extends DocumentData = DocumentData
@@ -2,7 +2,7 @@ import { useMutation, UseMutationOptions } from "@tanstack/react-query";
2
2
  import { runTransaction, Transaction } from "firebase/firestore";
3
3
 
4
4
  import { FirebaseError } from "firebase/app";
5
- import { useFirestore } from "../useFirestore";
5
+ import { useFirestore } from "./useFirestore";
6
6
 
7
7
  export type UseRunTransactionValues = <AppModelType = unknown>(transaction: Transaction) => AppModelType;
8
8
 
@@ -10,6 +10,13 @@ export type UseRunTransactionOptions<AppModelType = unknown, TContext = unknown>
10
10
  options?: Omit<UseMutationOptions<AppModelType, FirebaseError, UseRunTransactionValues, TContext>, "mutationFn">;
11
11
  };
12
12
 
13
+ /**
14
+ * Custom hook to execute a Firestore transaction using the useMutation hook.
15
+ *
16
+ * @param {UseRunTransactionOptions<AppModelType, TContext>} options - Configuration options for running the transaction.
17
+ * @param {Object} options.options - Options to customize the behavior of useMutation and runTransaction.
18
+ * @returns {UseMutationResult} The result object from the useMutation hook, allowing to track the transaction state and outcome.
19
+ */
13
20
  export const useRunTransaction = <AppModelType = unknown, TContext = unknown>({
14
21
  options = {}
15
22
  }: UseRunTransactionOptions<AppModelType, TContext>) => {
@@ -20,6 +20,15 @@ export type UseSetDocMutationOptions<
20
20
  >;
21
21
  };
22
22
 
23
+ /**
24
+ * Custom hook to create a mutation for setting a document in a Firestore-like database.
25
+ * The mutation can be configured with options and reference to specific document path.
26
+ *
27
+ * @param {UseSetDocMutationOptions<AppModelType, DbModelType, TContext>} param0 - The options for configuring the mutation, including the document reference and additional mutation options.
28
+ * @param {Object} param0.reference - The reference object that contains the path to the document.
29
+ * @param {Object} param0.options - Additional options for the mutation, can configure aspects like onSuccess or onError callbacks.
30
+ * @returns {MutationResult} The result of the mutation operation, which includes states like isLoading, isSuccess, isError, and methods to control the mutation process.
31
+ */
23
32
  export const useSetDocMutation = <
24
33
  AppModelType = unknown,
25
34
  DbModelType extends DocumentData = DocumentData,
@@ -28,6 +28,25 @@ export type UseUpdateDocMutationOptions<
28
28
  >;
29
29
  };
30
30
 
31
+ /**
32
+ * Custom hook that sets up a mutation for updating a document in a Firestore database.
33
+ *
34
+ * This hook utilizes `useMutation` for performing asynchronous operations to update the document
35
+ * and retrieve the latest data snapshot. The update functionality can be configured with a custom
36
+ * converter if needed.
37
+ *
38
+ * @param {UseUpdateDocMutationOptions<AppModelType, DbModelType, TContext>} options - Configuration options for the mutation,
39
+ * including Firestore reference, an optional Firestore data converter, and additional mutation options.
40
+ *
41
+ * `reference` - The Firestore document reference that identifies the document to be updated.
42
+ *
43
+ * `converter` - An optional Firestore converter for transforming the database response into a custom type.
44
+ *
45
+ * `options` - Additional options that customize the mutation's behavior.
46
+ *
47
+ * @returns {UseMutationResult<AppModelType, Error, {data: AppModelType}, TContext>} An object returned by `useMutation`
48
+ * which includes functions to start the mutation and properties that represent the different states of the mutation.
49
+ */
31
50
  export const useUpdateDocMutation = <
32
51
  AppModelType extends DocumentData = DocumentData,
33
52
  DbModelType extends DocumentData = DocumentData,
@@ -6,6 +6,20 @@ export type GetDocDataOptions<
6
6
  DbModelType extends DocumentData = DocumentData
7
7
  > = GetDocSnapOptions<AppModelType, DbModelType>;
8
8
 
9
+ /**
10
+ * Asynchronously retrieves document data from a specified database and reference.
11
+ * Utilizes the helper function `getDocSnap` to fetch the document snapshot and
12
+ * checks if the document exists before returning its data.
13
+ *
14
+ * @param {Object} options - The options for fetching document data.
15
+ * @param {FirebaseFirestore} options.db - The Firestore database instance.
16
+ * @param {DocumentReference=} options.reference - The document reference. This is an optional parameter.
17
+ * @param {string=} options.path - The path to the document in the database. This is an optional parameter.
18
+ * @param {Array<string>=} options.pathSegments - The path segments for the document's path. This is an optional parameter.
19
+ *
20
+ * @returns {Promise<AppModelType | null>} Returns a promise that resolves to the document data if it exists,
21
+ * or null if the document does not exist.
22
+ */
9
23
  export const getDocData = async <
10
24
  AppModelType extends DocumentData = DocumentData,
11
25
  DbModelType extends DocumentData = DocumentData
@@ -10,6 +10,17 @@ export type GetDocRefOptions<
10
10
  pathSegments?: string[];
11
11
  };
12
12
 
13
+ /**
14
+ * Retrieves a document reference based on provided database options.
15
+ * Either `reference` or `path` must be provided, not both.
16
+ *
17
+ * @param {Object} options - The options for getting a document reference.
18
+ * @param {Firestore} options.db - The Firestore database instance.
19
+ * @param {DocumentReference | CollectionReference | null} [options.reference] - Reference to a document or collection.
20
+ * @param {string | null} [options.path] - Path to the document.
21
+ * @param {string[] | null} [options.pathSegments] - Additional path segments if any.
22
+ * @returns {DocumentReference<AppModelType, DbModelType>} The document reference.
23
+ */
13
24
  export const getDocRef = async <
14
25
  AppModelType extends DocumentData = DocumentData,
15
26
  DbModelType extends DocumentData = DocumentData
@@ -6,6 +6,22 @@ export type GetDocSnapOptions<
6
6
  DbModelType extends DocumentData = DocumentData
7
7
  > = GetDocRefOptions<AppModelType, DbModelType>;
8
8
 
9
+ /**
10
+ * Asynchronously retrieves a document snapshot from Firestore using a specified document reference
11
+ * or path information. This function handles getting the appropriate document reference based on
12
+ * the parameters provided, and then fetches the document data from Firestore.
13
+ *
14
+ * @param {GetDocSnapOptions<AppModelType, DbModelType>} options - An object containing options
15
+ * for retrieving the document
16
+ * snapshot.
17
+ * @param {DocumentData} options.db - The Firestore database instance to perform the operation on.
18
+ * @param {string} options.reference - A string representing a direct Firestore document reference.
19
+ * @param {string} options.path - A Firestore path string leading to the document.
20
+ * @param {string[]} options.pathSegments - An array of path segments to build the Firestore path.
21
+ * @returns {Promise<DocumentData | null>} A promise that resolves to the document data if the
22
+ * document exists, or `null` if the document does not exist
23
+ * or if the reference could not be obtained.
24
+ */
9
25
  export const getDocSnap = async <
10
26
  AppModelType extends DocumentData = DocumentData,
11
27
  DbModelType extends DocumentData = DocumentData
package/src/index.ts CHANGED
@@ -1,10 +1,5 @@
1
- export * from "./useAuth";
2
- export * from "./useFirebase";
3
- export * from "./useFirestore";
4
- export * from "./FirebaseContextProvider";
5
- export * from "./useAnalytics";
6
- export * from "./useRemoteConfig";
7
1
  export * from "./analytics";
8
2
  export * from "./auth";
3
+ export * from "./context";
9
4
  export * from "./firestore";
10
5
  export * from "./remoteConfig";
@@ -1,2 +1,3 @@
1
1
  export * from "./useFetchAndActivate";
2
2
  export * from "./useGetValue";
3
+ export * from "./useRemoteConfig";
@@ -1,16 +1,25 @@
1
- import { useRemoteConfig } from "../useRemoteConfig";
1
+ import { useRemoteConfig } from "./useRemoteConfig";
2
2
  import { ensureInitialized, fetchAndActivate } from "firebase/remote-config";
3
3
  import { useCallback, useMemo, useState } from "react";
4
4
 
5
+ /**
6
+ * Custom hook to fetch and activate remote configuration settings.
7
+ * Initializes remote configuration, fetches, activates it, and tracks the fetch status.
8
+ * @returns {Object} An object containing:
9
+ * - {Function} fetchAndActivate - Callback function to fetch and activate remote configuration.
10
+ * - {Boolean} isFetched - Boolean representing whether the fetch-and-activate process completed.
11
+ */
5
12
  export const useFetchAndActivate = () => {
6
13
  const remoteConfig = useRemoteConfig();
7
14
  const [isFetched, setIsFetched] = useState(false);
8
15
 
9
16
  const fetchAndActivateCallback = useCallback(async () => {
10
17
  try {
11
- await ensureInitialized(remoteConfig);
12
- await fetchAndActivate(remoteConfig);
13
- setIsFetched(true);
18
+ if (remoteConfig) {
19
+ await ensureInitialized(remoteConfig);
20
+ await fetchAndActivate(remoteConfig);
21
+ setIsFetched(true);
22
+ }
14
23
  } catch (e) {
15
24
  setIsFetched(true);
16
25
  console.log(`Cannot read remote config: ${(e as Error)?.message}`);