react-query-firebase 2.4.1 → 2.6.0

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/package.json CHANGED
@@ -74,5 +74,5 @@
74
74
  "docs:build": "vitepress build docs",
75
75
  "docs:preview": "vitepress preview docs"
76
76
  },
77
- "version": "2.4.1"
77
+ "version": "2.6.0"
78
78
  }
@@ -11,3 +11,4 @@ export * from "./useIdToken";
11
11
  export * from "./useReauthenticateWitCredentialMutation";
12
12
  export * from "./useReauthenticateWitRedirectMutation";
13
13
  export * from "./useAuthStateReady";
14
+ export * from "./useSignInAnonymouslyMutation";
@@ -11,3 +11,4 @@ export * from "./useIdToken";
11
11
  export * from "./useReauthenticateWitCredentialMutation";
12
12
  export * from "./useReauthenticateWitRedirectMutation";
13
13
  export * from "./useAuthStateReady";
14
+ export * from "./useSignInAnonymouslyMutation";
@@ -11,3 +11,4 @@ export * from "./useIdToken";
11
11
  export * from "./useReauthenticateWitCredentialMutation";
12
12
  export * from "./useReauthenticateWitRedirectMutation";
13
13
  export * from "./useAuthStateReady";
14
+ export * from "./useSignInAnonymouslyMutation";
@@ -1,5 +1,6 @@
1
1
  export declare const CREATE_USER_WITH_EMAIL_AND_PASSWORD_MUTATION_KEY: readonly ["FIREBASE", "AUTH", "CREATE_USER_WITH_EMAIL_AND_PASSWORD_MUTATION"];
2
2
  export declare const SEND_EMAIL_VERIFICATION_MUTATION_KEY: readonly ["FIREBASE", "AUTH", "SEND_EMAIL_VERIFICATION_MUTATION"];
3
+ export declare const SIGN_IN_ANONYMOUSLY_MUTATION_KEY: readonly ["FIREBASE", "AUTH", "SIGN_IN_ANONYMOUSLY_MUTATION"];
3
4
  export declare const SIGN_IN_WITH_EMAIL_AND_PASSWORD_MUTATION_KEY: readonly ["FIREBASE", "AUTH", "SIGN_IN_WITH_EMAIL_AND_PASSWORD_MUTATION"];
4
5
  export declare const SIGN_IN_WITH_REDIRECT_MUTATION_KEY: readonly ["FIREBASE", "AUTH", "SIGN_IN_WITH_REDIRECT_MUTATION"];
5
6
  export declare const SIGN_OUT_MUTATION_KEY: readonly ["FIREBASE", "AUTH", "SIGN_OUT"];
@@ -4,6 +4,7 @@ export const CREATE_USER_WITH_EMAIL_AND_PASSWORD_MUTATION_KEY = [
4
4
  "CREATE_USER_WITH_EMAIL_AND_PASSWORD_MUTATION"
5
5
  ];
6
6
  export const SEND_EMAIL_VERIFICATION_MUTATION_KEY = ["FIREBASE", "AUTH", "SEND_EMAIL_VERIFICATION_MUTATION"];
7
+ export const SIGN_IN_ANONYMOUSLY_MUTATION_KEY = ["FIREBASE", "AUTH", "SIGN_IN_ANONYMOUSLY_MUTATION"];
7
8
  export const SIGN_IN_WITH_EMAIL_AND_PASSWORD_MUTATION_KEY = [
8
9
  "FIREBASE",
9
10
  "AUTH",
@@ -4,6 +4,7 @@ export const CREATE_USER_WITH_EMAIL_AND_PASSWORD_MUTATION_KEY = [
4
4
  "CREATE_USER_WITH_EMAIL_AND_PASSWORD_MUTATION"
5
5
  ] as const;
6
6
  export const SEND_EMAIL_VERIFICATION_MUTATION_KEY = ["FIREBASE", "AUTH", "SEND_EMAIL_VERIFICATION_MUTATION"] as const;
7
+ export const SIGN_IN_ANONYMOUSLY_MUTATION_KEY = ["FIREBASE", "AUTH", "SIGN_IN_ANONYMOUSLY_MUTATION"] as const;
7
8
  export const SIGN_IN_WITH_EMAIL_AND_PASSWORD_MUTATION_KEY = [
8
9
  "FIREBASE",
9
10
  "AUTH",
@@ -0,0 +1,36 @@
1
+ import { UseMutationOptions } from "@tanstack/react-query";
2
+ import { FirebaseAuthTypes } from "@react-native-firebase/auth";
3
+ import { ReactNativeFirebase } from "@react-native-firebase/app";
4
+ /**
5
+ * @inline
6
+ */
7
+ export type UseSignInAnonymouslyMutationOptions = {
8
+ /**
9
+ * Options for useMutation hook excluding mutationFn. MutationKey will be equal to reference.path by default.
10
+ */
11
+ options?: Omit<UseMutationOptions<FirebaseAuthTypes.UserCredential, ReactNativeFirebase.NativeFirebaseError, void>, "mutationFn">;
12
+ };
13
+ /**
14
+ * Executes a mutation and returns users credentials
15
+ *
16
+ * @group Hook
17
+ *
18
+ * @param {useSignInAnonymouslyMutation} options - Configuration options for the mutation.
19
+ *
20
+ * @returns {UseMutationResult<AppModelType, Error, UseAddDocMutationValues<AppModelType>, TContext>} A mutation result
21
+ *
22
+ * @example
23
+ * ```jsx
24
+ * export const MyComponent = () => {
25
+ * const {mutate} = useSignInAnonymouslyMutation({
26
+ * options: {
27
+ * },
28
+ * });
29
+ *
30
+ * // ....
31
+ * mutate();
32
+ * // ....
33
+ * };
34
+ * ```
35
+ */
36
+ export declare const useSignInAnonymouslyMutation: ({ options }?: UseSignInAnonymouslyMutationOptions) => import("@tanstack/react-query").UseMutationResult<FirebaseAuthTypes.UserCredential, ReactNativeFirebase.NativeFirebaseError, void, unknown>;
@@ -0,0 +1,35 @@
1
+ import { useMutation } from "@tanstack/react-query";
2
+ import { signInAnonymously } from "@react-native-firebase/auth";
3
+ import { useAuth } from "./useAuth";
4
+ import { SIGN_IN_ANONYMOUSLY_MUTATION_KEY } from "./mutation-keys";
5
+ /**
6
+ * Executes a mutation and returns users credentials
7
+ *
8
+ * @group Hook
9
+ *
10
+ * @param {useSignInAnonymouslyMutation} options - Configuration options for the mutation.
11
+ *
12
+ * @returns {UseMutationResult<AppModelType, Error, UseAddDocMutationValues<AppModelType>, TContext>} A mutation result
13
+ *
14
+ * @example
15
+ * ```jsx
16
+ * export const MyComponent = () => {
17
+ * const {mutate} = useSignInAnonymouslyMutation({
18
+ * options: {
19
+ * },
20
+ * });
21
+ *
22
+ * // ....
23
+ * mutate();
24
+ * // ....
25
+ * };
26
+ * ```
27
+ */
28
+ export const useSignInAnonymouslyMutation = ({ options = {} } = {}) => {
29
+ const firebaseAuth = useAuth();
30
+ return useMutation({
31
+ ...options,
32
+ mutationKey: SIGN_IN_ANONYMOUSLY_MUTATION_KEY,
33
+ mutationFn: async () => signInAnonymously(firebaseAuth)
34
+ });
35
+ };
@@ -0,0 +1,52 @@
1
+ import { useMutation, UseMutationOptions } from "@tanstack/react-query";
2
+ import { signInAnonymously, FirebaseAuthTypes } from "@react-native-firebase/auth";
3
+
4
+ import { useAuth } from "./useAuth";
5
+ import { SIGN_IN_ANONYMOUSLY_MUTATION_KEY } from "./mutation-keys";
6
+ import { ReactNativeFirebase } from "@react-native-firebase/app";
7
+
8
+ /**
9
+ * @inline
10
+ */
11
+ export type UseSignInAnonymouslyMutationOptions = {
12
+ /**
13
+ * Options for useMutation hook excluding mutationFn. MutationKey will be equal to reference.path by default.
14
+ */
15
+ options?: Omit<
16
+ UseMutationOptions<FirebaseAuthTypes.UserCredential, ReactNativeFirebase.NativeFirebaseError, void>,
17
+ "mutationFn"
18
+ >;
19
+ };
20
+
21
+ /**
22
+ * Executes a mutation and returns users credentials
23
+ *
24
+ * @group Hook
25
+ *
26
+ * @param {useSignInAnonymouslyMutation} options - Configuration options for the mutation.
27
+ *
28
+ * @returns {UseMutationResult<AppModelType, Error, UseAddDocMutationValues<AppModelType>, TContext>} A mutation result
29
+ *
30
+ * @example
31
+ * ```jsx
32
+ * export const MyComponent = () => {
33
+ * const {mutate} = useSignInAnonymouslyMutation({
34
+ * options: {
35
+ * },
36
+ * });
37
+ *
38
+ * // ....
39
+ * mutate();
40
+ * // ....
41
+ * };
42
+ * ```
43
+ */
44
+ export const useSignInAnonymouslyMutation = ({ options = {} }: UseSignInAnonymouslyMutationOptions = {}) => {
45
+ const firebaseAuth = useAuth();
46
+
47
+ return useMutation({
48
+ ...options,
49
+ mutationKey: SIGN_IN_ANONYMOUSLY_MUTATION_KEY,
50
+ mutationFn: async () => signInAnonymously(firebaseAuth)
51
+ });
52
+ };
@@ -20,3 +20,4 @@ export * from "./utils/getDocSnap";
20
20
  export * from "./utils/getDocRef";
21
21
  export * from "./utils/buildQueryConstraint";
22
22
  export * from "./utils/buildCompositeFilter";
23
+ export * from "./useEnsureDoc";
@@ -20,3 +20,4 @@ export * from "./utils/getDocSnap";
20
20
  export * from "./utils/getDocRef";
21
21
  export * from "./utils/buildQueryConstraint";
22
22
  export * from "./utils/buildCompositeFilter";
23
+ export * from "./useEnsureDoc";
@@ -20,3 +20,4 @@ export * from "./utils/getDocSnap";
20
20
  export * from "./utils/getDocRef";
21
21
  export * from "./utils/buildQueryConstraint";
22
22
  export * from "./utils/buildCompositeFilter";
23
+ export * from "./useEnsureDoc";
@@ -0,0 +1,40 @@
1
+ import { UseQueryOptions } from "@tanstack/react-query";
2
+ import { AppModel } from "../../types";
3
+ import { GetDocDataOptions } from "./utils/getDocData";
4
+ /**
5
+ * @inline
6
+ */
7
+ export type UseEnsureDocOptions<AppModelType extends AppModel = AppModel> = {
8
+ /**
9
+ * Reference to a document that must be written
10
+ */
11
+ defaults: AppModelType;
12
+ /**
13
+ * Options for useMutation hook excluding mutationFn.
14
+ */
15
+ options: Omit<UseQueryOptions<AppModelType, Error, AppModelType>, "queryFn"> & Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
16
+ } & Omit<GetDocDataOptions<AppModelType>, "db">;
17
+ /**
18
+ * This hook checks if a doc with a requested reference exists.
19
+ * It creates a document with requested data if it does not exist.
20
+ *
21
+ * @group Hook
22
+ *
23
+ * @param {UseEnsureDocOptions<AppModelType>} options - Configuration options for mutation.
24
+ *
25
+ * @returns {UseQueryResult<AppModelType, Error>} A mutation result
26
+ *
27
+ * @example
28
+ * ```jsx
29
+ * export const MyComponent = () => {
30
+ * const {data} = useEnsureDocQuery({
31
+ * options: {
32
+ * },
33
+ * reference: collection().doc(),
34
+ * defaults: {prop1: 'value1'}
35
+ * });
36
+ *
37
+ * };
38
+ * ```
39
+ */
40
+ export declare const useEnsureDoc: <AppModelType extends AppModel = AppModel>({ reference, path, pathSegments, defaults, options }: UseEnsureDocOptions<AppModelType>) => import("@tanstack/react-query").UseQueryResult<AppModelType, Error>;
@@ -0,0 +1,47 @@
1
+ import { useQuery } from "@tanstack/react-query";
2
+ import { getDoc, setDoc } from "@react-native-firebase/firestore";
3
+ import { getDocRef } from "./utils/getDocRef";
4
+ import { getDocSnap } from "./utils/getDocSnap";
5
+ import { useFirestore } from "./useFirestore";
6
+ /**
7
+ * This hook checks if a doc with a requested reference exists.
8
+ * It creates a document with requested data if it does not exist.
9
+ *
10
+ * @group Hook
11
+ *
12
+ * @param {UseEnsureDocOptions<AppModelType>} options - Configuration options for mutation.
13
+ *
14
+ * @returns {UseQueryResult<AppModelType, Error>} A mutation result
15
+ *
16
+ * @example
17
+ * ```jsx
18
+ * export const MyComponent = () => {
19
+ * const {data} = useEnsureDocQuery({
20
+ * options: {
21
+ * },
22
+ * reference: collection().doc(),
23
+ * defaults: {prop1: 'value1'}
24
+ * });
25
+ *
26
+ * };
27
+ * ```
28
+ */
29
+ export const useEnsureDoc = ({ reference, path, pathSegments, defaults, options }) => {
30
+ const db = useFirestore();
31
+ return useQuery({
32
+ ...options,
33
+ queryFn: async () => {
34
+ const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
35
+ if (existingDocSnap?.exists) {
36
+ return { ...existingDocSnap.data(), uid: existingDocSnap.id };
37
+ }
38
+ const docRef = getDocRef({ db, reference, path, pathSegments });
39
+ if (!docRef) {
40
+ throw new Error(`Cannot fetch document reference using data: ${reference?.path}, ${path}, ${pathSegments?.join("/")}`);
41
+ }
42
+ await setDoc(docRef, defaults);
43
+ const docSnap = await getDoc(docRef);
44
+ return { ...docSnap.data(), uid: docSnap.id };
45
+ }
46
+ });
47
+ };
@@ -0,0 +1,79 @@
1
+ import { useQuery, UseQueryOptions } from "@tanstack/react-query";
2
+ import { getDoc, setDoc } from "@react-native-firebase/firestore";
3
+
4
+ import { AppModel } from "../../types";
5
+ import { GetDocDataOptions } from "./utils/getDocData";
6
+ import { getDocRef } from "./utils/getDocRef";
7
+ import { getDocSnap } from "./utils/getDocSnap";
8
+ import { useFirestore } from "./useFirestore";
9
+
10
+ /**
11
+ * @inline
12
+ */
13
+ export type UseEnsureDocOptions<AppModelType extends AppModel = AppModel> = {
14
+ /**
15
+ * Reference to a document that must be written
16
+ */
17
+ defaults: AppModelType;
18
+
19
+ /**
20
+ * Options for useMutation hook excluding mutationFn.
21
+ */
22
+ options: Omit<UseQueryOptions<AppModelType, Error, AppModelType>, "queryFn"> &
23
+ Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
24
+ } & Omit<GetDocDataOptions<AppModelType>, "db">;
25
+
26
+ /**
27
+ * This hook checks if a doc with a requested reference exists.
28
+ * It creates a document with requested data if it does not exist.
29
+ *
30
+ * @group Hook
31
+ *
32
+ * @param {UseEnsureDocOptions<AppModelType>} options - Configuration options for mutation.
33
+ *
34
+ * @returns {UseQueryResult<AppModelType, Error>} A mutation result
35
+ *
36
+ * @example
37
+ * ```jsx
38
+ * export const MyComponent = () => {
39
+ * const {data} = useEnsureDocQuery({
40
+ * options: {
41
+ * },
42
+ * reference: collection().doc(),
43
+ * defaults: {prop1: 'value1'}
44
+ * });
45
+ *
46
+ * };
47
+ * ```
48
+ */
49
+ export const useEnsureDoc = <AppModelType extends AppModel = AppModel>({
50
+ reference,
51
+ path,
52
+ pathSegments,
53
+ defaults,
54
+ options
55
+ }: UseEnsureDocOptions<AppModelType>) => {
56
+ const db = useFirestore();
57
+
58
+ return useQuery({
59
+ ...options,
60
+ queryFn: async () => {
61
+ const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
62
+
63
+ if (existingDocSnap?.exists) {
64
+ return { ...(existingDocSnap.data() as AppModelType), uid: existingDocSnap.id };
65
+ }
66
+
67
+ const docRef = getDocRef({ db, reference, path, pathSegments });
68
+ if (!docRef) {
69
+ throw new Error(
70
+ `Cannot fetch document reference using data: ${reference?.path}, ${path}, ${pathSegments?.join("/")}`
71
+ );
72
+ }
73
+
74
+ await setDoc<AppModelType>(docRef, defaults);
75
+ const docSnap = await getDoc(docRef);
76
+ return { ...(docSnap.data() as AppModelType), uid: docSnap.id };
77
+ }
78
+ });
79
+ };
@@ -11,3 +11,4 @@ export * from "./useSignOutMutation";
11
11
  export * from "./useIdToken";
12
12
  export * from "./useReauthenticateWitCredentialMutation";
13
13
  export * from "./useReauthenticateWitRedirectMutation";
14
+ export * from "./useSignInAnonymouslyMutation";
package/web/auth/index.js CHANGED
@@ -11,3 +11,4 @@ export * from "./useSignOutMutation";
11
11
  export * from "./useIdToken";
12
12
  export * from "./useReauthenticateWitCredentialMutation";
13
13
  export * from "./useReauthenticateWitRedirectMutation";
14
+ export * from "./useSignInAnonymouslyMutation";
package/web/auth/index.ts CHANGED
@@ -11,3 +11,4 @@ export * from "./useSignOutMutation";
11
11
  export * from "./useIdToken";
12
12
  export * from "./useReauthenticateWitCredentialMutation";
13
13
  export * from "./useReauthenticateWitRedirectMutation";
14
+ export * from "./useSignInAnonymouslyMutation";
@@ -1,5 +1,6 @@
1
1
  export declare const CREATE_USER_WITH_EMAIL_AND_PASSWORD_MUTATION_KEY: readonly ["FIREBASE", "AUTH", "CREATE_USER_WITH_EMAIL_AND_PASSWORD_MUTATION"];
2
2
  export declare const SEND_EMAIL_VERIFICATION_MUTATION_KEY: readonly ["FIREBASE", "AUTH", "SEND_EMAIL_VERIFICATION_MUTATION"];
3
+ export declare const SIGN_IN_ANONYMOUSLY_MUTATION_KEY: readonly ["FIREBASE", "AUTH", "SIGN_IN_ANONYMOUSLY_MUTATION"];
3
4
  export declare const SIGN_IN_WITH_EMAIL_AND_PASSWORD_MUTATION_KEY: readonly ["FIREBASE", "AUTH", "SIGN_IN_WITH_EMAIL_AND_PASSWORD_MUTATION"];
4
5
  export declare const SIGN_IN_WITH_REDIRECT_MUTATION_KEY: readonly ["FIREBASE", "AUTH", "SIGN_IN_WITH_REDIRECT_MUTATION"];
5
6
  export declare const SIGN_OUT_MUTATION_KEY: readonly ["FIREBASE", "AUTH", "SIGN_OUT"];
@@ -4,6 +4,7 @@ export const CREATE_USER_WITH_EMAIL_AND_PASSWORD_MUTATION_KEY = [
4
4
  "CREATE_USER_WITH_EMAIL_AND_PASSWORD_MUTATION"
5
5
  ];
6
6
  export const SEND_EMAIL_VERIFICATION_MUTATION_KEY = ["FIREBASE", "AUTH", "SEND_EMAIL_VERIFICATION_MUTATION"];
7
+ export const SIGN_IN_ANONYMOUSLY_MUTATION_KEY = ["FIREBASE", "AUTH", "SIGN_IN_ANONYMOUSLY_MUTATION"];
7
8
  export const SIGN_IN_WITH_EMAIL_AND_PASSWORD_MUTATION_KEY = [
8
9
  "FIREBASE",
9
10
  "AUTH",
@@ -4,6 +4,7 @@ export const CREATE_USER_WITH_EMAIL_AND_PASSWORD_MUTATION_KEY = [
4
4
  "CREATE_USER_WITH_EMAIL_AND_PASSWORD_MUTATION"
5
5
  ] as const;
6
6
  export const SEND_EMAIL_VERIFICATION_MUTATION_KEY = ["FIREBASE", "AUTH", "SEND_EMAIL_VERIFICATION_MUTATION"] as const;
7
+ export const SIGN_IN_ANONYMOUSLY_MUTATION_KEY = ["FIREBASE", "AUTH", "SIGN_IN_ANONYMOUSLY_MUTATION"] as const;
7
8
  export const SIGN_IN_WITH_EMAIL_AND_PASSWORD_MUTATION_KEY = [
8
9
  "FIREBASE",
9
10
  "AUTH",
@@ -0,0 +1,36 @@
1
+ import { UseMutationOptions } from "@tanstack/react-query";
2
+ import { UserCredential } from "firebase/auth";
3
+ import { ReactNativeFirebase } from "@react-native-firebase/app";
4
+ /**
5
+ * @inline
6
+ */
7
+ export type UseSignInAnonymouslyMutationOptions = {
8
+ /**
9
+ * Options for useMutation hook excluding mutationFn. MutationKey will be equal to reference.path by default.
10
+ */
11
+ options?: Omit<UseMutationOptions<UserCredential, ReactNativeFirebase.NativeFirebaseError, void>, "mutationFn">;
12
+ };
13
+ /**
14
+ * Executes a mutation and returns users credentials
15
+ *
16
+ * @group Hook
17
+ *
18
+ * @param {useSignInAnonymouslyMutation} options - Configuration options for the mutation.
19
+ *
20
+ * @returns {UseMutationResult<AppModelType, Error, UseAddDocMutationValues<AppModelType>, TContext>} A mutation result
21
+ *
22
+ * @example
23
+ * ```jsx
24
+ * export const MyComponent = () => {
25
+ * const {mutate} = useSignInAnonymouslyMutation({
26
+ * options: {
27
+ * },
28
+ * });
29
+ *
30
+ * // ....
31
+ * mutate();
32
+ * // ....
33
+ * };
34
+ * ```
35
+ */
36
+ export declare const useSignInAnonymouslyMutation: ({ options }?: UseSignInAnonymouslyMutationOptions) => import("@tanstack/react-query").UseMutationResult<UserCredential, ReactNativeFirebase.NativeFirebaseError, void, unknown>;
@@ -0,0 +1,35 @@
1
+ import { useMutation } from "@tanstack/react-query";
2
+ import { signInAnonymously } from "firebase/auth";
3
+ import { useAuth } from "./useAuth";
4
+ import { SIGN_IN_ANONYMOUSLY_MUTATION_KEY } from "./mutation-keys";
5
+ /**
6
+ * Executes a mutation and returns users credentials
7
+ *
8
+ * @group Hook
9
+ *
10
+ * @param {useSignInAnonymouslyMutation} options - Configuration options for the mutation.
11
+ *
12
+ * @returns {UseMutationResult<AppModelType, Error, UseAddDocMutationValues<AppModelType>, TContext>} A mutation result
13
+ *
14
+ * @example
15
+ * ```jsx
16
+ * export const MyComponent = () => {
17
+ * const {mutate} = useSignInAnonymouslyMutation({
18
+ * options: {
19
+ * },
20
+ * });
21
+ *
22
+ * // ....
23
+ * mutate();
24
+ * // ....
25
+ * };
26
+ * ```
27
+ */
28
+ export const useSignInAnonymouslyMutation = ({ options = {} } = {}) => {
29
+ const firebaseAuth = useAuth();
30
+ return useMutation({
31
+ ...options,
32
+ mutationKey: SIGN_IN_ANONYMOUSLY_MUTATION_KEY,
33
+ mutationFn: async () => signInAnonymously(firebaseAuth)
34
+ });
35
+ };
@@ -0,0 +1,49 @@
1
+ import { useMutation, UseMutationOptions } from "@tanstack/react-query";
2
+ import { signInAnonymously, UserCredential } from "firebase/auth";
3
+
4
+ import { useAuth } from "./useAuth";
5
+ import { SIGN_IN_ANONYMOUSLY_MUTATION_KEY } from "./mutation-keys";
6
+ import { ReactNativeFirebase } from "@react-native-firebase/app";
7
+
8
+ /**
9
+ * @inline
10
+ */
11
+ export type UseSignInAnonymouslyMutationOptions = {
12
+ /**
13
+ * Options for useMutation hook excluding mutationFn. MutationKey will be equal to reference.path by default.
14
+ */
15
+ options?: Omit<UseMutationOptions<UserCredential, ReactNativeFirebase.NativeFirebaseError, void>, "mutationFn">;
16
+ };
17
+
18
+ /**
19
+ * Executes a mutation and returns users credentials
20
+ *
21
+ * @group Hook
22
+ *
23
+ * @param {useSignInAnonymouslyMutation} options - Configuration options for the mutation.
24
+ *
25
+ * @returns {UseMutationResult<AppModelType, Error, UseAddDocMutationValues<AppModelType>, TContext>} A mutation result
26
+ *
27
+ * @example
28
+ * ```jsx
29
+ * export const MyComponent = () => {
30
+ * const {mutate} = useSignInAnonymouslyMutation({
31
+ * options: {
32
+ * },
33
+ * });
34
+ *
35
+ * // ....
36
+ * mutate();
37
+ * // ....
38
+ * };
39
+ * ```
40
+ */
41
+ export const useSignInAnonymouslyMutation = ({ options = {} }: UseSignInAnonymouslyMutationOptions = {}) => {
42
+ const firebaseAuth = useAuth();
43
+
44
+ return useMutation({
45
+ ...options,
46
+ mutationKey: SIGN_IN_ANONYMOUSLY_MUTATION_KEY,
47
+ mutationFn: async () => signInAnonymously(firebaseAuth)
48
+ });
49
+ };
@@ -15,6 +15,7 @@ export * from "./useSetDocMutation";
15
15
  export * from "./useUpdateDocMutation";
16
16
  export * from "./useGetRealtimeDocData";
17
17
  export * from "./useQueryConstraints";
18
+ export * from "./useEnsureDoc";
18
19
  export * from "./utils/getDocData";
19
20
  export * from "./utils/getDocSnap";
20
21
  export * from "./utils/getDocRef";
@@ -15,6 +15,7 @@ export * from "./useSetDocMutation";
15
15
  export * from "./useUpdateDocMutation";
16
16
  export * from "./useGetRealtimeDocData";
17
17
  export * from "./useQueryConstraints";
18
+ export * from "./useEnsureDoc";
18
19
  export * from "./utils/getDocData";
19
20
  export * from "./utils/getDocSnap";
20
21
  export * from "./utils/getDocRef";
@@ -15,6 +15,7 @@ export * from "./useSetDocMutation";
15
15
  export * from "./useUpdateDocMutation";
16
16
  export * from "./useGetRealtimeDocData";
17
17
  export * from "./useQueryConstraints";
18
+ export * from "./useEnsureDoc";
18
19
  export * from "./utils/getDocData";
19
20
  export * from "./utils/getDocSnap";
20
21
  export * from "./utils/getDocRef";
@@ -0,0 +1,40 @@
1
+ import { UseQueryOptions } from "@tanstack/react-query";
2
+ import { AppModel } from "../../types";
3
+ import { GetDocDataOptions } from "./utils/getDocData";
4
+ /**
5
+ * @inline
6
+ */
7
+ export type UseEnsureDocOptions<AppModelType extends AppModel = AppModel> = {
8
+ /**
9
+ * Reference to a document that must be written
10
+ */
11
+ defaults: AppModelType;
12
+ /**
13
+ * Options for useMutation hook excluding mutationFn.
14
+ */
15
+ options: Omit<UseQueryOptions<AppModelType, Error, AppModelType>, "queryFn"> & Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
16
+ } & Omit<GetDocDataOptions<AppModelType>, "db">;
17
+ /**
18
+ * This hook checks if a doc with a requested reference exists.
19
+ * It creates a document with requested data if it does not exist.
20
+ *
21
+ * @group Hook
22
+ *
23
+ * @param {UseEnsureDocOptions<AppModelType>} options - Configuration options for mutation.
24
+ *
25
+ * @returns {UseQueryResult<AppModelType, Error>} A mutation result
26
+ *
27
+ * @example
28
+ * ```jsx
29
+ * export const MyComponent = () => {
30
+ * const {data} = useEnsureDocQuery({
31
+ * options: {
32
+ * },
33
+ * reference: collection().doc(),
34
+ * defaults: {prop1: 'value1'}
35
+ * });
36
+ *
37
+ * };
38
+ * ```
39
+ */
40
+ export declare const useEnsureDoc: <AppModelType extends AppModel = AppModel>({ reference, path, pathSegments, defaults, options }: UseEnsureDocOptions<AppModelType>) => import("@tanstack/react-query").UseQueryResult<AppModelType, Error>;
@@ -0,0 +1,47 @@
1
+ import { useQuery } from "@tanstack/react-query";
2
+ import { getDoc, setDoc } from "firebase/firestore";
3
+ import { getDocRef } from "./utils/getDocRef";
4
+ import { getDocSnap } from "./utils/getDocSnap";
5
+ import { useFirestore } from "./useFirestore";
6
+ /**
7
+ * This hook checks if a doc with a requested reference exists.
8
+ * It creates a document with requested data if it does not exist.
9
+ *
10
+ * @group Hook
11
+ *
12
+ * @param {UseEnsureDocOptions<AppModelType>} options - Configuration options for mutation.
13
+ *
14
+ * @returns {UseQueryResult<AppModelType, Error>} A mutation result
15
+ *
16
+ * @example
17
+ * ```jsx
18
+ * export const MyComponent = () => {
19
+ * const {data} = useEnsureDocQuery({
20
+ * options: {
21
+ * },
22
+ * reference: collection().doc(),
23
+ * defaults: {prop1: 'value1'}
24
+ * });
25
+ *
26
+ * };
27
+ * ```
28
+ */
29
+ export const useEnsureDoc = ({ reference, path, pathSegments, defaults, options }) => {
30
+ const db = useFirestore();
31
+ return useQuery({
32
+ ...options,
33
+ queryFn: async () => {
34
+ const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
35
+ if (existingDocSnap?.exists) {
36
+ return { ...existingDocSnap.data(), uid: existingDocSnap.id };
37
+ }
38
+ const docRef = getDocRef({ db, reference, path, pathSegments });
39
+ if (!docRef) {
40
+ throw new Error(`Cannot fetch document reference using data: ${reference?.path}, ${path}, ${pathSegments?.join("/")}`);
41
+ }
42
+ await setDoc(docRef, defaults);
43
+ const docSnap = await getDoc(docRef);
44
+ return { ...docSnap.data(), uid: docSnap.id };
45
+ }
46
+ });
47
+ };
@@ -0,0 +1,79 @@
1
+ import { useQuery, UseQueryOptions } from "@tanstack/react-query";
2
+ import { getDoc, setDoc } from "firebase/firestore";
3
+
4
+ import { AppModel } from "../../types";
5
+ import { GetDocDataOptions } from "./utils/getDocData";
6
+ import { getDocRef } from "./utils/getDocRef";
7
+ import { getDocSnap } from "./utils/getDocSnap";
8
+ import { useFirestore } from "./useFirestore";
9
+
10
+ /**
11
+ * @inline
12
+ */
13
+ export type UseEnsureDocOptions<AppModelType extends AppModel = AppModel> = {
14
+ /**
15
+ * Reference to a document that must be written
16
+ */
17
+ defaults: AppModelType;
18
+
19
+ /**
20
+ * Options for useMutation hook excluding mutationFn.
21
+ */
22
+ options: Omit<UseQueryOptions<AppModelType, Error, AppModelType>, "queryFn"> &
23
+ Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
24
+ } & Omit<GetDocDataOptions<AppModelType>, "db">;
25
+
26
+ /**
27
+ * This hook checks if a doc with a requested reference exists.
28
+ * It creates a document with requested data if it does not exist.
29
+ *
30
+ * @group Hook
31
+ *
32
+ * @param {UseEnsureDocOptions<AppModelType>} options - Configuration options for mutation.
33
+ *
34
+ * @returns {UseQueryResult<AppModelType, Error>} A mutation result
35
+ *
36
+ * @example
37
+ * ```jsx
38
+ * export const MyComponent = () => {
39
+ * const {data} = useEnsureDocQuery({
40
+ * options: {
41
+ * },
42
+ * reference: collection().doc(),
43
+ * defaults: {prop1: 'value1'}
44
+ * });
45
+ *
46
+ * };
47
+ * ```
48
+ */
49
+ export const useEnsureDoc = <AppModelType extends AppModel = AppModel>({
50
+ reference,
51
+ path,
52
+ pathSegments,
53
+ defaults,
54
+ options
55
+ }: UseEnsureDocOptions<AppModelType>) => {
56
+ const db = useFirestore();
57
+
58
+ return useQuery({
59
+ ...options,
60
+ queryFn: async () => {
61
+ const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
62
+
63
+ if (existingDocSnap?.exists) {
64
+ return { ...(existingDocSnap.data() as AppModelType), uid: existingDocSnap.id };
65
+ }
66
+
67
+ const docRef = getDocRef({ db, reference, path, pathSegments });
68
+ if (!docRef) {
69
+ throw new Error(
70
+ `Cannot fetch document reference using data: ${reference?.path}, ${path}, ${pathSegments?.join("/")}`
71
+ );
72
+ }
73
+
74
+ await setDoc<AppModelType, AppModelType>(docRef, defaults);
75
+ const docSnap = await getDoc(docRef);
76
+ return { ...(docSnap.data() as AppModelType), uid: docSnap.id };
77
+ }
78
+ });
79
+ };