@blotoutio/edgetag-sdk-js 0.7.1 → 0.7.2

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.
Files changed (3) hide show
  1. package/index.cjs +46 -31
  2. package/index.esm.js +46 -31
  3. package/package.json +1 -1
package/index.cjs CHANGED
@@ -59,11 +59,32 @@ function __awaiter(thisArg, _arguments, P, generator) {
59
59
  });
60
60
  }
61
61
 
62
+ const getMessage = (error) => {
63
+ if (error instanceof Error) {
64
+ return error.message;
65
+ }
66
+ if (typeof error === 'string') {
67
+ return error;
68
+ }
69
+ try {
70
+ return JSON.stringify(error);
71
+ }
72
+ catch (_a) {
73
+ return error;
74
+ }
75
+ };
76
+ const log = (data) => {
77
+ console.log('[EdgeTag]', getMessage(data));
78
+ };
79
+ const error = (data) => {
80
+ console.error('[EdgeTag]', getMessage(data));
81
+ };
82
+
62
83
  let endpointUrl = '';
63
84
  const generateUrl = (path) => {
64
85
  const endpoint = getUrl();
65
86
  if (!endpoint) {
66
- console.log('URL is not valid');
87
+ log('URL is not valid');
67
88
  return '';
68
89
  }
69
90
  return `${endpoint}${path}`;
@@ -109,7 +130,7 @@ const setPreferences = (preferences) => {
109
130
  return false;
110
131
  }
111
132
  if (!preferences.edgeURL) {
112
- console.error('Please provide URL for EdgeTag');
133
+ error('Please provide URL for EdgeTag');
113
134
  return false;
114
135
  }
115
136
  consentDisabled = !!preferences.disableConsentCheck;
@@ -343,7 +364,7 @@ const saveLocal = (value, key) => {
343
364
  localStorage.setItem(key, JSON.stringify(value));
344
365
  }
345
366
  catch (_a) {
346
- console.log('Local storage not supported');
367
+ log('Local storage not supported.');
347
368
  }
348
369
  };
349
370
  const getLocal = (key) => {
@@ -369,7 +390,7 @@ const saveSession = (value, key) => {
369
390
  sessionStorage.setItem(key, JSON.stringify(value));
370
391
  }
371
392
  catch (_a) {
372
- console.log('Session storage not supported');
393
+ log('Session storage not supported.');
373
394
  }
374
395
  };
375
396
  const getSession = (key) => {
@@ -413,8 +434,7 @@ const beacon = (url, payload) => {
413
434
  return navigator.sendBeacon(url, blob);
414
435
  }
415
436
  catch (e) {
416
- console.log('Beacon not supported.');
417
- return Promise.reject(new Error('Beacon not supported'));
437
+ return Promise.reject(new Error('Beacon not supported.'));
418
438
  }
419
439
  };
420
440
  const fallbackAjax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
@@ -452,7 +472,7 @@ const ajax = (method, url, payload) => __awaiter(void 0, void 0, void 0, functio
452
472
  return yield fallbackAjax(method, url, payload)
453
473
  .then((data) => {
454
474
  if (data.status < 200 || data.status >= 300) {
455
- return Promise.reject(new Error(JSON.stringify(data.body)));
475
+ return Promise.reject(new Error(`Request failed with code ${data.status} occurred: ${JSON.stringify(data.body)}`));
456
476
  }
457
477
  return Promise.resolve(data.body);
458
478
  })
@@ -469,13 +489,11 @@ const ajax = (method, url, payload) => __awaiter(void 0, void 0, void 0, functio
469
489
  .then((response) => response.json().then((data) => ({ status: response.status, body: data })))
470
490
  .then((data) => {
471
491
  if (data.status < 200 || data.status >= 300) {
472
- // Q: do we need to retry?
473
- return Promise.reject(new Error(JSON.stringify(data.body)));
492
+ return Promise.reject(new Error(`Request failed with code ${data.status}: ${JSON.stringify(data.body)}`));
474
493
  }
475
494
  return Promise.resolve(data.body);
476
495
  })
477
496
  .catch((error) => {
478
- // Q: do we need to retry?
479
497
  return Promise.reject(new Error(error));
480
498
  });
481
499
  });
@@ -503,7 +521,7 @@ const getStandardPayload = (payload) => {
503
521
  function postRequest(url, data, options) {
504
522
  return __awaiter(this, void 0, void 0, function* () {
505
523
  if (!url) {
506
- return Promise.reject(new Error('URL is empty'));
524
+ return Promise.reject(new Error('URL is empty.'));
507
525
  }
508
526
  const payload = getStandardPayload(data);
509
527
  if (options && options.method === 'beacon') {
@@ -515,7 +533,7 @@ function postRequest(url, data, options) {
515
533
  function getRequest(url, options) {
516
534
  return __awaiter(this, void 0, void 0, function* () {
517
535
  if (!url) {
518
- return Promise.reject(new Error('URL is empty'));
536
+ return Promise.reject(new Error('URL is empty.'));
519
537
  }
520
538
  if (options && options.method === 'beacon') {
521
539
  return {
@@ -526,9 +544,6 @@ function getRequest(url, options) {
526
544
  });
527
545
  }
528
546
 
529
- const info = (data) => {
530
- };
531
-
532
547
  let memoryConsent;
533
548
  const saveConsent = (consent) => {
534
549
  setConsent(consent);
@@ -539,7 +554,7 @@ const handleConsent = (consent) => {
539
554
  consentString: consent,
540
555
  };
541
556
  saveConsent(consent);
542
- postRequest(getConsentURL(), payload).catch(info);
557
+ postRequest(getConsentURL(), payload).catch(error);
543
558
  };
544
559
  const setConsent = (newConsent) => {
545
560
  memoryConsent = newConsent;
@@ -591,13 +606,13 @@ const processStubs = () => {
591
606
  stubs = [];
592
607
  }
593
608
  catch (e) {
594
- console.error(e);
609
+ error(e);
595
610
  }
596
611
  };
597
612
 
598
613
  const sendTag = ({ eventName, eventId, data, providerData, providers, options, }) => {
599
614
  if (!allowProviders(providers)) {
600
- console.log('Provider is not allowed.');
615
+ log('Provider is not allowed.');
601
616
  return;
602
617
  }
603
618
  const payload = {
@@ -610,7 +625,7 @@ const sendTag = ({ eventName, eventId, data, providerData, providers, options, }
610
625
  if (providers) {
611
626
  payload.providers = providers;
612
627
  }
613
- postRequest(getTagURL(), payload, options).catch(info);
628
+ postRequest(getTagURL(), payload, options).catch(error);
614
629
  };
615
630
  const handleTag = (eventName, data = {}, providers, options) => {
616
631
  if (!isInitialized()) {
@@ -621,11 +636,11 @@ const handleTag = (eventName, data = {}, providers, options) => {
621
636
  return;
622
637
  }
623
638
  if (!allowProviders(providers)) {
624
- console.log('Provider is not allowed.');
639
+ log('Provider is not allowed.');
625
640
  return;
626
641
  }
627
642
  if (!allowTag(providers)) {
628
- console.log('No consent');
643
+ log('Consent is missing.');
629
644
  return;
630
645
  }
631
646
  let eventId = data['eventId'];
@@ -665,11 +680,11 @@ const handleTag = (eventName, data = {}, providers, options) => {
665
680
 
666
681
  const handleData = (data, options) => {
667
682
  if (!data || Object.keys(data).length === 0) {
668
- console.error('Provide data for data API.');
683
+ error('Provide data for data API.');
669
684
  return;
670
685
  }
671
686
  saveKV(data);
672
- postRequest(getDataURL(), { data }, options).catch(info);
687
+ postRequest(getDataURL(), { data }, options).catch(error);
673
688
  };
674
689
 
675
690
  const saveDataToEdge = (key, value, provider) => {
@@ -681,7 +696,7 @@ const saveDataToEdge = (key, value, provider) => {
681
696
  value = JSON.stringify(value);
682
697
  }
683
698
  catch (_a) {
684
- console.log('Error stringify value');
699
+ log('Error stringify value.');
685
700
  return;
686
701
  }
687
702
  }
@@ -801,17 +816,17 @@ const handleInit = (preferences) => {
801
816
  getRequest(url.href)
802
817
  .then((result) => {
803
818
  if (!result) {
804
- console.log('init failed');
819
+ log('Initialization failed');
805
820
  return;
806
821
  }
807
822
  handleManifest(result.result);
808
823
  })
809
- .catch(info);
824
+ .catch(error);
810
825
  };
811
826
 
812
827
  const handleUser = (key, value, options) => {
813
828
  if (!key || !value) {
814
- console.error('Key or Value is missing in user API.');
829
+ error('Key or Value is missing in user API.');
815
830
  return;
816
831
  }
817
832
  saveKV({
@@ -820,19 +835,19 @@ const handleUser = (key, value, options) => {
820
835
  postRequest(getUserURL(), {
821
836
  key,
822
837
  value,
823
- }, options).catch(info);
838
+ }, options).catch(error);
824
839
  };
825
840
 
826
841
  const handleGetData = (keys, callback) => {
827
842
  if (!keys || keys.length === 0) {
828
- console.error('Provide keys for get data API.');
843
+ error('Provide keys for get data API.');
829
844
  return;
830
845
  }
831
846
  getRequest(getGetDataURL(keys))
832
847
  .then((result) => {
833
848
  callback((result === null || result === void 0 ? void 0 : result.result) || {});
834
849
  })
835
- .catch(info);
850
+ .catch(error);
836
851
  };
837
852
 
838
853
  const handleKeys = (callback) => {
@@ -840,7 +855,7 @@ const handleKeys = (callback) => {
840
855
  .then((result) => {
841
856
  callback((result === null || result === void 0 ? void 0 : result.result) || []);
842
857
  })
843
- .catch(info);
858
+ .catch(error);
844
859
  };
845
860
 
846
861
  const init = (preferences) => {
package/index.esm.js CHANGED
@@ -37,11 +37,32 @@ function __awaiter(thisArg, _arguments, P, generator) {
37
37
  });
38
38
  }
39
39
 
40
+ const getMessage = (error) => {
41
+ if (error instanceof Error) {
42
+ return error.message;
43
+ }
44
+ if (typeof error === 'string') {
45
+ return error;
46
+ }
47
+ try {
48
+ return JSON.stringify(error);
49
+ }
50
+ catch (_a) {
51
+ return error;
52
+ }
53
+ };
54
+ const log = (data) => {
55
+ console.log('[EdgeTag]', getMessage(data));
56
+ };
57
+ const error = (data) => {
58
+ console.error('[EdgeTag]', getMessage(data));
59
+ };
60
+
40
61
  let endpointUrl = '';
41
62
  const generateUrl = (path) => {
42
63
  const endpoint = getUrl();
43
64
  if (!endpoint) {
44
- console.log('URL is not valid');
65
+ log('URL is not valid');
45
66
  return '';
46
67
  }
47
68
  return `${endpoint}${path}`;
@@ -87,7 +108,7 @@ const setPreferences = (preferences) => {
87
108
  return false;
88
109
  }
89
110
  if (!preferences.edgeURL) {
90
- console.error('Please provide URL for EdgeTag');
111
+ error('Please provide URL for EdgeTag');
91
112
  return false;
92
113
  }
93
114
  consentDisabled = !!preferences.disableConsentCheck;
@@ -321,7 +342,7 @@ const saveLocal = (value, key) => {
321
342
  localStorage.setItem(key, JSON.stringify(value));
322
343
  }
323
344
  catch (_a) {
324
- console.log('Local storage not supported');
345
+ log('Local storage not supported.');
325
346
  }
326
347
  };
327
348
  const getLocal = (key) => {
@@ -347,7 +368,7 @@ const saveSession = (value, key) => {
347
368
  sessionStorage.setItem(key, JSON.stringify(value));
348
369
  }
349
370
  catch (_a) {
350
- console.log('Session storage not supported');
371
+ log('Session storage not supported.');
351
372
  }
352
373
  };
353
374
  const getSession = (key) => {
@@ -391,8 +412,7 @@ const beacon = (url, payload) => {
391
412
  return navigator.sendBeacon(url, blob);
392
413
  }
393
414
  catch (e) {
394
- console.log('Beacon not supported.');
395
- return Promise.reject(new Error('Beacon not supported'));
415
+ return Promise.reject(new Error('Beacon not supported.'));
396
416
  }
397
417
  };
398
418
  const fallbackAjax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
@@ -430,7 +450,7 @@ const ajax = (method, url, payload) => __awaiter(void 0, void 0, void 0, functio
430
450
  return yield fallbackAjax(method, url, payload)
431
451
  .then((data) => {
432
452
  if (data.status < 200 || data.status >= 300) {
433
- return Promise.reject(new Error(JSON.stringify(data.body)));
453
+ return Promise.reject(new Error(`Request failed with code ${data.status} occurred: ${JSON.stringify(data.body)}`));
434
454
  }
435
455
  return Promise.resolve(data.body);
436
456
  })
@@ -447,13 +467,11 @@ const ajax = (method, url, payload) => __awaiter(void 0, void 0, void 0, functio
447
467
  .then((response) => response.json().then((data) => ({ status: response.status, body: data })))
448
468
  .then((data) => {
449
469
  if (data.status < 200 || data.status >= 300) {
450
- // Q: do we need to retry?
451
- return Promise.reject(new Error(JSON.stringify(data.body)));
470
+ return Promise.reject(new Error(`Request failed with code ${data.status}: ${JSON.stringify(data.body)}`));
452
471
  }
453
472
  return Promise.resolve(data.body);
454
473
  })
455
474
  .catch((error) => {
456
- // Q: do we need to retry?
457
475
  return Promise.reject(new Error(error));
458
476
  });
459
477
  });
@@ -481,7 +499,7 @@ const getStandardPayload = (payload) => {
481
499
  function postRequest(url, data, options) {
482
500
  return __awaiter(this, void 0, void 0, function* () {
483
501
  if (!url) {
484
- return Promise.reject(new Error('URL is empty'));
502
+ return Promise.reject(new Error('URL is empty.'));
485
503
  }
486
504
  const payload = getStandardPayload(data);
487
505
  if (options && options.method === 'beacon') {
@@ -493,7 +511,7 @@ function postRequest(url, data, options) {
493
511
  function getRequest(url, options) {
494
512
  return __awaiter(this, void 0, void 0, function* () {
495
513
  if (!url) {
496
- return Promise.reject(new Error('URL is empty'));
514
+ return Promise.reject(new Error('URL is empty.'));
497
515
  }
498
516
  if (options && options.method === 'beacon') {
499
517
  return {
@@ -504,9 +522,6 @@ function getRequest(url, options) {
504
522
  });
505
523
  }
506
524
 
507
- const info = (data) => {
508
- };
509
-
510
525
  let memoryConsent;
511
526
  const saveConsent = (consent) => {
512
527
  setConsent(consent);
@@ -517,7 +532,7 @@ const handleConsent = (consent) => {
517
532
  consentString: consent,
518
533
  };
519
534
  saveConsent(consent);
520
- postRequest(getConsentURL(), payload).catch(info);
535
+ postRequest(getConsentURL(), payload).catch(error);
521
536
  };
522
537
  const setConsent = (newConsent) => {
523
538
  memoryConsent = newConsent;
@@ -569,13 +584,13 @@ const processStubs = () => {
569
584
  stubs = [];
570
585
  }
571
586
  catch (e) {
572
- console.error(e);
587
+ error(e);
573
588
  }
574
589
  };
575
590
 
576
591
  const sendTag = ({ eventName, eventId, data, providerData, providers, options, }) => {
577
592
  if (!allowProviders(providers)) {
578
- console.log('Provider is not allowed.');
593
+ log('Provider is not allowed.');
579
594
  return;
580
595
  }
581
596
  const payload = {
@@ -588,7 +603,7 @@ const sendTag = ({ eventName, eventId, data, providerData, providers, options, }
588
603
  if (providers) {
589
604
  payload.providers = providers;
590
605
  }
591
- postRequest(getTagURL(), payload, options).catch(info);
606
+ postRequest(getTagURL(), payload, options).catch(error);
592
607
  };
593
608
  const handleTag = (eventName, data = {}, providers, options) => {
594
609
  if (!isInitialized()) {
@@ -599,11 +614,11 @@ const handleTag = (eventName, data = {}, providers, options) => {
599
614
  return;
600
615
  }
601
616
  if (!allowProviders(providers)) {
602
- console.log('Provider is not allowed.');
617
+ log('Provider is not allowed.');
603
618
  return;
604
619
  }
605
620
  if (!allowTag(providers)) {
606
- console.log('No consent');
621
+ log('Consent is missing.');
607
622
  return;
608
623
  }
609
624
  let eventId = data['eventId'];
@@ -643,11 +658,11 @@ const handleTag = (eventName, data = {}, providers, options) => {
643
658
 
644
659
  const handleData = (data, options) => {
645
660
  if (!data || Object.keys(data).length === 0) {
646
- console.error('Provide data for data API.');
661
+ error('Provide data for data API.');
647
662
  return;
648
663
  }
649
664
  saveKV(data);
650
- postRequest(getDataURL(), { data }, options).catch(info);
665
+ postRequest(getDataURL(), { data }, options).catch(error);
651
666
  };
652
667
 
653
668
  const saveDataToEdge = (key, value, provider) => {
@@ -659,7 +674,7 @@ const saveDataToEdge = (key, value, provider) => {
659
674
  value = JSON.stringify(value);
660
675
  }
661
676
  catch (_a) {
662
- console.log('Error stringify value');
677
+ log('Error stringify value.');
663
678
  return;
664
679
  }
665
680
  }
@@ -779,17 +794,17 @@ const handleInit = (preferences) => {
779
794
  getRequest(url.href)
780
795
  .then((result) => {
781
796
  if (!result) {
782
- console.log('init failed');
797
+ log('Initialization failed');
783
798
  return;
784
799
  }
785
800
  handleManifest(result.result);
786
801
  })
787
- .catch(info);
802
+ .catch(error);
788
803
  };
789
804
 
790
805
  const handleUser = (key, value, options) => {
791
806
  if (!key || !value) {
792
- console.error('Key or Value is missing in user API.');
807
+ error('Key or Value is missing in user API.');
793
808
  return;
794
809
  }
795
810
  saveKV({
@@ -798,19 +813,19 @@ const handleUser = (key, value, options) => {
798
813
  postRequest(getUserURL(), {
799
814
  key,
800
815
  value,
801
- }, options).catch(info);
816
+ }, options).catch(error);
802
817
  };
803
818
 
804
819
  const handleGetData = (keys, callback) => {
805
820
  if (!keys || keys.length === 0) {
806
- console.error('Provide keys for get data API.');
821
+ error('Provide keys for get data API.');
807
822
  return;
808
823
  }
809
824
  getRequest(getGetDataURL(keys))
810
825
  .then((result) => {
811
826
  callback((result === null || result === void 0 ? void 0 : result.result) || {});
812
827
  })
813
- .catch(info);
828
+ .catch(error);
814
829
  };
815
830
 
816
831
  const handleKeys = (callback) => {
@@ -818,7 +833,7 @@ const handleKeys = (callback) => {
818
833
  .then((result) => {
819
834
  callback((result === null || result === void 0 ? void 0 : result.result) || []);
820
835
  })
821
- .catch(info);
836
+ .catch(error);
822
837
  };
823
838
 
824
839
  const init = (preferences) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-js",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "JS SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",