@altcraft/react-native-sdk 0.0.3 → 0.0.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.
package/src/Utilities.ts CHANGED
@@ -1,7 +1,70 @@
1
1
  import type { Subscription } from './NativeSdk';
2
2
 
3
- export class Utilities {
3
+ /** Supported action types for profile field manipulation. */
4
+ export type ActionFieldType =
5
+ | 'set'
6
+ | 'unset'
7
+ | 'incr'
8
+ | 'add'
9
+ | 'delete'
10
+ | 'upsert';
11
+
12
+ /**
13
+ * Builder for structured profile field actions.
14
+ *
15
+ * Produces a map entry compatible with the native SDK's ActionFieldBuilder.
16
+ * The resulting map is intended to be merged into `profileFields` before
17
+ * passing to `pushSubscribe`, `updateProfileFields`, or similar APIs.
18
+ */
19
+ export class ActionFieldBuilder {
20
+ private key: string;
21
+
22
+ constructor(key: string) {
23
+ this.key = key;
24
+ }
25
+
26
+ /** Set field to a specific value. */
27
+ set(value: unknown): Record<string, unknown> {
28
+ return { [this.key]: { action: 'set', value } };
29
+ }
30
+
31
+ /** Unset (remove) a field value. */
32
+ unset(value: unknown): Record<string, unknown> {
33
+ return { [this.key]: { action: 'unset', value } };
34
+ }
35
+
36
+ /** Increment a numeric field by the given value. */
37
+ incr(value: unknown): Record<string, unknown> {
38
+ return { [this.key]: { action: 'incr', value } };
39
+ }
40
+
41
+ /** Add a value to a set/array field. */
42
+ add(value: unknown): Record<string, unknown> {
43
+ return { [this.key]: { action: 'add', value } };
44
+ }
45
+
46
+ /** Delete a value from a set/array field. */
47
+ delete(value: unknown): Record<string, unknown> {
48
+ return { [this.key]: { action: 'delete', value } };
49
+ }
4
50
 
51
+ /** Upsert: set if missing, update if exists. */
52
+ upsert(value: unknown): Record<string, unknown> {
53
+ return { [this.key]: { action: 'upsert', value } };
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Creates an ActionFieldBuilder for the given field key.
59
+ *
60
+ * @param key Profile field key to operate on.
61
+ * @returns Builder instance with action methods.
62
+ */
63
+ export function actionField(key: string): ActionFieldBuilder {
64
+ return new ActionFieldBuilder(key);
65
+ }
66
+
67
+ export class Utilities {
5
68
  /** Converts a JS object to { [key: string]: string }
6
69
  * for the RN bridge; drops null/undefined. */
7
70
  static toNativeStringMap(
@@ -70,7 +133,8 @@ export class Utilities {
70
133
  };
71
134
 
72
135
  if (subscription.status != null) result.status = subscription.status;
73
- if (subscription.priority != null) result.priority = String(subscription.priority);
136
+ if (subscription.priority != null)
137
+ result.priority = String(subscription.priority);
74
138
 
75
139
  if (subscription.custom_fields != null) {
76
140
  try {
package/src/index.tsx CHANGED
@@ -14,7 +14,7 @@ import NativeSDK, {
14
14
  type SdkEvent,
15
15
  } from './NativeSdk';
16
16
 
17
- import { Utilities } from './Utilities';
17
+ import { Utilities, ActionFieldBuilder, actionField } from './Utilities';
18
18
 
19
19
  // ---------------- SDK API ----------------
20
20
 
@@ -182,7 +182,10 @@ export function getPushToken(): Promise<TokenData | null> {
182
182
  * @param token Token string or `null` to clear.
183
183
  * @returns Promise that resolves when the token is saved on native side.
184
184
  */
185
- export function setPushToken(provider: string, token: string | null): Promise<void> {
185
+ export function setPushToken(
186
+ provider: string,
187
+ token: string | null
188
+ ): Promise<void> {
186
189
  return NativeSDK.setPushToken(provider, token);
187
190
  }
188
191
 
@@ -197,7 +200,6 @@ export function clear(): Promise<void> {
197
200
  return NativeSDK.clear();
198
201
  }
199
202
 
200
-
201
203
  // ---------------- MobileEvent ----------------
202
204
 
203
205
  /**
@@ -233,7 +235,7 @@ export function mobileEvent(
233
235
  matchingType,
234
236
  Utilities.toNativeStringMap(profileFields),
235
237
  Utilities.subscriptionToNativeMap(subscription),
236
- utm
238
+ utm ?? { campaign: null, content: null, keyword: null, medium: null, source: null, temp: null }
237
239
  );
238
240
  }
239
241
 
@@ -244,54 +246,66 @@ const ALTCRAFT_SDK_EVENT_NAME = 'AltcraftSdkEvent' as const;
244
246
  const nativeEventEmitter = new NativeEventEmitter(
245
247
  NativeModules.SDKEventEmitter
246
248
  );
249
+
247
250
  let eventsSubscription: EventsSubscription = null;
251
+ const eventHandlers = new Set<(event: SdkEvent) => void>();
252
+
253
+ function ensureNativeSubscription(): void {
254
+ if (eventsSubscription === null) {
255
+ NativeSDK.subscribeToEvents();
256
+ eventsSubscription = nativeEventEmitter.addListener(
257
+ ALTCRAFT_SDK_EVENT_NAME,
258
+ (...args: unknown[]) => {
259
+ const event = (args[0] ?? null) as SdkEvent | null;
260
+ if (!event) return;
261
+
262
+ const handlers = Array.from(eventHandlers);
263
+ for (const handler of handlers) {
264
+ try {
265
+ handler(event);
266
+ } catch (e) {
267
+ console.warn('[AltcraftSDK] subscribeToEvents handler error:', e);
268
+ }
269
+ }
270
+ }
271
+ );
272
+ }
273
+ }
248
274
 
249
275
  /**
250
276
  * Subscribes to native SDK events and routes them to the provided handler.
251
277
  *
252
- * The SDK emits events via `NativeEventEmitter` with name {@link ALTCRAFT_SDK_EVENT_NAME}.
253
- * Calling this function multiple times replaces the previous JS listener.
278
+ * Multiple callers can subscribe independently — each handler receives every event.
279
+ * The native subscription is activated only once (first caller).
254
280
  *
255
281
  * @param handler Callback invoked for each incoming SDK event.
256
282
  * The event object is produced by native and follows {@link SdkEvent}.
257
283
  */
258
284
  export function subscribeToEvents(handler: (event: SdkEvent) => void): void {
259
- if (eventsSubscription) {
260
- eventsSubscription.remove();
261
- eventsSubscription = null;
262
- }
263
-
264
- // Enables native-side subscription (starts producing events).
265
- NativeSDK.subscribeToEvents();
266
-
267
- eventsSubscription = nativeEventEmitter.addListener(
268
- ALTCRAFT_SDK_EVENT_NAME,
269
- (...args: unknown[]) => {
270
- const event = (args[0] ?? null) as SdkEvent | null;
271
- if (!event) return;
272
-
273
- try {
274
- handler(event);
275
- } catch (e) {
276
- console.warn('[AltcraftSDK] subscribeToEvents handler error:', e);
277
- }
278
- }
279
- );
285
+ eventHandlers.add(handler);
286
+ ensureNativeSubscription();
280
287
  }
281
288
 
282
289
  /**
283
- * Unsubscribes from native SDK events.
290
+ * Unsubscribes a specific handler from native SDK events.
284
291
  *
285
- * Removes the JS listener and instructs native module to stop emitting events.
292
+ * When the last handler is removed, the native subscription is also released.
286
293
  * Safe to call even if not subscribed.
287
294
  */
288
- export function unsubscribeFromEvent(): void {
289
- if (eventsSubscription) {
290
- eventsSubscription.remove();
291
- eventsSubscription = null;
295
+ export function unsubscribeFromEvent(
296
+ handler?: (event: SdkEvent) => void
297
+ ): void {
298
+ if (handler) {
299
+ eventHandlers.delete(handler);
292
300
  }
293
301
 
294
- NativeSDK.unsubscribeFromEvent();
302
+ if (eventHandlers.size === 0) {
303
+ if (eventsSubscription) {
304
+ eventsSubscription.remove();
305
+ eventsSubscription = null;
306
+ }
307
+ NativeSDK.unsubscribeFromEvent();
308
+ }
295
309
  }
296
310
 
297
311
  //Android-only behavior
@@ -310,7 +324,7 @@ export function takePush(message: Record<string, string>): void {
310
324
 
311
325
  /**
312
326
  * Requests notification permission (Android-only behavior).
313
- * On Android it may trigger runtime permission flow,
327
+ * On Android it may trigger runtime permission flow,
314
328
  * n iOS it can be a no-op or routed to native implementation.
315
329
  */
316
330
  export function requestNotificationPermission(): void {
@@ -332,9 +346,14 @@ export function deliveryEvent(
332
346
  const nativePayload =
333
347
  message && Object.keys(message).length > 0 ? { ...message } : null;
334
348
 
335
- (NativeSDK as unknown as {
336
- deliveryEvent: (m: Record<string, string> | null, uid: string | null) => void;
337
- }).deliveryEvent(nativePayload, messageUID);
349
+ (
350
+ NativeSDK as unknown as {
351
+ deliveryEvent: (
352
+ m: Record<string, string> | null,
353
+ uid: string | null
354
+ ) => void;
355
+ }
356
+ ).deliveryEvent(nativePayload, messageUID);
338
357
  }
339
358
 
340
359
  /**
@@ -350,9 +369,11 @@ export function openEvent(
350
369
  const nativePayload =
351
370
  message && Object.keys(message).length > 0 ? { ...message } : null;
352
371
 
353
- (NativeSDK as unknown as {
354
- openEvent: (m: Record<string, string> | null, uid: string | null) => void;
355
- }).openEvent(nativePayload, messageUID);
372
+ (
373
+ NativeSDK as unknown as {
374
+ openEvent: (m: Record<string, string> | null, uid: string | null) => void;
375
+ }
376
+ ).openEvent(nativePayload, messageUID);
356
377
  }
357
378
 
358
379
  // ---------------- UserDefaults ----------------
@@ -372,6 +393,96 @@ export function setUserDefaultsValue(
372
393
  return NativeSDK.setUserDefaultsValue(suiteName, key, str);
373
394
  }
374
395
 
396
+ // ---------------- Profile API ----------------
397
+
398
+ /**
399
+ * Updates profile fields on the server.
400
+ *
401
+ * @param profileFields Optional profile fields to update.
402
+ * @param skipTriggers Optional flag to skip trigger execution on the server.
403
+ */
404
+ export function updateProfileFields(
405
+ profileFields: Record<string, unknown> | null = null,
406
+ skipTriggers: boolean | null = null
407
+ ): void {
408
+ NativeSDK.updateProfileFields(
409
+ Utilities.toNativeStringMap(profileFields),
410
+ skipTriggers
411
+ );
412
+ }
413
+
414
+ // ---------------- Token management API ----------------
415
+
416
+ /**
417
+ * Forces a push token refresh (delete current token + re-register).
418
+ *
419
+ * @returns Promise that resolves when the token update flow completes.
420
+ */
421
+ export function forcedTokenUpdate(): Promise<void> {
422
+ return NativeSDK.forcedTokenUpdate();
423
+ }
424
+
425
+ /**
426
+ * Deletes the device token for a given provider.
427
+ *
428
+ * @param provider Provider identifier (e.g. `android-firebase`, `ios-firebase`).
429
+ * @returns Promise that resolves when the delete call completes.
430
+ */
431
+ export function deleteDeviceToken(provider: string): Promise<void> {
432
+ return NativeSDK.deleteDeviceToken(provider);
433
+ }
434
+
435
+ /**
436
+ * Changes the push provider priority list at runtime.
437
+ *
438
+ * @param priorityList Ordered list of provider identifiers.
439
+ * @returns Promise that resolves when the priority list is updated.
440
+ */
441
+ export function changePushProviderPriorityList(
442
+ priorityList: string[] | null
443
+ ): Promise<void> {
444
+ if (!priorityList || priorityList.length === 0) {
445
+ return Promise.resolve();
446
+ }
447
+ return NativeSDK.changePushProviderPriorityList(priorityList);
448
+ }
449
+
450
+ // ---------------- PushReceiver helpers ----------------
451
+
452
+ /**
453
+ * Checks whether a push payload belongs to Altcraft.
454
+ *
455
+ * @param message Push payload map (string-only).
456
+ * @returns `true` if the message contains the Altcraft marker key.
457
+ */
458
+ export function isAltcraftPush(message: Record<string, string>): boolean {
459
+ if (!message || Object.keys(message).length === 0) return false;
460
+ return NativeSDK.isAltcraftPush({ ...message });
461
+ }
462
+
463
+ // ---------------- SDK lifecycle ----------------
464
+
465
+ /**
466
+ * Allows re-running initial retry/scheduling/token-check logic
467
+ * in this process on the next initialization (Android only).
468
+ * No-op on iOS.
469
+ */
470
+ export function unlockInitialOperationsInThisSession(): void {
471
+ NativeSDK.unlockInitialOperationsInThisSession();
472
+ }
473
+
474
+ // ---------------- iOS-only ----------------
475
+
476
+ /**
477
+ * Sets the App Group name for shared UserDefaults and Core Data (iOS only).
478
+ * Must be called before SDK initialization.
479
+ *
480
+ * @param groupName App Group identifier or `null` to clear.
481
+ */
482
+ export function setAppGroup(groupName: string | null): void {
483
+ NativeSDK.setAppGroup(groupName);
484
+ }
485
+
375
486
  // ---------------- Default export ----------------
376
487
 
377
488
  /**
@@ -423,10 +534,27 @@ const AltcraftSDK = {
423
534
  deliveryEvent,
424
535
  openEvent,
425
536
  requestNotificationPermission,
537
+ unlockInitialOperationsInThisSession,
538
+
539
+ // Profile API
540
+ updateProfileFields,
541
+
542
+ // Token management
543
+ forcedTokenUpdate,
544
+ deleteDeviceToken,
545
+ changePushProviderPriorityList,
546
+
547
+ // PushReceiver helpers
548
+ isAltcraftPush,
549
+
550
+ // iOS only
551
+ setAppGroup,
426
552
  };
427
553
 
428
554
  export default AltcraftSDK;
429
555
 
556
+ export { ActionFieldBuilder, actionField };
557
+
430
558
  export type {
431
559
  AltcraftConfig,
432
560
  CategoryData,