Skip to content

Commit 32374cb

Browse files
committed
feat: add script to generate native code and update IterableAuthResponse types for better null handling
1 parent 0b955cf commit 32374cb

9 files changed

Lines changed: 66 additions & 29 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"docs": "typedoc",
4444
"clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
4545
"prepare": "yarn add_build_info; yarn bob build",
46-
"release": "release-it"
46+
"release": "release-it",
47+
"generateNativeCode": "node scripts/generateNativeCode.js"
4748
},
4849
"keywords": [
4950
"react-native",

scripts/generateNativeCode.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { execSync } = require('child_process');
2+
const path = require('path');
3+
4+
const rootDir = path.resolve(__dirname, '..');
5+
6+
// Generate native code
7+
console.log('Generating native code...');
8+
execSync('npx react-native codegen', {
9+
cwd: rootDir,
10+
stdio: 'inherit',
11+
env: {
12+
...process.env,
13+
RCT_NEW_ARCH_ENABLED: '1',
14+
},
15+
});

src/core/api/NativeRNIterableApi.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export interface IterableConfigDict {
1616
// dataRegion: IterableDataRegion;
1717
// pushPlatform?: IterablePushPlatform;
1818
encryptionEnforced?: boolean;
19+
logLevel?: string;
20+
dataRegion?: string;
21+
pushPlatform?: string;
1922
}
2023

2124
/**
@@ -25,20 +28,20 @@ export interface Spec extends TurboModule {
2528
// Native SDK Functions
2629
initializeWithApiKey(
2730
apiKey: string,
28-
config: { [key: string]: IterableConfigDict },
31+
config: IterableConfigDict,
2932
version?: string
3033
): Promise<boolean>;
3134

3235
initialize2WithApiKey(
3336
apiKey: string,
34-
config: { [key: string]: IterableConfigDict },
37+
config: IterableConfigDict,
3538
apiEndPointOverride: string,
3639
version: string
3740
): Promise<boolean>;
3841

39-
setEmail(email: string, authToken: string | null): void;
42+
setEmail(email: string | null | undefined, authToken: string | null | undefined): void;
4043
getEmail(): Promise<string | null>;
41-
setUserId(userId: string, authToken: string | null): void;
44+
setUserId(userId: string | null | undefined, authToken: string | null | undefined): void;
4245
getUserId(): Promise<string | null>;
4346

4447
// Iterable API Request Functions
@@ -70,20 +73,20 @@ export interface Spec extends TurboModule {
7073
messageId: string,
7174
location: number,
7275
source: number,
73-
clickedUrl: string | null
76+
clickedUrl?: string | null
7477
): void;
7578

7679
inAppConsume(messageId: string, location: number, source: number): void;
7780
trackEvent(name: string, dataFields: { [key: string]: string | number | boolean | null } | null): void;
7881
updateUser(dataFields: { [key: string]: string | number | boolean | null }, mergeNestedObjects: boolean): void;
79-
updateEmail(email: string, authToken: string | null): void;
82+
updateEmail(email: string, authToken: string | null | undefined): void;
8083
handleAppLink(appLink: string): Promise<boolean>;
8184

8285
updateSubscriptions(
83-
emailListIds: number[] | null,
84-
unsubscribedChannelIds: number[] | null,
85-
unsubscribedMessageTypeIds: number[] | null,
86-
subscribedMessageTypeIds: number[] | null,
86+
emailListIds: number[] | null | undefined,
87+
unsubscribedChannelIds: number[] | null | undefined,
88+
unsubscribedMessageTypeIds: number[] | null | undefined,
89+
subscribedMessageTypeIds: number[] | null | undefined,
8790
campaignId: number,
8891
templateId: number
8992
): void;

src/core/classes/Iterable.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
Linking,
3-
NativeEventEmitter,
4-
NativeModules,
3+
// NativeEventEmitter,
4+
// NativeModules,
55
Platform,
66
} from 'react-native';
77

@@ -199,7 +199,7 @@ export class Iterable {
199199
* });
200200
* ```
201201
*/
202-
static getEmail(): Promise<string | undefined> {
202+
static getEmail(): Promise<string | null | undefined> {
203203
Iterable?.logger?.log('getEmail');
204204

205205
return RNIterableAPI.getEmail();
@@ -264,7 +264,7 @@ export class Iterable {
264264
* });
265265
* ```
266266
*/
267-
static getUserId(): Promise<string | undefined> {
267+
static getUserId(): Promise<string | undefined | null> {
268268
Iterable?.logger?.log('getUserId');
269269

270270
return RNIterableAPI.getUserId();
@@ -327,12 +327,12 @@ export class Iterable {
327327
Iterable?.logger?.log('getAttributionInfo');
328328

329329
return RNIterableAPI.getAttributionInfo().then(
330-
(dict?: IterableAttributionInfo) => {
330+
(dict: { [key: string]: string | number | boolean | null } | null) => {
331331
if (dict) {
332332
return new IterableAttributionInfo(
333-
dict.campaignId,
334-
dict.templateId,
335-
dict.messageId
333+
dict.campaignId as number,
334+
dict.templateId as number,
335+
dict.messageId as string
336336
);
337337
} else {
338338
return undefined;
@@ -365,9 +365,10 @@ export class Iterable {
365365
* Iterable.setAttributionInfo(attributionInfo);
366366
* ```
367367
*/
368-
static setAttributionInfo(attributionInfo?: IterableAttributionInfo) {
368+
static setAttributionInfo(attributionInfo?: IterableAttributionInfo | null ) {
369369
Iterable?.logger?.log('setAttributionInfo');
370370

371+
// @ts-ignore
371372
RNIterableAPI.setAttributionInfo(attributionInfo);
372373
}
373374

@@ -412,9 +413,9 @@ export class Iterable {
412413
RNIterableAPI.trackPushOpenWithCampaignId(
413414
campaignId,
414415
templateId,
415-
messageId,
416+
messageId as string,
416417
appAlreadyRunning,
417-
dataFields
418+
dataFields as { [key: string]: string | number | boolean | null } | null
418419
);
419420
}
420421

@@ -447,6 +448,7 @@ export class Iterable {
447448
static updateCart(items: IterableCommerceItem[]) {
448449
Iterable?.logger?.log('updateCart');
449450

451+
// @ts-ignore
450452
RNIterableAPI.updateCart(items);
451453
}
452454

@@ -464,6 +466,7 @@ export class Iterable {
464466
if (Platform.OS === 'android') {
465467
Iterable?.logger?.log('Attempting to wake the app');
466468

469+
// @ts-ignore
467470
RNIterableAPI.wakeApp();
468471
}
469472
}
@@ -499,6 +502,7 @@ export class Iterable {
499502
) {
500503
Iterable?.logger?.log('trackPurchase');
501504

505+
// @ts-ignore
502506
RNIterableAPI.trackPurchase(total, items, dataFields);
503507
}
504508

@@ -668,6 +672,7 @@ export class Iterable {
668672
static trackEvent(name: string, dataFields?: unknown) {
669673
Iterable?.logger?.log('trackEvent');
670674

675+
// @ts-ignore
671676
RNIterableAPI.trackEvent(name, dataFields);
672677
}
673678

@@ -716,6 +721,7 @@ export class Iterable {
716721
) {
717722
Iterable?.logger?.log('updateUser');
718723

724+
// @ts-ignore
719725
RNIterableAPI.updateUser(dataFields, mergeNestedObjects);
720726
}
721727

@@ -736,7 +742,7 @@ export class Iterable {
736742
* Iterable.updateEmail('my.new.email@gmail.com', 'myAuthToken');
737743
* ```
738744
*/
739-
static updateEmail(email: string, authToken?: string) {
745+
static updateEmail(email: string, authToken?: string | null) {
740746
Iterable?.logger?.log('updateEmail');
741747

742748
RNIterableAPI.updateEmail(email, authToken);
@@ -961,7 +967,7 @@ export class Iterable {
961967
// Else it will be looked for as a String.
962968
if (typeof promiseResult === typeof new IterableAuthResponse()) {
963969
RNIterableAPI.passAlongAuthToken(
964-
(promiseResult as IterableAuthResponse).authToken
970+
(promiseResult as IterableAuthResponse).authToken as string | null
965971
);
966972

967973
setTimeout(() => {

src/core/classes/IterableAuthResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
export class IterableAuthResponse {
77
/** JWT Token */
8-
authToken?: string = '';
8+
authToken?: string | null = '';
99
/** Callback when the authentication to Iterable succeeds */
1010
successCallback?: () => void;
1111
/** Callback when the authentication to Iterable fails */

src/core/classes/IterableConfig.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { type IterableInAppMessage } from '../../inApp/classes/IterableInAppMessage';
22
import { IterableInAppShowResponse } from '../../inApp/enums';
3+
import type { IterableConfigDict } from '../api/NativeRNIterableApi';
34
import {
45
IterableDataRegion,
56
IterableLogLevel,
@@ -299,7 +300,7 @@ export class IterableConfig {
299300
*
300301
* @returns An object representing the configuration.
301302
*/
302-
toDict() {
303+
toDict():IterableConfigDict {
303304
return {
304305
pushIntegrationName: this.pushIntegrationName,
305306
autoPushRegistration: this.autoPushRegistration,
@@ -333,13 +334,16 @@ export class IterableConfig {
333334
// eslint-disable-next-line eqeqeq
334335
authHandlerPresent: this.authHandler != undefined,
335336
/** The log level for the SDK. */
337+
// @ts-ignore
336338
logLevel: this.logLevel,
337339
expiringAuthTokenRefreshPeriod: this.expiringAuthTokenRefreshPeriod,
338340
allowedProtocols: this.allowedProtocols,
339341
androidSdkUseInMemoryStorageForInApps:
340342
this.androidSdkUseInMemoryStorageForInApps,
341343
useInMemoryStorageForInApps: this.useInMemoryStorageForInApps,
344+
// @ts-ignore
342345
dataRegion: this.dataRegion,
346+
// @ts-ignore
343347
pushPlatform: this.pushPlatform,
344348
encryptionEnforced: this.encryptionEnforced,
345349
};

src/inApp/classes/IterableInAppManager.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NativeModules } from 'react-native';
1+
// import { NativeModules } from 'react-native';
22

33
import { Iterable } from '../../core/classes/Iterable';
44
import type {
@@ -39,6 +39,7 @@ export class IterableInAppManager {
3939
getMessages(): Promise<IterableInAppMessage[]> {
4040
Iterable?.logger?.log('InAppManager.getMessages');
4141

42+
// @ts-expect-error fix when you know the types
4243
return RNIterableAPI.getInAppMessages();
4344
}
4445

@@ -62,6 +63,7 @@ export class IterableInAppManager {
6263
getInboxMessages(): Promise<IterableInAppMessage[]> {
6364
Iterable?.logger?.log('InAppManager.getInboxMessages');
6465

66+
// @ts-expect-error fix when you know the types
6567
return RNIterableAPI.getInboxMessages();
6668
}
6769

@@ -89,6 +91,7 @@ export class IterableInAppManager {
8991
): Promise<string | undefined> {
9092
Iterable?.logger?.log('InAppManager.show');
9193

94+
// @ts-expect-error fix when you know the types
9295
return RNIterableAPI.showMessage(message.messageId, consume);
9396
}
9497

@@ -154,6 +157,7 @@ export class IterableInAppManager {
154157
): Promise<IterableHtmlInAppContent> {
155158
Iterable?.logger?.log('InAppManager.getHtmlContentForMessage');
156159

160+
// @ts-expect-error fix when you know the types
157161
return RNIterableAPI.getHtmlInAppContentForMessage(message.messageId);
158162
}
159163

src/inbox/classes/IterableInboxDataModel.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class IterableInboxDataModel {
102102
);
103103

104104
return RNIterableAPI.getHtmlInAppContentForMessage(id).then(
105+
// @ts-expect-error fix when you know the types
105106
(content: IterableHtmlInAppContentRaw) => {
106107
return IterableHtmlInAppContent.fromDict(content);
107108
}
@@ -139,6 +140,7 @@ export class IterableInboxDataModel {
139140
*/
140141
async refresh(): Promise<IterableInboxRowViewModel[]> {
141142
return RNIterableAPI.getInboxMessages().then(
143+
// @ts-expect-error fix when you know the types
142144
(messages: IterableInAppMessage[]) => {
143145
return this.processMessages(messages);
144146
},
@@ -154,6 +156,7 @@ export class IterableInboxDataModel {
154156
* @param visibleRows - An array of `IterableInboxImpressionRowInfo` objects representing the rows that are currently visible.
155157
*/
156158
startSession(visibleRows: IterableInboxImpressionRowInfo[] = []) {
159+
// @ts-expect-error fix when you know the types
157160
RNIterableAPI.startSession(visibleRows);
158161
}
159162

@@ -181,6 +184,7 @@ export class IterableInboxDataModel {
181184
* Defaults to an empty array if not provided.
182185
*/
183186
updateVisibleRows(visibleRows: IterableInboxImpressionRowInfo[] = []) {
187+
// @ts-expect-error fix when you know the types
184188
RNIterableAPI.updateVisibleRows(visibleRows);
185189
}
186190

src/inbox/components/IterableInbox.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { useIsFocused } from '@react-navigation/native';
22
import { useEffect, useState } from 'react';
33
import {
44
Animated,
5-
NativeEventEmitter,
6-
NativeModules,
5+
// NativeEventEmitter,
6+
// NativeModules,
77
Platform,
88
StyleSheet,
99
Text,

0 commit comments

Comments
 (0)