@flagship.io/react-sdk 3.0.6-alpha.0 → 3.0.7

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,14 +1,3 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -18,57 +7,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
18
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
19
8
  });
20
9
  };
21
- var __generator = (this && this.__generator) || function (thisArg, body) {
22
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
- function verb(n) { return function (v) { return step([n, v]); }; }
25
- function step(op) {
26
- if (f) throw new TypeError("Generator is already executing.");
27
- while (_) try {
28
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
- if (y = 0, t) op = [op[0] & 2, t.value];
30
- switch (op[0]) {
31
- case 0: case 1: t = op; break;
32
- case 4: _.label++; return { value: op[1], done: false };
33
- case 5: _.label++; y = op[1]; op = [0]; continue;
34
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
- default:
36
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
- if (t[2]) _.ops.pop();
41
- _.trys.pop(); continue;
42
- }
43
- op = body.call(thisArg, _);
44
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
- }
47
- };
48
10
  import { useContext } from 'react';
49
11
  import { FlagshipContext } from './FlagshipContext';
50
12
  import { logError, logWarn } from './utils';
51
13
  import { Flag } from './Flag';
52
14
  import { noVisitorDefault, noVisitorMessage } from './constants';
53
- var checkType = function (value, defaultValue) {
54
- return (typeof value === 'object' &&
55
- typeof defaultValue === 'object' &&
56
- Array.isArray(value) === Array.isArray(defaultValue)) ||
57
- typeof value === typeof defaultValue;
58
- };
59
- var fsModificationsSync = function (args) {
60
- var visitor = args.visitor, params = args.params, activateAll = args.activateAll, state = args.state, functionName = args.functionName, config = args.config;
15
+ const checkType = (value, defaultValue) => (typeof value === 'object' &&
16
+ typeof defaultValue === 'object' &&
17
+ Array.isArray(value) === Array.isArray(defaultValue)) ||
18
+ typeof value === typeof defaultValue;
19
+ const fsModificationsSync = (args) => {
20
+ const { visitor, params, activateAll, state, functionName, config } = args;
61
21
  if (visitor) {
62
22
  return visitor.getModificationsSync(params, activateAll);
63
23
  }
64
- var check = !state.status.isSdkReady &&
24
+ const check = !state.status.isSdkReady &&
65
25
  !!state.modifications &&
66
26
  state.modifications.size > 0;
67
- var flags = {};
27
+ const flags = {};
68
28
  if (check) {
69
- params.forEach(function (item) {
29
+ params.forEach((item) => {
70
30
  var _a;
71
- var modification = (_a = state.modifications) === null || _a === void 0 ? void 0 : _a.get(item.key);
31
+ const modification = (_a = state.modifications) === null || _a === void 0 ? void 0 : _a.get(item.key);
72
32
  if (modification && checkType(modification === null || modification === void 0 ? void 0 : modification.value, item.defaultValue)) {
73
33
  flags[item.key] = modification.value;
74
34
  }
@@ -79,7 +39,7 @@ var fsModificationsSync = function (args) {
79
39
  return flags;
80
40
  }
81
41
  logWarn(config, noVisitorDefault, functionName);
82
- params.forEach(function (item) {
42
+ params.forEach((item) => {
83
43
  flags[item.key] = item.defaultValue;
84
44
  });
85
45
  return flags;
@@ -88,32 +48,32 @@ var fsModificationsSync = function (args) {
88
48
  * 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.
89
49
  * @deprecated use useFsFlag instead
90
50
  */
91
- export var useFsModifications = function (params, activateAll) {
92
- var state = useContext(FlagshipContext).state;
93
- var visitor = state.visitor, config = state.config;
94
- var functionName = 'useFsModifications';
51
+ export const useFsModifications = (params, activateAll) => {
52
+ const { state } = useContext(FlagshipContext);
53
+ const { visitor, config } = state;
54
+ const functionName = 'useFsModifications';
95
55
  return fsModificationsSync({
96
- functionName: functionName,
97
- state: state,
98
- visitor: visitor,
99
- config: config,
100
- params: params,
101
- activateAll: activateAll
56
+ functionName,
57
+ state,
58
+ visitor,
59
+ config,
60
+ params,
61
+ activateAll
102
62
  });
103
63
  };
104
64
  /**
105
65
  * 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.
106
66
  * @deprecated use useFsFlag instead
107
67
  */
108
- export var useFsModification = function (params) {
68
+ export const useFsModification = (params) => {
109
69
  var _a;
110
- var state = useContext(FlagshipContext).state;
111
- var visitor = state.visitor, config = state.config;
112
- var functionName = 'useFsModifications';
70
+ const { state } = useContext(FlagshipContext);
71
+ const { visitor, config } = state;
72
+ const functionName = 'useFsModifications';
113
73
  if (visitor) {
114
74
  return visitor.getModificationSync(params);
115
75
  }
116
- var modification = (_a = state.modifications) === null || _a === void 0 ? void 0 : _a.get(params.key);
76
+ const modification = (_a = state.modifications) === null || _a === void 0 ? void 0 : _a.get(params.key);
117
77
  if (!state.status.isSdkReady &&
118
78
  modification &&
119
79
  checkType(modification === null || modification === void 0 ? void 0 : modification.value, params.defaultValue)) {
@@ -122,13 +82,13 @@ export var useFsModification = function (params) {
122
82
  logWarn(config, noVisitorDefault, functionName);
123
83
  return params.defaultValue;
124
84
  };
125
- var fsModificationInfoSync = function (args) {
85
+ const fsModificationInfoSync = (args) => {
126
86
  var _a;
127
- var key = args.key, visitor = args.visitor, state = args.state;
87
+ const { key, visitor, state } = args;
128
88
  if (visitor) {
129
89
  return visitor.getModificationInfoSync(key);
130
90
  }
131
- var modification = (_a = state.modifications) === null || _a === void 0 ? void 0 : _a.get(key);
91
+ const modification = (_a = state.modifications) === null || _a === void 0 ? void 0 : _a.get(key);
132
92
  if (!state.status.isSdkReady && modification) {
133
93
  return modification;
134
94
  }
@@ -139,44 +99,33 @@ var fsModificationInfoSync = function (args) {
139
99
  * @param {string} key key which identify the modification.
140
100
  * @deprecated use useFsFlag instead
141
101
  */
142
- export var useFsModificationInfo = function (key) {
143
- var state = useContext(FlagshipContext).state;
144
- var visitor = state.visitor;
145
- return fsModificationInfoSync({ key: key, state: state, visitor: visitor });
102
+ export const useFsModificationInfo = (key) => {
103
+ const { state } = useContext(FlagshipContext);
104
+ const { visitor } = state;
105
+ return fsModificationInfoSync({ key, state, visitor });
146
106
  };
147
- var fsActivate = function (params, functionName, visitor, config) { return __awaiter(void 0, void 0, void 0, function () {
148
- var error_1;
149
- return __generator(this, function (_a) {
150
- switch (_a.label) {
151
- case 0:
152
- _a.trys.push([0, 2, , 3]);
153
- if (!visitor) {
154
- logWarn(config, noVisitorMessage, functionName);
155
- return [2 /*return*/];
156
- }
157
- return [4 /*yield*/, visitor.activateModifications(params)
158
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
159
- ];
160
- case 1:
161
- _a.sent();
162
- return [3 /*break*/, 3];
163
- case 2:
164
- error_1 = _a.sent();
165
- logWarn(config, error_1.message || error_1, functionName);
166
- return [3 /*break*/, 3];
167
- case 3: return [2 /*return*/];
107
+ const fsActivate = (params, functionName, visitor, config) => __awaiter(void 0, void 0, void 0, function* () {
108
+ try {
109
+ if (!visitor) {
110
+ logWarn(config, noVisitorMessage, functionName);
111
+ return;
168
112
  }
169
- });
170
- }); };
113
+ yield visitor.activateModifications(params);
114
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
115
+ }
116
+ catch (error) {
117
+ logWarn(config, error.message || error, functionName);
118
+ }
119
+ });
171
120
  /**
172
121
  * This hook returns a flag object by its key. If no flag match the given key an empty flag will be returned.
173
122
  * @param key
174
123
  * @param defaultValue
175
124
  * @returns
176
125
  */
177
- export var useFsFlag = function (key, defaultValue) {
178
- var state = useContext(FlagshipContext).state;
179
- var visitor = state.visitor;
126
+ export const useFsFlag = (key, defaultValue) => {
127
+ const { state } = useContext(FlagshipContext);
128
+ const { visitor } = state;
180
129
  if (!visitor) {
181
130
  return new Flag(defaultValue, key, state.modifications);
182
131
  }
@@ -188,26 +137,17 @@ export var useFsFlag = function (key, defaultValue) {
188
137
  * @deprecated use useFsFlag instead
189
138
  * @returns
190
139
  */
191
- export var useFsActivate = function (params) { return __awaiter(void 0, void 0, void 0, function () {
192
- var state, visitor, config, functionName;
193
- return __generator(this, function (_a) {
194
- switch (_a.label) {
195
- case 0:
196
- state = useContext(FlagshipContext).state;
197
- visitor = state.visitor, config = state.config;
198
- functionName = 'useFsModifications';
199
- return [4 /*yield*/, fsActivate(params, functionName, visitor, config)];
200
- case 1:
201
- _a.sent();
202
- return [2 /*return*/];
203
- }
204
- });
205
- }); };
206
- export var useFlagship = function () {
207
- var state = useContext(FlagshipContext).state;
208
- var visitor = state.visitor, config = state.config;
209
- var fsUpdateContext = function (context) {
210
- var functionName = 'updateContext';
140
+ export const useFsActivate = (params) => __awaiter(void 0, void 0, void 0, function* () {
141
+ const { state } = useContext(FlagshipContext);
142
+ const { visitor, config } = state;
143
+ const functionName = 'useFsModifications';
144
+ yield fsActivate(params, functionName, visitor, config);
145
+ });
146
+ export const useFlagship = () => {
147
+ const { state } = useContext(FlagshipContext);
148
+ const { visitor, config } = state;
149
+ const fsUpdateContext = (context) => {
150
+ const functionName = 'updateContext';
211
151
  if (!visitor) {
212
152
  logError(config, noVisitorMessage, functionName);
213
153
  return;
@@ -215,24 +155,24 @@ export var useFlagship = function () {
215
155
  visitor.clearContext();
216
156
  visitor.updateContext(context);
217
157
  };
218
- var fsClearContext = function () {
219
- var functionName = 'cleanContext';
158
+ const fsClearContext = () => {
159
+ const functionName = 'cleanContext';
220
160
  if (!visitor) {
221
161
  logError(config, noVisitorMessage, functionName);
222
162
  return;
223
163
  }
224
164
  visitor.clearContext();
225
165
  };
226
- var fsAuthenticate = function (visitorId) {
227
- var functionName = 'authenticate';
166
+ const fsAuthenticate = (visitorId) => {
167
+ const functionName = 'authenticate';
228
168
  if (!visitor) {
229
169
  logError(config, noVisitorMessage, functionName);
230
170
  return;
231
171
  }
232
172
  visitor.authenticate(visitorId);
233
173
  };
234
- var fsUnauthenticate = function () {
235
- var functionName = 'unauthenticate';
174
+ const fsUnauthenticate = () => {
175
+ const functionName = 'unauthenticate';
236
176
  if (!visitor) {
237
177
  logError(config, noVisitorMessage, functionName);
238
178
  return;
@@ -242,8 +182,8 @@ export var useFlagship = function () {
242
182
  /**
243
183
  * Send a Hit to Flagship servers for reporting.
244
184
  */
245
- var fsSendHit = function (hit) {
246
- var functionName = 'sendHit';
185
+ const fsSendHit = (hit) => {
186
+ const functionName = 'sendHit';
247
187
  if (!visitor) {
248
188
  logError(config, noVisitorMessage, functionName);
249
189
  return Promise.resolve();
@@ -253,63 +193,43 @@ export var useFlagship = function () {
253
193
  /**
254
194
  * Send a Hit to Flagship servers for reporting.
255
195
  */
256
- var fsSendHits = function (hit) {
257
- var functionName = 'sendHits';
196
+ const fsSendHits = (hit) => {
197
+ const functionName = 'sendHits';
258
198
  if (!visitor) {
259
199
  logError(config, noVisitorMessage, functionName);
260
200
  return Promise.resolve();
261
201
  }
262
202
  return visitor.sendHits(hit);
263
203
  };
264
- var modifications = visitor === null || visitor === void 0 ? void 0 : visitor.getModificationsArray();
204
+ let modifications = visitor === null || visitor === void 0 ? void 0 : visitor.getModificationsArray();
265
205
  if (!state.status.isSdkReady && state.modifications) {
266
206
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
267
- modifications = Array.from(state.modifications, function (_a) {
268
- var _key = _a[0], item = _a[1];
269
- return item;
270
- });
207
+ modifications = Array.from(state.modifications, ([_key, item]) => item);
271
208
  }
272
- var activateModification = function (params) { return __awaiter(void 0, void 0, void 0, function () {
273
- var functionName;
274
- return __generator(this, function (_a) {
275
- switch (_a.label) {
276
- case 0:
277
- functionName = 'activateModification';
278
- return [4 /*yield*/, fsActivate(params, functionName, visitor, config)];
279
- case 1:
280
- _a.sent();
281
- return [2 /*return*/];
282
- }
283
- });
284
- }); };
285
- var synchronizeModifications = function () { return __awaiter(void 0, void 0, void 0, function () {
286
- return __generator(this, function (_a) {
287
- switch (_a.label) {
288
- case 0:
289
- if (!visitor) {
290
- logWarn(config, noVisitorMessage, 'synchronizeModifications');
291
- return [2 /*return*/];
292
- }
293
- return [4 /*yield*/, visitor.synchronizeModifications()];
294
- case 1:
295
- _a.sent();
296
- return [2 /*return*/];
297
- }
298
- });
299
- }); };
300
- var getModifications = function (params, activateAll) {
301
- var functionName = 'getModifications';
209
+ const activateModification = (params) => __awaiter(void 0, void 0, void 0, function* () {
210
+ const functionName = 'activateModification';
211
+ yield fsActivate(params, functionName, visitor, config);
212
+ });
213
+ const synchronizeModifications = () => __awaiter(void 0, void 0, void 0, function* () {
214
+ if (!visitor) {
215
+ logWarn(config, noVisitorMessage, 'synchronizeModifications');
216
+ return;
217
+ }
218
+ yield visitor.synchronizeModifications();
219
+ });
220
+ const getModifications = (params, activateAll) => {
221
+ const functionName = 'getModifications';
302
222
  return fsModificationsSync({
303
- functionName: functionName,
304
- state: state,
305
- visitor: visitor,
306
- config: config,
307
- params: params,
308
- activateAll: activateAll
223
+ functionName,
224
+ state,
225
+ visitor,
226
+ config,
227
+ params,
228
+ activateAll
309
229
  });
310
230
  };
311
- var getModificationInfo = function (key) {
312
- return fsModificationInfoSync({ key: key, state: state, visitor: visitor });
231
+ const getModificationInfo = (key) => {
232
+ return fsModificationInfoSync({ key, state, visitor });
313
233
  };
314
234
  function getFlag(key, defaultValue) {
315
235
  if (!visitor) {
@@ -334,25 +254,25 @@ export var useFlagship = function () {
334
254
  return {
335
255
  visitorId: visitor === null || visitor === void 0 ? void 0 : visitor.visitorId,
336
256
  anonymousId: visitor === null || visitor === void 0 ? void 0 : visitor.anonymousId,
337
- context: __assign({}, visitor === null || visitor === void 0 ? void 0 : visitor.context),
257
+ context: Object.assign({}, visitor === null || visitor === void 0 ? void 0 : visitor.context),
338
258
  hasConsented: visitor === null || visitor === void 0 ? void 0 : visitor.hasConsented,
339
- setConsent: setConsent,
259
+ setConsent,
340
260
  updateContext: fsUpdateContext,
341
261
  clearContext: fsClearContext,
342
262
  authenticate: fsAuthenticate,
343
263
  unauthenticate: fsUnauthenticate,
344
264
  status: state.status,
345
- activateModification: activateModification,
346
- synchronizeModifications: synchronizeModifications,
347
- getModifications: getModifications,
265
+ activateModification,
266
+ synchronizeModifications,
267
+ getModifications,
348
268
  modifications: modifications || [],
349
269
  flagsData: (visitor === null || visitor === void 0 ? void 0 : visitor.getFlagsDataArray()) || [],
350
- getModificationInfo: getModificationInfo,
270
+ getModificationInfo,
351
271
  hit: {
352
272
  send: fsSendHit,
353
273
  sendMultiple: fsSendHits
354
274
  },
355
- getFlag: getFlag,
356
- fetchFlags: fetchFlags
275
+ getFlag,
276
+ fetchFlags
357
277
  };
358
278
  };
package/dist/constants.js CHANGED
@@ -1,4 +1,4 @@
1
- export var noVisitorMessage = 'flagship Visitor not initialized.';
2
- export var noVisitorDefault = 'fsVisitor not initialized, returns default value';
3
- export var GET_FLAG_CAST_ERROR = 'Flag for key {0} has a different type. Default value is returned.';
4
- export var GET_METADATA_CAST_ERROR = 'Flag for key {0} has a different type with defaultValue, an empty metadata object is returned';
1
+ export const noVisitorMessage = 'flagship Visitor not initialized.';
2
+ export const noVisitorDefault = 'fsVisitor not initialized, returns default value';
3
+ export const GET_FLAG_CAST_ERROR = 'Flag for key {0} has a different type. Default value is returned.';
4
+ export const GET_METADATA_CAST_ERROR = 'Flag for key {0} has a different type with defaultValue, an empty metadata object is returned';