Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Migrate Modules to TypeScript #951

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ test/integrations/requirejs/test-requirejs-bundle.js
browserstack.err
local.log
test/cross-browser-testing/CBT-tests-es5.js
test/cross-browser-testing/CBT-tests.js
test/cross-browser-testing/CBT-tests.js
coverage/
89 changes: 0 additions & 89 deletions src/aliasRequestApiClient.ts

This file was deleted.

5 changes: 4 additions & 1 deletion src/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ export default function APIClient(
this.queueEventForBatchUpload(event);
}

if (event.EventName !== Types.MessageType.AppStateTransition) {
// https://go.mparticle.com/work/SQDSDKS-6935
// While Event Name is 'usually' a string, there are some cases where it is a number
// in that it could be a type of MessageType Enum
if (event.EventName as unknown as number !== Types.MessageType.AppStateTransition) {
if (kitBlocker && kitBlocker.kitBlockingEnabled) {
event = kitBlocker.createBlockedEvent(event);
}
Expand Down
4 changes: 2 additions & 2 deletions src/audienceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
FetchUploader,
XHRUploader,
AsyncUploader,
fetchPayload
IFetchPayload
} from './uploaders';
import Audience from './audience';

Expand Down Expand Up @@ -38,7 +38,7 @@ export default class AudienceManager {
public async sendGetUserAudienceRequest(mpid: string, callback: (userAudiences: IAudienceMemberships) => void) {
this.logger.verbose('Fetching user audiences from server');

const fetchPayload: fetchPayload = {
const fetchPayload: IFetchPayload = {
method: 'GET',
headers: {
Accept: '*/*',
Expand Down
50 changes: 28 additions & 22 deletions src/batchUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Batch } from '@mparticle/event-models';
import Constants from './constants';
import { SDKEvent, MParticleWebSDK, SDKLoggerApi } from './sdkRuntimeModels';
import { convertEvents } from './sdkToEventsApiConverter';
import Types from './types';
import { MessageType } from './types';
import { getRampNumber, isEmpty } from './utils';
import { SessionStorageVault, LocalStorageVault } from './vault';
import {
AsyncUploader,
FetchUploader,
XHRUploader,
fetchPayload,
IFetchPayload,
} from './uploaders';
import { IMParticleUser } from './identity-user-interfaces';

Expand Down Expand Up @@ -167,29 +167,35 @@ export class BatchUploader {
* @param event event that should be queued
*/
public queueEvent(event: SDKEvent): void {
if (!isEmpty(event)) {
this.eventsQueuedForProcessing.push(event);
if (this.offlineStorageEnabled && this.eventVault) {
this.eventVault.store(this.eventsQueuedForProcessing);
}
this.mpInstance.Logger.verbose(
`Queuing event: ${JSON.stringify(event)}`
);
this.mpInstance.Logger.verbose(
`Queued event count: ${this.eventsQueuedForProcessing.length}`
);
if (isEmpty(event)) {
return;
}

// TODO: Remove this check once the v2 code path is removed
// https://go.mparticle.com/work/SQDSDKS-3720
if (
!this.batchingEnabled ||
Types.TriggerUploadType[event.EventDataType]
) {
this.prepareAndUpload(false, false);
}
const { verbose } = this.mpInstance.Logger;

this.eventsQueuedForProcessing.push(event);
if (this.offlineStorageEnabled && this.eventVault) {
this.eventVault.store(this.eventsQueuedForProcessing);
}

verbose(`Queuing event: ${JSON.stringify(event)}`);
verbose(`Queued event count: ${this.eventsQueuedForProcessing.length}`);

if (this.shouldTriggerImmediateUpload(event.EventDataType)) {
this.prepareAndUpload(false, false);
}
}

// https://go.mparticle.com/work/SQDSDKS-3720
private shouldTriggerImmediateUpload (eventDataType: number): boolean {
const priorityEvents = [
MessageType.Commerce,
MessageType.UserIdentityChange,
] as const;

return !this.batchingEnabled || priorityEvents.includes(eventDataType as typeof priorityEvents[number]);
};

/**
* This implements crucial logic to:
* - bucket pending events by MPID, and then by Session, and upload individual batches for each bucket.
Expand Down Expand Up @@ -366,7 +372,7 @@ export class BatchUploader {
logger.verbose(`Batch count: ${uploads.length}`);

for (let i = 0; i < uploads.length; i++) {
const fetchPayload: fetchPayload = {
const fetchPayload: IFetchPayload = {
method: 'POST',
headers: {
Accept: BatchUploader.CONTENT_TYPE,
Expand Down
18 changes: 4 additions & 14 deletions src/configAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import {
import { Dictionary } from './utils';
import {
AsyncUploader,
fetchPayload,
IFetchPayload,
FetchUploader,
XHRUploader,
} from './uploaders';
import { IPixelConfiguration } from './cookieSyncManager.interfaces';

export interface IKitConfigs extends IKitFilterSettings {
name: string;
Expand Down Expand Up @@ -65,17 +66,6 @@ export interface IConsentRuleValue {
hasConsented: boolean;
}

export interface IPixelConfig {
name: string;
moduleId: number;
esId: number;
isDebug: boolean;
isProduction: boolean;
settings: Dictionary;
frequencyCap: number;
pixelUrl: string;
redirectUrl: string;
}

export interface IConfigResponse {
appName: string;
Expand All @@ -85,7 +75,7 @@ export interface IConfigResponse {
secureServiceUrl: string;
minWebviewBridgeVersion: number;
workspaceToken: string;
pixelConfigs: IPixelConfig[];
pixelConfigs: IPixelConfiguration[];
flags: SDKEventCustomFlags;
}

Expand Down Expand Up @@ -135,7 +125,7 @@ export default function ConfigAPIClient(

this.getSDKConfiguration = async (): Promise<IConfigResponse> => {
let configResponse: IConfigResponse;
const fetchPayload: fetchPayload = {
const fetchPayload: IFetchPayload = {
method: 'get',
headers: {
Accept: 'text/plain;charset=UTF-8',
Expand Down
4 changes: 4 additions & 0 deletions src/consent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export interface SDKConsentApi {
createConsentState: (consentState?: ConsentState) => ConsentState;
ConsentSerialization: IConsentSerialization;
createPrivacyConsent: ICreatePrivacyConsentFunction;
isEnabledForUserConsent: (
consentRules: IConsentRules,
user: IMParticleUser
) => boolean;
}

export interface IConsentSerialization {
Expand Down
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ const Constants = {
Login: 'login',
Identify: 'identify',
},

Environment: {
Development: 'development',
Production: 'production',
},
} as const;

export default Constants;
Expand Down
39 changes: 39 additions & 0 deletions src/cookieSyncManager.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { MPID } from '@mparticle/web-sdk';
import { Dictionary } from './utils';
import { IConsentRules } from './consent';

export type CookieSyncDates = Dictionary<number>;

export interface IPixelConfiguration {
name?: string;
moduleId: number;
esId?: number;
isDebug?: boolean;
isProduction?: boolean;
settings: Dictionary<string>;
frequencyCap: number;
pixelUrl: string;
redirectUrl: string;
filteringConsentRuleValues?: IConsentRules;
}
export interface ICookieSyncManager {
attemptCookieSync: (
previousMPID: MPID,
mpid: MPID,
mpidIsNotInCookies?: boolean
) => void;
performCookieSync: (
url: string,
moduleId: string,
mpid: MPID,
cookieSyncDates: CookieSyncDates,
filteringConsentRuleValues: IConsentRules,
mpidIsNotInCookies: boolean,
requiresConsent: boolean
) => void;
combineUrlWithRedirect: (
mpid: MPID,
pixelUrl: string,
redirectUrl: string
) => string;
}
Loading
Loading