Skip to content

Commit 948c6a6

Browse files
refactor: Migrate Forwarder module to TypeScript
1 parent e0dda73 commit 948c6a6

20 files changed

+235
-63
lines changed

src/apiClient.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import Constants from './constants';
2-
import Types from './types';
2+
import Types, { MessageType } from './types';
33
import { BatchUploader } from './batchUploader';
44
import { SDKEvent, SDKDataPlan } from './sdkRuntimeModels';
55
import KitBlocker from './kitBlocking';
66
import { Dictionary, isEmpty, parseNumber } from './utils';
77
import { IUploadObject } from './serverModel';
8-
import { MPForwarder } from './forwarders.interfaces';
98
import { IMParticleUser, ISDKUserAttributes } from './identity-user-interfaces';
109
import { AsyncUploader, FetchUploader, XHRUploader } from './uploaders';
1110
import { IMParticleWebSDKInstance } from './mp-instance';
11+
import { IMPForwarder } from './forwarders.interfaces';
1212

1313
export interface IAPIClient {
1414
uploader: BatchUploader | null;
@@ -23,7 +23,7 @@ export interface IAPIClient {
2323
) => void;
2424
initializeForwarderStatsUploader: () => AsyncUploader;
2525
prepareForwardingStats: (
26-
forwarder: MPForwarder,
26+
forwarder: IMPForwarder,
2727
event: IUploadObject
2828
) => void;
2929
}
@@ -139,7 +139,7 @@ export default function APIClient(
139139
// https://go.mparticle.com/work/SQDSDKS-6935
140140
// While Event Name is 'usually' a string, there are some cases where it is a number
141141
// in that it could be a type of MessageType Enum
142-
if (event.EventName as unknown as number !== Types.MessageType.AppStateTransition) {
142+
if (event.EventName as unknown as number !== MessageType.AppStateTransition) {
143143
if (kitBlocker && kitBlocker.kitBlockingEnabled) {
144144
event = kitBlocker.createBlockedEvent(event);
145145
}
@@ -192,7 +192,7 @@ export default function APIClient(
192192
};
193193

194194
this.prepareForwardingStats = function(
195-
forwarder: MPForwarder,
195+
forwarder: IMPForwarder,
196196
event:SDKEvent,
197197
) : void {
198198
let forwardingStatsData: IForwardingStatsData;
@@ -205,8 +205,8 @@ export default function APIClient(
205205
n: event.EventName,
206206
attrs: event.EventAttributes,
207207
sdk: event.SDKVersion,
208-
dt: event.EventDataType,
209-
et: event.EventCategory,
208+
dt: event.EventDataType as number,
209+
et: event.EventCategory as number,
210210
dbg: event.Debug,
211211
ct: event.Timestamp,
212212
eec: event.ExpandedEventCount,

src/configAPIClient.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from './uploaders';
1515
import { IPixelConfiguration } from './cookieSyncManager';
1616
import { IMParticleWebSDKInstance } from './mp-instance';
17+
import { UserAttributeFilters, UserIdentityFilters } from './forwarders.interfaces';
1718

1819
export interface IKitConfigs extends IKitFilterSettings {
1920
name: string;
@@ -26,15 +27,19 @@ export interface IKitConfigs extends IKitFilterSettings {
2627
settings: Dictionary;
2728
eventSubscriptionId: number;
2829
excludeAnonymousUser: boolean;
30+
31+
// https://go.mparticle.com/work/SQDSDKS-5156
32+
isSandbox?: boolean;
33+
hasSandbox?: boolean;
2934
}
3035

3136
export interface IKitFilterSettings {
3237
eventTypeFilters: number[];
3338
eventNameFilters: number[];
3439
screenNameFilters: number[];
3540
screenAttributeFilters: number[];
36-
userIdentityFilters: number[];
37-
userAttributeFilters: number[];
41+
userIdentityFilters: UserIdentityFilters;
42+
userAttributeFilters: UserAttributeFilters;
3843
attributeFilters: number[];
3944
filteringEventAttributeValue?: IFilteringEventAttributeValue;
4045
filteringUserAttributeValue?: IFilteringUserAttributeValue;

src/forwarders.interfaces.ts

Lines changed: 129 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
1-
import { SDKEvent, SDKEventCustomFlags } from './sdkRuntimeModels';
1+
import { SDKEvent, SDKEventCustomFlags, SDKInitConfig } from './sdkRuntimeModels';
22
import { Dictionary } from './utils';
3-
import { IKitConfigs } from './configAPIClient';
4-
import { IdentityApiData } from '@mparticle/web-sdk';
3+
import { IFilteringConsentRuleValues, IFilteringEventAttributeValue, IFilteringUserAttributeValue, IKitConfigs, IKitFilterSettings } from './configAPIClient';
4+
import { Callback, IdentityApiData, Logger, UserIdentities } from '@mparticle/web-sdk';
55
import {
66
IMParticleUser,
7+
ISDKUserAttributes,
78
ISDKUserIdentity,
89
UserAttributes,
910
} from './identity-user-interfaces';
11+
import { IForwardingStatsData } from './apiClient';
12+
import { IPixelConfiguration } from './cookieSyncManager';
13+
import { IdentityAPIMethod } from './identity.interfaces';
14+
import { AsyncUploader } from './uploaders';
15+
import { IdentityType } from './types';
1016

1117
// TODO: https://go.mparticle.com/work/SQDSDKS-6035
12-
export type Kit = Dictionary;
18+
export interface Kit {
19+
// constructor?: new () => IMPForwarder;
20+
constructor: () => any;
21+
name: string;
22+
getId: () => number, // Should this be optional?
23+
suffix?: string,
24+
};
25+
26+
// TODO: Get rid of this
1327
export type MPForwarder = Dictionary;
1428

1529
// The state of the kit when accessed via window.KitName via CDN
1630
// or imported as an NPM package
17-
export interface UnregisteredKit {
31+
export interface UnregisteredKit{
32+
// FIXME: Consider renaming config to kit config?
1833
register(config): void;
34+
name: string;
1935
}
2036

2137
// The state of the kit after being added to forwarderConstructors in the CDN
2238
// or after registered to SDKConfig.kits via NPM
2339
export interface RegisteredKit {
2440
constructor(): void;
41+
// suffix?: string;
2542
name: string;
2643
getId(): number;
44+
filters: IKitFilterSettings;
2745
}
2846

2947
// The state of the kit after being configured. This is what the kit looks like when acted on.
@@ -80,3 +98,109 @@ export type forwardingStatsCallback = (
8098
forwarder: ConfiguredKit,
8199
event: SDKEvent
82100
) => void;
101+
102+
103+
export type UserIdentityFilters = typeof IdentityType[];
104+
export type UserAttributeFilters = string[];
105+
106+
interface ForwarderSettings {
107+
PriorityValue?: number;
108+
}
109+
110+
export interface IMPForwarder {
111+
112+
// @deprecated
113+
setForwarderUserIdentities: (userIdentities: UserIdentities) => void;
114+
115+
setForwarderOnUserIdentified: (user: IMParticleUser) => void;
116+
setForwarderOnIdentityComplete: (user: IMParticleUser, identityMethod: IdentityAPIMethod) => void;
117+
handleForwarderUserAttributes: (functionNameKey: string, key: string, value: string) => void;
118+
id: number;
119+
settings: ForwarderSettings;
120+
forwarderStatsUploader: AsyncUploader;
121+
isInitialized: boolean;
122+
filteringConsentRuleValues: IFilteringConsentRuleValues;
123+
filteringUserAttributeValue: IFilteringUserAttributeValue;
124+
filteringEventAttributeValue: IFilteringEventAttributeValue;
125+
excludeAnonymousUser: boolean;
126+
userIdentityFilters: UserIdentityFilters;
127+
userAttributeFilters: UserAttributeFilters;
128+
initialized: boolean;
129+
logger: Logger;
130+
131+
suffix?: string;
132+
133+
eventSubscriptionId: number;
134+
135+
eventNameFilters: number[];
136+
eventTypeFilters: number[];
137+
attributeFilters: number[];
138+
139+
screenNameFilters: number[];
140+
screenAttributeFilters: number[];
141+
142+
143+
// Side loaded kit functioanlity in Forwarder methods
144+
kitInstance: UnregisteredKit;
145+
146+
// https://go.mparticle.com/work/SQDSDKS-5156
147+
isSandbox?: boolean;
148+
hasSandbox?: boolean;
149+
isVisible?: boolean;
150+
151+
configureSideloadedKit: (kitConstructor: RegisteredKit) => void;
152+
153+
sendSingleForwardingStatsToServer: (forwardingStatsData: IForwardingStatsData) => void;
154+
applyToForwarders: (functionName: string, functionArgs: any[]) => void;
155+
sendEventToForwarders: (event: SDKEvent) => void;
156+
processPixelConfigs: (pixelConfigs: SDKInitConfig) => void;
157+
configurePixel: (pixelConfig: IPixelConfiguration) => void;
158+
returnConfiguredKit: (forwarder: RegisteredKit, config: IKitConfigs) => IMPForwarder;
159+
160+
processSideloadedKits: (mpConfig: SDKInitConfig) => void;
161+
162+
init: (
163+
setting: ForwarderSettings,
164+
forwarderSettingsCallback: Callback,
165+
testMode: boolean,
166+
trackerId: string | number | null,
167+
filteredUserAttributes: ISDKUserAttributes,
168+
filteredUserIdentities: ISDKUserIdentity[],
169+
appVersion: string,
170+
appName: string,
171+
customFlags: SDKEventCustomFlags,
172+
clientId: string
173+
) => void;
174+
175+
initForwarders: (userIdentities: UserIdentities, forwarderStatsCallback: Callback) => void;
176+
isEnabledForUserAttributes: (filterObject?: IFilteringUserAttributeValue, user?: IMParticleUser) => boolean;
177+
isEnabledForUnknownUser: (excludeAnonymousUserBoolean: boolean, user: IMParticleUser) => boolean;
178+
179+
process?: (event: SDKEvent) => string | void;
180+
name?: string;
181+
setUserAttribute?: (key: string, value: string) => string | void;
182+
removeUserAttribute?: (key: string) => string | void;
183+
setUserIdentity?: (identity: string, type: number) => string | void;
184+
185+
// Techically these do not return a value, but we sometimes
186+
// debug message as a string
187+
onUserIdentified?: (user: IMParticleUser, identityApiData?: IdentityApiData) => string;
188+
onIdentifyComplete?: (user: IMParticleUser, identityApiData: IdentityApiData) => string;
189+
onLoginComplete?: (user: IMParticleUser, identityApiData: IdentityApiData) => string;
190+
onLogoutComplete?: (user: IMParticleUser, identityApiData: IdentityApiData) => string;
191+
onModifyComplete?: (user: IMParticleUser, identityApiData: IdentityApiData) => string;
192+
setOptOut: (optOut: boolean) => string;
193+
194+
getForwarderStatsQueue: () => IForwardingStatsData[];
195+
setForwarderStatsQueue: (queue: IForwardingStatsData[]) => void;
196+
processForwarders: (config: SDKInitConfig, forwardingStatsCallback: Callback) => void;
197+
processUIEnabledKits: (config: SDKInitConfig) => void;
198+
returnKitConstructors: () => Dictionary<RegisteredKit>;
199+
configureUIEnabledKit: (config: IKitConfigs, kitConstructor: Dictionary<RegisteredKit>) => void;
200+
201+
getKitConstructors: () => RegisteredKit[];
202+
getKitInstance: (kitName: string) => UnregisteredKit;
203+
getKitInstanceByConstructor: (kitConstructor: RegisteredKit) => UnregisteredKit;
204+
getKitInstanceByConstructorName: (kitConstructorName: string) => UnregisteredKit;
205+
getKitInstanceByConstructorSuffix: (kitConstructorSuffix: string) => UnregisteredKit;
206+
}

src/forwarders.js renamed to src/forwarders.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import Types from './types';
1+
import Types, { EventType } from './types';
22
import filteredMparticleUser from './filteredMparticleUser';
3-
import { isEmpty } from './utils';
3+
import { inArray, isEmpty, valueof } from './utils';
44
import KitFilterHelper from './kitFilterHelper';
55
import Constants from './constants';
66
import APIClient from './apiClient';
7+
import { IMPForwarder } from './forwarders.interfaces';
8+
import { IMParticleWebSDKInstance } from './mp-instance';
9+
import KitBlocker from './kitBlocking';
10+
import { IKitConfigs } from './configAPIClient';
711

812
const { Modify, Identify, Login, Logout } = Constants.IdentityMethods;
913

10-
export default function Forwarders(mpInstance, kitBlocker) {
14+
export default function Forwarders(this: IMPForwarder, mpInstance: IMParticleWebSDKInstance, kitBlocker: KitBlocker) {
1115
var self = this;
1216
this.forwarderStatsUploader = new APIClient(
1317
mpInstance,
@@ -169,7 +173,7 @@ export default function Forwarders(mpInstance, kitBlocker) {
169173
mpInstance.Logger.verbose(result);
170174
}
171175
} catch (e) {
172-
mpInstance.Logger.verbose(e);
176+
mpInstance.Logger.verbose(e as string);
173177
}
174178
}
175179
});
@@ -184,7 +188,7 @@ export default function Forwarders(mpInstance, kitBlocker) {
184188
if (event.UserIdentities && event.UserIdentities.length) {
185189
event.UserIdentities.forEach(function(userIdentity, i) {
186190
if (
187-
mpInstance._Helpers.inArray(
191+
inArray(
188192
filterList,
189193
KitFilterHelper.hashUserIdentity(
190194
userIdentity.Type
@@ -215,15 +219,15 @@ export default function Forwarders(mpInstance, kitBlocker) {
215219
attrName
216220
);
217221

218-
if (mpInstance._Helpers.inArray(filterList, hash)) {
222+
if (inArray(filterList, hash)) {
219223
delete event.EventAttributes[attrName];
220224
}
221225
}
222226
}
223227
},
224228
inFilteredList = function(filterList, hash) {
225229
if (filterList && filterList.length) {
226-
if (mpInstance._Helpers.inArray(filterList, hash)) {
230+
if (inArray(filterList, hash)) {
227231
return true;
228232
}
229233
}
@@ -242,10 +246,13 @@ export default function Forwarders(mpInstance, kitBlocker) {
242246
) {
243247
hashedEventName = KitFilterHelper.hashEventName(
244248
event.EventName,
245-
event.EventCategory
246-
);
249+
250+
// FIXME: Set up union of EventType and EventCategory
251+
event.EventCategory as valueof<typeof EventType>
252+
);
247253
hashedEventType = KitFilterHelper.hashEventType(
248-
event.EventCategory
254+
// FIXME: Set up union of EventType and EventCategory
255+
event.EventCategory as valueof<typeof EventType>
249256
);
250257

251258
for (
@@ -260,6 +267,8 @@ export default function Forwarders(mpInstance, kitBlocker) {
260267
// Supported message types for attribute forwarding rules are defined in the forwardingRuleMessageTypes array
261268

262269
if (
270+
// FIXME:
271+
// @ts-expect-error
263272
forwardingRuleMessageTypes.indexOf(event.EventDataType) >
264273
-1 &&
265274
mpInstance._Store.activeForwarders[i]
@@ -443,7 +452,7 @@ export default function Forwarders(mpInstance, kitBlocker) {
443452
mpInstance.Logger.verbose(result);
444453
}
445454
} catch (e) {
446-
mpInstance.Logger.error(e);
455+
mpInstance.Logger.error(e as string);
447456
}
448457
});
449458
};
@@ -710,11 +719,12 @@ export default function Forwarders(mpInstance, kitBlocker) {
710719
// kits can be included via mParticle UI, or via sideloaded kit config API
711720
this.configureSideloadedKit = function(kitConstructor) {
712721
mpInstance._Store.configuredForwarders.push(
713-
this.returnConfiguredKit(kitConstructor, kitConstructor.filters)
722+
// FIXME: Figure out why filters should be typed as IKitConfigs
723+
this.returnConfiguredKit(kitConstructor, kitConstructor.filters as IKitConfigs)
714724
);
715725
};
716726

717-
this.returnConfiguredKit = function(forwarder, config = {}) {
727+
this.returnConfiguredKit = function(forwarder, config = {} as IKitConfigs) {
718728
const newForwarder = new forwarder.constructor();
719729
newForwarder.id = config.moduleId;
720730

src/identity-user-interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface ICart {
2929
// https://go.mparticle.com/work/SQDSDKS-5033
3030
// https://go.mparticle.com/work/SQDSDKS-6354
3131
export interface IMParticleUser extends User {
32-
getAllUserAttributes(): any;
32+
getAllUserAttributes(): UserAttributes;
3333
setUserTag(tagName: string, value?: any): void;
3434
setUserAttribute(key: string, value: any): void;
3535
getUserAudiences?(callback?: IdentityCallback): void;

src/kitBlocking.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { convertEvent } from './sdkToEventsApiConverter';
2-
import { SDKEvent, MParticleWebSDK, KitBlockerDataPlan, SDKProduct } from './sdkRuntimeModels';
2+
import { SDKEvent, KitBlockerDataPlan, SDKProduct } from './sdkRuntimeModels';
33
import { BaseEvent, EventTypeEnum, CommerceEvent, ScreenViewEvent, CustomEvent } from '@mparticle/event-models';
4-
import Types from './types'
4+
import Types, { CommerceEventType } from './types'
55
import { DataPlanPoint } from '@mparticle/data-planning-models';
66
import { IMParticleWebSDKInstance } from './mp-instance';
77

@@ -415,12 +415,12 @@ export default class KitBlocker {
415415
}
416416
if (matchedEvent) {
417417
switch (event.EventCategory) {
418-
case Types.CommerceEventType.ProductImpression:
418+
case CommerceEventType.ProductImpression:
419419
clonedEvent.ProductImpressions.forEach(impression=> {
420420
removeAttribute(matchedEvent, impression?.ProductList)
421421
});
422422
break;
423-
case Types.CommerceEventType.ProductPurchase:
423+
case CommerceEventType.ProductPurchase:
424424
removeAttribute(matchedEvent, clonedEvent.ProductAction?.ProductList)
425425
break;
426426
default:

0 commit comments

Comments
 (0)