@mparticle/web-sdk 2.16.0 → 2.16.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-sdk",
3
- "version": "2.16.0",
3
+ "version": "2.16.3",
4
4
  "description": "mParticle core SDK for web applications",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
@@ -27,7 +27,6 @@
27
27
  "build:npm": "cross-env ENVIRONMENT=prod BUILD=cjs rollup --config rollup.config.js",
28
28
  "build:esm": "cross-env ENVIRONMENT=prod BUILD=esm rollup --config rollup.config.js",
29
29
  "build:stub": "cross-env ENVIRONMENT=prod BUILD=stub rollup --config rollup.config.js",
30
- "build:docs": "yuidoc src/",
31
30
  "build:snippet": "uglifyjs snippet.js -nm -o snippet.min.js",
32
31
  "build:browserify:cjs": "browserify test/integrations/cjs/browserify/index.js -o test/integrations/cjs/dist/browserify-output.js && npm run test:karma:browserify:cjs",
33
32
  "build:rollup:cjs": "rollup --config test/integrations/cjs/rollup/rollup.config.js && npm run test:karma:rollup:cjs",
@@ -55,34 +54,45 @@
55
54
  "lint": "eslint src/ test/src/",
56
55
  "gts:check": "gts check",
57
56
  "gts:fix": "gts fix",
58
- "prettier": "node_modules/.bin/prettier --check \"**/*.js\""
57
+ "prettier": "node_modules/.bin/prettier --check \"**/*.js\"",
58
+ "uglify": "npx uglify-js dist/mparticle.js --output dist/mparticle.min.js",
59
+ "gzip": "gzip -c dist/mparticle.min.js > dist/mparticle.min.js.gz",
60
+ "bundle": "npm run uglify && npm run gzip",
61
+ "report:bundled": "ls -l dist/mparticle.min.js.gz | awk '{print $5}'",
62
+ "report:bundled:human": "ls -lh dist/mparticle.min.js.gz | awk '{print $5}'"
59
63
  },
60
64
  "pre-commit": [
61
65
  "lint"
62
66
  ],
63
67
  "devDependencies": {
64
- "@babel/core": "^7.6.0",
68
+ "@babel/core": "^7.18.6",
69
+ "@babel/eslint-parser": "^7.18.2",
70
+ "@babel/eslint-plugin": "^7.17.7",
65
71
  "@babel/plugin-proposal-class-properties": "^7.5.5",
66
72
  "@babel/plugin-proposal-object-rest-spread": "^7.5.5",
67
73
  "@babel/plugin-transform-runtime": "^7.6.0",
68
74
  "@babel/preset-env": "^7.6.0",
69
75
  "@babel/preset-typescript": "^7.6.0",
70
76
  "@mparticle/data-planning-models": "^0.1.0",
71
- "@mparticle/event-models": "^1.1.4",
77
+ "@mparticle/event-models": "^1.1.6",
72
78
  "@rollup/plugin-json": "^4.1.0",
79
+ "@rollup/plugin-typescript": "^8.3.3",
80
+ "@rollup/plugin-babel": "5.3.1",
81
+ "@rollup/plugin-commonjs": "22.0.1",
82
+ "@rollup/plugin-node-resolve": "13.3.0",
73
83
  "@semantic-release/changelog": "^5.0.1",
74
84
  "@semantic-release/exec": "^5.0.0",
75
85
  "@semantic-release/git": "^9.0.0",
76
86
  "@types/chai": "^4.2.3",
77
87
  "@types/mocha": "^9.0.0",
88
+ "@types/mparticle__web-sdk": "^2.16.1",
78
89
  "@types/node": "^16.0.0",
79
- "babel-eslint": "^10.0.3",
80
90
  "babel-preset-minify": "^0.5.1",
81
91
  "browser-sync": "^2.26.3",
82
92
  "browserify": "^17.0.0",
83
93
  "chai": "^4.2.0",
84
94
  "cross-env": "^7.0.3",
85
- "eslint": "^7.25.0",
95
+ "eslint": "^7.32.0",
86
96
  "eslint-config-prettier": "8.3.0",
87
97
  "eslint-plugin-prettier": "3.4.1",
88
98
  "fetch-mock": "^7.5.1",
@@ -100,11 +110,7 @@
100
110
  "mocha": "^9.0.0",
101
111
  "pre-commit": "^1.2.2",
102
112
  "prettier": "1.18.2",
103
- "rollup": "^1.19.4",
104
- "rollup-plugin-babel": "^4.3.3",
105
- "rollup-plugin-commonjs": "^10.1.0",
106
- "rollup-plugin-node-resolve": "^5.2.0",
107
- "rollup-plugin-typescript": "^1.0.1",
113
+ "rollup": "^2.75.7",
108
114
  "shelljs": "^0.8.4",
109
115
  "should": "^7.1.0",
110
116
  "sinon": "^11.0.0",
@@ -112,8 +118,7 @@
112
118
  "typescript": "^4.2.4",
113
119
  "uglify-js": "^3.4.9",
114
120
  "webpack": "^5.36.2",
115
- "webpack-cli": "^4.6.0",
116
- "yuidocjs": "^0.10.2"
121
+ "webpack-cli": "^4.6.0"
117
122
  },
118
123
  "dependencies": {
119
124
  "@babel/runtime": "^7.6.0",
@@ -136,7 +136,11 @@ export class BatchUploader {
136
136
 
137
137
  if (onCreateBatchCallback) {
138
138
  uploadBatchObject = onCreateBatchCallback(uploadBatchObject);
139
- uploadBatchObject.mutated = true;
139
+ if (uploadBatchObject) {
140
+ uploadBatchObject.modified = true;
141
+ } else {
142
+ mpInstance.Logger.warning('Skiping batch upload because no batch was returned from onCreateBatch callback');
143
+ }
140
144
  }
141
145
 
142
146
  if (uploadBatchObject) {
package/src/constants.ts CHANGED
@@ -59,8 +59,7 @@ const Constants = {
59
59
  'identityRequests to modify require userIdentities to be present. Request not sent to server. Please fix and try again',
60
60
  IdentityRequesetInvalidKey:
61
61
  'There is an invalid key on your identityRequest object. It can only contain a `userIdentities` object and a `onUserAlias` function. Request not sent to server. Please fix and try again.',
62
- OnUserAliasType:
63
- 'The onUserAlias value must be a function. The onUserAlias provided is of type',
62
+ OnUserAliasType: 'The onUserAlias value must be a function.',
64
63
  UserIdentities:
65
64
  'The userIdentities key must be an object with keys of identityTypes and values of strings. Request not sent to server. Please fix and try again.',
66
65
  UserIdentitiesInvalidKey:
package/src/helpers.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import Types from './types';
2
2
  import Constants from './constants';
3
3
  import Slugify from 'slugify';
4
+ import * as utils from './utils';
5
+ import Validators from './validators';
4
6
 
5
- var StorageNames = Constants.StorageNames,
6
- pluses = /\+/g;
7
+ var StorageNames = Constants.StorageNames;
7
8
 
8
9
  export default function Helpers(mpInstance) {
9
10
  var self = this;
@@ -19,14 +20,6 @@ export default function Helpers(mpInstance) {
19
20
  return false;
20
21
  };
21
22
 
22
- this.returnConvertedBoolean = function(data) {
23
- if (data === 'false' || data === '0') {
24
- return false;
25
- } else {
26
- return Boolean(data);
27
- }
28
- };
29
-
30
23
  this.getFeatureFlag = function(feature) {
31
24
  if (mpInstance._Store.SDKConfig.flags.hasOwnProperty(feature)) {
32
25
  return mpInstance._Store.SDKConfig.flags[feature];
@@ -250,26 +243,6 @@ export default function Helpers(mpInstance) {
250
243
  return target;
251
244
  };
252
245
 
253
- this.isObject = function(value) {
254
- var objType = Object.prototype.toString.call(value);
255
-
256
- return objType === '[object Object]' || objType === '[object Error]';
257
- };
258
-
259
- this.inArray = function(items, name) {
260
- var i = 0;
261
-
262
- if (Array.prototype.indexOf) {
263
- return items.indexOf(name, 0) >= 0;
264
- } else {
265
- for (var n = items.length; i < n; i++) {
266
- if (i in items && items[i] === name) {
267
- return true;
268
- }
269
- }
270
- }
271
- };
272
-
273
246
  this.createServiceUrl = function(secureServiceUrl, devToken) {
274
247
  var serviceScheme =
275
248
  window.mParticle && mpInstance._Store.SDKConfig.forceHttps
@@ -415,36 +388,6 @@ export default function Helpers(mpInstance) {
415
388
  return filteredUserAttributes;
416
389
  };
417
390
 
418
- this.findKeyInObject = function(obj, key) {
419
- if (key && obj) {
420
- for (var prop in obj) {
421
- if (
422
- obj.hasOwnProperty(prop) &&
423
- prop.toLowerCase() === key.toLowerCase()
424
- ) {
425
- return prop;
426
- }
427
- }
428
- }
429
-
430
- return null;
431
- };
432
-
433
- this.decoded = function(s) {
434
- return decodeURIComponent(s.replace(pluses, ' '));
435
- };
436
-
437
- this.converted = function(s) {
438
- if (s.indexOf('"') === 0) {
439
- s = s
440
- .slice(1, -1)
441
- .replace(/\\"/g, '"')
442
- .replace(/\\\\/g, '\\');
443
- }
444
-
445
- return s;
446
- };
447
-
448
391
  this.isEventType = function(type) {
449
392
  for (var prop in Types.EventType) {
450
393
  if (Types.EventType.hasOwnProperty(prop)) {
@@ -456,16 +399,8 @@ export default function Helpers(mpInstance) {
456
399
  return false;
457
400
  };
458
401
 
459
- this.parseNumber = function(value) {
460
- if (isNaN(value) || !isFinite(value)) {
461
- return 0;
462
- }
463
- var floatValue = parseFloat(value);
464
- return isNaN(floatValue) ? 0 : floatValue;
465
- };
466
-
467
402
  this.parseStringOrNumber = function(value) {
468
- if (self.Validators.isStringOrNumber(value)) {
403
+ if (Validators.isStringOrNumber(value)) {
469
404
  return value;
470
405
  } else {
471
406
  return null;
@@ -531,157 +466,6 @@ export default function Helpers(mpInstance) {
531
466
  return sanitizedAttrs;
532
467
  };
533
468
 
534
- this.Validators = {
535
- isValidAttributeValue: function(value) {
536
- return (
537
- value !== undefined &&
538
- !self.isObject(value) &&
539
- !Array.isArray(value)
540
- );
541
- },
542
-
543
- // Neither null nor undefined can be a valid Key
544
- isValidKeyValue: function(key) {
545
- return Boolean(key && !self.isObject(key) && !Array.isArray(key));
546
- },
547
-
548
- isStringOrNumber: function(value) {
549
- return typeof value === 'string' || typeof value === 'number';
550
- },
551
-
552
- isNumber: function(value) {
553
- return typeof value === 'number';
554
- },
555
-
556
- isFunction: function(fn) {
557
- return typeof fn === 'function';
558
- },
559
-
560
- validateIdentities: function(identityApiData, method) {
561
- var validIdentityRequestKeys = {
562
- userIdentities: 1,
563
- onUserAlias: 1,
564
- copyUserAttributes: 1,
565
- };
566
- if (identityApiData) {
567
- if (method === 'modify') {
568
- if (
569
- (self.isObject(identityApiData.userIdentities) &&
570
- !Object.keys(identityApiData.userIdentities)
571
- .length) ||
572
- !self.isObject(identityApiData.userIdentities)
573
- ) {
574
- return {
575
- valid: false,
576
- error:
577
- Constants.Messages.ValidationMessages
578
- .ModifyIdentityRequestUserIdentitiesPresent,
579
- };
580
- }
581
- }
582
- for (var key in identityApiData) {
583
- if (identityApiData.hasOwnProperty(key)) {
584
- if (!validIdentityRequestKeys[key]) {
585
- return {
586
- valid: false,
587
- error:
588
- Constants.Messages.ValidationMessages
589
- .IdentityRequesetInvalidKey,
590
- };
591
- }
592
- if (
593
- key === 'onUserAlias' &&
594
- !mpInstance._Helpers.Validators.isFunction(
595
- identityApiData[key]
596
- )
597
- ) {
598
- return {
599
- valid: false,
600
- error:
601
- Constants.Messages.ValidationMessages
602
- .OnUserAliasType +
603
- typeof identityApiData[key],
604
- };
605
- }
606
- }
607
- }
608
- if (Object.keys(identityApiData).length === 0) {
609
- return {
610
- valid: true,
611
- };
612
- } else {
613
- // identityApiData.userIdentities can't be undefined
614
- if (identityApiData.userIdentities === undefined) {
615
- return {
616
- valid: false,
617
- error:
618
- Constants.Messages.ValidationMessages
619
- .UserIdentities,
620
- };
621
- // identityApiData.userIdentities can be null, but if it isn't null or undefined (above conditional), it must be an object
622
- } else if (
623
- identityApiData.userIdentities !== null &&
624
- !self.isObject(identityApiData.userIdentities)
625
- ) {
626
- return {
627
- valid: false,
628
- error:
629
- Constants.Messages.ValidationMessages
630
- .UserIdentities,
631
- };
632
- }
633
- if (
634
- self.isObject(identityApiData.userIdentities) &&
635
- Object.keys(identityApiData.userIdentities).length
636
- ) {
637
- for (var identityType in identityApiData.userIdentities) {
638
- if (
639
- identityApiData.userIdentities.hasOwnProperty(
640
- identityType
641
- )
642
- ) {
643
- if (
644
- Types.IdentityType.getIdentityType(
645
- identityType
646
- ) === false
647
- ) {
648
- return {
649
- valid: false,
650
- error:
651
- Constants.Messages
652
- .ValidationMessages
653
- .UserIdentitiesInvalidKey,
654
- };
655
- }
656
- if (
657
- !(
658
- typeof identityApiData.userIdentities[
659
- identityType
660
- ] === 'string' ||
661
- identityApiData.userIdentities[
662
- identityType
663
- ] === null
664
- )
665
- ) {
666
- return {
667
- valid: false,
668
- error:
669
- Constants.Messages
670
- .ValidationMessages
671
- .UserIdentitiesInvalidValues,
672
- };
673
- }
674
- }
675
- }
676
- }
677
- }
678
- }
679
- return {
680
- valid: true,
681
- };
682
- },
683
- };
684
-
685
469
  this.isDelayedByIntegration = function(
686
470
  delayedIntegrations,
687
471
  timeoutStart,
@@ -724,4 +508,16 @@ export default function Helpers(mpInstance) {
724
508
  this.isSlug = function(str) {
725
509
  return str === Slugify(str);
726
510
  };
511
+
512
+ // Utility Functions
513
+ this.converted = utils.converted;
514
+ this.findKeyInObject = utils.findKeyInObject;
515
+ this.parseNumber = utils.parseNumber;
516
+ this.inArray = utils.inArray;
517
+ this.isObject = utils.isObject;
518
+ this.decoded = utils.decoded;
519
+ this.returnConvertedBoolean = utils.returnConvertedBoolean;
520
+
521
+ // Imported Validators
522
+ this.Validators = Validators;
727
523
  }
@@ -327,8 +327,7 @@ export function convertPromotionAction(
327
327
  return null;
328
328
  }
329
329
  const promotionAction: EventsApi.PromotionAction = {
330
- action: sdkEvent.PromotionAction
331
- .PromotionActionType as EventsApi.PromotionActionActionEnum,
330
+ action: sdkEvent.PromotionAction.PromotionActionType as EventsApi.PromotionActionActionEnum,
332
331
  promotions: convertPromotions(sdkEvent.PromotionAction.PromotionList),
333
332
  };
334
333
  return promotionAction;
package/src/utils.ts ADDED
@@ -0,0 +1,82 @@
1
+ type valueof<T> = T[keyof T];
2
+
3
+ const inArray = (items: any[], name: string): boolean => {
4
+ let i = 0;
5
+
6
+ if (Array.prototype.indexOf) {
7
+ return items.indexOf(name, 0) >= 0;
8
+ } else {
9
+ for (var n = items.length; i < n; i++) {
10
+ if (i in items && items[i] === name) {
11
+ return true;
12
+ }
13
+ }
14
+ }
15
+
16
+ return false;
17
+ };
18
+
19
+ const findKeyInObject = (obj: any, key: string): string => {
20
+ if (key && obj) {
21
+ for (var prop in obj) {
22
+ if (
23
+ obj.hasOwnProperty(prop) &&
24
+ prop.toLowerCase() === key.toLowerCase()
25
+ ) {
26
+ return prop;
27
+ }
28
+ }
29
+ }
30
+
31
+ return null;
32
+ };
33
+
34
+ const isObject = (value: any): boolean => {
35
+ var objType = Object.prototype.toString.call(value);
36
+ return objType === '[object Object]' || objType === '[object Error]';
37
+ };
38
+
39
+ const parseNumber = (value: string | number): number => {
40
+ if (isNaN(value as number) || !isFinite(value as number)) {
41
+ return 0;
42
+ }
43
+ var floatValue = parseFloat(value as string);
44
+ return isNaN(floatValue) ? 0 : floatValue;
45
+ };
46
+
47
+ // FIXME: REFACTOR for V3
48
+ // only used in store.js to sanitize server-side formatting of
49
+ // booleans when checking for `isDevelopmentMode`
50
+ // Should be removed in v3
51
+ const returnConvertedBoolean = (data: string | boolean | number) => {
52
+ if (data === 'false' || data === '0') {
53
+ return false;
54
+ } else {
55
+ return Boolean(data);
56
+ }
57
+ };
58
+
59
+ const decoded = (s: string): string =>
60
+ decodeURIComponent(s.replace(/\+/g, ' '));
61
+
62
+ const converted = (s: string): string => {
63
+ if (s.indexOf('"') === 0) {
64
+ s = s
65
+ .slice(1, -1)
66
+ .replace(/\\"/g, '"')
67
+ .replace(/\\\\/g, '\\');
68
+ }
69
+
70
+ return s;
71
+ };
72
+
73
+ export {
74
+ valueof,
75
+ converted,
76
+ decoded,
77
+ findKeyInObject,
78
+ inArray,
79
+ isObject,
80
+ parseNumber,
81
+ returnConvertedBoolean,
82
+ };
@@ -0,0 +1,163 @@
1
+ import Types from './types';
2
+ import { isObject } from './utils';
3
+ import Constants from './constants';
4
+ import { IdentityApiData } from '@mparticle/web-sdk';
5
+ import { valueof } from './utils';
6
+
7
+ type IdentityAPIMethod = 'login' | 'logout' | 'identify' | 'modify';
8
+
9
+ type ValidationIdentitiesReturn = {
10
+ valid: boolean;
11
+ error?: valueof<typeof Constants.Messages.ValidationMessages>;
12
+ };
13
+
14
+ const Validators = {
15
+ isValidAttributeValue: function(value: any): boolean {
16
+ return value !== undefined && !isObject(value) && !Array.isArray(value);
17
+ },
18
+
19
+ // Neither null nor undefined can be a valid Key
20
+ isValidKeyValue: function(key: any): boolean {
21
+ return Boolean(
22
+ key &&
23
+ !isObject(key) &&
24
+ !Array.isArray(key) &&
25
+ !this.isFunction(key)
26
+ );
27
+ },
28
+
29
+ isStringOrNumber: function(value: any): boolean {
30
+ return typeof value === 'string' || typeof value === 'number';
31
+ },
32
+
33
+ isNumber: function(value: any): boolean {
34
+ return typeof value === 'number';
35
+ },
36
+
37
+ isFunction: (fn: any): boolean => {
38
+ return typeof fn === 'function';
39
+ },
40
+
41
+ validateIdentities: function(
42
+ identityApiData: IdentityApiData,
43
+ method?: IdentityAPIMethod
44
+ ): ValidationIdentitiesReturn {
45
+ var validIdentityRequestKeys = {
46
+ userIdentities: 1,
47
+ onUserAlias: 1,
48
+ copyUserAttributes: 1,
49
+ };
50
+ if (identityApiData) {
51
+ if (method === 'modify') {
52
+ if (
53
+ (isObject(identityApiData.userIdentities) &&
54
+ !Object.keys(identityApiData.userIdentities).length) ||
55
+ !isObject(identityApiData.userIdentities)
56
+ ) {
57
+ return {
58
+ valid: false,
59
+ error:
60
+ Constants.Messages.ValidationMessages
61
+ .ModifyIdentityRequestUserIdentitiesPresent,
62
+ };
63
+ }
64
+ }
65
+ for (var key in identityApiData) {
66
+ if (identityApiData.hasOwnProperty(key)) {
67
+ if (!validIdentityRequestKeys[key]) {
68
+ return {
69
+ valid: false,
70
+ error:
71
+ Constants.Messages.ValidationMessages
72
+ .IdentityRequesetInvalidKey,
73
+ };
74
+ }
75
+ if (
76
+ key === 'onUserAlias' &&
77
+ !Validators.isFunction(identityApiData[key])
78
+ ) {
79
+ return {
80
+ valid: false,
81
+ error:
82
+ Constants.Messages.ValidationMessages
83
+ .OnUserAliasType,
84
+ };
85
+ }
86
+ }
87
+ }
88
+ if (Object.keys(identityApiData).length === 0) {
89
+ return {
90
+ valid: true,
91
+ };
92
+ } else {
93
+ // identityApiData.userIdentities can't be undefined
94
+ if (identityApiData.userIdentities === undefined) {
95
+ return {
96
+ valid: false,
97
+ error:
98
+ Constants.Messages.ValidationMessages
99
+ .UserIdentities,
100
+ };
101
+ // identityApiData.userIdentities can be null, but if it isn't null or undefined (above conditional), it must be an object
102
+ } else if (
103
+ identityApiData.userIdentities !== null &&
104
+ !isObject(identityApiData.userIdentities)
105
+ ) {
106
+ return {
107
+ valid: false,
108
+ error:
109
+ Constants.Messages.ValidationMessages
110
+ .UserIdentities,
111
+ };
112
+ }
113
+ if (
114
+ isObject(identityApiData.userIdentities) &&
115
+ Object.keys(identityApiData.userIdentities).length
116
+ ) {
117
+ for (var identityType in identityApiData.userIdentities) {
118
+ if (
119
+ identityApiData.userIdentities.hasOwnProperty(
120
+ identityType
121
+ )
122
+ ) {
123
+ if (
124
+ Types.IdentityType.getIdentityType(
125
+ identityType
126
+ ) === false
127
+ ) {
128
+ return {
129
+ valid: false,
130
+ error:
131
+ Constants.Messages.ValidationMessages
132
+ .UserIdentitiesInvalidKey,
133
+ };
134
+ }
135
+ if (
136
+ !(
137
+ typeof identityApiData.userIdentities[
138
+ identityType
139
+ ] === 'string' ||
140
+ identityApiData.userIdentities[
141
+ identityType
142
+ ] === null
143
+ )
144
+ ) {
145
+ return {
146
+ valid: false,
147
+ error:
148
+ Constants.Messages.ValidationMessages
149
+ .UserIdentitiesInvalidValues,
150
+ };
151
+ }
152
+ }
153
+ }
154
+ }
155
+ }
156
+ }
157
+ return {
158
+ valid: true,
159
+ };
160
+ },
161
+ };
162
+
163
+ export default Validators;