Skip to content

Commit aa37ca0

Browse files
refactor: Create Distinct Kit Interfaces
1 parent b70bd20 commit aa37ca0

File tree

5 files changed

+35
-28
lines changed

5 files changed

+35
-28
lines changed

src/forwarders.interfaces.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
import { SDKEvent, SDKEventCustomFlags } from './sdkRuntimeModels';
22
import { Dictionary } from './utils';
3-
import { IKitConfigs } from './configAPIClient';
3+
import { IKitConfigs, IKitFilterSettings } from './configAPIClient';
44
import { IdentityApiData } from '@mparticle/web-sdk';
55
import {
66
IMParticleUser,
77
ISDKUserIdentity,
88
UserAttributes,
99
} from './identity-user-interfaces';
1010

11-
// TODO: https://go.mparticle.com/work/SQDSDKS-6035
12-
export type Kit = Dictionary;
11+
// TODO: https://go.mparticle.com/work/SQDSDKS-4475
1312
export type MPForwarder = Dictionary;
1413

1514
// The state of the kit when accessed via window.KitName via CDN
1615
// or imported as an NPM package
1716
export interface UnregisteredKit {
18-
register(config): void;
17+
constructor: () => void;
18+
register(config: KitRegistrationConfig): void;
19+
name: string;
20+
suffix?: string;
1921
}
2022

2123
// The state of the kit after being added to forwarderConstructors in the CDN
2224
// or after registered to SDKConfig.kits via NPM
2325
export interface RegisteredKit {
24-
constructor(): void;
25-
name: string;
26-
getId(): number;
26+
constructor: () => void;
27+
28+
// Applies to sideloaded kits only
29+
filters: IKitFilterSettings;
30+
}
31+
32+
// This is the subset of the SDKConfig.kits object that is used to register kits.
33+
export interface KitRegistrationConfig {
34+
kits: Dictionary<RegisteredKit>;
2735
}
2836

2937
// The state of the kit after being configured. This is what the kit looks like when acted on.
@@ -45,22 +53,22 @@ export interface ConfiguredKit
4553
onIdentifyComplete(
4654
user: IMParticleUser,
4755
filteredIdentityRequest: IdentityApiData
48-
): string | KitMappedMethodFailure;
56+
): string;
4957
onLoginComplete(
5058
user: IMParticleUser,
5159
filteredIdentityRequest: IdentityApiData
52-
): string | KitMappedMethodFailure;
60+
): string;
5361
onLogoutComplete(
5462
user: IMParticleUser,
5563
filteredIdentityRequest: IdentityApiData
56-
): string | KitMappedMethodFailure;
64+
): string;
5765
onModifyComplete(
5866
user: IMParticleUser,
5967
filteredIdentityRequest: IdentityApiData
60-
): string | KitMappedMethodFailure;
61-
onUserIdentified(user: IMParticleUser): string | KitMappedMethodFailure;
68+
): string;
69+
onUserIdentified(user: IMParticleUser): string;
6270
process(event: SDKEvent): string;
63-
setOptOut(isOptingOut: boolean): string | KitMappedMethodFailure;
71+
setOptOut(isOptingOut: boolean): string;
6472
removeUserAttribute(key: string): string;
6573
setUserAttribute(key: string, value: string): string;
6674
setUserIdentity(id: UserIdentityId, type: UserIdentityType): void;
@@ -69,9 +77,6 @@ export interface ConfiguredKit
6977
isSandbox: boolean;
7078
hasSandbox: boolean;
7179
}
72-
export interface KitMappedMethodFailure {
73-
error: string;
74-
}
7580

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

src/sdkRuntimeModels.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { IKitConfigs } from './configAPIClient';
1414
import { SDKConsentApi, SDKConsentState } from './consent';
1515
import MPSideloadedKit from './sideloadedKit';
1616
import { ISessionManager } from './sessionManager';
17-
import { Kit, MPForwarder } from './forwarders.interfaces';
17+
import { ConfiguredKit, MPForwarder, UnregisteredKit } from './forwarders.interfaces';
1818
import {
1919
SDKIdentityApi,
2020
IAliasCallback,
@@ -163,7 +163,7 @@ export interface SDKProduct {
163163

164164
// https://go.mparticle.com/work/SQDSDKS-6949
165165
export interface MParticleWebSDK {
166-
addForwarder(mockForwarder: MPForwarder): void;
166+
addForwarder(forwarder: UnregisteredKit): void;
167167
IdentityType: valueof<typeof IdentityType>;
168168
CommerceEventType: valueof<typeof CommerceEventType>;
169169
EventType: valueof<typeof EventType>;
@@ -179,7 +179,7 @@ export interface MParticleWebSDK {
179179
configurePixel(config: IPixelConfiguration): void;
180180
endSession(): void;
181181
init(apiKey: string, config: SDKInitConfig, instanceName?: string): void;
182-
_getActiveForwarders(): MPForwarder[];
182+
_getActiveForwarders(): ConfiguredKit[];
183183
_getIntegrationDelays(): IntegrationDelays;
184184
_setIntegrationDelay(module: number, shouldDelayIntegration: boolean): void;
185185
_setWrapperSDKInfo(name: WrapperSDKTypes, version: string): void;
@@ -266,7 +266,7 @@ export interface SDKInitConfig
266266
logLevel?: LogLevelType;
267267

268268
kitConfigs?: IKitConfigs[];
269-
kits?: Dictionary<Kit>;
269+
kits?: Dictionary<UnregisteredKit>;
270270
sideloadedKits?: MPForwarder[];
271271
dataPlanOptions?: KitBlockerOptions;
272272
flags?: Dictionary;

src/store.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
returnConvertedBoolean,
3131
} from './utils';
3232
import { IMinifiedConsentJSONObject, SDKConsentState } from './consent';
33-
import { Kit, MPForwarder } from './forwarders.interfaces';
33+
import { ConfiguredKit, MPForwarder, UnregisteredKit } from './forwarders.interfaces';
3434
import { IdentityCallback, UserAttributes } from './identity-user-interfaces';
3535
import {
3636
IGlobalStoreV2MinifiedKeys,
@@ -61,7 +61,7 @@ export interface SDKConfig {
6161
package?: string;
6262
flags?: IFeatureFlags;
6363
kitConfigs: IKitConfigs[];
64-
kits: Dictionary<Kit>;
64+
kits: Dictionary<UnregisteredKit>;
6565
logLevel?: LogLevelType;
6666
cookieDomain?: string;
6767
maxCookieSize?: number | undefined;
@@ -176,7 +176,7 @@ export interface IStore {
176176
isLocalStorageAvailable: boolean | null;
177177
storageName: string | null;
178178
prodStorageName: string | null;
179-
activeForwarders: MPForwarder[];
179+
activeForwarders: ConfiguredKit[];
180180
kits: Dictionary<MPForwarder>;
181181
sideloadedKits: MPForwarder[];
182182
configuredForwarders: MPForwarder[];

test/jest/sideloadedKit.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { UnregisteredKit } from '../../src/forwarders.interfaces';
55
import { IKitFilterSettings } from '../../src/configAPIClient';
66

77
const mockKitInstance: UnregisteredKit = {
8-
register: function() {}
8+
register: function() {},
9+
name: 'mock-kit',
10+
constructor: function() {},
911
};
1012

1113
describe('MPSideloadedKit', () => {

test/jest/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UnregisteredKit } from "../../src/forwarders.interfaces";
1+
import { ConfiguredKit, KitRegistrationConfig, RegisteredKit, UnregisteredKit } from "../../src/forwarders.interfaces";
22
import { SDKInitConfig } from "../../src/sdkRuntimeModels";
33

44
// export interface IMockForwarder {
@@ -37,11 +37,11 @@ export class MockForwarder {
3737
this.moduleId = id || 1;
3838
}
3939

40-
public register = (config: SDKInitConfig): void => {
40+
public register = (config: KitRegistrationConfig): void => {
4141
if (config.kits) {
4242
config.kits[this.name] = {
43-
constructor: this.constructor,
44-
};
43+
constructor: this.constructor as () => RegisteredKit,
44+
} as unknown as RegisteredKit;
4545
}
4646
}
4747

0 commit comments

Comments
 (0)