@flagship.io/react-sdk 3.0.5 → 3.0.6-alpha.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,474 +0,0 @@
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
-
7
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
8
-
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."); }
10
-
11
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
12
-
13
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
14
-
15
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
16
-
17
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
18
-
19
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
20
-
21
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
22
-
23
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
24
-
25
- import { useContext } from 'react';
26
- import { FlagshipContext } from './FlagshipContext';
27
- import { logError, logWarn } from './utils';
28
- import { Flag } from './Flag';
29
- import { noVisitorDefault, noVisitorMessage } from './constants';
30
-
31
- var checkType = function checkType(value, defaultValue) {
32
- return _typeof(value) === 'object' && _typeof(defaultValue) === 'object' && Array.isArray(value) === Array.isArray(defaultValue) || _typeof(value) === _typeof(defaultValue);
33
- };
34
-
35
- var fsModificationsSync = function fsModificationsSync(args) {
36
- var visitor = args.visitor,
37
- params = args.params,
38
- activateAll = args.activateAll,
39
- state = args.state,
40
- functionName = args.functionName,
41
- config = args.config;
42
-
43
- if (visitor) {
44
- return visitor.getModificationsSync(params, activateAll);
45
- }
46
-
47
- var check = !state.status.isSdkReady && !!state.modifications && state.modifications.size > 0;
48
- var flags = {};
49
-
50
- if (check) {
51
- params.forEach(function (item) {
52
- var _state$modifications;
53
-
54
- var modification = (_state$modifications = state.modifications) === null || _state$modifications === void 0 ? void 0 : _state$modifications.get(item.key);
55
-
56
- if (modification && checkType(modification === null || modification === void 0 ? void 0 : modification.value, item.defaultValue)) {
57
- flags[item.key] = modification.value;
58
- } else {
59
- flags[item.key] = item.defaultValue;
60
- }
61
- });
62
- return flags;
63
- }
64
-
65
- logWarn(config, noVisitorDefault, functionName);
66
- params.forEach(function (item) {
67
- flags[item.key] = item.defaultValue;
68
- });
69
- return flags;
70
- };
71
- /**
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 useFsFlag instead
74
- */
75
-
76
-
77
- export var useFsModifications = function useFsModifications(params, activateAll) {
78
- var _useContext = useContext(FlagshipContext),
79
- state = _useContext.state;
80
-
81
- var visitor = state.visitor,
82
- config = state.config;
83
- var functionName = 'useFsModifications';
84
- return fsModificationsSync({
85
- functionName: functionName,
86
- state: state,
87
- visitor: visitor,
88
- config: config,
89
- params: params,
90
- activateAll: activateAll
91
- });
92
- };
93
- /**
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 useFsFlag instead
96
- */
97
-
98
- export var useFsModification = function useFsModification(params) {
99
- var _state$modifications2;
100
-
101
- var _useContext2 = useContext(FlagshipContext),
102
- state = _useContext2.state;
103
-
104
- var visitor = state.visitor,
105
- config = state.config;
106
- var functionName = 'useFsModifications';
107
-
108
- if (visitor) {
109
- return visitor.getModificationSync(params);
110
- }
111
-
112
- var modification = (_state$modifications2 = state.modifications) === null || _state$modifications2 === void 0 ? void 0 : _state$modifications2.get(params.key);
113
-
114
- if (!state.status.isSdkReady && modification && checkType(modification === null || modification === void 0 ? void 0 : modification.value, params.defaultValue)) {
115
- return modification.value;
116
- }
117
-
118
- logWarn(config, noVisitorDefault, functionName);
119
- return params.defaultValue;
120
- };
121
-
122
- var fsModificationInfoSync = function fsModificationInfoSync(args) {
123
- var _state$modifications3;
124
-
125
- var key = args.key,
126
- visitor = args.visitor,
127
- state = args.state;
128
-
129
- if (visitor) {
130
- return visitor.getModificationInfoSync(key);
131
- }
132
-
133
- var modification = (_state$modifications3 = state.modifications) === null || _state$modifications3 === void 0 ? void 0 : _state$modifications3.get(key);
134
-
135
- if (!state.status.isSdkReady && modification) {
136
- return modification;
137
- }
138
-
139
- return null;
140
- };
141
- /**
142
- * Get the campaign modification information value matching the given key.
143
- * @param {string} key key which identify the modification.
144
- * @deprecated use useFsFlag instead
145
- */
146
-
147
-
148
- export var useFsModificationInfo = function useFsModificationInfo(key) {
149
- var _useContext3 = useContext(FlagshipContext),
150
- state = _useContext3.state;
151
-
152
- var visitor = state.visitor;
153
- return fsModificationInfoSync({
154
- key: key,
155
- state: state,
156
- visitor: visitor
157
- });
158
- };
159
-
160
- var fsActivate = /*#__PURE__*/function () {
161
- var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(params, functionName, visitor, config) {
162
- return regeneratorRuntime.wrap(function _callee$(_context) {
163
- while (1) {
164
- switch (_context.prev = _context.next) {
165
- case 0:
166
- _context.prev = 0;
167
-
168
- if (visitor) {
169
- _context.next = 4;
170
- break;
171
- }
172
-
173
- logWarn(config, noVisitorMessage, functionName);
174
- return _context.abrupt("return");
175
-
176
- case 4:
177
- _context.next = 6;
178
- return visitor.activateModifications(params);
179
-
180
- case 6:
181
- _context.next = 11;
182
- break;
183
-
184
- case 8:
185
- _context.prev = 8;
186
- _context.t0 = _context["catch"](0);
187
- logWarn(config, _context.t0.message || _context.t0, functionName);
188
-
189
- case 11:
190
- case "end":
191
- return _context.stop();
192
- }
193
- }
194
- }, _callee, null, [[0, 8]]);
195
- }));
196
-
197
- return function fsActivate(_x, _x2, _x3, _x4) {
198
- return _ref.apply(this, arguments);
199
- };
200
- }();
201
- /**
202
- * This hook returns a flag object by its key. If no flag match the given key an empty flag will be returned.
203
- * @param key
204
- * @param defaultValue
205
- * @returns
206
- */
207
-
208
-
209
- export var useFsFlag = function useFsFlag(key, defaultValue) {
210
- var _useContext4 = useContext(FlagshipContext),
211
- state = _useContext4.state;
212
-
213
- var visitor = state.visitor;
214
-
215
- if (!visitor) {
216
- return new Flag(defaultValue, key, state.modifications);
217
- }
218
-
219
- return visitor.getFlag(key, defaultValue);
220
- };
221
- /**
222
- * Report this user has seen this modification. Report this user has seen these modifications.
223
- * @param params
224
- * @deprecated use useFsFlag instead
225
- * @returns
226
- */
227
-
228
- export var useFsActivate = /*#__PURE__*/function () {
229
- var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(params) {
230
- var _useContext5, state, visitor, config, functionName;
231
-
232
- return regeneratorRuntime.wrap(function _callee2$(_context2) {
233
- while (1) {
234
- switch (_context2.prev = _context2.next) {
235
- case 0:
236
- _useContext5 = useContext(FlagshipContext), state = _useContext5.state;
237
- visitor = state.visitor, config = state.config;
238
- functionName = 'useFsModifications';
239
- _context2.next = 5;
240
- return fsActivate(params, functionName, visitor, config);
241
-
242
- case 5:
243
- case "end":
244
- return _context2.stop();
245
- }
246
- }
247
- }, _callee2);
248
- }));
249
-
250
- return function useFsActivate(_x5) {
251
- return _ref2.apply(this, arguments);
252
- };
253
- }();
254
- export var useFlagship = function useFlagship() {
255
- var _useContext6 = useContext(FlagshipContext),
256
- state = _useContext6.state;
257
-
258
- var visitor = state.visitor,
259
- config = state.config;
260
-
261
- var fsUpdateContext = function fsUpdateContext(context) {
262
- var functionName = 'updateContext';
263
-
264
- if (!visitor) {
265
- logError(config, noVisitorMessage, functionName);
266
- return;
267
- }
268
-
269
- visitor.clearContext();
270
- visitor.updateContext(context);
271
- };
272
-
273
- var fsClearContext = function fsClearContext() {
274
- var functionName = 'cleanContext';
275
-
276
- if (!visitor) {
277
- logError(config, noVisitorMessage, functionName);
278
- return;
279
- }
280
-
281
- visitor.clearContext();
282
- };
283
-
284
- var fsAuthenticate = function fsAuthenticate(visitorId) {
285
- var functionName = 'authenticate';
286
-
287
- if (!visitor) {
288
- logError(config, noVisitorMessage, functionName);
289
- return;
290
- }
291
-
292
- visitor.authenticate(visitorId);
293
- };
294
-
295
- var fsUnauthenticate = function fsUnauthenticate() {
296
- var functionName = 'unauthenticate';
297
-
298
- if (!visitor) {
299
- logError(config, noVisitorMessage, functionName);
300
- return;
301
- }
302
-
303
- visitor.unauthenticate();
304
- };
305
- /**
306
- * Send a Hit to Flagship servers for reporting.
307
- */
308
-
309
-
310
- var fsSendHit = function fsSendHit(hit) {
311
- var functionName = 'sendHit';
312
-
313
- if (!visitor) {
314
- logError(config, noVisitorMessage, functionName);
315
- return Promise.resolve();
316
- }
317
-
318
- return visitor.sendHit(hit);
319
- };
320
- /**
321
- * Send a Hit to Flagship servers for reporting.
322
- */
323
-
324
-
325
- var fsSendHits = function fsSendHits(hit) {
326
- var functionName = 'sendHits';
327
-
328
- if (!visitor) {
329
- logError(config, noVisitorMessage, functionName);
330
- return Promise.resolve();
331
- }
332
-
333
- return visitor.sendHits(hit);
334
- };
335
-
336
- var modifications = visitor === null || visitor === void 0 ? void 0 : visitor.getModificationsArray();
337
-
338
- if (!state.status.isSdkReady && state.modifications) {
339
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
340
- modifications = Array.from(state.modifications, function (_ref3) {
341
- var _ref4 = _slicedToArray(_ref3, 2),
342
- _key = _ref4[0],
343
- item = _ref4[1];
344
-
345
- return item;
346
- });
347
- }
348
-
349
- var activateModification = /*#__PURE__*/function () {
350
- var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(params) {
351
- var functionName;
352
- return regeneratorRuntime.wrap(function _callee3$(_context3) {
353
- while (1) {
354
- switch (_context3.prev = _context3.next) {
355
- case 0:
356
- functionName = 'activateModification';
357
- _context3.next = 3;
358
- return fsActivate(params, functionName, visitor, config);
359
-
360
- case 3:
361
- case "end":
362
- return _context3.stop();
363
- }
364
- }
365
- }, _callee3);
366
- }));
367
-
368
- return function activateModification(_x6) {
369
- return _ref5.apply(this, arguments);
370
- };
371
- }();
372
-
373
- var synchronizeModifications = /*#__PURE__*/function () {
374
- var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
375
- return regeneratorRuntime.wrap(function _callee4$(_context4) {
376
- while (1) {
377
- switch (_context4.prev = _context4.next) {
378
- case 0:
379
- if (visitor) {
380
- _context4.next = 3;
381
- break;
382
- }
383
-
384
- logWarn(config, noVisitorMessage, 'synchronizeModifications');
385
- return _context4.abrupt("return");
386
-
387
- case 3:
388
- _context4.next = 5;
389
- return visitor.synchronizeModifications();
390
-
391
- case 5:
392
- case "end":
393
- return _context4.stop();
394
- }
395
- }
396
- }, _callee4);
397
- }));
398
-
399
- return function synchronizeModifications() {
400
- return _ref6.apply(this, arguments);
401
- };
402
- }();
403
-
404
- var getModifications = function getModifications(params, activateAll) {
405
- var functionName = 'getModifications';
406
- return fsModificationsSync({
407
- functionName: functionName,
408
- state: state,
409
- visitor: visitor,
410
- config: config,
411
- params: params,
412
- activateAll: activateAll
413
- });
414
- };
415
-
416
- var getModificationInfo = function getModificationInfo(key) {
417
- return fsModificationInfoSync({
418
- key: key,
419
- state: state,
420
- visitor: visitor
421
- });
422
- };
423
-
424
- function getFlag(key, defaultValue) {
425
- if (!visitor) {
426
- return new Flag(defaultValue, key, state.modifications);
427
- }
428
-
429
- return visitor.getFlag(key, defaultValue);
430
- }
431
-
432
- function fetchFlags() {
433
- if (!visitor) {
434
- logWarn(config, noVisitorMessage, 'fetchFlags');
435
- return Promise.resolve();
436
- }
437
-
438
- return visitor.fetchFlags();
439
- }
440
-
441
- function setConsent(hasConsented) {
442
- if (!visitor) {
443
- logWarn(config, noVisitorMessage, 'setConsent');
444
- return;
445
- }
446
-
447
- visitor.setConsent(hasConsented);
448
- }
449
-
450
- return {
451
- visitorId: visitor === null || visitor === void 0 ? void 0 : visitor.visitorId,
452
- anonymousId: visitor === null || visitor === void 0 ? void 0 : visitor.anonymousId,
453
- context: _objectSpread({}, visitor === null || visitor === void 0 ? void 0 : visitor.context),
454
- hasConsented: visitor === null || visitor === void 0 ? void 0 : visitor.hasConsented,
455
- setConsent: setConsent,
456
- updateContext: fsUpdateContext,
457
- clearContext: fsClearContext,
458
- authenticate: fsAuthenticate,
459
- unauthenticate: fsUnauthenticate,
460
- status: state.status,
461
- activateModification: activateModification,
462
- synchronizeModifications: synchronizeModifications,
463
- getModifications: getModifications,
464
- modifications: modifications || [],
465
- flagsData: (visitor === null || visitor === void 0 ? void 0 : visitor.getFlagsDataArray()) || [],
466
- getModificationInfo: getModificationInfo,
467
- hit: {
468
- send: fsSendHit,
469
- sendMultiple: fsSendHits
470
- },
471
- getFlag: getFlag,
472
- fetchFlags: fetchFlags
473
- };
474
- };
package/dist/esm/index.js DELETED
@@ -1,3 +0,0 @@
1
- export * from './FlagshipContext';
2
- export * from '@flagship.io/js-sdk';
3
- export * from './FlagshipHooks';
package/dist/esm/utils.js DELETED
@@ -1,90 +0,0 @@
1
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
2
-
3
- import { LogLevel } from '@flagship.io/js-sdk';
4
- import { useEffect, useRef } from 'react';
5
- export function logError(config, message, tag) {
6
- if (!config || !config.logManager || typeof config.logManager.error !== 'function' || !config.logLevel || config.logLevel < LogLevel.ERROR) {
7
- return;
8
- }
9
-
10
- config.logManager.error(message, tag);
11
- }
12
- export function logInfo(config, message, tag) {
13
- if (!config || !config.logManager || typeof config.logManager.info !== 'function' || !config.logLevel || config.logLevel < LogLevel.INFO) {
14
- return;
15
- }
16
-
17
- config.logManager.info(message, tag);
18
- }
19
- export function logWarn(config, message, tag) {
20
- if (!config || !config.logManager || typeof config.logManager.warning !== 'function' || !config.logLevel || config.logLevel < LogLevel.WARNING) {
21
- return;
22
- }
23
-
24
- config.logManager.warning(message, tag);
25
- }
26
- export var getModificationsFromCampaigns = function getModificationsFromCampaigns(campaigns) {
27
- var modifications = new Map();
28
-
29
- if (!campaigns || !Array.isArray(campaigns)) {
30
- return modifications;
31
- }
32
-
33
- campaigns.forEach(function (campaign) {
34
- var object = campaign.variation.modifications.value;
35
-
36
- for (var key in object) {
37
- var value = object[key];
38
- modifications.set(key, {
39
- key: key,
40
- campaignId: campaign.id,
41
- variationGroupId: campaign.variationGroupId,
42
- variationId: campaign.variation.id,
43
- isReference: campaign.variation.reference,
44
- value: value
45
- });
46
- }
47
- });
48
- return modifications;
49
- };
50
- export function uuidV4() {
51
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (char) {
52
- var rand = Math.random() * 16 | 0;
53
- var value = char === 'x' ? rand : rand & 0x3 | 0x8;
54
- return value.toString(16);
55
- });
56
- }
57
- export function useNonInitialEffect(effect, deps) {
58
- var initialRender = useRef(true);
59
- useEffect(function () {
60
- if (initialRender.current) {
61
- initialRender.current = false;
62
- return;
63
- }
64
-
65
- if (typeof effect === 'function') {
66
- return effect();
67
- }
68
- }, deps);
69
- }
70
- export function hasSameType(flagValue, defaultValue) {
71
- if (_typeof(flagValue) !== _typeof(defaultValue)) {
72
- return false;
73
- }
74
-
75
- if (_typeof(flagValue) === 'object' && _typeof(defaultValue) === 'object' && Array.isArray(flagValue) !== Array.isArray(defaultValue)) {
76
- return false;
77
- }
78
-
79
- return true;
80
- }
81
- export function sprintf(format) {
82
- var formatted = format;
83
-
84
- for (var i = 0; i < (arguments.length <= 1 ? 0 : arguments.length - 1); i++) {
85
- var element = i + 1 < 1 || arguments.length <= i + 1 ? undefined : arguments[i + 1];
86
- formatted = formatted.replace(new RegExp("\\{".concat(i, "\\}"), 'g'), element);
87
- }
88
-
89
- return formatted;
90
- }