gomarketme-react-native
Version:
Affiliate Marketing for React Native and Expo iOS and Android apps.
173 lines (172 loc) • 6.49 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SaleDistribution = exports.Affiliate = exports.Campaign = exports.GoMarketMeAffiliateMarketingData = void 0;
const react_native_1 = require("react-native");
const LINKING_ERROR = "The native GoMarketMe module is not linked. Make sure you rebuilt your app after installing gomarketme-react-native.";
class GoMarketMeAffiliateMarketingData {
constructor(campaign, affiliate, saleDistribution, affiliateCampaignCode, deviceId, offerCode) {
this.campaign = campaign;
this.affiliate = affiliate;
this.saleDistribution = saleDistribution;
this.affiliateCampaignCode = affiliateCampaignCode;
this.deviceId = deviceId;
this.offerCode = offerCode;
}
static fromJson(json) {
if (!json || Object.keys(json).length === 0) {
return null;
}
return new GoMarketMeAffiliateMarketingData(Campaign.fromJson(asRecord(json.campaign)), Affiliate.fromJson(asRecord(json.affiliate)), SaleDistribution.fromJson(asRecord(json.sale_distribution)), asString(json.affiliate_campaign_code), asString(json.device_id), json.offer_code == null ? undefined : String(json.offer_code));
}
toJson() {
return {
campaign: this.campaign.toJson(),
affiliate: this.affiliate.toJson(),
sale_distribution: this.saleDistribution.toJson(),
affiliate_campaign_code: this.affiliateCampaignCode,
device_id: this.deviceId,
offer_code: this.offerCode,
};
}
}
exports.GoMarketMeAffiliateMarketingData = GoMarketMeAffiliateMarketingData;
class Campaign {
constructor(id, name, status, type, publicLinkUrl) {
this.id = id;
this.name = name;
this.status = status;
this.type = type;
this.publicLinkUrl = publicLinkUrl;
}
static fromJson(json) {
return new Campaign(asString(json.id), asString(json.name), asString(json.status), asString(json.type), json.public_link_url == null ? undefined : String(json.public_link_url));
}
toJson() {
return {
id: this.id,
name: this.name,
status: this.status,
type: this.type,
public_link_url: this.publicLinkUrl,
};
}
}
exports.Campaign = Campaign;
class Affiliate {
constructor(id, firstName, lastName, countryCode, instagramAccount, tiktokAccount, xAccount) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.countryCode = countryCode;
this.instagramAccount = instagramAccount;
this.tiktokAccount = tiktokAccount;
this.xAccount = xAccount;
}
static fromJson(json) {
return new Affiliate(asString(json.id), asString(json.first_name), asString(json.last_name), asString(json.country_code), asString(json.instagram_account), asString(json.tiktok_account), asString(json.x_account));
}
toJson() {
return {
id: this.id,
first_name: this.firstName,
last_name: this.lastName,
country_code: this.countryCode,
instagram_account: this.instagramAccount,
tiktok_account: this.tiktokAccount,
x_account: this.xAccount,
};
}
}
exports.Affiliate = Affiliate;
class SaleDistribution {
constructor(platformPercentage, affiliatePercentage) {
this.platformPercentage = platformPercentage;
this.affiliatePercentage = affiliatePercentage;
}
static fromJson(json) {
return new SaleDistribution(asString(json.platform_percentage), asString(json.affiliate_percentage));
}
toJson() {
return {
platform_percentage: this.platformPercentage,
affiliate_percentage: this.affiliatePercentage,
};
}
}
exports.SaleDistribution = SaleDistribution;
class GoMarketMe {
constructor() {
this.sdkType = 'ReactNative';
this.sdkVersion = '5.0.6';
this.isInitializing = false;
this.isInitialized = false;
}
static getInstance() {
if (!GoMarketMe.instance) {
GoMarketMe.instance = new GoMarketMe();
}
return GoMarketMe.instance;
}
get initialized() {
return this.isInitialized;
}
async initialize(apiKey) {
var _a;
const trimmedApiKey = apiKey.trim();
if (!trimmedApiKey) {
log('Initialization skipped because apiKey is empty.');
return;
}
if (this.isInitialized || this.isInitializing) {
log('Initialization skipped because SDK is already initialized or initializing.');
return;
}
this.isInitializing = true;
try {
const response = await nativeModule().initialize(trimmedApiKey, this.sdkType, this.sdkVersion, !__DEV__);
this.affiliateMarketingData = GoMarketMeAffiliateMarketingData.fromJson((_a = response.affiliateMarketingData) !== null && _a !== void 0 ? _a : null);
this.isInitialized = true;
}
catch (error) {
log(`Error initializing GoMarketMe: ${String(error)}`);
}
finally {
this.isInitializing = false;
}
}
async syncAllTransactions() {
if (!this.isInitialized) {
throw new Error('GoMarketMe SDK must be initialized before syncing transactions.');
}
const syncAllTransactions = nativeModule().syncAllTransactions;
if (typeof syncAllTransactions !== 'function') {
throw new Error('GoMarketMe transaction sync is not available in the linked native module.');
}
return syncAllTransactions();
}
stop() {
var _a, _b;
(_b = (_a = nativeModule()).stop) === null || _b === void 0 ? void 0 : _b.call(_a);
this.isInitialized = false;
this.isInitializing = false;
}
}
function nativeModule() {
const module = react_native_1.NativeModules.GoMarketMeReactNative;
if (!module) {
throw new Error(LINKING_ERROR);
}
return module;
}
function log(message) {
if (__DEV__) {
console.log(`[GoMarketMe] ${message}`);
}
}
function asRecord(value) {
return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
}
function asString(value) {
return value == null ? '' : String(value);
}
exports.default = GoMarketMe.getInstance();