@explorins/pers-sdk-react-native 2.1.2 → 2.1.5

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 (38) hide show
  1. package/README.md +7 -7
  2. package/dist/hooks/index.d.ts +6 -0
  3. package/dist/hooks/index.d.ts.map +1 -1
  4. package/dist/hooks/index.js +1 -0
  5. package/dist/hooks/useAnalytics.d.ts +37 -14
  6. package/dist/hooks/useAnalytics.d.ts.map +1 -1
  7. package/dist/hooks/useAnalytics.js +239 -19
  8. package/dist/hooks/useCampaigns.d.ts +14 -6
  9. package/dist/hooks/useCampaigns.d.ts.map +1 -1
  10. package/dist/hooks/useCampaigns.js +144 -10
  11. package/dist/hooks/useRedemptions.d.ts +5 -2
  12. package/dist/hooks/useRedemptions.d.ts.map +1 -1
  13. package/dist/hooks/useRedemptions.js +53 -2
  14. package/dist/hooks/useTokenBalances.d.ts.map +1 -1
  15. package/dist/hooks/useTokenBalances.js +21 -8
  16. package/dist/hooks/useTransactions.d.ts +8 -5
  17. package/dist/hooks/useTransactions.d.ts.map +1 -1
  18. package/dist/hooks/useTransactions.js +70 -27
  19. package/dist/hooks/useTriggerSources.d.ts +76 -0
  20. package/dist/hooks/useTriggerSources.d.ts.map +1 -0
  21. package/dist/hooks/useTriggerSources.js +272 -0
  22. package/dist/index.d.ts +12 -3
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +2742 -495
  25. package/dist/index.js.map +1 -1
  26. package/dist/providers/PersSDKProvider.d.ts +1 -12
  27. package/dist/providers/PersSDKProvider.d.ts.map +1 -1
  28. package/dist/providers/PersSDKProvider.js +50 -25
  29. package/package.json +2 -2
  30. package/src/hooks/index.ts +17 -1
  31. package/src/hooks/useAnalytics.ts +268 -21
  32. package/src/hooks/useCampaigns.ts +176 -14
  33. package/src/hooks/useRedemptions.ts +66 -3
  34. package/src/hooks/useTokenBalances.ts +23 -9
  35. package/src/hooks/useTransactions.ts +84 -29
  36. package/src/hooks/useTriggerSources.ts +301 -0
  37. package/src/index.ts +33 -3
  38. package/src/providers/PersSDKProvider.tsx +58 -39
@@ -0,0 +1,301 @@
1
+ import { useCallback } from 'react';
2
+ import { usePersSDK } from '../providers/PersSDKProvider';
3
+ import type {
4
+ TriggerSourceDTO,
5
+ TriggerSourceCreateRequestDTO,
6
+ PaginatedResponseDTO
7
+ } from '@explorins/pers-shared';
8
+ import type { TriggerSourceQueryOptions } from '@explorins/pers-sdk/trigger-source';
9
+
10
+ // Re-export for consumers
11
+ export type { TriggerSourceQueryOptions } from '@explorins/pers-sdk/trigger-source';
12
+
13
+ /**
14
+ * React hook for TriggerSource operations in the PERS SDK
15
+ *
16
+ * Manages trigger sources which are physical or digital activation points for campaigns:
17
+ * - **QR_CODE**: Scannable QR codes at physical locations
18
+ * - **NFC_TAG**: NFC tap points for contactless interactions
19
+ * - **GPS_GEOFENCE**: Location-based triggers with radius detection
20
+ * - **API_WEBHOOK**: External system integration triggers
21
+ * - **TRANSACTION**: Purchase/payment based triggers
22
+ *
23
+ * TriggerSources are standalone entities that can be created, managed, and then
24
+ * assigned to campaigns. This separation allows reuse across multiple campaigns.
25
+ *
26
+ * **Admin Only**: All create, update, and delete operations require admin authentication.
27
+ *
28
+ * @returns TriggerSource hook with CRUD operations
29
+ *
30
+ * @example Basic TriggerSource Operations
31
+ * ```typescript
32
+ * function TriggerSourceManager() {
33
+ * const {
34
+ * getAll,
35
+ * getById,
36
+ * create,
37
+ * update,
38
+ * remove
39
+ * } = useTriggerSources();
40
+ *
41
+ * // List all QR code trigger sources
42
+ * const loadQRSources = async () => {
43
+ * const { data: sources } = await getAll({ type: 'QR_CODE' });
44
+ * console.log('QR Sources:', sources);
45
+ * };
46
+ *
47
+ * // Create a new QR code for a store
48
+ * const createStoreQR = async () => {
49
+ * const source = await create({
50
+ * type: 'QR_CODE',
51
+ * name: 'Store Entrance QR',
52
+ * description: 'Scan at the entrance',
53
+ * businessId: 'business-123',
54
+ * coordsLatitude: 47.6062,
55
+ * coordsLongitude: -122.3321
56
+ * });
57
+ * console.log('Created:', source.id);
58
+ * };
59
+ * }
60
+ * ```
61
+ *
62
+ * @example GPS Geofence Trigger
63
+ * ```typescript
64
+ * const { create } = useTriggerSources();
65
+ *
66
+ * // Create a GPS geofence around a location
67
+ * const geofence = await create({
68
+ * type: 'GPS_GEOFENCE',
69
+ * name: 'Downtown Area',
70
+ * coordsLatitude: 47.6062,
71
+ * coordsLongitude: -122.3321,
72
+ * metadata: { radiusMeters: 100 }
73
+ * });
74
+ * ```
75
+ */
76
+ export const useTriggerSources = () => {
77
+ const { sdk, isInitialized, isAuthenticated } = usePersSDK();
78
+
79
+ /**
80
+ * Get trigger sources with optional filters and pagination
81
+ *
82
+ * Retrieves trigger sources (QR codes, NFC tags, GPS geofences, webhooks, etc.)
83
+ * that can be used to activate campaigns. Supports filtering by type, business,
84
+ * campaign association, and active status.
85
+ *
86
+ * @param options - Filter and pagination options
87
+ * @returns Promise resolving to paginated trigger sources
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * // Get all QR code trigger sources
92
+ * const { data: qrSources } = await getAll({ type: 'QR_CODE' });
93
+ *
94
+ * // Get trigger sources for a specific business
95
+ * const { data: businessSources, pagination } = await getAll({
96
+ * businessId: 'business-123',
97
+ * active: true,
98
+ * page: 1,
99
+ * limit: 20
100
+ * });
101
+ *
102
+ * // Get trigger sources assigned to a campaign
103
+ * const { data: campaignSources } = await getAll({
104
+ * campaignId: 'campaign-456'
105
+ * });
106
+ * ```
107
+ */
108
+ const getAll = useCallback(async (
109
+ options?: TriggerSourceQueryOptions
110
+ ): Promise<PaginatedResponseDTO<TriggerSourceDTO>> => {
111
+ if (!isInitialized || !sdk) {
112
+ throw new Error('SDK not initialized. Call initialize() first.');
113
+ }
114
+
115
+ try {
116
+ const result = await sdk.triggerSources.getAll(options);
117
+ return result;
118
+ } catch (error) {
119
+ console.error('Failed to fetch trigger sources:', error);
120
+ throw error;
121
+ }
122
+ }, [sdk, isInitialized]);
123
+
124
+ /**
125
+ * Get trigger source by ID
126
+ *
127
+ * Retrieves detailed information for a specific trigger source including
128
+ * its type, location, metadata, and configuration.
129
+ *
130
+ * @param triggerSourceId - UUID of the trigger source
131
+ * @returns Promise resolving to trigger source details
132
+ * @throws {PersApiError} When trigger source with specified ID is not found
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * const source = await getById('source-123');
137
+ *
138
+ * console.log('Trigger Source:', source.name);
139
+ * console.log('Type:', source.type);
140
+ * console.log('Active:', source.isActive);
141
+ *
142
+ * if (source.coordsLatitude && source.coordsLongitude) {
143
+ * console.log('Location:', source.coordsLatitude, source.coordsLongitude);
144
+ * }
145
+ * ```
146
+ */
147
+ const getById = useCallback(async (
148
+ triggerSourceId: string
149
+ ): Promise<TriggerSourceDTO> => {
150
+ if (!isInitialized || !sdk) {
151
+ throw new Error('SDK not initialized. Call initialize() first.');
152
+ }
153
+
154
+ try {
155
+ const result = await sdk.triggerSources.getById(triggerSourceId);
156
+ return result;
157
+ } catch (error) {
158
+ console.error('Failed to fetch trigger source:', error);
159
+ throw error;
160
+ }
161
+ }, [sdk, isInitialized]);
162
+
163
+ /**
164
+ * Admin: Create a new trigger source
165
+ *
166
+ * Creates a new trigger source (QR code, NFC tag, GPS geofence, webhook, or
167
+ * transaction-based trigger) that can be assigned to campaigns. Requires
168
+ * administrator privileges.
169
+ *
170
+ * @param triggerSource - Trigger source creation data
171
+ * @returns Promise resolving to created trigger source
172
+ * @throws {PersApiError} When not authenticated as admin or validation fails
173
+ *
174
+ * @example
175
+ * ```typescript
176
+ * // Create a QR code trigger source
177
+ * const qrSource = await create({
178
+ * type: 'QR_CODE',
179
+ * name: 'Store Entrance QR',
180
+ * description: 'QR code at main entrance',
181
+ * businessId: 'business-123',
182
+ * coordsLatitude: 47.6062,
183
+ * coordsLongitude: -122.3321
184
+ * });
185
+ *
186
+ * // Create an NFC tag trigger source
187
+ * const nfcSource = await create({
188
+ * type: 'NFC_TAG',
189
+ * name: 'Product Display NFC',
190
+ * metadata: { productId: 'SKU-12345' }
191
+ * });
192
+ * ```
193
+ */
194
+ const create = useCallback(async (
195
+ triggerSource: TriggerSourceCreateRequestDTO
196
+ ): Promise<TriggerSourceDTO> => {
197
+ if (!isInitialized || !sdk) {
198
+ throw new Error('SDK not initialized. Call initialize() first.');
199
+ }
200
+ if (!isAuthenticated) {
201
+ throw new Error('SDK not authenticated. create requires admin authentication.');
202
+ }
203
+
204
+ try {
205
+ const result = await sdk.triggerSources.create(triggerSource);
206
+ return result;
207
+ } catch (error) {
208
+ console.error('Failed to create trigger source:', error);
209
+ throw error;
210
+ }
211
+ }, [sdk, isInitialized, isAuthenticated]);
212
+
213
+ /**
214
+ * Admin: Update a trigger source
215
+ *
216
+ * Updates an existing trigger source's configuration. All fields are optional;
217
+ * only provided fields will be updated. Requires administrator privileges.
218
+ *
219
+ * @param triggerSourceId - UUID of the trigger source to update
220
+ * @param triggerSource - Updated trigger source data (partial)
221
+ * @returns Promise resolving to updated trigger source
222
+ * @throws {PersApiError} When not authenticated as admin or trigger source not found
223
+ *
224
+ * @example
225
+ * ```typescript
226
+ * // Update trigger source name and location
227
+ * const updated = await update('source-123', {
228
+ * name: 'Updated Store Entrance QR',
229
+ * description: 'New description',
230
+ * coordsLatitude: 47.6063,
231
+ * coordsLongitude: -122.3322
232
+ * });
233
+ * ```
234
+ */
235
+ const update = useCallback(async (
236
+ triggerSourceId: string,
237
+ triggerSource: Partial<TriggerSourceCreateRequestDTO>
238
+ ): Promise<TriggerSourceDTO> => {
239
+ if (!isInitialized || !sdk) {
240
+ throw new Error('SDK not initialized. Call initialize() first.');
241
+ }
242
+ if (!isAuthenticated) {
243
+ throw new Error('SDK not authenticated. update requires admin authentication.');
244
+ }
245
+
246
+ try {
247
+ const result = await sdk.triggerSources.update(triggerSourceId, triggerSource);
248
+ return result;
249
+ } catch (error) {
250
+ console.error('Failed to update trigger source:', error);
251
+ throw error;
252
+ }
253
+ }, [sdk, isInitialized, isAuthenticated]);
254
+
255
+ /**
256
+ * Admin: Delete a trigger source
257
+ *
258
+ * Soft deletes a trigger source, making it inactive. The trigger source will
259
+ * be removed from any campaigns it was assigned to. Requires administrator
260
+ * privileges.
261
+ *
262
+ * @param triggerSourceId - UUID of the trigger source to delete
263
+ * @returns Promise resolving to success status
264
+ * @throws {PersApiError} When not authenticated as admin or trigger source not found
265
+ *
266
+ * @example
267
+ * ```typescript
268
+ * const success = await remove('source-123');
269
+ * console.log('Trigger source deleted:', success);
270
+ * ```
271
+ */
272
+ const remove = useCallback(async (
273
+ triggerSourceId: string
274
+ ): Promise<boolean> => {
275
+ if (!isInitialized || !sdk) {
276
+ throw new Error('SDK not initialized. Call initialize() first.');
277
+ }
278
+ if (!isAuthenticated) {
279
+ throw new Error('SDK not authenticated. remove requires admin authentication.');
280
+ }
281
+
282
+ try {
283
+ const result = await sdk.triggerSources.delete(triggerSourceId);
284
+ return result;
285
+ } catch (error) {
286
+ console.error('Failed to delete trigger source:', error);
287
+ throw error;
288
+ }
289
+ }, [sdk, isInitialized, isAuthenticated]);
290
+
291
+ return {
292
+ getAll,
293
+ getById,
294
+ create,
295
+ update,
296
+ remove,
297
+ isAvailable: isInitialized && !!sdk?.triggerSources,
298
+ };
299
+ };
300
+
301
+ export type TriggerSourceHook = ReturnType<typeof useTriggerSources>;
package/src/index.ts CHANGED
@@ -262,11 +262,20 @@ export {
262
262
  useFiles,
263
263
  useAnalytics,
264
264
  useDonations,
265
- useEvents
265
+ useEvents,
266
+ useTriggerSources
266
267
  } from './hooks';
267
268
 
268
269
  // Re-export signing status types for convenience
269
- export type { OnStatusUpdateFn, StatusUpdateData, SigningStatusType, TransactionSigningResult, SubmissionResult, AuthenticatedUser } from './hooks';
270
+ export type {
271
+ TransactionSignerHook,
272
+ OnStatusUpdateFn,
273
+ StatusUpdateData,
274
+ SigningStatusType,
275
+ TransactionSigningResult,
276
+ SubmissionResult,
277
+ AuthenticatedUser
278
+ } from './hooks';
270
279
 
271
280
  // Re-export event types for convenience
272
281
  export type { EventsHook, PersEvent, EventHandler, EventFilter, Unsubscribe } from './hooks';
@@ -274,6 +283,21 @@ export type { EventsHook, PersEvent, EventHandler, EventFilter, Unsubscribe } fr
274
283
  // Re-export token balance types for convenience
275
284
  export type { TokenBalanceWithToken, UseTokenBalancesOptions, UseTokenBalancesResult } from './hooks';
276
285
 
286
+ // Re-export campaign types for convenience
287
+ export type { CampaignClaimFilters, CampaignHook } from './hooks';
288
+
289
+ // Re-export redemption types for convenience
290
+ export type { RedemptionRedeemFilters, RedemptionHook } from './hooks';
291
+
292
+ // Re-export transaction types for convenience
293
+ export type { TransactionQueryOptions, TransactionHook } from './hooks';
294
+
295
+ // Re-export trigger source types for convenience
296
+ export type { TriggerSourceQueryOptions, TriggerSourceHook } from './hooks';
297
+
298
+ // Re-export analytics types for convenience
299
+ export type { AnalyticsHook } from './hooks';
300
+
277
301
  // ==============================================================================
278
302
  // PLATFORM ADAPTERS
279
303
  // ==============================================================================
@@ -284,9 +308,15 @@ export type { TokenBalanceWithToken, UseTokenBalancesOptions, UseTokenBalancesRe
284
308
  * Provides platform-specific networking with proper error handling, timeout management,
285
309
  * and React Native compatibility. Automatically handles headers, authentication tokens,
286
310
  * and response parsing.
311
+ *
312
+ * @see {@link ReactNativeHttpClient} - The HTTP client implementation
313
+ * @see {@link HttpClient} - The HTTP client interface
314
+ * @see {@link RequestOptions} - Request configuration options
287
315
  */
288
316
  export {
289
- ReactNativeHttpClient
317
+ ReactNativeHttpClient,
318
+ type HttpClient,
319
+ type RequestOptions
290
320
  } from './providers/react-native-http-client';
291
321
 
292
322
  // ==============================================================================
@@ -28,19 +28,6 @@ export interface PersSDKContext {
28
28
  // Main SDK instance
29
29
  sdk: PersSDK | null;
30
30
 
31
- // Manager shortcuts for convenience
32
- auth: AuthManager | null;
33
- users: UserManager | null;
34
- tokens: TokenManager | null;
35
- businesses: BusinessManager | null;
36
- campaigns: CampaignManager | null;
37
- redemptions: RedemptionManager | null;
38
- transactions: TransactionManager | null;
39
- purchases: PurchaseManager | null;
40
- tenants: TenantManager | null;
41
- analytics: AnalyticsManager | null;
42
- donations: DonationManager | null;
43
-
44
31
  // Platform-specific providers
45
32
  authProvider: DefaultAuthProvider | null;
46
33
 
@@ -53,6 +40,7 @@ export interface PersSDKContext {
53
40
  initialize: (config: PersConfig) => Promise<void>;
54
41
  setAuthenticationState: (user: UserDTO | AdminDTO | null, isAuthenticated: boolean) => void;
55
42
  refreshUserData: () => Promise<void>;
43
+ restoreSession: () => Promise<UserDTO | null>;
56
44
  }
57
45
 
58
46
  // Create the context
@@ -64,6 +52,12 @@ export const PersSDKProvider: React.FC<{
64
52
  config?: PersConfig;
65
53
  }> = ({ children, config }) => {
66
54
  const initializingRef = useRef(false);
55
+
56
+ // State refs for stable functions to read current values
57
+ const sdkRef = useRef<PersSDK | null>(null);
58
+ const isInitializedRef = useRef(false);
59
+ const isAuthenticatedRef = useRef(false);
60
+
67
61
  const [sdk, setSdk] = useState<PersSDK | null>(null);
68
62
  const [authProvider, setAuthProvider] = useState<DefaultAuthProvider | null>(null);
69
63
 
@@ -71,6 +65,11 @@ export const PersSDKProvider: React.FC<{
71
65
  const [isAuthenticated, setIsAuthenticated] = useState(false);
72
66
  const [user, setUser] = useState<UserDTO | AdminDTO | null>(null);
73
67
 
68
+ // Keep state refs in sync immediately (not in useEffect to avoid race conditions)
69
+ sdkRef.current = sdk;
70
+ isInitializedRef.current = isInitialized;
71
+ isAuthenticatedRef.current = isAuthenticated;
72
+
74
73
  const initialize = useCallback(async (config: PersConfig) => {
75
74
  // Prevent multiple initializations
76
75
  if (isInitialized || initializingRef.current) {
@@ -147,18 +146,44 @@ export const PersSDKProvider: React.FC<{
147
146
  }, [config, isInitialized, initialize]);
148
147
 
149
148
  const refreshUserData = useCallback(async (): Promise<void> => {
150
- if (!sdk || !isAuthenticated || !isInitialized) {
151
- throw new Error('SDK not initialized or not authenticated. Cannot refresh user data.');
149
+ // Read from refs to get current values
150
+ const currentSdk = sdkRef.current;
151
+ const currentIsInitialized = isInitializedRef.current;
152
+
153
+ if (!currentSdk || !currentIsInitialized) {
154
+ throw new Error('SDK not initialized. Cannot refresh user data.');
152
155
  }
153
156
 
154
157
  try {
155
- const freshUserData = await sdk.users.getCurrentUser();
158
+ const freshUserData = await currentSdk.users.getCurrentUser();
156
159
  setUser(freshUserData);
157
160
  } catch (error) {
158
161
  console.error('Failed to refresh user data:', error);
159
162
  throw error;
160
163
  }
161
- }, [sdk, isAuthenticated, isInitialized]);
164
+ }, []); // No dependencies - reads from refs
165
+
166
+ const restoreSession = useCallback(async (): Promise<UserDTO | null> => {
167
+ // Read from refs to get current values
168
+ const currentSdk = sdkRef.current;
169
+ const currentIsInitialized = isInitializedRef.current;
170
+
171
+ if (!currentSdk || !currentIsInitialized) {
172
+ throw new Error('SDK not initialized. Call initialize() first.');
173
+ }
174
+
175
+ try {
176
+ const userData = await currentSdk.restoreSession();
177
+ if (userData) {
178
+ setAuthenticationState(userData, true);
179
+ }
180
+ return userData;
181
+ } catch (error) {
182
+ console.error('[PersSDK] Failed to restore session:', error);
183
+ throw error;
184
+ }
185
+ }, [setAuthenticationState]); // Depends on setAuthenticationState
186
+
162
187
  // Listen for authentication events from core SDK
163
188
  // Set up immediately when SDK is created (don't wait for isInitialized)
164
189
  // to catch session_restored events that fire during SDK initialization
@@ -171,13 +196,18 @@ export const PersSDKProvider: React.FC<{
171
196
  // Session restored successfully - sync React state
172
197
  if (event.type === 'session_restored') {
173
198
  console.log('[PersSDK] Session restoration event received, syncing state...');
174
- sdk.users.getCurrentUser()
175
- .then(userData => {
176
- setAuthenticationState(userData, true);
177
- })
178
- .catch(error => {
179
- console.error('[PersSDK] Failed to sync restored session:', error);
180
- });
199
+ // Read user from event details if available, otherwise fetch
200
+ const userId = event.details?.userId;
201
+ if (userId) {
202
+ // User ID available, fetch user data
203
+ sdk.users.getCurrentUser()
204
+ .then(userData => {
205
+ setAuthenticationState(userData, true);
206
+ })
207
+ .catch(error => {
208
+ console.error('[PersSDK] Failed to sync restored session:', error);
209
+ });
210
+ }
181
211
  }
182
212
 
183
213
  // Session restoration failed or auth error - clear React state
@@ -222,19 +252,6 @@ export const PersSDKProvider: React.FC<{
222
252
  // Main SDK instance
223
253
  sdk,
224
254
 
225
- // Manager shortcuts for convenience
226
- auth: sdk?.auth || null,
227
- users: sdk?.users || null,
228
- tokens: sdk?.tokens || null,
229
- businesses: sdk?.businesses || null,
230
- campaigns: sdk?.campaigns || null,
231
- redemptions: sdk?.redemptions || null,
232
- transactions: sdk?.transactions || null,
233
- purchases: sdk?.purchases || null,
234
- tenants: sdk?.tenants || null,
235
- analytics: sdk?.analytics || null,
236
- donations: sdk?.donations || null,
237
-
238
255
  // Platform-specific providers
239
256
  authProvider,
240
257
 
@@ -243,10 +260,11 @@ export const PersSDKProvider: React.FC<{
243
260
  isAuthenticated,
244
261
  user,
245
262
 
246
- // Methods
263
+ // Methods - expose functions directly, not through refs
247
264
  initialize,
248
265
  setAuthenticationState,
249
266
  refreshUserData,
267
+ restoreSession,
250
268
  }), [
251
269
  sdk,
252
270
  authProvider,
@@ -255,7 +273,8 @@ export const PersSDKProvider: React.FC<{
255
273
  user,
256
274
  initialize,
257
275
  setAuthenticationState,
258
- refreshUserData
276
+ refreshUserData,
277
+ restoreSession
259
278
  ]);
260
279
 
261
280
  return (