Skip to content

Commit f2cf388

Browse files
committed
refactor: replace RNIterableAPI calls with IterableApi methods in Iterable class
1 parent 4ee7bdf commit f2cf388

2 files changed

Lines changed: 35 additions & 94 deletions

File tree

src/core/classes/Iterable.ts

Lines changed: 34 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { IterableConfig } from './IterableConfig';
2424
import { IterableLogger } from './IterableLogger';
2525
import type { IterableAuthFailure } from '../types/IterableAuthFailure';
2626
import { IterableAuthManager } from './IterableAuthManager';
27+
import { IterableApi } from './IterableApi';
2728

2829
const RNEventEmitter = new NativeEventEmitter(RNIterableAPI);
2930

@@ -139,16 +140,13 @@ export class Iterable {
139140
config: IterableConfig = new IterableConfig()
140141
): Promise<boolean> {
141142
Iterable.savedConfig = config;
142-
143143
Iterable.logger = new IterableLogger(Iterable.savedConfig);
144144

145-
Iterable?.logger?.log('initialize: ' + apiKey);
146-
147145
this.setupEventHandlers();
148146

149147
const version = this.getVersionFromPackageJson();
150148

151-
return RNIterableAPI.initializeWithApiKey(apiKey, config.toDict(), version);
149+
return IterableApi.initializeWithApiKey(apiKey, config, version);
152150
}
153151

154152
/**
@@ -163,17 +161,15 @@ export class Iterable {
163161
apiEndPoint: string
164162
): Promise<boolean> {
165163
Iterable.savedConfig = config;
166-
167164
Iterable.logger = new IterableLogger(Iterable.savedConfig);
168165

169-
Iterable?.logger?.log('initialize2: ' + apiKey);
170-
171166
this.setupEventHandlers();
167+
172168
const version = this.getVersionFromPackageJson();
173169

174-
return RNIterableAPI.initialize2WithApiKey(
170+
return IterableApi.initialize2WithApiKey(
175171
apiKey,
176-
config.toDict(),
172+
config,
177173
version,
178174
apiEndPoint
179175
);
@@ -229,9 +225,7 @@ export class Iterable {
229225
* ```
230226
*/
231227
static setEmail(email: string | null, authToken?: string | null) {
232-
Iterable?.logger?.log('setEmail: ' + email);
233-
234-
RNIterableAPI.setEmail(email, authToken);
228+
IterableApi.setEmail(email, authToken);
235229
}
236230

237231
/**
@@ -245,9 +239,7 @@ export class Iterable {
245239
* ```
246240
*/
247241
static getEmail(): Promise<string | null> {
248-
Iterable?.logger?.log('getEmail');
249-
250-
return RNIterableAPI.getEmail();
242+
return IterableApi.getEmail();
251243
}
252244

253245
/**
@@ -294,9 +286,7 @@ export class Iterable {
294286
* taken
295287
*/
296288
static setUserId(userId?: string | null, authToken?: string | null) {
297-
Iterable?.logger?.log('setUserId: ' + userId);
298-
299-
RNIterableAPI.setUserId(userId, authToken);
289+
IterableApi.setUserId(userId, authToken);
300290
}
301291

302292
/**
@@ -310,9 +300,7 @@ export class Iterable {
310300
* ```
311301
*/
312302
static getUserId(): Promise<string | null | undefined> {
313-
Iterable?.logger?.log('getUserId');
314-
315-
return RNIterableAPI.getUserId();
303+
return IterableApi.getUserId();
316304
}
317305

318306
/**
@@ -324,9 +312,7 @@ export class Iterable {
324312
* ```
325313
*/
326314
static disableDeviceForCurrentUser() {
327-
Iterable?.logger?.log('disableDeviceForCurrentUser');
328-
329-
RNIterableAPI.disableDeviceForCurrentUser();
315+
IterableApi.disableDeviceForCurrentUser();
330316
}
331317

332318
/**
@@ -341,9 +327,7 @@ export class Iterable {
341327
* ```
342328
*/
343329
static getLastPushPayload(): Promise<unknown> {
344-
Iterable?.logger?.log('getLastPushPayload');
345-
346-
return RNIterableAPI.getLastPushPayload();
330+
return IterableApi.getLastPushPayload();
347331
}
348332

349333
/**
@@ -369,9 +353,7 @@ export class Iterable {
369353
* ```
370354
*/
371355
static getAttributionInfo(): Promise<IterableAttributionInfo | undefined> {
372-
Iterable?.logger?.log('getAttributionInfo');
373-
374-
return RNIterableAPI.getAttributionInfo().then(
356+
return IterableApi.getAttributionInfo().then(
375357
(
376358
dict: {
377359
campaignId: number;
@@ -417,13 +399,7 @@ export class Iterable {
417399
* ```
418400
*/
419401
static setAttributionInfo(attributionInfo?: IterableAttributionInfo) {
420-
Iterable?.logger?.log('setAttributionInfo');
421-
422-
RNIterableAPI.setAttributionInfo(
423-
attributionInfo as unknown as {
424-
[key: string]: string | number | boolean;
425-
} | null
426-
);
402+
IterableApi.setAttributionInfo(attributionInfo);
427403
}
428404

429405
/**
@@ -462,14 +438,12 @@ export class Iterable {
462438
appAlreadyRunning: boolean,
463439
dataFields?: unknown
464440
) {
465-
Iterable?.logger?.log('trackPushOpenWithCampaignId');
466-
467-
RNIterableAPI.trackPushOpenWithCampaignId(
441+
IterableApi.trackPushOpenWithCampaignId(
468442
campaignId,
469443
templateId,
470444
messageId as string,
471445
appAlreadyRunning,
472-
dataFields as { [key: string]: string | number | boolean } | undefined
446+
dataFields
473447
);
474448
}
475449

@@ -500,11 +474,7 @@ export class Iterable {
500474
* ```
501475
*/
502476
static updateCart(items: IterableCommerceItem[]) {
503-
Iterable?.logger?.log('updateCart');
504-
505-
RNIterableAPI.updateCart(
506-
items as unknown as { [key: string]: string | number | boolean }[]
507-
);
477+
IterableApi.updateCart(items);
508478
}
509479

510480
/**
@@ -519,9 +489,7 @@ export class Iterable {
519489
*/
520490
static wakeApp() {
521491
if (Platform.OS === 'android') {
522-
Iterable?.logger?.log('Attempting to wake the app');
523-
524-
RNIterableAPI.wakeApp();
492+
IterableApi.wakeApp();
525493
}
526494
}
527495

@@ -556,11 +524,7 @@ export class Iterable {
556524
) {
557525
Iterable?.logger?.log('trackPurchase');
558526

559-
RNIterableAPI.trackPurchase(
560-
total,
561-
items as unknown as { [key: string]: string | number | boolean }[],
562-
dataFields as { [key: string]: string | number | boolean } | undefined
563-
);
527+
IterableApi.trackPurchase(total, items, dataFields);
564528
}
565529

566530
/**
@@ -586,9 +550,13 @@ export class Iterable {
586550
message: IterableInAppMessage,
587551
location: IterableInAppLocation
588552
) {
589-
Iterable?.logger?.log('trackInAppOpen');
590-
591-
RNIterableAPI.trackInAppOpen(message.messageId, location);
553+
if (!message?.messageId) {
554+
Iterable?.logger?.log(
555+
`Skipping trackInAppOpen because message ID is required, but received ${message}.`
556+
);
557+
return;
558+
}
559+
IterableApi.trackInAppOpen(message, location);
592560
}
593561

594562
/**
@@ -617,9 +585,7 @@ export class Iterable {
617585
location: IterableInAppLocation,
618586
clickedUrl: string
619587
) {
620-
Iterable?.logger?.log('trackInAppClick');
621-
622-
RNIterableAPI.trackInAppClick(message.messageId, location, clickedUrl);
588+
IterableApi.trackInAppClick(message, location, clickedUrl);
623589
}
624590

625591
/**
@@ -650,14 +616,7 @@ export class Iterable {
650616
source: IterableInAppCloseSource,
651617
clickedUrl?: string
652618
) {
653-
Iterable?.logger?.log('trackInAppClose');
654-
655-
RNIterableAPI.trackInAppClose(
656-
message.messageId,
657-
location,
658-
source,
659-
clickedUrl
660-
);
619+
IterableApi.trackInAppClose(message, location, source, clickedUrl);
661620
}
662621

663622
/**
@@ -701,9 +660,7 @@ export class Iterable {
701660
location: IterableInAppLocation,
702661
source: IterableInAppDeleteSource
703662
) {
704-
Iterable?.logger?.log('inAppConsume');
705-
706-
RNIterableAPI.inAppConsume(message.messageId, location, source);
663+
IterableApi.inAppConsume(message, location, source);
707664
}
708665

709666
/**
@@ -727,12 +684,7 @@ export class Iterable {
727684
* ```
728685
*/
729686
static trackEvent(name: string, dataFields?: unknown) {
730-
Iterable?.logger?.log('trackEvent');
731-
732-
RNIterableAPI.trackEvent(
733-
name,
734-
dataFields as { [key: string]: string | number | boolean } | undefined
735-
);
687+
IterableApi.trackEvent(name, dataFields);
736688
}
737689

738690
/**
@@ -778,12 +730,7 @@ export class Iterable {
778730
dataFields: unknown | undefined,
779731
mergeNestedObjects: boolean
780732
) {
781-
Iterable?.logger?.log('updateUser');
782-
783-
RNIterableAPI.updateUser(
784-
dataFields as { [key: string]: string | number | boolean },
785-
mergeNestedObjects
786-
);
733+
IterableApi.updateUser(dataFields, mergeNestedObjects);
787734
}
788735

789736
/**
@@ -804,9 +751,7 @@ export class Iterable {
804751
* ```
805752
*/
806753
static updateEmail(email: string, authToken?: string) {
807-
Iterable?.logger?.log('updateEmail');
808-
809-
RNIterableAPI.updateEmail(email, authToken);
754+
IterableApi.updateEmail(email, authToken);
810755
}
811756

812757
/**
@@ -888,9 +833,7 @@ export class Iterable {
888833
*/
889834
/* eslint-enable tsdoc/syntax */
890835
static handleAppLink(link: string): Promise<boolean> {
891-
Iterable?.logger?.log('handleAppLink');
892-
893-
return RNIterableAPI.handleAppLink(link);
836+
return IterableApi.handleAppLink(link);
894837
}
895838

896839
/**
@@ -935,9 +878,7 @@ export class Iterable {
935878
campaignId: number,
936879
templateId: number
937880
) {
938-
Iterable?.logger?.log('updateSubscriptions');
939-
940-
RNIterableAPI.updateSubscriptions(
881+
IterableApi.updateSubscriptions(
941882
emailListIds,
942883
unsubscribedChannelIds,
943884
unsubscribedMessageTypeIds,
@@ -1011,7 +952,7 @@ export class Iterable {
1011952
const message = IterableInAppMessage.fromDict(messageDict);
1012953
// MOB-10423: Check if we can use chain operator (?.) here instead
1013954
const result = Iterable.savedConfig.inAppHandler!(message);
1014-
RNIterableAPI.setInAppShowResponse(result);
955+
IterableApi.setInAppShowResponse(result);
1015956
}
1016957
);
1017958
}

src/core/classes/IterableApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ export class IterableApi {
578578
*
579579
* @param attributionInfo - The attribution info.
580580
*/
581-
static setAttributionInfo(attributionInfo: IterableAttributionInfo) {
581+
static setAttributionInfo(attributionInfo?: IterableAttributionInfo) {
582582
// IterableLogger.log('setAttributionInfo: ', attributionInfo);
583583
return RNIterableAPI.setAttributionInfo(attributionInfo);
584584
}

0 commit comments

Comments
 (0)