Skip to content

Commit

Permalink
refactor: Create Identity and User Interfaces for TypeScript (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle authored May 8, 2024
1 parent 7e9eb0c commit 31569ed
Show file tree
Hide file tree
Showing 20 changed files with 635 additions and 193 deletions.
5 changes: 3 additions & 2 deletions src/apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import Constants from './constants';
import Types from './types';
import { BatchUploader } from './batchUploader';
import { MParticleUser, MParticleWebSDK, SDKEvent } from './sdkRuntimeModels';
import { MParticleWebSDK, SDKEvent } from './sdkRuntimeModels';
import KitBlocker from './kitBlocking';
import { Dictionary, getRampNumber, isEmpty, parseNumber } from './utils';
import { IUploadObject } from './serverModel';
import { MPForwarder } from './forwarders.interfaces';
import { IMParticleUser } from './identity-user-interfaces';

export type ForwardingStatsData = Dictionary<any>;

export interface IAPIClient {
uploader: BatchUploader | null;
queueEventForBatchUpload: (event: SDKEvent) => void;
processQueuedEvents: () => void;
appendUserInfoToEvents: (user: MParticleUser, events: SDKEvent[]) => void;
appendUserInfoToEvents: (user: IMParticleUser, events: SDKEvent[]) => void;
sendEventToServer: (event: SDKEvent, _options?: Dictionary<any>) => void;
sendSingleEventToServer: (event: SDKEvent) => void;
sendBatchForwardingStatsToServer: (
Expand Down
14 changes: 5 additions & 9 deletions src/batchUploader.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { Batch } from '@mparticle/event-models';
import Constants from './constants';
import {
SDKEvent,
MParticleUser,
MParticleWebSDK,
SDKLoggerApi,
} from './sdkRuntimeModels';
import { SDKEvent, MParticleWebSDK, SDKLoggerApi } from './sdkRuntimeModels';
import { convertEvents } from './sdkToEventsApiConverter';
import Types from './types';
import { getRampNumber, isEmpty } from './utils';
import { SessionStorageVault, LocalStorageVault } from './vault';
import {
import {
AsyncUploader,
FetchUploader,
XHRUploader,
fetchPayload
fetchPayload,
} from './uploaders';
import { IMParticleUser } from './identity-user-interfaces';

/**
* BatchUploader contains all the logic to store/retrieve events and batches
Expand Down Expand Up @@ -205,7 +201,7 @@ export class BatchUploader {
*/
private static createNewBatches(
sdkEvents: SDKEvent[],
defaultUser: MParticleUser,
defaultUser: IMParticleUser,
mpInstance: MParticleWebSDK
): Batch[] | null {
if (!defaultUser || !sdkEvents || !sdkEvents.length) {
Expand Down
7 changes: 4 additions & 3 deletions src/consent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
GDPRConsentState,
PrivacyConsentState,
} from '@mparticle/web-sdk';
import { MParticleUser, MParticleWebSDK } from './sdkRuntimeModels';
import { MParticleWebSDK } from './sdkRuntimeModels';
import { Dictionary, isObject } from './utils';
import KitFilterHelper from './kitFilterHelper';
import Constants from './constants';
import { IMParticleUser } from './identity-user-interfaces';

const { CCPAPurpose } = Constants;

Expand Down Expand Up @@ -108,7 +109,7 @@ export interface IConsentState extends ConsentState {
export interface IConsent {
isEnabledForUserConsent: (
consentRules: IConsentRules,
user: MParticleUser
user: IMParticleUser
) => boolean;
createPrivacyConsent: ICreatePrivacyConsentFunction;
createConsentState: (consentState?: ConsentState) => ConsentState;
Expand All @@ -123,7 +124,7 @@ export default function Consent(this: IConsent, mpInstance: MParticleWebSDK) {
// forwarder should be initialized
this.isEnabledForUserConsent = function(
consentRules: IConsentRules,
user: MParticleUser
user: IMParticleUser
): boolean {
if (
!consentRules ||
Expand Down
38 changes: 20 additions & 18 deletions src/forwarders.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {
MParticleUser,
SDKEvent,
SDKEventCustomFlags,
SDKUserIdentity,
} from './sdkRuntimeModels';
import { SDKEvent, SDKEventCustomFlags } from './sdkRuntimeModels';
import { Dictionary } from './utils';
import { IKitConfigs } from './configAPIClient';
import { UserAttributes } from './persistence.interfaces';
import { IdentityApiData } from '@mparticle/web-sdk';
import {
IMParticleUser,
ISDKUserIdentity,
UserAttributes,
} from './identity-user-interfaces';

// TODO: https://go.mparticle.com/work/SQDSDKS-6035
export type Kit = Dictionary;
Expand All @@ -19,7 +18,7 @@ export interface UnregisteredKit {
register(config): void;
}

// The state of the kit after being added to forwarderConstructors in the CDN
// The state of the kit after being added to forwarderConstructors in the CDN
// or after registered to SDKConfig.kits via NPM
export interface RegisteredKit {
constructor(): void;
Expand All @@ -37,47 +36,50 @@ export interface ConfiguredKit
testMode: boolean,
trackerId: string | null,
userAttributes: UserAttributes,
userIdentities: SDKUserIdentity,
userIdentities: ISDKUserIdentity,
appVersion: string,
appName: string,
customFlags: SDKEventCustomFlags,
clientId: string
): string;
onIdentifyComplete(
user: MParticleUser,
user: IMParticleUser,
filteredIdentityRequest: IdentityApiData
): string | KitMappedMethodFailure;
onLoginComplete(
user: MParticleUser,
user: IMParticleUser,
filteredIdentityRequest: IdentityApiData
): string | KitMappedMethodFailure;
onLogoutComplete(
user: MParticleUser,
user: IMParticleUser,
filteredIdentityRequest: IdentityApiData
): string | KitMappedMethodFailure;
onModifyComplete(
user: MParticleUser,
user: IMParticleUser,
filteredIdentityRequest: IdentityApiData
): string | KitMappedMethodFailure;
onUserIdentified(user: MParticleUser): string | KitMappedMethodFailure;
onUserIdentified(user: IMParticleUser): string | KitMappedMethodFailure;
process(event: SDKEvent): string;
setOptOut(isOptingOut: boolean): string | KitMappedMethodFailure;
removeUserAttribute(key: string): string;
setUserAttribute(key: string, value:string): string;
setUserAttribute(key: string, value: string): string;

// TODO: Convert type to enum during Identity migration
// https://go.mparticle.com/work/SQDSDKS-5218
setUserIdentity(id: UserIdentityId, type: UserIdentityType): void;

// TODO: https://go.mparticle.com/work/SQDSDKS-5156
isSandbox: boolean;
isSandbox: boolean;
hasSandbox: boolean;
}
export interface KitMappedMethodFailure {
error: string
error: string;
}

export type UserIdentityId = string;
export type UserIdentityType = number;

export type forwardingStatsCallback = (forwarder: ConfiguredKit, event: SDKEvent) => void;
export type forwardingStatsCallback = (
forwarder: ConfiguredKit,
event: SDKEvent
) => void;
64 changes: 64 additions & 0 deletions src/identity-user-interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
AllUserAttributes,
IdentityCallback,
Product,
User,
} from '@mparticle/web-sdk';
import { SDKIdentityTypeEnum } from './identity.interfaces';
import { MessageType } from './types.interfaces';
import { BaseEvent } from './sdkRuntimeModels';

// https://go.mparticle.com/work/SQDSDKS-5033
// https://go.mparticle.com/work/SQDSDKS-6354
export interface IMParticleUser extends User {
getAllUserAttributes(): any;
setUserTag(tagName: string, value?: any): void;
setUserAttribute(key: string, value: any): void;
getUserAudiences?(callback?: IdentityCallback): void;
}

export interface ISDKUserIdentity {
Identity: string;
Type: number;
}

export interface ISDKUserIdentityChanges {
New: ISDKUserIdentityChangeData;
Old: ISDKUserIdentityChangeData;
}

export interface ISDKUserIdentityChangeData {
IdentityType: SDKIdentityTypeEnum;
Identity: string;
CreatedThisBatch: boolean;
// https://go.mparticle.com/work/SQDSDKS-6438
Timestamp?: number;
}

export interface IUserIdentityChangeEvent extends BaseEvent {
messageType: MessageType.UserIdentityChange;
userIdentityChanges: ISDKUserIdentityChanges;
}

export interface ISDKUserAttributeChangeData {
UserAttributeName: string;
New: string;
Old: string;
Deleted: boolean;
IsNewAttribute: boolean;
}

export interface IUserAttributeChangeEvent extends BaseEvent {
messageType: MessageType.UserAttributeChange;
userAttributeChanges: ISDKUserAttributeChangeData;
}

export interface mParticleUserCart {
add(product: Product, logEvent: boolean): void;
remove(product: Product, logEvent: boolean): void;
clear(): void;
getCartProducts(): Product[];
}

// https://go.mparticle.com/work/SQDSDKS-5196
export type UserAttributes = AllUserAttributes;
2 changes: 1 addition & 1 deletion src/identity-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Dictionary, parseNumber, isObject, generateHash } from './utils';
import { BaseVault } from './vault';
import Types from './types';
import { IdentityApiData, UserIdentities, IdentityCallback } from '@mparticle/web-sdk';
import { IdentityAPIMethod, MParticleWebSDK } from './sdkRuntimeModels';
import { IdentityAPIMethod } from './identity.interfaces';

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

Expand Down
Loading

0 comments on commit 31569ed

Please sign in to comment.