@mparticle/web-sdk 2.23.9 → 2.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/store.ts CHANGED
@@ -20,7 +20,7 @@ import {
20
20
  SDKInitConfig,
21
21
  SDKProduct,
22
22
  } from './sdkRuntimeModels';
23
- import { isNumber, isDataPlanSlug, Dictionary } from './utils';
23
+ import { isNumber, isDataPlanSlug, Dictionary, parseNumber } from './utils';
24
24
  import { SDKConsentState } from './consent';
25
25
  import { Kit, MPForwarder } from './forwarders.interfaces';
26
26
 
@@ -43,7 +43,7 @@ export interface SDKConfig {
43
43
  appName?: string;
44
44
  appVersion?: string;
45
45
  package?: string;
46
- flags?: { [key: string]: string | number | boolean };
46
+ flags?: IFeatureFlags;
47
47
  kitConfigs: IKitConfigs[];
48
48
  kits: Dictionary<Kit>;
49
49
  logLevel?: LogLevelType;
@@ -89,8 +89,8 @@ function createSDKConfig(config: SDKInitConfig): SDKConfig {
89
89
  }
90
90
  }
91
91
 
92
- for (const prop in Constants.DefaultUrls) {
93
- sdkConfig[prop] = Constants.DefaultUrls[prop];
92
+ for (const prop in Constants.DefaultBaseUrls) {
93
+ sdkConfig[prop] = Constants.DefaultBaseUrls[prop];
94
94
  }
95
95
 
96
96
  return sdkConfig;
@@ -110,6 +110,15 @@ interface WrapperSDKInfo {
110
110
  isInfoSet: boolean;
111
111
  }
112
112
 
113
+ // https://go.mparticle.com/work/SQDSDKS-5954
114
+ export interface IFeatureFlags {
115
+ reportBatching?: string;
116
+ eventBatchingIntervalMillis?: number;
117
+ offlineStorage?: string;
118
+ directURLRouting?: boolean;
119
+ cacheIdentity?: boolean;
120
+ }
121
+
113
122
  // Temporary Interface until Store can be refactored as a class
114
123
  export interface IStore {
115
124
  isEnabled: boolean;
@@ -159,7 +168,8 @@ export interface IStore {
159
168
  export default function Store(
160
169
  this: IStore,
161
170
  config: SDKInitConfig,
162
- mpInstance: MParticleWebSDK
171
+ mpInstance: MParticleWebSDK,
172
+ apiKey?: string
163
173
  ) {
164
174
  const defaultStore: Partial<IStore> = {
165
175
  isEnabled: true,
@@ -209,12 +219,21 @@ export default function Store(
209
219
  for (var key in defaultStore) {
210
220
  this[key] = defaultStore[key];
211
221
  }
222
+ this.devToken = apiKey || null;
212
223
 
213
224
  this.integrationDelayTimeoutStart = Date.now();
214
225
 
215
- this.SDKConfig = createSDKConfig(config);
216
226
  // Set configuration to default settings
227
+ this.SDKConfig = createSDKConfig(config);
228
+
217
229
  if (config) {
230
+ if (!config.hasOwnProperty('flags')) {
231
+ this.SDKConfig.flags = {};
232
+ }
233
+
234
+ this.SDKConfig.flags = processFlags(config, this
235
+ .SDKConfig as SDKConfig);
236
+
218
237
  if (config.deviceId) {
219
238
  this.deviceId = config.deviceId;
220
239
  }
@@ -226,28 +245,14 @@ export default function Store(
226
245
  this.SDKConfig.isDevelopmentMode = false;
227
246
  }
228
247
 
229
- if (config.hasOwnProperty('v1SecureServiceUrl')) {
230
- this.SDKConfig.v1SecureServiceUrl = config.v1SecureServiceUrl;
231
- }
232
-
233
- if (config.hasOwnProperty('v2SecureServiceUrl')) {
234
- this.SDKConfig.v2SecureServiceUrl = config.v2SecureServiceUrl;
235
- }
236
-
237
- if (config.hasOwnProperty('v3SecureServiceUrl')) {
238
- this.SDKConfig.v3SecureServiceUrl = config.v3SecureServiceUrl;
239
- }
240
-
241
- if (config.hasOwnProperty('identityUrl')) {
242
- this.SDKConfig.identityUrl = config.identityUrl;
243
- }
244
-
245
- if (config.hasOwnProperty('aliasUrl')) {
246
- this.SDKConfig.aliasUrl = config.aliasUrl;
247
- }
248
+ const baseUrls: Dictionary<string> = processBaseUrls(
249
+ config,
250
+ this.SDKConfig.flags,
251
+ apiKey
252
+ );
248
253
 
249
- if (config.hasOwnProperty('configUrl')) {
250
- this.SDKConfig.configUrl = config.configUrl;
254
+ for (const baseUrlKeys in baseUrls) {
255
+ this.SDKConfig[baseUrlKeys] = baseUrls[baseUrlKeys];
251
256
  }
252
257
 
253
258
  if (config.hasOwnProperty('logLevel')) {
@@ -412,32 +417,114 @@ export default function Store(
412
417
  this.SDKConfig.onCreateBatch = undefined;
413
418
  }
414
419
  }
420
+ }
421
+ }
415
422
 
416
- if (!config.hasOwnProperty('flags')) {
417
- this.SDKConfig.flags = {};
418
- }
419
- if (
420
- !this.SDKConfig.flags.hasOwnProperty(
421
- Constants.FeatureFlags.EventBatchingIntervalMillis
422
- )
423
- ) {
424
- this.SDKConfig.flags[
425
- Constants.FeatureFlags.EventBatchingIntervalMillis
426
- ] = Constants.DefaultConfig.uploadInterval;
427
- }
428
- if (
429
- !this.SDKConfig.flags.hasOwnProperty(
430
- Constants.FeatureFlags.ReportBatching
431
- )
432
- ) {
433
- this.SDKConfig.flags[Constants.FeatureFlags.ReportBatching] = false;
434
- }
435
- if (
436
- !this.SDKConfig.flags.hasOwnProperty(
437
- Constants.FeatureFlags.OfflineStorage
438
- )
439
- ) {
440
- this.SDKConfig.flags[Constants.FeatureFlags.OfflineStorage] = 0;
441
- }
423
+ export function processFlags(
424
+ config: SDKInitConfig,
425
+ SDKConfig: SDKConfig
426
+ ): IFeatureFlags {
427
+ const flags: IFeatureFlags = {};
428
+ const {
429
+ ReportBatching,
430
+ EventBatchingIntervalMillis,
431
+ OfflineStorage,
432
+ DirectUrlRouting,
433
+ CacheIdentity,
434
+ } = Constants.FeatureFlags;
435
+
436
+ if (!config.flags) {
437
+ return {};
438
+ }
439
+
440
+ // Passed in config flags take priority over defaults
441
+ flags[ReportBatching] = config.flags[ReportBatching] || false;
442
+ // The server returns stringified numbers, sowe need to parse
443
+ flags[EventBatchingIntervalMillis] =
444
+ parseNumber(config.flags[EventBatchingIntervalMillis]) ||
445
+ Constants.DefaultConfig.uploadInterval;
446
+ flags[OfflineStorage] = config.flags[OfflineStorage] || '0';
447
+ flags[DirectUrlRouting] = config.flags[DirectUrlRouting] === 'True';
448
+ flags[CacheIdentity] = config.flags[CacheIdentity] === 'True';
449
+
450
+ return flags;
451
+ }
452
+
453
+ export function processBaseUrls(
454
+ config: SDKInitConfig,
455
+ flags: IFeatureFlags,
456
+ apiKey?: string
457
+ ): Dictionary<string> {
458
+ // an API key is not present in a webview only mode. In this case, no baseUrls are needed
459
+ if (!apiKey) {
460
+ return {};
461
+ }
462
+
463
+ // Set default baseUrls
464
+ let baseUrls: Dictionary<string>;
465
+
466
+ // When direct URL routing is false, update baseUrls based custom urls
467
+ // passed to the config
468
+ if (flags.directURLRouting) {
469
+ return processDirectBaseUrls(config, apiKey);
470
+ } else {
471
+ return processCustomBaseUrls(config);
442
472
  }
443
473
  }
474
+
475
+ function processCustomBaseUrls(config: SDKInitConfig): Dictionary<string> {
476
+ const defaultBaseUrls: Dictionary<string> = Constants.DefaultBaseUrls;
477
+ const newBaseUrls: Dictionary<string> = {};
478
+
479
+ // If there is no custo base url, we use the default base url
480
+ for (let baseUrlKey in defaultBaseUrls) {
481
+ newBaseUrls[baseUrlKey] =
482
+ config[baseUrlKey] || defaultBaseUrls[baseUrlKey];
483
+ }
484
+
485
+ return newBaseUrls;
486
+ }
487
+
488
+ function processDirectBaseUrls(
489
+ config: SDKInitConfig,
490
+ apiKey: string
491
+ ): Dictionary {
492
+ const defaultBaseUrls = Constants.DefaultBaseUrls;
493
+ const directBaseUrls: Dictionary<string> = {};
494
+ // When Direct URL Routing is true, we create a new set of baseUrls that
495
+ // include the silo in the urls. mParticle API keys are prefixed with the
496
+ // silo and a hyphen (ex. "us1-", "us2-", "eu1-"). us1 was the first silo,
497
+ // and before other silos existed, there were no prefixes and all apiKeys
498
+ // were us1. As such, if we split on a '-' and the resulting array length
499
+ // is 1, then it is an older APIkey that should route to us1.
500
+ // When splitKey.length is greater than 1, then splitKey[0] will be
501
+ // us1, us2, eu1, au1, or st1, etc as new silos are added
502
+ const DEFAULT_SILO = 'us1';
503
+ const splitKey: Array<string> = apiKey.split('-');
504
+ const routingPrefix: string =
505
+ splitKey.length <= 1 ? DEFAULT_SILO : splitKey[0];
506
+
507
+ for (let baseUrlKey in defaultBaseUrls) {
508
+ // Any custom endpoints passed to mpConfig will take priority over direct
509
+ // mapping to the silo. The most common use case is a customer provided CNAME.
510
+ if (baseUrlKey === 'configUrl') {
511
+ directBaseUrls[baseUrlKey] =
512
+ config[baseUrlKey] || defaultBaseUrls[baseUrlKey];
513
+ continue;
514
+ }
515
+
516
+ if (config.hasOwnProperty(baseUrlKey)) {
517
+ directBaseUrls[baseUrlKey] = config[baseUrlKey];
518
+ } else {
519
+ const urlparts = defaultBaseUrls[baseUrlKey].split('.');
520
+
521
+ directBaseUrls[baseUrlKey] = [
522
+ urlparts[0],
523
+ routingPrefix,
524
+ ...urlparts.slice(1),
525
+ ].join('.');
526
+ }
527
+ }
528
+
529
+ return directBaseUrls;
530
+ }
package/src/validators.ts CHANGED
@@ -8,8 +8,7 @@ import {
8
8
  } from './utils';
9
9
  import Constants from './constants';
10
10
  import { IdentityApiData } from '@mparticle/web-sdk';
11
-
12
- type IdentityAPIMethod = 'login' | 'logout' | 'identify' | 'modify';
11
+ import { IdentityAPIMethod } from './sdkRuntimeModels';
13
12
 
14
13
  type ValidationIdentitiesReturn = {
15
14
  valid: boolean;