Skip to content

Commit 5239dcd

Browse files
Save Point
1 parent 4a84ff4 commit 5239dcd

2 files changed

Lines changed: 76 additions & 77 deletions

File tree

src/forwarders.interfaces.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,19 @@ export interface IMPForwarder {
185185
isEnabledForUserAttributes: (filterObject?: IFilteringUserAttributeValue, user?: IMParticleUser) => boolean;
186186
isEnabledForUnknownUser: (excludeAnonymousUserBoolean: boolean, user: IMParticleUser) => boolean;
187187

188-
process?: (event: SDKEvent) => string | void;
189188
name?: string;
190-
setUserAttribute?: (key: string, value: string | string[]) => string | void;
191-
removeUserAttribute?: (key: string) => string | void;
192-
setUserIdentity?: (identity: string, type: number) => string | void;
193-
194-
// Techically these do not return a value, but we sometimes
195-
// debug message as a string
189+
190+
// Techically these do not return a value, but we sometimes use a string as a debug message
196191
onUserIdentified?: (user: IMParticleUser, identityApiData?: IdentityApiData) => string;
197192
onIdentifyComplete?: (user: IMParticleUser, identityApiData: IdentityApiData) => string;
198193
onLoginComplete?: (user: IMParticleUser, identityApiData: IdentityApiData) => string;
199194
onLogoutComplete?: (user: IMParticleUser, identityApiData: IdentityApiData) => string;
200195
onModifyComplete?: (user: IMParticleUser, identityApiData: IdentityApiData) => string;
201196
setOptOut: (optOut: boolean) => string;
197+
setUserAttribute?: (key: string, value: string | string[]) => string;
198+
removeUserAttribute?: (key: string) => string;
199+
process?: (event: SDKEvent) => string;
200+
setUserIdentity?: (identity: string, type: number) => string;
202201

203202
getForwarderStatsQueue: () => IForwardingStatsData[];
204203
setForwarderStatsQueue: (queue: IForwardingStatsData[]) => void;

src/forwarders.ts

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
388388
}
389389
};
390390

391-
this.handleForwarderUserAttributes = function(functionNameKey, key, value) {
391+
this.handleForwarderUserAttributes = (functionNameKey: string, key: string, value: string | string[]) => {
392392
if (
393393
(kitBlocker && kitBlocker.isAttributeKeyBlocked(key)) ||
394394
!mpInstance._Store.activeForwarders.length
@@ -397,7 +397,7 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
397397
}
398398

399399
mpInstance._Store.activeForwarders.forEach(function(forwarder) {
400-
const forwarderFunction = forwarder[functionNameKey];
400+
const forwarderFunction: IMPForwarder = forwarder[functionNameKey];
401401
if (
402402
!forwarderFunction ||
403403
mpInstance._Helpers.isFilteredUserAttribute(
@@ -408,7 +408,7 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
408408
return;
409409
}
410410
try {
411-
let result;
411+
let result: string;
412412

413413
if (
414414
functionNameKey ===
@@ -432,15 +432,16 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
432432
};
433433

434434
// TODO: https://go.mparticle.com/work/SQDSDKS-6036
435-
this.setForwarderUserIdentities = function(userIdentities) {
436-
mpInstance._Store.activeForwarders.forEach(function(forwarder) {
437-
var filteredUserIdentities = mpInstance._Helpers.filterUserIdentities(
435+
// @deprecated
436+
this.setForwarderUserIdentities = (userIdentities: UserIdentities) => {
437+
mpInstance._Store.activeForwarders.forEach((forwarder) => {
438+
const filteredUserIdentities: ISDKUserIdentity[] = mpInstance._Helpers.filterUserIdentities(
438439
userIdentities,
439440
forwarder.userIdentityFilters
440441
);
441442
if (forwarder.setUserIdentity) {
442-
filteredUserIdentities.forEach(function(identity) {
443-
var result = forwarder.setUserIdentity(
443+
filteredUserIdentities.forEach((identity) => {
444+
const result: string = forwarder.setUserIdentity(
444445
identity.Identity,
445446
identity.Type
446447
);
@@ -452,35 +453,35 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
452453
});
453454
};
454455

455-
this.setForwarderOnUserIdentified = function(user) {
456-
mpInstance._Store.activeForwarders.forEach(function(forwarder) {
457-
var filteredUser = filteredMparticleUser(
456+
this.setForwarderOnUserIdentified = (user: IMParticleUser) => {
457+
mpInstance._Store.activeForwarders.forEach((forwarder) => {
458+
const filteredUser = filteredMparticleUser(
458459
user.getMPID(),
459460
forwarder,
460461
mpInstance,
461462
kitBlocker
462463
);
463464
if (forwarder.onUserIdentified) {
464-
var result = forwarder.onUserIdentified(filteredUser);
465+
const result: string = forwarder.onUserIdentified(filteredUser);
465466
if (result) {
466467
mpInstance.Logger.verbose(result);
467468
}
468469
}
469470
});
470471
};
471472

472-
this.setForwarderOnIdentityComplete = function(user, identityMethod) {
473-
var result;
473+
this.setForwarderOnIdentityComplete = (user: IMParticleUser, identityMethod: IdentityAPIMethod) => {
474+
let result: string;
474475

475-
mpInstance._Store.activeForwarders.forEach(function(forwarder) {
476-
var filteredUser = filteredMparticleUser(
476+
mpInstance._Store.activeForwarders.forEach((forwarder: IMPForwarder) => {
477+
const filteredUser: IMParticleUser = filteredMparticleUser(
477478
user.getMPID(),
478479
forwarder,
479480
mpInstance,
480481
kitBlocker
481482
);
482483

483-
const filteredUserIdentities = filteredUser.getUserIdentities();
484+
const filteredUserIdentities: IdentityApiData = filteredUser.getUserIdentities();
484485

485486
if (identityMethod === Identify) {
486487
if (forwarder.onIdentifyComplete) {
@@ -526,35 +527,33 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
526527
});
527528
};
528529

529-
this.getForwarderStatsQueue = function() {
530-
return mpInstance._Persistence.forwardingStatsBatches
531-
.forwardingStatsEventQueue;
532-
};
530+
this.getForwarderStatsQueue = () =>
531+
mpInstance._Persistence.forwardingStatsBatches.forwardingStatsEventQueue;
533532

534-
this.setForwarderStatsQueue = function(queue) {
533+
this.setForwarderStatsQueue = (queue: IForwardingStatsData[]) =>
535534
mpInstance._Persistence.forwardingStatsBatches.forwardingStatsEventQueue = queue;
536-
};
537535

538536
// Processing forwarders is a 2 step process:
539537
// 1. Configure the kit
540538
// 2. Initialize the kit
541539
// There are 2 types of kits:
542540
// 1. UI-enabled kits
543541
// 2. Sideloaded kits.
544-
this.processForwarders = function(config, forwardingStatsCallback) {
542+
this.processForwarders = (config: SDKInitConfig, forwardingStatsCallback: Callback) => {
545543
if (!config) {
546544
mpInstance.Logger.warning(
547545
'No config was passed. Cannot process forwarders'
548546
);
549-
} else {
550-
this.processUIEnabledKits(config);
551-
this.processSideloadedKits(config);
552-
553-
self.initForwarders(
554-
mpInstance._Store.SDKConfig.identifyRequest.userIdentities,
555-
forwardingStatsCallback
556-
);
547+
return;
557548
}
549+
550+
this.processUIEnabledKits(config);
551+
this.processSideloadedKits(config);
552+
553+
self.initForwarders(
554+
mpInstance._Store.SDKConfig.identifyRequest.userIdentities,
555+
forwardingStatsCallback
556+
);
558557
};
559558

560559
// These are kits that are enabled via the mParticle UI.
@@ -563,14 +562,12 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
563562
// The kit configuration will be compared with the kit constructors to determine
564563
// if there is a match before being initialized.
565564
// Only kits that are configured properly can be active and used for kit forwarding.
566-
this.processUIEnabledKits = function(config) {
567-
let kits = this.returnKitConstructors();
565+
this.processUIEnabledKits = (config: SDKInitConfig) => {
566+
const kits: Dictionary<RegisteredKit> = this.returnKitConstructors();
568567

569568
try {
570-
if (Array.isArray(config.kitConfigs) && config.kitConfigs.length) {
571-
config.kitConfigs.forEach(function(kitConfig) {
572-
self.configureUIEnabledKit(kitConfig, kits);
573-
});
569+
if (Array.isArray(config.kitConfigs) && !isEmpty(config.kitConfigs)) {
570+
config.kitConfigs.forEach((kitConfig) => self.configureUIEnabledKit(kitConfig, kits));
574571
}
575572
} catch (e) {
576573
mpInstance.Logger.error(
@@ -580,7 +577,8 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
580577
}
581578
};
582579

583-
this.returnKitConstructors = function() {
580+
this.returnKitConstructors = () => {
581+
// FIXME: Try to set this up with registered kits or something similar
584582
let kits = {};
585583
// If there are kits inside of mpInstance._Store.SDKConfig.kits, then mParticle is self hosted
586584
if (!isEmpty(mpInstance._Store.SDKConfig.kits)) {
@@ -608,26 +606,24 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
608606
return kits;
609607
};
610608

611-
this.configureUIEnabledKit = function(configuration, kits) {
612-
let newKit = null;
613-
const config = configuration;
609+
this.configureUIEnabledKit = (config: IKitConfigs, kits: Dictionary<RegisteredKit>) => {
610+
let newKit: IMPForwarder | null = null;
611+
const { SDKConfig } = mpInstance._Store;
612+
613+
for (let kitName in kits) {
614+
const { suffix, name, isDebug, isSandbox } = config;
614615

615-
for (let name in kits) {
616616
// Configs are returned with suffixes also. We need to consider the
617617
// config suffix here to match the constructor suffix
618-
let kitNameWithConfigSuffix;
619-
if (config.suffix) {
620-
kitNameWithConfigSuffix = `${config.name}-${config.suffix}`;
621-
}
618+
const kitNameWithConfigSuffix: string = suffix ? `${name}-${suffix}` : undefined;
622619

623-
if (name === kitNameWithConfigSuffix || name === config.name) {
624-
if (
625-
config.isDebug ===
626-
mpInstance._Store.SDKConfig.isDevelopmentMode ||
627-
config.isSandbox ===
628-
mpInstance._Store.SDKConfig.isDevelopmentMode
629-
) {
630-
newKit = this.returnConfiguredKit(kits[name], config);
620+
const isDevelopmentMode: boolean =
621+
isDebug === SDKConfig.isDevelopmentMode ||
622+
isSandbox === SDKConfig.isDevelopmentMode;
623+
624+
if (kitName === kitNameWithConfigSuffix || kitName === name) {
625+
if (isDevelopmentMode) {
626+
newKit = this.returnConfiguredKit(kits[kitName], config);
631627

632628
mpInstance._Store.configuredForwarders.push(newKit);
633629
break;
@@ -643,20 +639,24 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
643639
// In the future, when all kits are moved to the mpConfig rather than
644640
// there being a separate process for MP configured kits and
645641
// sideloaded kits, this will need to be refactored.
646-
this.processSideloadedKits = function(mpConfig) {
642+
// FIXME: Fix types here
643+
this.processSideloadedKits = (mpConfig: SDKInitConfig) => {
647644
try {
648645
if (Array.isArray(mpConfig.sideloadedKits)) {
649646
const registeredSideloadedKits: KitRegistrationConfig = { kits: {} };
647+
648+
// FIXME: Expected type causes error
650649
const unregisteredSideloadedKits = mpConfig.sideloadedKits;
651650

652-
unregisteredSideloadedKits.forEach(function(unregisteredKit) {
651+
// FIXME: Define type
652+
unregisteredSideloadedKits.forEach((unregisteredKit) => {
653653
try {
654654
// Register each sideloaded kit, which adds a key of the sideloaded kit name
655655
// and a value of the sideloaded kit constructor.
656656
unregisteredKit.kitInstance.register(
657657
registeredSideloadedKits
658658
);
659-
const kitName = unregisteredKit.kitInstance.name;
659+
const kitName: string = unregisteredKit.kitInstance.name;
660660
// Then add the kit filters to each registered kit.
661661
registeredSideloadedKits.kits[kitName].filters =
662662
unregisteredKit.filterDictionary;
@@ -691,7 +691,7 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
691691
};
692692

693693
// kits can be included via mParticle UI, or via sideloaded kit config API
694-
this.configureSideloadedKit = function(kitConstructor) {
694+
this.configureSideloadedKit = (kitConstructor: RegisteredKit) => {
695695
mpInstance._Store.configuredForwarders.push(
696696
// FIXME: Figure out why filters should be typed as IKitConfigs
697697
this.returnConfiguredKit(kitConstructor, kitConstructor.filters as IKitConfigs)
@@ -735,21 +735,21 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
735735
return newForwarder;
736736
};
737737

738-
this.configurePixel = function(settings) {
738+
this.configurePixel = (settings: IPixelConfiguration) => {
739+
const { SDKConfig } = mpInstance._Store;
740+
739741
if (
740-
settings.isDebug ===
741-
mpInstance._Store.SDKConfig.isDevelopmentMode ||
742-
settings.isProduction !==
743-
mpInstance._Store.SDKConfig.isDevelopmentMode
742+
settings.isDebug === SDKConfig.isDevelopmentMode ||
743+
settings.isProduction !== SDKConfig.isDevelopmentMode
744744
) {
745745
mpInstance._Store.pixelConfigurations.push(settings);
746746
}
747747
};
748748

749-
this.processPixelConfigs = function(config) {
749+
this.processPixelConfigs = (config: SDKInitConfig) => {
750750
try {
751751
if (!isEmpty(config.pixelConfigs)) {
752-
config.pixelConfigs.forEach(function(pixelConfig) {
752+
config.pixelConfigs.forEach((pixelConfig: IPixelConfiguration) => {
753753
self.configurePixel(pixelConfig);
754754
});
755755
}
@@ -761,9 +761,9 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
761761
}
762762
};
763763

764-
this.sendSingleForwardingStatsToServer = async forwardingStatsData => {
764+
this.sendSingleForwardingStatsToServer = async (forwardingStatsData: IForwardingStatsData) => {
765765
// https://go.mparticle.com/work/SQDSDKS-6568
766-
const fetchPayload = {
766+
const fetchPayload: IFetchPayload = {
767767
method: 'post',
768768
body: JSON.stringify(forwardingStatsData),
769769
headers: {
@@ -772,9 +772,9 @@ export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWe
772772
},
773773
};
774774

775-
const response = await this.forwarderStatsUploader.upload(fetchPayload);
775+
const response: Response = await this.forwarderStatsUploader.upload(fetchPayload);
776776

777-
let message;
777+
let message: string;
778778
// This is a fire and forget, so we only need to log the response based on the code, and not return any response body
779779
if (response.status === 202) {
780780
// https://go.mparticle.com/work/SQDSDKS-6670

0 commit comments

Comments
 (0)