@umituz/react-native-subscription 3.1.8 → 3.1.10
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 +1 -1
- package/src/domains/credits/application/CreditsInitializer.ts +3 -2
- package/src/domains/credits/application/DeductCreditsCommand.ts +4 -3
- package/src/domains/credits/application/PurchaseMetadataGenerator.ts +1 -1
- package/src/domains/credits/application/RefundCreditsCommand.ts +4 -3
- package/src/domains/credits/application/creditDocumentHelpers.ts +2 -1
- package/src/domains/credits/infrastructure/CreditsRepository.ts +4 -3
- package/src/domains/credits/infrastructure/operations/CreditsFetcher.ts +1 -2
- package/src/domains/credits/infrastructure/operations/CreditsInitializer.ts +2 -1
- package/src/domains/credits/infrastructure/operations/CreditsWriter.ts +6 -5
- package/src/domains/credits/presentation/useCreditsRealTime.ts +21 -68
- package/src/domains/subscription/presentation/flowInitialState.ts +22 -0
- package/src/domains/subscription/presentation/flowTypes.ts +106 -0
- package/src/domains/subscription/presentation/usePremiumActions.ts +5 -6
- package/src/domains/subscription/presentation/useSubscriptionFlow.ts +25 -92
- package/src/domains/wallet/presentation/hooks/useTransactionHistory.ts +34 -60
- package/src/shared/infrastructure/firestore/collectionUtils.ts +2 -7
- package/src/shared/presentation/hooks/useFirestoreRealTime.ts +214 -0
- package/src/shared/presentation/types/hookState.types.ts +97 -0
- package/src/shared/utils/dateConverter.ts +1 -1
- package/src/shared/utils/errorUtils.ts +195 -0
- package/src/shared/utils/logger.ts +140 -0
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useMemo } from "react";
|
|
2
2
|
import { useAuthStore, selectUserId } from "@umituz/react-native-auth";
|
|
3
|
-
import { collection,
|
|
3
|
+
import { collection, query, orderBy, limit } from "firebase/firestore";
|
|
4
4
|
import type {
|
|
5
5
|
CreditLog,
|
|
6
6
|
TransactionRepositoryConfig,
|
|
7
7
|
} from "../../domain/types/transaction.types";
|
|
8
8
|
import { requireFirestore } from "../../../../shared/infrastructure/firestore/collectionUtils";
|
|
9
|
+
import { useFirestoreCollectionRealTime } from "../../../../shared/presentation/hooks/useFirestoreRealTime";
|
|
9
10
|
|
|
10
11
|
export interface UseTransactionHistoryParams {
|
|
11
12
|
config: TransactionRepositoryConfig;
|
|
@@ -20,78 +21,51 @@ interface UseTransactionHistoryResult {
|
|
|
20
21
|
isEmpty: boolean;
|
|
21
22
|
}
|
|
22
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Mapper to convert Firestore document to CreditLog entity.
|
|
26
|
+
*/
|
|
27
|
+
function mapTransactionLog(doc: any, docId: string): CreditLog {
|
|
28
|
+
return {
|
|
29
|
+
id: docId,
|
|
30
|
+
...doc,
|
|
31
|
+
} as CreditLog;
|
|
32
|
+
}
|
|
33
|
+
|
|
23
34
|
export function useTransactionHistory({
|
|
24
35
|
config,
|
|
25
36
|
limit: limitCount = 50,
|
|
26
37
|
}: UseTransactionHistoryParams): UseTransactionHistoryResult {
|
|
27
38
|
const userId = useAuthStore(selectUserId);
|
|
28
|
-
const [transactions, setTransactions] = useState<CreditLog[]>([]);
|
|
29
|
-
const [isLoading, setIsLoading] = useState(true);
|
|
30
|
-
const [error, setError] = useState<Error | null>(null);
|
|
31
|
-
|
|
32
|
-
useEffect(() => {
|
|
33
|
-
if (!userId) {
|
|
34
|
-
setTransactions([]);
|
|
35
|
-
setIsLoading(false);
|
|
36
|
-
setError(null);
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
// Build collection query
|
|
41
|
+
const queryRef = useMemo(() => {
|
|
42
|
+
if (!userId) return null;
|
|
42
43
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
: config.collectionName;
|
|
44
|
+
const db = requireFirestore();
|
|
45
|
+
const collectionPath = config.useUserSubcollection
|
|
46
|
+
? `users/${userId}/${config.collectionName}`
|
|
47
|
+
: config.collectionName;
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const unsubscribe = onSnapshot(
|
|
56
|
-
q,
|
|
57
|
-
(snapshot) => {
|
|
58
|
-
const logs: CreditLog[] = [];
|
|
59
|
-
snapshot.forEach((doc) => {
|
|
60
|
-
logs.push({
|
|
61
|
-
id: doc.id,
|
|
62
|
-
...doc.data(),
|
|
63
|
-
} as CreditLog);
|
|
64
|
-
});
|
|
65
|
-
setTransactions(logs);
|
|
66
|
-
setIsLoading(false);
|
|
67
|
-
},
|
|
68
|
-
(err) => {
|
|
69
|
-
console.error("[useTransactionHistory] Snapshot error:", err);
|
|
70
|
-
setError(err as Error);
|
|
71
|
-
setIsLoading(false);
|
|
72
|
-
}
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
return () => unsubscribe();
|
|
76
|
-
} catch (err) {
|
|
77
|
-
const error = err instanceof Error ? err : new Error(String(err));
|
|
78
|
-
console.error("[useTransactionHistory] Setup error:", err);
|
|
79
|
-
setError(error);
|
|
80
|
-
setIsLoading(false);
|
|
81
|
-
}
|
|
49
|
+
return query(
|
|
50
|
+
collection(db, collectionPath),
|
|
51
|
+
orderBy("timestamp", "desc"),
|
|
52
|
+
limit(limitCount)
|
|
53
|
+
);
|
|
82
54
|
}, [userId, config.collectionName, config.useUserSubcollection, limitCount]);
|
|
83
55
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
56
|
+
// Use generic real-time sync hook
|
|
57
|
+
const { data, isLoading, error, refetch, isEmpty } = useFirestoreCollectionRealTime(
|
|
58
|
+
userId,
|
|
59
|
+
queryRef,
|
|
60
|
+
mapTransactionLog,
|
|
61
|
+
"useTransactionHistory"
|
|
62
|
+
);
|
|
89
63
|
|
|
90
64
|
return {
|
|
91
|
-
transactions,
|
|
65
|
+
transactions: data,
|
|
92
66
|
isLoading,
|
|
93
67
|
error,
|
|
94
68
|
refetch,
|
|
95
|
-
isEmpty
|
|
69
|
+
isEmpty,
|
|
96
70
|
};
|
|
97
71
|
}
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import { collection, doc } from "firebase/firestore";
|
|
2
|
-
import {
|
|
3
|
-
getFirestore,
|
|
4
|
-
type CollectionReference,
|
|
5
|
-
type DocumentReference,
|
|
6
|
-
type Firestore,
|
|
7
|
-
} from "@umituz/react-native-firebase";
|
|
1
|
+
import { collection, doc, type CollectionReference, type DocumentReference } from "firebase/firestore";
|
|
2
|
+
import { getFirestore, type Firestore } from "@umituz/react-native-firebase";
|
|
8
3
|
|
|
9
4
|
export interface CollectionConfig {
|
|
10
5
|
collectionName: string;
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic real-time sync hook for Firestore.
|
|
3
|
+
*
|
|
4
|
+
* Eliminates 90% duplication from real-time hooks:
|
|
5
|
+
* - useCreditsRealTime: 116 → ~35 lines
|
|
6
|
+
* - useTransactionHistory: 98 → ~30 lines
|
|
7
|
+
*
|
|
8
|
+
* Features:
|
|
9
|
+
* - Generic type support for any document/collection
|
|
10
|
+
* - Automatic cleanup with unsubscribe
|
|
11
|
+
* - Consistent error handling
|
|
12
|
+
* - Loading state management
|
|
13
|
+
* - Type-safe mapper function
|
|
14
|
+
* - Support for both document and collection queries
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { useEffect, useState, useCallback } from "react";
|
|
18
|
+
import {
|
|
19
|
+
onSnapshot,
|
|
20
|
+
type Query,
|
|
21
|
+
type DocumentReference,
|
|
22
|
+
} from "firebase/firestore";
|
|
23
|
+
import type { HookState, HookStateWithEmpty } from "../types/hookState.types";
|
|
24
|
+
import { logError, logWarn } from "../../utils/logger";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Configuration for building a Firestore document reference.
|
|
28
|
+
*/
|
|
29
|
+
export interface DocumentConfig {
|
|
30
|
+
/** Collection name */
|
|
31
|
+
collectionName: string;
|
|
32
|
+
|
|
33
|
+
/** Whether to use a user subcollection (users/{userId}/{collectionName}) */
|
|
34
|
+
useUserSubcollection: boolean;
|
|
35
|
+
|
|
36
|
+
/** Document ID (fixed string or function that takes userId) */
|
|
37
|
+
docId: string | ((userId: string) => string);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Configuration for building a Firestore collection query.
|
|
42
|
+
*/
|
|
43
|
+
export interface CollectionConfig {
|
|
44
|
+
/** Collection name */
|
|
45
|
+
collectionName: string;
|
|
46
|
+
|
|
47
|
+
/** Whether to use a user subcollection (users/{userId}/{collectionName}) */
|
|
48
|
+
useUserSubcollection: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Query builder for collection queries.
|
|
53
|
+
* Takes a Firestore collection reference and returns a Query with constraints.
|
|
54
|
+
*/
|
|
55
|
+
export type QueryBuilder<T> = (collection: any) => Query<T>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Mapper function to convert Firestore document data to domain entity.
|
|
59
|
+
*/
|
|
60
|
+
export type Mapper<TDocument, TEntity> = (
|
|
61
|
+
doc: TDocument,
|
|
62
|
+
docId: string
|
|
63
|
+
) => TEntity;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Generic hook for real-time document sync via Firestore onSnapshot.
|
|
67
|
+
*
|
|
68
|
+
* @template TDocument - Firestore document type
|
|
69
|
+
* @template TEntity - Domain entity type
|
|
70
|
+
*
|
|
71
|
+
* @param userId - User ID to fetch document for
|
|
72
|
+
* @param docRef - Document reference from Firestore
|
|
73
|
+
* @param mapper - Function to map document data to entity
|
|
74
|
+
* @param tag - Logging tag for debugging
|
|
75
|
+
*
|
|
76
|
+
* @returns Hook state with data, loading, error, and refetch
|
|
77
|
+
*/
|
|
78
|
+
export function useFirestoreDocumentRealTime<TDocument, TEntity>(
|
|
79
|
+
userId: string | null | undefined,
|
|
80
|
+
docRef: DocumentReference<TDocument> | null,
|
|
81
|
+
mapper: Mapper<TDocument, TEntity>,
|
|
82
|
+
tag: string
|
|
83
|
+
): HookState<TEntity> {
|
|
84
|
+
const [data, setData] = useState<TEntity | null>(null);
|
|
85
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
86
|
+
const [error, setError] = useState<Error | null>(null);
|
|
87
|
+
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
// Reset state when userId changes
|
|
90
|
+
if (!userId) {
|
|
91
|
+
setData(null);
|
|
92
|
+
setIsLoading(false);
|
|
93
|
+
setError(null);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
setIsLoading(true);
|
|
98
|
+
setError(null);
|
|
99
|
+
|
|
100
|
+
if (!docRef) {
|
|
101
|
+
setIsLoading(false);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const unsubscribe = onSnapshot(
|
|
106
|
+
docRef,
|
|
107
|
+
(snapshot) => {
|
|
108
|
+
if (snapshot.exists()) {
|
|
109
|
+
const entity = mapper(snapshot.data() as TDocument, snapshot.id);
|
|
110
|
+
setData(entity);
|
|
111
|
+
} else {
|
|
112
|
+
setData(null);
|
|
113
|
+
}
|
|
114
|
+
setIsLoading(false);
|
|
115
|
+
},
|
|
116
|
+
(err: Error) => {
|
|
117
|
+
logError(tag, "Snapshot error", err, { userId });
|
|
118
|
+
setError(err);
|
|
119
|
+
setIsLoading(false);
|
|
120
|
+
}
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
return () => {
|
|
124
|
+
unsubscribe();
|
|
125
|
+
};
|
|
126
|
+
}, [userId, docRef, mapper, tag]);
|
|
127
|
+
|
|
128
|
+
const refetch = useCallback(() => {
|
|
129
|
+
// Real-time sync doesn't need refetch, but keep for API compatibility
|
|
130
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
131
|
+
logWarn(tag, "Refetch called - not needed for real-time sync");
|
|
132
|
+
}
|
|
133
|
+
}, [tag]);
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
data,
|
|
137
|
+
isLoading,
|
|
138
|
+
error,
|
|
139
|
+
refetch,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Generic hook for real-time collection sync via Firestore onSnapshot.
|
|
145
|
+
*
|
|
146
|
+
* @template TDocument - Firestore document type
|
|
147
|
+
* @template TEntity - Domain entity type
|
|
148
|
+
*
|
|
149
|
+
* @param userId - User ID to fetch collection for
|
|
150
|
+
* @param query - Firestore query to listen to
|
|
151
|
+
* @param mapper - Function to map document data to entity
|
|
152
|
+
* @param tag - Logging tag for debugging
|
|
153
|
+
*
|
|
154
|
+
* @returns Hook state with array data, loading, error, refetch, and isEmpty
|
|
155
|
+
*/
|
|
156
|
+
export function useFirestoreCollectionRealTime<TDocument, TEntity>(
|
|
157
|
+
userId: string | null | undefined,
|
|
158
|
+
query: Query<TDocument>,
|
|
159
|
+
mapper: Mapper<TDocument, TEntity>,
|
|
160
|
+
tag: string
|
|
161
|
+
): HookStateWithEmpty<TEntity> {
|
|
162
|
+
const [data, setData] = useState<TEntity[]>([]);
|
|
163
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
164
|
+
const [error, setError] = useState<Error | null>(null);
|
|
165
|
+
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
// Reset state when userId changes
|
|
168
|
+
if (!userId) {
|
|
169
|
+
setData([]);
|
|
170
|
+
setIsLoading(false);
|
|
171
|
+
setError(null);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
setIsLoading(true);
|
|
176
|
+
setError(null);
|
|
177
|
+
|
|
178
|
+
const unsubscribe = onSnapshot(
|
|
179
|
+
query,
|
|
180
|
+
(snapshot) => {
|
|
181
|
+
const entities: TEntity[] = [];
|
|
182
|
+
snapshot.forEach((doc) => {
|
|
183
|
+
entities.push(mapper(doc.data() as TDocument, doc.id));
|
|
184
|
+
});
|
|
185
|
+
setData(entities);
|
|
186
|
+
setIsLoading(false);
|
|
187
|
+
},
|
|
188
|
+
(err: Error) => {
|
|
189
|
+
logError(tag, "Snapshot error", err, { userId });
|
|
190
|
+
setError(err);
|
|
191
|
+
setIsLoading(false);
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
return () => {
|
|
196
|
+
unsubscribe();
|
|
197
|
+
};
|
|
198
|
+
}, [userId, query, mapper, tag]);
|
|
199
|
+
|
|
200
|
+
const refetch = useCallback(() => {
|
|
201
|
+
// Real-time sync doesn't need refetch, but keep for API compatibility
|
|
202
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
203
|
+
logWarn(tag, "Refetch called - not needed for real-time sync");
|
|
204
|
+
}
|
|
205
|
+
}, [tag]);
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
data,
|
|
209
|
+
isLoading,
|
|
210
|
+
error,
|
|
211
|
+
refetch,
|
|
212
|
+
isEmpty: data.length === 0,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standardized hook state types for consistent return values across all hooks.
|
|
3
|
+
*
|
|
4
|
+
* Benefits:
|
|
5
|
+
* - Type consistency across 25+ hooks
|
|
6
|
+
* - Easier to understand and maintain
|
|
7
|
+
* - Better IDE autocomplete
|
|
8
|
+
* - Predictable API surface
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Standard hook state returned by data-fetching hooks.
|
|
13
|
+
* Provides consistent interface for loading, error, and data states.
|
|
14
|
+
*
|
|
15
|
+
* @template T - The type of data returned by the hook
|
|
16
|
+
*/
|
|
17
|
+
export interface HookState<T> {
|
|
18
|
+
/** The fetched data, or null if not yet loaded or on error */
|
|
19
|
+
data: T | null;
|
|
20
|
+
|
|
21
|
+
/** Whether the hook is currently fetching data */
|
|
22
|
+
isLoading: boolean;
|
|
23
|
+
|
|
24
|
+
/** Any error that occurred during fetching, or null if no error */
|
|
25
|
+
error: Error | null;
|
|
26
|
+
|
|
27
|
+
/** Function to manually refetch data (no-op for real-time sync hooks) */
|
|
28
|
+
refetch: () => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Extended hook state that includes isEmpty for collection queries.
|
|
33
|
+
* Useful when you need to distinguish between "no data" and "loading".
|
|
34
|
+
*
|
|
35
|
+
* @template T - The type of data in the array (typically a document type)
|
|
36
|
+
*/
|
|
37
|
+
export interface HookStateWithEmpty<T> extends HookState<T[]> {
|
|
38
|
+
/** Whether the data array is empty (only meaningful when isLoading is false) */
|
|
39
|
+
isEmpty: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Extended hook state that includes metadata.
|
|
44
|
+
* Useful for hooks that need to return additional computed values.
|
|
45
|
+
*
|
|
46
|
+
* @template T - The type of data returned by the hook
|
|
47
|
+
* @template M - The type of metadata
|
|
48
|
+
*/
|
|
49
|
+
export interface HookStateWithMeta<T, M> extends HookState<T> {
|
|
50
|
+
/** Additional computed or derived metadata */
|
|
51
|
+
meta: M;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Factory function to create a standard HookState.
|
|
56
|
+
* Useful for testing or when you need to construct state objects.
|
|
57
|
+
*/
|
|
58
|
+
export function createHookState<T>(
|
|
59
|
+
data: T | null,
|
|
60
|
+
isLoading: boolean,
|
|
61
|
+
error: Error | null,
|
|
62
|
+
refetch: () => void = () => {}
|
|
63
|
+
): HookState<T> {
|
|
64
|
+
return { data, isLoading, error, refetch };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Factory function to create a HookStateWithEmpty.
|
|
69
|
+
* Automatically computes isEmpty from the data array.
|
|
70
|
+
*/
|
|
71
|
+
export function createHookStateWithEmpty<T>(
|
|
72
|
+
data: T[] | null,
|
|
73
|
+
isLoading: boolean,
|
|
74
|
+
error: Error | null,
|
|
75
|
+
refetch: () => void = () => {}
|
|
76
|
+
): HookStateWithEmpty<T> {
|
|
77
|
+
return {
|
|
78
|
+
data,
|
|
79
|
+
isLoading,
|
|
80
|
+
error,
|
|
81
|
+
refetch,
|
|
82
|
+
isEmpty: data === null ? false : data.length === 0,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Factory function to create a HookStateWithMeta.
|
|
88
|
+
*/
|
|
89
|
+
export function createHookStateWithMeta<T, M>(
|
|
90
|
+
data: T | null,
|
|
91
|
+
isLoading: boolean,
|
|
92
|
+
error: Error | null,
|
|
93
|
+
meta: M,
|
|
94
|
+
refetch: () => void = () => {}
|
|
95
|
+
): HookStateWithMeta<T, M> {
|
|
96
|
+
return { data, isLoading, error, refetch, meta };
|
|
97
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized error handling utilities.
|
|
3
|
+
*
|
|
4
|
+
* Benefits:
|
|
5
|
+
* - Removes 45+ duplicated error handling blocks
|
|
6
|
+
* - Consistent error logging everywhere
|
|
7
|
+
* - Easier debugging with better error context
|
|
8
|
+
* - Type-safe error handling
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { logError } from "./logger";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Safely convert unknown error to Error object.
|
|
15
|
+
* Useful when catching errors from external APIs.
|
|
16
|
+
*/
|
|
17
|
+
export function toError(error: unknown): Error {
|
|
18
|
+
if (error instanceof Error) {
|
|
19
|
+
return error;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (typeof error === "string") {
|
|
23
|
+
return new Error(error);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (error === null || error === undefined) {
|
|
27
|
+
return new Error("Unknown error occurred");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
return new Error(JSON.stringify(error));
|
|
32
|
+
} catch {
|
|
33
|
+
return new Error(String(error));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Create an error with a code and optional cause.
|
|
39
|
+
* Wraps the BaseError pattern for convenience.
|
|
40
|
+
*/
|
|
41
|
+
export function createError(
|
|
42
|
+
message: string,
|
|
43
|
+
code: string,
|
|
44
|
+
cause?: Error
|
|
45
|
+
): Error {
|
|
46
|
+
const error = new Error(message);
|
|
47
|
+
error.name = code;
|
|
48
|
+
|
|
49
|
+
if (cause) {
|
|
50
|
+
// @ts-ignore - adding cause property
|
|
51
|
+
error.cause = cause;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return error;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Log an error with consistent formatting.
|
|
59
|
+
* Only logs in __DEV__ mode.
|
|
60
|
+
*/
|
|
61
|
+
export function logAndReturnError(
|
|
62
|
+
tag: string,
|
|
63
|
+
message: string,
|
|
64
|
+
error: unknown,
|
|
65
|
+
context?: Record<string, unknown>
|
|
66
|
+
): Error {
|
|
67
|
+
const normalizedError = toError(error);
|
|
68
|
+
|
|
69
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
70
|
+
logError(tag, message, normalizedError, {
|
|
71
|
+
...context,
|
|
72
|
+
originalError: error,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return normalizedError;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Type guard to check if value is an Error.
|
|
81
|
+
*/
|
|
82
|
+
export function isError(value: unknown): value is Error {
|
|
83
|
+
return value instanceof Error;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Type guard to check if error has a code property.
|
|
88
|
+
*/
|
|
89
|
+
export function isErrorWithCode(error: Error): error is Error & { code: string } {
|
|
90
|
+
return "code" in error && typeof error.code === "string";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Type guard to check if error has a cause property.
|
|
95
|
+
*/
|
|
96
|
+
export function isErrorWithCause(error: Error): error is Error & { cause: Error } {
|
|
97
|
+
return "cause" in error && error.cause instanceof Error;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Wrap an async function with error handling.
|
|
102
|
+
* Returns a Result type with success/error states.
|
|
103
|
+
*/
|
|
104
|
+
export async function tryAsync<T>(
|
|
105
|
+
fn: () => Promise<T>,
|
|
106
|
+
context: { tag: string; operation: string }
|
|
107
|
+
): Promise<{ success: true; data: T } | { success: false; error: Error }> {
|
|
108
|
+
try {
|
|
109
|
+
const data = await fn();
|
|
110
|
+
return { success: true, data };
|
|
111
|
+
} catch (error) {
|
|
112
|
+
const normalizedError = logAndReturnError(
|
|
113
|
+
context.tag,
|
|
114
|
+
`Failed to ${context.operation}`,
|
|
115
|
+
error
|
|
116
|
+
);
|
|
117
|
+
return { success: false, error: normalizedError };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Wrap a synchronous function with error handling.
|
|
123
|
+
* Returns a Result type with success/error states.
|
|
124
|
+
*/
|
|
125
|
+
export function trySync<T>(
|
|
126
|
+
fn: () => T,
|
|
127
|
+
context: { tag: string; operation: string }
|
|
128
|
+
): { success: true; data: T } | { success: false; error: Error } {
|
|
129
|
+
try {
|
|
130
|
+
const data = fn();
|
|
131
|
+
return { success: true, data };
|
|
132
|
+
} catch (error) {
|
|
133
|
+
const normalizedError = logAndReturnError(
|
|
134
|
+
context.tag,
|
|
135
|
+
`Failed to ${context.operation}`,
|
|
136
|
+
error
|
|
137
|
+
);
|
|
138
|
+
return { success: false, error: normalizedError };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Assert that a condition is true, throw error otherwise.
|
|
144
|
+
* Useful for validation and runtime checks.
|
|
145
|
+
*/
|
|
146
|
+
export function assert(
|
|
147
|
+
condition: boolean,
|
|
148
|
+
message: string,
|
|
149
|
+
code: string = "ASSERTION_ERROR"
|
|
150
|
+
): asserts condition {
|
|
151
|
+
if (!condition) {
|
|
152
|
+
throw createError(message, code);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Assert that a value is not null/undefined.
|
|
158
|
+
* Throws error if value is null/undefined, returns value otherwise.
|
|
159
|
+
* Useful for type narrowing.
|
|
160
|
+
*/
|
|
161
|
+
export function assertNotNil<T>(
|
|
162
|
+
value: T | null | undefined,
|
|
163
|
+
message: string = "Value should not be null or undefined"
|
|
164
|
+
): T {
|
|
165
|
+
assert(value !== null && value !== undefined, message, "NOT_NIL_ERROR");
|
|
166
|
+
return value;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Create a Firestore-specific error with consistent formatting.
|
|
171
|
+
*/
|
|
172
|
+
export function createFirestoreError(
|
|
173
|
+
operation: string,
|
|
174
|
+
error: unknown
|
|
175
|
+
): Error {
|
|
176
|
+
return createError(
|
|
177
|
+
`Firestore ${operation} failed`,
|
|
178
|
+
"FIRESTORE_ERROR",
|
|
179
|
+
toError(error)
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Create a RevenueCat-specific error with consistent formatting.
|
|
185
|
+
*/
|
|
186
|
+
export function createRevenueCatError(
|
|
187
|
+
operation: string,
|
|
188
|
+
error: unknown
|
|
189
|
+
): Error {
|
|
190
|
+
return createError(
|
|
191
|
+
`RevenueCat ${operation} failed`,
|
|
192
|
+
"REVENUECAT_ERROR",
|
|
193
|
+
toError(error)
|
|
194
|
+
);
|
|
195
|
+
}
|