react-native-unit-components
Version:
Unit React Native components
119 lines (101 loc) • 4.46 kB
text/typescript
import { NativeModules, Platform } from 'react-native';
import { fetchUnitScript } from './UnitComponentsSdk.api';
import { UnitComponentsUiManager } from './unitComponentsUiManager/unitComponentsUiManager';
import type { UNFonts } from '../types/shared/fonts.types';
import { warnClientIfNeeded } from '../utils/webVersioningStrategy.utils';
import { UNComponentsSDKConstants } from './UnitComponentsSdk.constants';
import type { UNComponentsWebVersioningStrategy } from '../types/shared';
import { setTheme, setLanguage, setCustomerToken } from '../slices/ConfigurationSlice';
import { store } from '../store/store';
import { UNComponentsEnvironment } from '../types/shared/env.types';
import { UserDataKeys } from '../types/internal/unitStore.types';
import UNStoreManagerHelper from '../nativeModulesHelpers/UNStoreModuleHelper/UNStoreModuleHelper';
import { UnitComponentsHelpersManager } from './unitComponentsHelpersManager/UnitComponentsHelpersManager';
import UNSecurityManagerHelper from '../nativeModulesHelpers/UNSecurityHelper';
import { UNComponentsSecuritySettings, UNComponentsSnapshotProtectionStrategy } from '../types/shared/securitySettings';
import UNSnapshotProtectionHelper from '../nativeModulesHelpers/UNSanpshotProtectionHelper/UNSnapshotProtectionHelper';
export class UnitComponentsSDK {
public static ui: UnitComponentsUiManager;
public static helpers: UnitComponentsHelpersManager;
protected static env?: UNComponentsEnvironment;
protected static webVersioningStrategy: UNComponentsWebVersioningStrategy;
protected static securitySettings: UNComponentsSecuritySettings = UNComponentsSDKConstants.securitySettings;
protected static fonts?: UNFonts;
protected static signedNonce?: string;
protected static pushProvisionModule?: typeof NativeModules;
protected static sdkVersion = '2.0.0';
public static init = async (
env: UNComponentsEnvironment,
theme?: string,
language?: string,
fonts?: UNFonts,
webVersioningStrategy: UNComponentsWebVersioningStrategy = UNComponentsSDKConstants.webSDKRecommendedStrategy,
securitySettings: UNComponentsSecuritySettings = UNComponentsSDKConstants.securitySettings
) => {
try {
this.webVersioningStrategy = webVersioningStrategy;
this.ui = new UnitComponentsUiManager();
this.helpers = new UnitComponentsHelpersManager();
const isJailbroke = await UNSecurityManagerHelper.isDeviceJailbroke();
if (isJailbroke) {
throw Error('Device is jailbroke');
}
this.env = env;
this.securitySettings = securitySettings;
this.fonts = fonts;
store.dispatch(setTheme(theme));
store.dispatch(setLanguage(language));
warnClientIfNeeded(this.webVersioningStrategy);
const shouldProtectFromSnapshot = securitySettings.snapshotProtectionStrategy != UNComponentsSnapshotProtectionStrategy.None;
if (Platform.OS === 'ios') {
UNSnapshotProtectionHelper.setIosSecurity(shouldProtectFromSnapshot, 'light');
}
await fetchUnitScript();
} catch (e) {
console.log(e);
}
};
public static isInitialized = () => {
return this.env != undefined;
};
public static setPushProvisioningModule = (pushProvisionModule?: typeof NativeModules) => {
this.pushProvisionModule = pushProvisionModule;
};
public static getPushProvisionModule = () => {
return this.pushProvisionModule;
};
public static getSecuritySettings = () => {
return this.securitySettings;
};
/**
* @deprecated - Unit SDK is managing the signedNonce, no need to provide it
*/
public static setSignedNonce = (signedNonce: string) => {
this.signedNonce = signedNonce;
};
public static getEnv = () => {
return this.env;
};
public static getWebVersioningStrategy = () => {
return this.webVersioningStrategy;
};
public static getFonts = () => {
return this.fonts;
};
/**
* @deprecated - Unit SDK is managing the signedNonce, no need to provide it
*/
public static getSignedNonce = () => {
return this.signedNonce;
};
public static setCustomerToken = (customerToken: string) => {
store.dispatch(setCustomerToken(customerToken));
};
public static getSdkVersion = () => {
return this.sdkVersion;
};
public static cleanUserData = () => {
UNStoreManagerHelper.cleanValue(UserDataKeys.unitCustomerToken);
UNStoreManagerHelper.cleanValue(UserDataKeys.unitVerifiedToken);
};
}