@mparticle/web-sdk 2.78.0 → 2.79.1

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.
@@ -1,14 +1,72 @@
1
1
  import Types from './types';
2
2
  import Constants from './constants';
3
3
  import * as utils from './utils';
4
+ import { Dictionary } from './utils';
4
5
  import Validators from './validators';
5
6
  import KitFilterHelper from './kitFilterHelper';
7
+ import { IMParticleWebSDKInstance } from './mp-instance';
8
+ import { SDKHelpersApi } from './sdkRuntimeModels';
9
+ import { IMParticleUser, IdentityCallback, IdentityResult, ISDKUserIdentity } from './identity-user-interfaces';
10
+ import { IAliasResult } from './identity.interfaces';
11
+ import { AliasUsersCallback, MPID } from '@mparticle/web-sdk';
6
12
 
7
- var StorageNames = Constants.StorageNames;
13
+ const StorageNames = Constants.StorageNames;
8
14
 
9
- export default function Helpers(mpInstance) {
10
- var self = this;
11
- this.canLog = function() {
15
+ function appendFilteredUserIdentity(
16
+ filteredUserIdentities: ISDKUserIdentity[],
17
+ identity: ISDKUserIdentity,
18
+ userIdentityType: number
19
+ ): void {
20
+ if (userIdentityType === Types.IdentityType.CustomerId) {
21
+ filteredUserIdentities.unshift(identity);
22
+ } else {
23
+ filteredUserIdentities.push(identity);
24
+ }
25
+ }
26
+
27
+ function buildFilteredUserIdentities(
28
+ userIdentitiesObject: utils.Dictionary<string> | null | undefined,
29
+ filterList: number[],
30
+ inArray: (items: any[], value: any) => boolean
31
+ ): ISDKUserIdentity[] {
32
+ const filteredUserIdentities: ISDKUserIdentity[] = [];
33
+
34
+ if (!userIdentitiesObject || !Object.keys(userIdentitiesObject).length) {
35
+ return filteredUserIdentities;
36
+ }
37
+
38
+ for (const userIdentityName in userIdentitiesObject) {
39
+ if (!userIdentitiesObject.hasOwnProperty(userIdentityName)) {
40
+ continue;
41
+ }
42
+
43
+ const userIdentityType = Types.IdentityType.getIdentityType(
44
+ userIdentityName
45
+ );
46
+
47
+ if (inArray(filterList, userIdentityType)) {
48
+ continue;
49
+ }
50
+
51
+ appendFilteredUserIdentity(
52
+ filteredUserIdentities,
53
+ {
54
+ Type: userIdentityType,
55
+ Identity: userIdentitiesObject[userIdentityName],
56
+ },
57
+ userIdentityType
58
+ );
59
+ }
60
+
61
+ return filteredUserIdentities;
62
+ }
63
+
64
+ export default function Helpers(
65
+ this: SDKHelpersApi,
66
+ mpInstance: IMParticleWebSDKInstance
67
+ ): void {
68
+ const self = this;
69
+ this.canLog = function(): boolean {
12
70
  if (
13
71
  mpInstance._Store.isEnabled &&
14
72
  (mpInstance._Store.devToken ||
@@ -20,7 +78,7 @@ export default function Helpers(mpInstance) {
20
78
  return false;
21
79
  };
22
80
 
23
- this.getFeatureFlag = function(feature) {
81
+ this.getFeatureFlag = function(feature: string): boolean | string {
24
82
  if (mpInstance._Store.SDKConfig.flags.hasOwnProperty(feature)) {
25
83
  return mpInstance._Store.SDKConfig.flags[feature];
26
84
  }
@@ -28,12 +86,12 @@ export default function Helpers(mpInstance) {
28
86
  };
29
87
 
30
88
  this.invokeCallback = function(
31
- callback,
32
- code,
33
- body,
34
- mParticleUser,
35
- previousMpid
36
- ) {
89
+ callback: IdentityCallback,
90
+ code: number,
91
+ body: string,
92
+ mParticleUser?: IMParticleUser,
93
+ previousMpid?: MPID
94
+ ): void {
37
95
  if (!callback) {
38
96
  mpInstance.Logger.warning('There is no callback provided');
39
97
  }
@@ -51,9 +109,9 @@ export default function Helpers(mpInstance) {
51
109
  },
52
110
  getPreviousUser: function() {
53
111
  if (!previousMpid) {
54
- var users = mpInstance.Identity.getUsers();
55
- var mostRecentUser = users.shift();
56
- var currentUser =
112
+ const users = mpInstance.Identity.getUsers();
113
+ let mostRecentUser = users.shift();
114
+ const currentUser =
57
115
  mParticleUser ||
58
116
  mpInstance.Identity.getCurrentUser();
59
117
  if (
@@ -69,7 +127,7 @@ export default function Helpers(mpInstance) {
69
127
  return mpInstance.Identity.getUser(previousMpid);
70
128
  }
71
129
  },
72
- });
130
+ } as unknown as IdentityResult);
73
131
  }
74
132
  } catch (e) {
75
133
  mpInstance.Logger.error(
@@ -78,19 +136,23 @@ export default function Helpers(mpInstance) {
78
136
  }
79
137
  };
80
138
 
81
- this.invokeAliasCallback = function(callback, code, message) {
139
+ this.invokeAliasCallback = function(
140
+ callback: AliasUsersCallback,
141
+ code: number,
142
+ message?: string
143
+ ): void {
82
144
  if (!callback) {
83
145
  mpInstance.Logger.warning('There is no callback provided');
84
146
  }
85
147
  try {
86
148
  if (self.Validators.isFunction(callback)) {
87
- var callbackMessage = {
149
+ const callbackMessage: Dictionary = {
88
150
  httpCode: code,
89
151
  };
90
152
  if (message) {
91
153
  callbackMessage.message = message;
92
154
  }
93
- callback(callbackMessage);
155
+ callback(callbackMessage as IAliasResult);
94
156
  }
95
157
  } catch (e) {
96
158
  mpInstance.Logger.error(
@@ -101,12 +163,15 @@ export default function Helpers(mpInstance) {
101
163
 
102
164
  this.extend = utils.extend;
103
165
 
104
- this.createServiceUrl = function(secureServiceUrl, devToken) {
105
- var serviceScheme =
166
+ this.createServiceUrl = function(
167
+ secureServiceUrl: string,
168
+ devToken?: string
169
+ ): string {
170
+ const serviceScheme =
106
171
  window.mParticle && mpInstance._Store.SDKConfig.forceHttps
107
172
  ? 'https://'
108
173
  : window.location.protocol + '//';
109
- var baseUrl;
174
+ let baseUrl;
110
175
  if (mpInstance._Store.SDKConfig.forceHttps) {
111
176
  baseUrl = 'https://' + secureServiceUrl;
112
177
  } else {
@@ -118,8 +183,8 @@ export default function Helpers(mpInstance) {
118
183
  return baseUrl;
119
184
  };
120
185
 
121
- this.createXHR = function(cb) {
122
- var xhr;
186
+ this.createXHR = function(cb: () => void): XMLHttpRequest {
187
+ let xhr;
123
188
 
124
189
  try {
125
190
  xhr = new window.XMLHttpRequest();
@@ -129,11 +194,13 @@ export default function Helpers(mpInstance) {
129
194
 
130
195
  if (xhr && cb && 'withCredentials' in xhr) {
131
196
  xhr.onreadystatechange = cb;
132
- } else if (typeof window.XDomainRequest !== 'undefined') {
197
+ } else if (
198
+ typeof (window as Dictionary).XDomainRequest !== 'undefined'
199
+ ) {
133
200
  mpInstance.Logger.verbose('Creating XDomainRequest object');
134
201
 
135
202
  try {
136
- xhr = new window.XDomainRequest();
203
+ xhr = new (window as Dictionary).XDomainRequest();
137
204
  xhr.onload = cb;
138
205
  } catch (e) {
139
206
  mpInstance.Logger.error('Error creating XDomainRequest object');
@@ -143,41 +210,25 @@ export default function Helpers(mpInstance) {
143
210
  return xhr;
144
211
  };
145
212
 
146
- this.filterUserIdentities = function(userIdentitiesObject, filterList) {
147
- var filteredUserIdentities = [];
148
-
149
- if (userIdentitiesObject && Object.keys(userIdentitiesObject).length) {
150
- for (var userIdentityName in userIdentitiesObject) {
151
- if (userIdentitiesObject.hasOwnProperty(userIdentityName)) {
152
- var userIdentityType = Types.IdentityType.getIdentityType(
153
- userIdentityName
154
- );
155
- if (!self.inArray(filterList, userIdentityType)) {
156
- var identity = {
157
- Type: userIdentityType,
158
- Identity: userIdentitiesObject[userIdentityName],
159
- };
160
- if (
161
- userIdentityType === Types.IdentityType.CustomerId
162
- ) {
163
- filteredUserIdentities.unshift(identity);
164
- } else {
165
- filteredUserIdentities.push(identity);
166
- }
167
- }
168
- }
169
- }
170
- }
171
-
172
- return filteredUserIdentities;
213
+ this.filterUserIdentities = function(
214
+ userIdentitiesObject: Dictionary<string>,
215
+ filterList: number[]
216
+ ): ISDKUserIdentity[] {
217
+ return buildFilteredUserIdentities(
218
+ userIdentitiesObject,
219
+ filterList,
220
+ self.inArray
221
+ );
173
222
  };
174
223
 
175
224
  this.filterUserIdentitiesForForwarders =
176
225
  KitFilterHelper.filterUserIdentities;
177
226
  this.filterUserAttributes = KitFilterHelper.filterUserAttributes;
178
227
 
179
- this.isEventType = function(type) {
180
- for (var prop in Types.EventType) {
228
+ this.isEventType = function(
229
+ type: utils.valueof<typeof Types.EventType>
230
+ ): boolean {
231
+ for (const prop in Types.EventType) {
181
232
  if (Types.EventType.hasOwnProperty(prop)) {
182
233
  if (Types.EventType[prop] === type) {
183
234
  return true;
@@ -187,14 +238,17 @@ export default function Helpers(mpInstance) {
187
238
  return false;
188
239
  };
189
240
 
190
- this.sanitizeAttributes = function(attrs, name) {
241
+ this.sanitizeAttributes = function(
242
+ attrs: Dictionary,
243
+ name: string
244
+ ): Dictionary<string> | null {
191
245
  if (!attrs || !self.isObject(attrs)) {
192
246
  return null;
193
247
  }
194
248
 
195
- var sanitizedAttrs = {};
249
+ const sanitizedAttrs: Dictionary<string> = {};
196
250
 
197
- for (var prop in attrs) {
251
+ for (const prop in attrs) {
198
252
  // Make sure that attribute values are not objects or arrays, which are not valid
199
253
  if (
200
254
  attrs.hasOwnProperty(prop) &&
@@ -216,17 +270,17 @@ export default function Helpers(mpInstance) {
216
270
  };
217
271
 
218
272
  this.isDelayedByIntegration = function(
219
- delayedIntegrations,
220
- timeoutStart,
221
- now
222
- ) {
273
+ delayedIntegrations: Dictionary<boolean>,
274
+ timeoutStart: number,
275
+ now: number
276
+ ): boolean {
223
277
  if (
224
278
  now - timeoutStart >
225
279
  mpInstance._Store.SDKConfig.integrationDelayTimeout
226
280
  ) {
227
281
  return false;
228
282
  }
229
- for (var integration in delayedIntegrations) {
283
+ for (const integration in delayedIntegrations) {
230
284
  if (delayedIntegrations[integration] === true) {
231
285
  return true;
232
286
  } else {
@@ -236,7 +290,7 @@ export default function Helpers(mpInstance) {
236
290
  return false;
237
291
  };
238
292
 
239
- this.createMainStorageName = function(workspaceToken) {
293
+ this.createMainStorageName = function(workspaceToken: string): string {
240
294
  if (workspaceToken) {
241
295
  return StorageNames.currentStorageName + '_' + workspaceToken;
242
296
  } else {
@@ -1,10 +1,10 @@
1
1
  // Base64 encoder/decoder - http://www.webtoolkit.info/javascript_base64.html
2
- var Base64 = {
2
+ const Base64 = {
3
3
  _keyStr:
4
4
  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
5
5
 
6
6
  // Input must be a string
7
- encode: function encode(input) {
7
+ encode: function encode(input: string): string {
8
8
  try {
9
9
  if (window.btoa && window.atob) {
10
10
  return window.btoa(unescape(encodeURIComponent(input)));
@@ -15,10 +15,10 @@ var Base64 = {
15
15
  return this._encode(input);
16
16
  },
17
17
 
18
- _encode: function _encode(input) {
19
- var output = '';
20
- var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
21
- var i = 0;
18
+ _encode: function _encode(input: string): string {
19
+ let output = '';
20
+ let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
21
+ let i = 0;
22
22
 
23
23
  input = UTF8.encode(input);
24
24
 
@@ -48,7 +48,7 @@ var Base64 = {
48
48
  return output;
49
49
  },
50
50
 
51
- decode: function decode(input) {
51
+ decode: function decode(input: string): string {
52
52
  try {
53
53
  if (window.btoa && window.atob) {
54
54
  return decodeURIComponent(escape(window.atob(input)));
@@ -59,11 +59,11 @@ var Base64 = {
59
59
  return Base64._decode(input);
60
60
  },
61
61
 
62
- _decode: function _decode(input) {
63
- var output = '';
64
- var chr1, chr2, chr3;
65
- var enc1, enc2, enc3, enc4;
66
- var i = 0;
62
+ _decode: function _decode(input: string): string {
63
+ let output = '';
64
+ let chr1, chr2, chr3;
65
+ let enc1, enc2, enc3, enc4;
66
+ let i = 0;
67
67
 
68
68
  input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
69
69
 
@@ -91,12 +91,12 @@ var Base64 = {
91
91
  },
92
92
  };
93
93
 
94
- var UTF8 = {
95
- encode: function encode(s) {
96
- var utftext = '';
94
+ const UTF8 = {
95
+ encode: function encode(s: string): string {
96
+ let utftext = '';
97
97
 
98
- for (var n = 0; n < s.length; n++) {
99
- var c = s.charCodeAt(n);
98
+ for (let n = 0; n < s.length; n++) {
99
+ const c = s.charCodeAt(n);
100
100
 
101
101
  if (c < 128) {
102
102
  utftext += String.fromCharCode(c);
@@ -112,10 +112,10 @@ var UTF8 = {
112
112
  return utftext;
113
113
  },
114
114
 
115
- decode: function decode(utftext) {
116
- var s = '';
117
- var i = 0;
118
- var c = 0,
115
+ decode: function decode(utftext: string): string {
116
+ let s = '';
117
+ let i = 0;
118
+ let c = 0,
119
119
  c1 = 0,
120
120
  c2 = 0;
121
121
 
@@ -146,14 +146,14 @@ export default {
146
146
  // Production steps of ECMA-262, Edition 5, 15.4.4.18
147
147
  // Reference: http://es5.github.io/#x15.4.4.18
148
148
  forEach: function(callback, thisArg) {
149
- var T, k;
149
+ let T, k;
150
150
 
151
151
  if (this == null) {
152
152
  throw new TypeError(' this is null or not defined');
153
153
  }
154
154
 
155
- var O = Object(this);
156
- var len = O.length >>> 0;
155
+ const O = Object(this);
156
+ const len = O.length >>> 0;
157
157
 
158
158
  if (typeof callback !== 'function') {
159
159
  throw new TypeError(callback + ' is not a function');
@@ -166,7 +166,7 @@ export default {
166
166
  k = 0;
167
167
 
168
168
  while (k < len) {
169
- var kValue;
169
+ let kValue;
170
170
  if (k in O) {
171
171
  kValue = O[k];
172
172
  callback.call(T, kValue, k, O);
@@ -179,14 +179,14 @@ export default {
179
179
  // Production steps of ECMA-262, Edition 5, 15.4.4.19
180
180
  // Reference: http://es5.github.io/#x15.4.4.19
181
181
  map: function(callback, thisArg) {
182
- var T, A, k;
182
+ let T, A, k;
183
183
 
184
184
  if (this === null) {
185
185
  throw new TypeError(' this is null or not defined');
186
186
  }
187
187
 
188
- var O = Object(this);
189
- var len = O.length >>> 0;
188
+ const O = Object(this);
189
+ const len = O.length >>> 0;
190
190
 
191
191
  if (typeof callback !== 'function') {
192
192
  throw new TypeError(callback + ' is not a function');
@@ -201,7 +201,7 @@ export default {
201
201
  k = 0;
202
202
 
203
203
  while (k < len) {
204
- var kValue, mappedValue;
204
+ let kValue, mappedValue;
205
205
  if (k in O) {
206
206
  kValue = O[k];
207
207
  mappedValue = callback.call(T, kValue, k, O);
@@ -223,17 +223,17 @@ export default {
223
223
  throw new TypeError();
224
224
  }
225
225
 
226
- var t = Object(this);
227
- var len = t.length >>> 0;
226
+ const t = Object(this);
227
+ const len = t.length >>> 0;
228
228
  if (typeof fun !== 'function') {
229
229
  throw new TypeError();
230
230
  }
231
231
 
232
- var res = [];
233
- var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
234
- for (var i = 0; i < len; i++) {
232
+ const res = [];
233
+ const thisArg = arguments.length >= 2 ? arguments[1] : void 0;
234
+ for (let i = 0; i < len; i++) {
235
235
  if (i in t) {
236
- var val = t[i];
236
+ const val = t[i];
237
237
  if (fun.call(thisArg, val, i, t)) {
238
238
  res.push(val);
239
239
  }
@@ -244,7 +244,7 @@ export default {
244
244
  },
245
245
 
246
246
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
247
- isArray: function(arg) {
247
+ isArray: function(arg: unknown): boolean {
248
248
  return Object.prototype.toString.call(arg) === '[object Array]';
249
249
  },
250
250
 
@@ -5,6 +5,8 @@ export interface IRoktLauncherOptions extends Dictionary<any> {
5
5
  noDeviceID?: boolean;
6
6
  noFunctional?: boolean;
7
7
  noTargeting?: boolean;
8
+ sessionId?: string;
9
+ sessionToken?: string;
8
10
  }
9
11
 
10
12
  export function normalizeRoktLauncherOptions(
@@ -265,7 +265,7 @@ export interface MParticleWebSDK {
265
265
  startTrackingLocation(callback?: Callback): void;
266
266
 
267
267
  stopTrackingLocation(): void;
268
- generateHash(value: string): string;
268
+ generateHash(value: string): number;
269
269
  setIntegrationAttribute(
270
270
  integrationModuleId: number,
271
271
  attrs: IntegrationAttribute
@@ -375,13 +375,29 @@ export interface SDKHelpersApi {
375
375
  findKeyInObject?(obj: any, key: string): string;
376
376
  parseNumber?(value: string | number): number;
377
377
  generateUniqueId();
378
- generateHash?(value: string): string;
378
+ generateHash?(value: string): number;
379
379
  // https://go.mparticle.com/work/SQDSDKS-6317
380
380
  getFeatureFlag?(feature: string): boolean | string; // TODO: Feature Constants should be converted to enum
381
+ decoded?(s: string): string;
382
+ parseStringOrNumber?(value: string | number): string | number | null;
383
+ inArray?(items: any[], value: any): boolean;
384
+ converted?(s: string): string;
385
+ filterUserIdentitiesForForwarders?(
386
+ userIdentities: Dictionary,
387
+ filterList: number[]
388
+ ): Dictionary;
389
+ filterUserAttributes?(
390
+ userAttributes: Dictionary,
391
+ filterList: number[]
392
+ ): Dictionary;
393
+ filterUserIdentities?(
394
+ userIdentitiesObject: Dictionary<string>,
395
+ filterList: number[]
396
+ ): ISDKUserIdentity[];
381
397
  invokeAliasCallback(
382
398
  aliasCallback: IAliasCallback,
383
399
  number: number,
384
- errorMessage: string
400
+ errorMessage?: string
385
401
  ): void;
386
402
  isDelayedByIntegration?(
387
403
  delayedIntegrations: Dictionary<boolean>,
package/src/store.ts CHANGED
@@ -87,6 +87,7 @@ export interface SDKConfig {
87
87
  maxProducts: number;
88
88
  requestConfig?: boolean;
89
89
  sessionTimeout?: number;
90
+ timeout?: number;
90
91
  useNativeSdk?: boolean;
91
92
  useCookieStorage?: boolean;
92
93
  v1SecureServiceUrl?: string;