Skip to content

Commit 6d86697

Browse files
refactor: Migrate mParticle Instance to TypeScript
1 parent c9a2723 commit 6d86697

40 files changed

+354
-294
lines changed

src/apiClient.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import Constants from './constants';
22
import Types from './types';
33
import { BatchUploader } from './batchUploader';
4-
import { MParticleWebSDK, SDKEvent, SDKDataPlan } from './sdkRuntimeModels';
4+
import { SDKEvent, SDKDataPlan } from './sdkRuntimeModels';
55
import KitBlocker from './kitBlocking';
6-
import { Dictionary, getRampNumber, isEmpty, parseNumber } from './utils';
6+
import { Dictionary, isEmpty, parseNumber } from './utils';
77
import { IUploadObject } from './serverModel';
88
import { MPForwarder } from './forwarders.interfaces';
99
import { IMParticleUser, ISDKUserAttributes } from './identity-user-interfaces';
1010
import { AsyncUploader, FetchUploader, XHRUploader } from './uploaders';
11+
import { IMParticleWebSDKInstance } from './mp-instance';
1112

1213
export interface IAPIClient {
1314
uploader: BatchUploader | null;
@@ -43,7 +44,7 @@ export interface IForwardingStatsData {
4344

4445
export default function APIClient(
4546
this: IAPIClient,
46-
mpInstance: MParticleWebSDK,
47+
mpInstance: IMParticleWebSDKInstance,
4748
kitBlocker: KitBlocker
4849
) {
4950
this.uploader = null;

src/batchUploader.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Batch } from '@mparticle/event-models';
22
import Constants from './constants';
3-
import { SDKEvent, MParticleWebSDK, SDKLoggerApi } from './sdkRuntimeModels';
3+
import { SDKEvent, SDKLoggerApi } from './sdkRuntimeModels';
44
import { convertEvents } from './sdkToEventsApiConverter';
55
import { MessageType } from './types';
66
import { getRampNumber, isEmpty } from './utils';
@@ -12,6 +12,7 @@ import {
1212
IFetchPayload,
1313
} from './uploaders';
1414
import { IMParticleUser } from './identity-user-interfaces';
15+
import { IMParticleWebSDKInstance } from './mp-instance';
1516

1617
/**
1718
* BatchUploader contains all the logic to store/retrieve events and batches
@@ -36,7 +37,7 @@ export class BatchUploader {
3637
uploadIntervalMillis: number;
3738
eventsQueuedForProcessing: SDKEvent[];
3839
batchesQueuedForProcessing: Batch[];
39-
mpInstance: MParticleWebSDK;
40+
mpInstance: IMParticleWebSDKInstance;
4041
uploadUrl: string;
4142
batchingEnabled: boolean;
4243
private eventVault: SessionStorageVault<SDKEvent[]>;
@@ -46,10 +47,10 @@ export class BatchUploader {
4647

4748
/**
4849
* Creates an instance of a BatchUploader
49-
* @param {MParticleWebSDK} mpInstance - the mParticle SDK instance
50+
* @param {IMParticleWebSDKInstance} mpInstance - the mParticle SDK instance
5051
* @param {number} uploadInterval - the desired upload interval in milliseconds
5152
*/
52-
constructor(mpInstance: MParticleWebSDK, uploadInterval: number) {
53+
constructor(mpInstance: IMParticleWebSDKInstance, uploadInterval: number) {
5354
this.mpInstance = mpInstance;
5455
this.uploadIntervalMillis = uploadInterval;
5556
this.batchingEnabled =
@@ -208,7 +209,7 @@ export class BatchUploader {
208209
private static createNewBatches(
209210
sdkEvents: SDKEvent[],
210211
defaultUser: IMParticleUser,
211-
mpInstance: MParticleWebSDK
212+
mpInstance: IMParticleWebSDKInstance
212213
): Batch[] | null {
213214
if (!defaultUser || !sdkEvents || !sdkEvents.length) {
214215
return null;
@@ -280,9 +281,9 @@ export class BatchUploader {
280281
* @param triggerFuture whether to trigger the loop again - for manual/forced uploads this should be false
281282
* @param useBeacon whether to use the beacon API - used when the page is being unloaded
282283
*/
283-
private async prepareAndUpload(
284-
triggerFuture: boolean,
285-
useBeacon: boolean
284+
public async prepareAndUpload(
285+
triggerFuture: boolean = false,
286+
useBeacon: boolean = false,
286287
): Promise<void> {
287288
// Fetch current user so that events can be grouped by MPID
288289
const currentUser = this.mpInstance.Identity.getCurrentUser();

src/configAPIClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { DataPlanConfig } from '@mparticle/web-sdk';
22
import {
33
BooleanStringLowerCase,
44
DataPlanResult,
5-
MParticleWebSDK,
65
SDKEventCustomFlags,
76
SDKInitConfig,
87
} from './sdkRuntimeModels';
@@ -14,6 +13,7 @@ import {
1413
XHRUploader,
1514
} from './uploaders';
1615
import { IPixelConfiguration } from './cookieSyncManager';
16+
import { IMParticleWebSDKInstance } from './mp-instance';
1717

1818
export interface IKitConfigs extends IKitFilterSettings {
1919
name: string;
@@ -82,7 +82,7 @@ export interface IConfigResponse {
8282
export interface IConfigAPIClient {
8383
apiKey: string;
8484
config: SDKInitConfig;
85-
mpInstance: MParticleWebSDK;
85+
mpInstance: IMParticleWebSDKInstance;
8686
getSDKConfiguration: () => Promise<IConfigResponse>;
8787
}
8888

@@ -113,7 +113,7 @@ export default function ConfigAPIClient(
113113
this: IConfigAPIClient,
114114
apiKey: string,
115115
config: SDKInitConfig,
116-
mpInstance: MParticleWebSDK
116+
mpInstance: IMParticleWebSDKInstance
117117
): void {
118118
const baseUrl = 'https://' + mpInstance._Store.SDKConfig.configUrl;
119119
const { isDevelopmentMode } = config;

src/consent.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import {
44
GDPRConsentState,
55
PrivacyConsentState,
66
} from '@mparticle/web-sdk';
7-
import { MParticleWebSDK } from './sdkRuntimeModels';
87
import { Dictionary, isObject } from './utils';
98
import KitFilterHelper from './kitFilterHelper';
109
import Constants from './constants';
1110
import { IMParticleUser } from './identity-user-interfaces';
11+
import { IMParticleWebSDKInstance } from './mp-instance';
1212

1313
const { CCPAPurpose } = Constants;
1414

@@ -36,12 +36,6 @@ export interface SDKConsentApi {
3636
createGDPRConsent: ICreatePrivacyConsentFunction;
3737
createCCPAConsent: ICreatePrivacyConsentFunction;
3838
createConsentState: (consentState?: ConsentState) => ConsentState;
39-
ConsentSerialization: IConsentSerialization;
40-
createPrivacyConsent: ICreatePrivacyConsentFunction;
41-
isEnabledForUserConsent: (
42-
consentRules: IConsentRules,
43-
user: IMParticleUser
44-
) => boolean;
4539
}
4640

4741
export interface IConsentSerialization {
@@ -120,7 +114,7 @@ export interface IConsent {
120114
ConsentSerialization: IConsentSerialization;
121115
}
122116

123-
export default function Consent(this: IConsent, mpInstance: MParticleWebSDK) {
117+
export default function Consent(this: IConsent, mpInstance: IMParticleWebSDKInstance) {
124118
const self = this;
125119

126120
// this function is called when consent is required to

src/cookieSyncManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
createCookieSyncUrl,
55
} from './utils';
66
import Constants from './constants';
7-
import { MParticleWebSDK } from './sdkRuntimeModels';
87
import { MPID } from '@mparticle/web-sdk';
98
import { IConsentRules } from './consent';
9+
import { IMParticleWebSDKInstance } from './mp-instance';
1010

1111
const { Messages } = Constants;
1212
const { InformationMessages } = Messages;
@@ -47,7 +47,7 @@ export interface ICookieSyncManager {
4747

4848
export default function CookieSyncManager(
4949
this: ICookieSyncManager,
50-
mpInstance: MParticleWebSDK
50+
mpInstance: IMParticleWebSDKInstance
5151
) {
5252
const self = this;
5353

src/events.interfaces.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export interface IEvents {
5353
logOptOut(): void;
5454
logProductActionEvent(
5555
productActionType: valueof<typeof ProductActionType>,
56-
product: SDKProduct,
56+
product: SDKProduct | SDKProduct[],
5757
attrs?: SDKEventAttrs,
5858
customFlags?: SDKEventCustomFlags,
5959
transactionAttributes?: TransactionAttributes,
@@ -68,13 +68,13 @@ export interface IEvents {
6868
): void;
6969
logPurchaseEvent(
7070
transactionAttributes: TransactionAttributes,
71-
product: SDKProduct,
71+
product: SDKProduct | SDKProduct[],
7272
attrs?: SDKEventAttrs,
7373
customFlags?: SDKEventCustomFlags
7474
): void;
7575
logRefundEvent(
7676
transactionAttributes: TransactionAttributes,
77-
product: SDKProduct,
77+
product: SDKProduct | SDKProduct[],
7878
attrs?: SDKEventAttrs,
7979
customFlags?: SDKEventCustomFlags
8080
): void;

src/identity-user-interfaces.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ export interface IdentityModifyResultBody {
115115
}
116116

117117
export interface mParticleUserCart {
118-
add(product: Product, logEvent: boolean): void;
119-
remove(product: Product, logEvent: boolean): void;
118+
add(product: SDKProduct | SDKProduct[], logEvent: boolean): void;
119+
remove(product: SDKProduct | SDKProduct[], logEvent: boolean): void;
120120
clear(): void;
121-
getCartProducts(): Product[];
121+
getCartProducts(): SDKProduct[];
122122
}
123123

124124
// https://go.mparticle.com/work/SQDSDKS-5196

src/identityApiClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
IdentityResultBody,
2525
IIdentityResponse,
2626
} from './identity-user-interfaces';
27-
import { MParticleWebSDK } from './sdkRuntimeModels';
27+
import { IMParticleWebSDKInstance } from './mp-instance';
2828

2929
const { HTTPCodes, Messages, IdentityMethods } = Constants;
3030

@@ -82,7 +82,7 @@ interface IAliasErrorResponse extends IdentityApiError {}
8282

8383
export default function IdentityAPIClient(
8484
this: IIdentityApiClient,
85-
mpInstance: MParticleWebSDK
85+
mpInstance: IMParticleWebSDKInstance
8686
) {
8787
this.sendAliasRequest = async function(
8888
aliasRequest: IAliasRequest,

src/kitBlocking.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SDKEvent, MParticleWebSDK, KitBlockerDataPlan, SDKProduct } from './sdk
33
import { BaseEvent, EventTypeEnum, CommerceEvent, ScreenViewEvent, CustomEvent } from '@mparticle/event-models';
44
import Types from './types'
55
import { DataPlanPoint } from '@mparticle/data-planning-models';
6+
import { IMParticleWebSDKInstance } from './mp-instance';
67

78
/*
89
TODO: Including this as a workaround because attempting to import it from
@@ -41,9 +42,9 @@ export default class KitBlocker {
4142
blockUserAttributes = false;
4243
blockUserIdentities = false;
4344
kitBlockingEnabled = false;
44-
mpInstance: MParticleWebSDK;
45+
mpInstance: IMParticleWebSDKInstance;
4546

46-
constructor(dataPlan: KitBlockerDataPlan, mpInstance: MParticleWebSDK) {
47+
constructor(dataPlan: KitBlockerDataPlan, mpInstance: IMParticleWebSDKInstance) {
4748
// if data plan is not requested, the data plan is {document: null}
4849
if (dataPlan && !dataPlan.document) {
4950
this.kitBlockingEnabled = false;

src/mockBatchCreator.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// This file is used ONLY for the mParticle ESLint plugin. It should NOT be used otherwise!
22

33
import ServerModel from './serverModel';
4-
import { SDKEvent, BaseEvent, MParticleWebSDK } from './sdkRuntimeModels';
4+
import { SDKEvent, BaseEvent } from './sdkRuntimeModels';
55
import { convertEvents } from './sdkToEventsApiConverter';
6-
import * as EventsApi from '@mparticle/event-models';
76
import { Batch } from '@mparticle/event-models';
8-
import { IMPSideloadedKit } from './sideloadedKit';
7+
import { IMParticleWebSDKInstance } from './mp-instance';
98

109
const mockFunction = function() {
1110
return null;
@@ -15,15 +14,15 @@ export default class _BatchValidator {
1514
return ({
1615
// Certain Helper, Store, and Identity properties need to be mocked to be used in the `returnBatch` method
1716
_Helpers: {
18-
sanitizeAttributes: window.mParticle.getInstance()._Helpers
17+
sanitizeAttributes: (window.mParticle.getInstance() as unknown as IMParticleWebSDKInstance)._Helpers
1918
.sanitizeAttributes,
2019
generateHash: function() {
2120
return 'mockHash';
2221
},
2322
generateUniqueId: function() {
2423
return 'mockId';
2524
},
26-
extend: window.mParticle.getInstance()._Helpers.extend,
25+
extend: (window.mParticle.getInstance() as unknown as IMParticleWebSDKInstance)._Helpers.extend,
2726
createServiceUrl: mockFunction,
2827
parseNumber: mockFunction,
2928
isObject: mockFunction,
@@ -118,7 +117,7 @@ export default class _BatchValidator {
118117
logLevel: 'none',
119118
setPosition: mockFunction,
120119
upload: mockFunction,
121-
} as unknown) as MParticleWebSDK;
120+
} as unknown) as IMParticleWebSDKInstance;
122121
}
123122

124123
private createSDKEventFunction(event): SDKEvent {

0 commit comments

Comments
 (0)