@flagship.io/react-sdk 2.2.0-beta.3 → 3.0.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.
@@ -1,3 +1,9 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
1
7
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
8
 
3
9
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
@@ -19,6 +25,8 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
19
25
  import { useContext } from 'react';
20
26
  import { FlagshipContext } from './FlagshipContext';
21
27
  import { logError, logWarn } from './utils';
28
+ import { Flag } from './Flag';
29
+ import { noVisitorDefault, noVisitorMessage } from './constants';
22
30
 
23
31
  var checkType = function checkType(value, defaultValue) {
24
32
  return _typeof(value) === 'object' && _typeof(defaultValue) === 'object' && Array.isArray(value) === Array.isArray(defaultValue) || _typeof(value) === _typeof(defaultValue);
@@ -62,6 +70,7 @@ var fsModificationsSync = function fsModificationsSync(args) {
62
70
  };
63
71
  /**
64
72
  * Retrieve a modification value by its key. If no modification match the given key or if the stored value type and default value type do not match, default value will be returned.
73
+ * @deprecated use useFsGetFlag instead
65
74
  */
66
75
 
67
76
 
@@ -83,6 +92,7 @@ export var useFsModifications = function useFsModifications(params, activateAll)
83
92
  };
84
93
  /**
85
94
  * Retrieve a modification value by its key. If no modification match the given key or if the stored value type and default value type do not match, default value will be returned.
95
+ * @deprecated use useFsGetFlag instead
86
96
  */
87
97
 
88
98
  export var useFsModification = function useFsModification(params) {
@@ -131,6 +141,7 @@ var fsModificationInfoSync = function fsModificationInfoSync(args) {
131
141
  /**
132
142
  * Get the campaign modification information value matching the given key.
133
143
  * @param {string} key key which identify the modification.
144
+ * @deprecated use useFsGetFlag instead
134
145
  */
135
146
 
136
147
 
@@ -187,110 +198,99 @@ var fsSynchronizeModifications = /*#__PURE__*/function () {
187
198
  return _ref.apply(this, arguments);
188
199
  };
189
200
  }();
190
- /**
191
- * This function calls the decision api and update all the campaigns modifications from the server according to the visitor context.
192
- */
193
-
194
-
195
- export var useFsSynchronizeModifications = /*#__PURE__*/function () {
196
- var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
197
- var _useContext4, state, visitor, config, functionName;
198
201
 
202
+ var fsActivate = /*#__PURE__*/function () {
203
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(params, functionName, visitor, config) {
199
204
  return regeneratorRuntime.wrap(function _callee2$(_context2) {
200
205
  while (1) {
201
206
  switch (_context2.prev = _context2.next) {
202
207
  case 0:
203
- _useContext4 = useContext(FlagshipContext), state = _useContext4.state;
204
- visitor = state.visitor, config = state.config;
205
- functionName = 'useFsSynchronizeModifications';
206
- _context2.next = 5;
207
- return fsSynchronizeModifications(functionName, visitor, config);
208
-
209
- case 5:
210
- case "end":
211
- return _context2.stop();
212
- }
213
- }
214
- }, _callee2);
215
- }));
216
-
217
- return function useFsSynchronizeModifications() {
218
- return _ref2.apply(this, arguments);
219
- };
220
- }();
221
-
222
- var fsActivate = /*#__PURE__*/function () {
223
- var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(params, functionName, visitor, config) {
224
- return regeneratorRuntime.wrap(function _callee3$(_context3) {
225
- while (1) {
226
- switch (_context3.prev = _context3.next) {
227
- case 0:
228
- _context3.prev = 0;
208
+ _context2.prev = 0;
229
209
 
230
210
  if (visitor) {
231
- _context3.next = 4;
211
+ _context2.next = 4;
232
212
  break;
233
213
  }
234
214
 
235
215
  logWarn(config, noVisitorMessage, functionName);
236
- return _context3.abrupt("return");
216
+ return _context2.abrupt("return");
237
217
 
238
218
  case 4:
239
- _context3.next = 6;
219
+ _context2.next = 6;
240
220
  return visitor.activateModifications(params);
241
221
 
242
222
  case 6:
243
- _context3.next = 11;
223
+ _context2.next = 11;
244
224
  break;
245
225
 
246
226
  case 8:
247
- _context3.prev = 8;
248
- _context3.t0 = _context3["catch"](0);
249
- logWarn(config, _context3.t0.message || _context3.t0, functionName);
227
+ _context2.prev = 8;
228
+ _context2.t0 = _context2["catch"](0);
229
+ logWarn(config, _context2.t0.message || _context2.t0, functionName);
250
230
 
251
231
  case 11:
252
232
  case "end":
253
- return _context3.stop();
233
+ return _context2.stop();
254
234
  }
255
235
  }
256
- }, _callee3, null, [[0, 8]]);
236
+ }, _callee2, null, [[0, 8]]);
257
237
  }));
258
238
 
259
239
  return function fsActivate(_x4, _x5, _x6, _x7) {
260
- return _ref3.apply(this, arguments);
240
+ return _ref2.apply(this, arguments);
261
241
  };
262
242
  }();
243
+ /**
244
+ * This hook returns a flag object by its key. If no flag match the given key an empty flag will be returned.
245
+ * @param key
246
+ * @param defaultValue
247
+ * @returns
248
+ */
249
+
250
+
251
+ export var useFsFlag = function useFsFlag(key, defaultValue) {
252
+ var _useContext4 = useContext(FlagshipContext),
253
+ state = _useContext4.state;
254
+
255
+ var visitor = state.visitor;
256
+
257
+ if (!visitor) {
258
+ return new Flag(defaultValue);
259
+ }
260
+
261
+ return visitor.getFlag(key, defaultValue);
262
+ };
263
263
  /**
264
264
  * Report this user has seen this modification. Report this user has seen these modifications.
265
265
  * @param params
266
+ * @deprecated use useFsGetFlag instead
266
267
  * @returns
267
268
  */
268
269
 
269
-
270
270
  export var useFsActivate = /*#__PURE__*/function () {
271
- var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(params) {
271
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(params) {
272
272
  var _useContext5, state, visitor, config, functionName;
273
273
 
274
- return regeneratorRuntime.wrap(function _callee4$(_context4) {
274
+ return regeneratorRuntime.wrap(function _callee3$(_context3) {
275
275
  while (1) {
276
- switch (_context4.prev = _context4.next) {
276
+ switch (_context3.prev = _context3.next) {
277
277
  case 0:
278
278
  _useContext5 = useContext(FlagshipContext), state = _useContext5.state;
279
279
  visitor = state.visitor, config = state.config;
280
280
  functionName = 'useFsModifications';
281
- _context4.next = 5;
281
+ _context3.next = 5;
282
282
  return fsActivate(params, functionName, visitor, config);
283
283
 
284
284
  case 5:
285
285
  case "end":
286
- return _context4.stop();
286
+ return _context3.stop();
287
287
  }
288
288
  }
289
- }, _callee4);
289
+ }, _callee3);
290
290
  }));
291
291
 
292
292
  return function useFsActivate(_x8) {
293
- return _ref4.apply(this, arguments);
293
+ return _ref3.apply(this, arguments);
294
294
  };
295
295
  }();
296
296
  export var useFlagship = function useFlagship() {
@@ -345,8 +345,8 @@ export var useFlagship = function useFlagship() {
345
345
  visitor.unauthenticate();
346
346
  };
347
347
  /**
348
- * Send a Hit to Flagship servers for reporting.
349
- */
348
+ * Send a Hit to Flagship servers for reporting.
349
+ */
350
350
 
351
351
 
352
352
  var fsSendHit = function fsSendHit(hit) {
@@ -360,8 +360,8 @@ export var useFlagship = function useFlagship() {
360
360
  return visitor.sendHit(hit);
361
361
  };
362
362
  /**
363
- * Send a Hit to Flagship servers for reporting.
364
- */
363
+ * Send a Hit to Flagship servers for reporting.
364
+ */
365
365
 
366
366
 
367
367
  var fsSendHits = function fsSendHits(hit) {
@@ -379,60 +379,60 @@ export var useFlagship = function useFlagship() {
379
379
 
380
380
  if (!state.status.isSdkReady && state.modifications) {
381
381
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
382
- modifications = Array.from(state.modifications, function (_ref5) {
383
- var _ref6 = _slicedToArray(_ref5, 2),
384
- _key = _ref6[0],
385
- item = _ref6[1];
382
+ modifications = Array.from(state.modifications, function (_ref4) {
383
+ var _ref5 = _slicedToArray(_ref4, 2),
384
+ _key = _ref5[0],
385
+ item = _ref5[1];
386
386
 
387
387
  return item;
388
388
  });
389
389
  }
390
390
 
391
391
  var activateModification = /*#__PURE__*/function () {
392
- var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(params) {
392
+ var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(params) {
393
393
  var functionName;
394
- return regeneratorRuntime.wrap(function _callee5$(_context5) {
394
+ return regeneratorRuntime.wrap(function _callee4$(_context4) {
395
395
  while (1) {
396
- switch (_context5.prev = _context5.next) {
396
+ switch (_context4.prev = _context4.next) {
397
397
  case 0:
398
398
  functionName = 'activateModification';
399
- _context5.next = 3;
399
+ _context4.next = 3;
400
400
  return fsActivate(params, functionName, visitor, config);
401
401
 
402
402
  case 3:
403
403
  case "end":
404
- return _context5.stop();
404
+ return _context4.stop();
405
405
  }
406
406
  }
407
- }, _callee5);
407
+ }, _callee4);
408
408
  }));
409
409
 
410
410
  return function activateModification(_x9) {
411
- return _ref7.apply(this, arguments);
411
+ return _ref6.apply(this, arguments);
412
412
  };
413
413
  }();
414
414
 
415
415
  var synchronizeModifications = /*#__PURE__*/function () {
416
- var _ref8 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() {
416
+ var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() {
417
417
  var functionName;
418
- return regeneratorRuntime.wrap(function _callee6$(_context6) {
418
+ return regeneratorRuntime.wrap(function _callee5$(_context5) {
419
419
  while (1) {
420
- switch (_context6.prev = _context6.next) {
420
+ switch (_context5.prev = _context5.next) {
421
421
  case 0:
422
422
  functionName = 'synchronizeModifications';
423
- _context6.next = 3;
423
+ _context5.next = 3;
424
424
  return fsSynchronizeModifications(functionName, visitor, config);
425
425
 
426
426
  case 3:
427
427
  case "end":
428
- return _context6.stop();
428
+ return _context5.stop();
429
429
  }
430
430
  }
431
- }, _callee6);
431
+ }, _callee5);
432
432
  }));
433
433
 
434
434
  return function synchronizeModifications() {
435
- return _ref8.apply(this, arguments);
435
+ return _ref7.apply(this, arguments);
436
436
  };
437
437
  }();
438
438
 
@@ -456,7 +456,38 @@ export var useFlagship = function useFlagship() {
456
456
  });
457
457
  };
458
458
 
459
+ function getFlag(key, defaultValue) {
460
+ if (!visitor) {
461
+ return new Flag(defaultValue);
462
+ }
463
+
464
+ return visitor.getFlag(key, defaultValue);
465
+ }
466
+
467
+ function fetchFlags() {
468
+ if (!visitor) {
469
+ logWarn(config, noVisitorMessage, 'fetchFlags');
470
+ return Promise.resolve();
471
+ }
472
+
473
+ return visitor === null || visitor === void 0 ? void 0 : visitor.fetchFlags();
474
+ }
475
+
476
+ function setConsent(hasConsented) {
477
+ if (!visitor) {
478
+ logWarn(config, noVisitorMessage, 'setConsent');
479
+ return;
480
+ }
481
+
482
+ visitor.setConsent(hasConsented);
483
+ }
484
+
459
485
  return {
486
+ visitorId: visitor === null || visitor === void 0 ? void 0 : visitor.visitorId,
487
+ anonymousId: visitor === null || visitor === void 0 ? void 0 : visitor.anonymousId,
488
+ context: _objectSpread({}, visitor === null || visitor === void 0 ? void 0 : visitor.context),
489
+ hasConsented: visitor === null || visitor === void 0 ? void 0 : visitor.hasConsented,
490
+ setConsent: setConsent,
460
491
  updateContext: fsUpdateContext,
461
492
  clearContext: fsClearContext,
462
493
  authenticate: fsAuthenticate,
@@ -466,15 +497,16 @@ export var useFlagship = function useFlagship() {
466
497
  synchronizeModifications: synchronizeModifications,
467
498
  getModifications: getModifications,
468
499
  modifications: modifications || [],
500
+ FlagsData: (visitor === null || visitor === void 0 ? void 0 : visitor.getFlagsDataArray()) || [],
469
501
  getModificationInfo: getModificationInfo,
470
502
  hit: {
471
503
  send: fsSendHit,
472
504
  sendMultiple: fsSendHits
473
- }
505
+ },
506
+ getFlag: getFlag,
507
+ fetchFlags: fetchFlags
474
508
  };
475
509
  };
476
- export var noVisitorMessage = 'sdk not correctly initialized... Make sure fsVisitor is ready.';
477
- export var noVisitorDefault = 'fsVisitor not initialized, returns default value';
478
510
 
479
511
  var reportNoVisitor = function reportNoVisitor(config, tag) {
480
512
  logError(config, noVisitorMessage, tag);
@@ -0,0 +1,2 @@
1
+ export var noVisitorMessage = 'sdk not correctly initialized... Make sure fsVisitor is ready.';
2
+ export var noVisitorDefault = 'fsVisitor not initialized, returns default value';
package/dist/esm/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export * from './FlagshipContext';
2
- export { DecisionMode } from '@flagship.io/js-sdk';
2
+ export * from '@flagship.io/js-sdk';
3
3
  export * from './FlagshipHooks';
package/dist/esm/utils.js CHANGED
@@ -20,6 +20,16 @@ export function logWarn(config, message, tag) {
20
20
 
21
21
  config.logManager.warning(message, tag);
22
22
  }
23
+ export function log(level, message, tag) {
24
+ var now = new Date(); // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
+
26
+ var getTwoDigit = function getTwoDigit(value) {
27
+ return value.toString().length === 1 ? "0".concat(value) : value;
28
+ };
29
+
30
+ var out = "[".concat(getTwoDigit(now.getFullYear()), "-").concat(getTwoDigit(now.getMonth()), "-").concat(getTwoDigit(now.getDay()), " ").concat(getTwoDigit(now.getHours()), ":").concat(getTwoDigit(now.getMinutes()), "] [Flagship SDK] [").concat(LogLevel[level], "] [").concat(tag, "] : ").concat(message);
31
+ console.log(out);
32
+ }
23
33
  export var getModificationsFromCampaigns = function getModificationsFromCampaigns(campaigns) {
24
34
  var modifications = new Map();
25
35
 
@@ -43,4 +53,11 @@ export var getModificationsFromCampaigns = function getModificationsFromCampaign
43
53
  }
44
54
  });
45
55
  return modifications;
46
- };
56
+ };
57
+ export function uuidV4() {
58
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (char) {
59
+ var rand = Math.random() * 16 | 0;
60
+ var value = char === 'x' ? rand : rand & 0x3 | 0x8;
61
+ return value.toString(16);
62
+ });
63
+ }