@blotoutio/edgetag-sdk-browser 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 (2) hide show
  1. package/index.js +46 -31
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -38,11 +38,32 @@
38
38
  });
39
39
  }
40
40
 
41
+ const getMessage = (error) => {
42
+ if (error instanceof Error) {
43
+ return error.message;
44
+ }
45
+ if (typeof error === 'string') {
46
+ return error;
47
+ }
48
+ try {
49
+ return JSON.stringify(error);
50
+ }
51
+ catch (_a) {
52
+ return error;
53
+ }
54
+ };
55
+ const log = (data) => {
56
+ console.log('[EdgeTag]', getMessage(data));
57
+ };
58
+ const error = (data) => {
59
+ console.error('[EdgeTag]', getMessage(data));
60
+ };
61
+
41
62
  let endpointUrl = '';
42
63
  const generateUrl = (path) => {
43
64
  const endpoint = getUrl();
44
65
  if (!endpoint) {
45
- console.log('URL is not valid');
66
+ log('URL is not valid');
46
67
  return '';
47
68
  }
48
69
  return `${endpoint}${path}`;
@@ -88,7 +109,7 @@
88
109
  return false;
89
110
  }
90
111
  if (!preferences.edgeURL) {
91
- console.error('Please provide URL for EdgeTag');
112
+ error('Please provide URL for EdgeTag');
92
113
  return false;
93
114
  }
94
115
  consentDisabled = !!preferences.disableConsentCheck;
@@ -322,7 +343,7 @@
322
343
  localStorage.setItem(key, JSON.stringify(value));
323
344
  }
324
345
  catch (_a) {
325
- console.log('Local storage not supported');
346
+ log('Local storage not supported.');
326
347
  }
327
348
  };
328
349
  const getLocal = (key) => {
@@ -348,7 +369,7 @@
348
369
  sessionStorage.setItem(key, JSON.stringify(value));
349
370
  }
350
371
  catch (_a) {
351
- console.log('Session storage not supported');
372
+ log('Session storage not supported.');
352
373
  }
353
374
  };
354
375
  const getSession = (key) => {
@@ -392,8 +413,7 @@
392
413
  return navigator.sendBeacon(url, blob);
393
414
  }
394
415
  catch (e) {
395
- console.log('Beacon not supported.');
396
- return Promise.reject(new Error('Beacon not supported'));
416
+ return Promise.reject(new Error('Beacon not supported.'));
397
417
  }
398
418
  };
399
419
  const fallbackAjax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
@@ -431,7 +451,7 @@
431
451
  return yield fallbackAjax(method, url, payload)
432
452
  .then((data) => {
433
453
  if (data.status < 200 || data.status >= 300) {
434
- return Promise.reject(new Error(JSON.stringify(data.body)));
454
+ return Promise.reject(new Error(`Request failed with code ${data.status} occurred: ${JSON.stringify(data.body)}`));
435
455
  }
436
456
  return Promise.resolve(data.body);
437
457
  })
@@ -448,13 +468,11 @@
448
468
  .then((response) => response.json().then((data) => ({ status: response.status, body: data })))
449
469
  .then((data) => {
450
470
  if (data.status < 200 || data.status >= 300) {
451
- // Q: do we need to retry?
452
- return Promise.reject(new Error(JSON.stringify(data.body)));
471
+ return Promise.reject(new Error(`Request failed with code ${data.status}: ${JSON.stringify(data.body)}`));
453
472
  }
454
473
  return Promise.resolve(data.body);
455
474
  })
456
475
  .catch((error) => {
457
- // Q: do we need to retry?
458
476
  return Promise.reject(new Error(error));
459
477
  });
460
478
  });
@@ -482,7 +500,7 @@
482
500
  function postRequest(url, data, options) {
483
501
  return __awaiter(this, void 0, void 0, function* () {
484
502
  if (!url) {
485
- return Promise.reject(new Error('URL is empty'));
503
+ return Promise.reject(new Error('URL is empty.'));
486
504
  }
487
505
  const payload = getStandardPayload(data);
488
506
  if (options && options.method === 'beacon') {
@@ -494,7 +512,7 @@
494
512
  function getRequest(url, options) {
495
513
  return __awaiter(this, void 0, void 0, function* () {
496
514
  if (!url) {
497
- return Promise.reject(new Error('URL is empty'));
515
+ return Promise.reject(new Error('URL is empty.'));
498
516
  }
499
517
  if (options && options.method === 'beacon') {
500
518
  return {
@@ -505,9 +523,6 @@
505
523
  });
506
524
  }
507
525
 
508
- const info = (data) => {
509
- };
510
-
511
526
  let memoryConsent;
512
527
  const saveConsent = (consent) => {
513
528
  setConsent(consent);
@@ -518,7 +533,7 @@
518
533
  consentString: consent,
519
534
  };
520
535
  saveConsent(consent);
521
- postRequest(getConsentURL(), payload).catch(info);
536
+ postRequest(getConsentURL(), payload).catch(error);
522
537
  };
523
538
  const setConsent = (newConsent) => {
524
539
  memoryConsent = newConsent;
@@ -1050,13 +1065,13 @@
1050
1065
  stubs = [];
1051
1066
  }
1052
1067
  catch (e) {
1053
- console.error(e);
1068
+ error(e);
1054
1069
  }
1055
1070
  };
1056
1071
 
1057
1072
  const sendTag = ({ eventName, eventId, data, providerData, providers, options, }) => {
1058
1073
  if (!allowProviders(providers)) {
1059
- console.log('Provider is not allowed.');
1074
+ log('Provider is not allowed.');
1060
1075
  return;
1061
1076
  }
1062
1077
  const payload = {
@@ -1069,7 +1084,7 @@
1069
1084
  if (providers) {
1070
1085
  payload.providers = providers;
1071
1086
  }
1072
- postRequest(getTagURL(), payload, options).catch(info);
1087
+ postRequest(getTagURL(), payload, options).catch(error);
1073
1088
  };
1074
1089
  const handleTag = (eventName, data = {}, providers, options) => {
1075
1090
  if (!isInitialized()) {
@@ -1080,11 +1095,11 @@
1080
1095
  return;
1081
1096
  }
1082
1097
  if (!allowProviders(providers)) {
1083
- console.log('Provider is not allowed.');
1098
+ log('Provider is not allowed.');
1084
1099
  return;
1085
1100
  }
1086
1101
  if (!allowTag(providers)) {
1087
- console.log('No consent');
1102
+ log('Consent is missing.');
1088
1103
  return;
1089
1104
  }
1090
1105
  let eventId = data['eventId'];
@@ -1124,11 +1139,11 @@
1124
1139
 
1125
1140
  const handleData = (data, options) => {
1126
1141
  if (!data || Object.keys(data).length === 0) {
1127
- console.error('Provide data for data API.');
1142
+ error('Provide data for data API.');
1128
1143
  return;
1129
1144
  }
1130
1145
  saveKV(data);
1131
- postRequest(getDataURL(), { data }, options).catch(info);
1146
+ postRequest(getDataURL(), { data }, options).catch(error);
1132
1147
  };
1133
1148
 
1134
1149
  const saveDataToEdge = (key, value, provider) => {
@@ -1140,7 +1155,7 @@
1140
1155
  value = JSON.stringify(value);
1141
1156
  }
1142
1157
  catch (_a) {
1143
- console.log('Error stringify value');
1158
+ log('Error stringify value.');
1144
1159
  return;
1145
1160
  }
1146
1161
  }
@@ -1260,17 +1275,17 @@
1260
1275
  getRequest(url.href)
1261
1276
  .then((result) => {
1262
1277
  if (!result) {
1263
- console.log('init failed');
1278
+ log('Initialization failed');
1264
1279
  return;
1265
1280
  }
1266
1281
  handleManifest(result.result);
1267
1282
  })
1268
- .catch(info);
1283
+ .catch(error);
1269
1284
  };
1270
1285
 
1271
1286
  const handleUser = (key, value, options) => {
1272
1287
  if (!key || !value) {
1273
- console.error('Key or Value is missing in user API.');
1288
+ error('Key or Value is missing in user API.');
1274
1289
  return;
1275
1290
  }
1276
1291
  saveKV({
@@ -1279,19 +1294,19 @@
1279
1294
  postRequest(getUserURL(), {
1280
1295
  key,
1281
1296
  value,
1282
- }, options).catch(info);
1297
+ }, options).catch(error);
1283
1298
  };
1284
1299
 
1285
1300
  const handleGetData = (keys, callback) => {
1286
1301
  if (!keys || keys.length === 0) {
1287
- console.error('Provide keys for get data API.');
1302
+ error('Provide keys for get data API.');
1288
1303
  return;
1289
1304
  }
1290
1305
  getRequest(getGetDataURL(keys))
1291
1306
  .then((result) => {
1292
1307
  callback((result === null || result === void 0 ? void 0 : result.result) || {});
1293
1308
  })
1294
- .catch(info);
1309
+ .catch(error);
1295
1310
  };
1296
1311
 
1297
1312
  const handleKeys = (callback) => {
@@ -1299,7 +1314,7 @@
1299
1314
  .then((result) => {
1300
1315
  callback((result === null || result === void 0 ? void 0 : result.result) || []);
1301
1316
  })
1302
- .catch(info);
1317
+ .catch(error);
1303
1318
  };
1304
1319
 
1305
1320
  const init = (preferences) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-browser",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",