@flagship.io/react-sdk 3.0.4-alpha.4 → 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.
- package/README.md +23 -12
- package/dist/Flag.d.ts +13 -0
- package/dist/Flag.js +68 -0
- package/dist/{types/FlagshipContext.d.ts → FlagshipContext.d.ts} +0 -0
- package/dist/FlagshipContext.js +181 -0
- package/dist/{types/FlagshipHooks.d.ts → FlagshipHooks.d.ts} +9 -9
- package/dist/FlagshipHooks.js +358 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +4 -0
- package/dist/index.browser.js +19207 -2
- package/dist/index.browser.js.map +1 -1
- package/dist/{types/index.d.ts → index.d.ts} +0 -0
- package/dist/index.js +3 -2
- package/dist/index.node.js +1535 -0
- package/dist/index.node.js.map +1 -0
- package/dist/{types/utils.d.ts → utils.d.ts} +2 -0
- package/dist/utils.js +94 -0
- package/package.json +9 -8
- package/dist/esm/Flag.js +0 -49
- package/dist/esm/FlagshipContext.js +0 -269
- package/dist/esm/FlagshipHooks.js +0 -474
- package/dist/esm/constants.js +0 -2
- package/dist/esm/index.js +0 -3
- package/dist/esm/utils.js +0 -67
- package/dist/index.browser.js.LICENSE.txt +0 -657
- package/dist/index.js.map +0 -1
- package/dist/types/Flag.d.ts +0 -9
- package/dist/types/constants.d.ts +0 -2
|
@@ -0,0 +1,358 @@
|
|
|
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
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
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
|
+
import { useContext } from 'react';
|
|
49
|
+
import { FlagshipContext } from './FlagshipContext';
|
|
50
|
+
import { logError, logWarn } from './utils';
|
|
51
|
+
import { Flag } from './Flag';
|
|
52
|
+
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;
|
|
61
|
+
if (visitor) {
|
|
62
|
+
return visitor.getModificationsSync(params, activateAll);
|
|
63
|
+
}
|
|
64
|
+
var check = !state.status.isSdkReady &&
|
|
65
|
+
!!state.modifications &&
|
|
66
|
+
state.modifications.size > 0;
|
|
67
|
+
var flags = {};
|
|
68
|
+
if (check) {
|
|
69
|
+
params.forEach(function (item) {
|
|
70
|
+
var _a;
|
|
71
|
+
var modification = (_a = state.modifications) === null || _a === void 0 ? void 0 : _a.get(item.key);
|
|
72
|
+
if (modification && checkType(modification === null || modification === void 0 ? void 0 : modification.value, item.defaultValue)) {
|
|
73
|
+
flags[item.key] = modification.value;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
flags[item.key] = item.defaultValue;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return flags;
|
|
80
|
+
}
|
|
81
|
+
logWarn(config, noVisitorDefault, functionName);
|
|
82
|
+
params.forEach(function (item) {
|
|
83
|
+
flags[item.key] = item.defaultValue;
|
|
84
|
+
});
|
|
85
|
+
return flags;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* 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
|
+
* @deprecated use useFsFlag instead
|
|
90
|
+
*/
|
|
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';
|
|
95
|
+
return fsModificationsSync({
|
|
96
|
+
functionName: functionName,
|
|
97
|
+
state: state,
|
|
98
|
+
visitor: visitor,
|
|
99
|
+
config: config,
|
|
100
|
+
params: params,
|
|
101
|
+
activateAll: activateAll
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* 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
|
+
* @deprecated use useFsFlag instead
|
|
107
|
+
*/
|
|
108
|
+
export var useFsModification = function (params) {
|
|
109
|
+
var _a;
|
|
110
|
+
var state = useContext(FlagshipContext).state;
|
|
111
|
+
var visitor = state.visitor, config = state.config;
|
|
112
|
+
var functionName = 'useFsModifications';
|
|
113
|
+
if (visitor) {
|
|
114
|
+
return visitor.getModificationSync(params);
|
|
115
|
+
}
|
|
116
|
+
var modification = (_a = state.modifications) === null || _a === void 0 ? void 0 : _a.get(params.key);
|
|
117
|
+
if (!state.status.isSdkReady &&
|
|
118
|
+
modification &&
|
|
119
|
+
checkType(modification === null || modification === void 0 ? void 0 : modification.value, params.defaultValue)) {
|
|
120
|
+
return modification.value;
|
|
121
|
+
}
|
|
122
|
+
logWarn(config, noVisitorDefault, functionName);
|
|
123
|
+
return params.defaultValue;
|
|
124
|
+
};
|
|
125
|
+
var fsModificationInfoSync = function (args) {
|
|
126
|
+
var _a;
|
|
127
|
+
var key = args.key, visitor = args.visitor, state = args.state;
|
|
128
|
+
if (visitor) {
|
|
129
|
+
return visitor.getModificationInfoSync(key);
|
|
130
|
+
}
|
|
131
|
+
var modification = (_a = state.modifications) === null || _a === void 0 ? void 0 : _a.get(key);
|
|
132
|
+
if (!state.status.isSdkReady && modification) {
|
|
133
|
+
return modification;
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Get the campaign modification information value matching the given key.
|
|
139
|
+
* @param {string} key key which identify the modification.
|
|
140
|
+
* @deprecated use useFsFlag instead
|
|
141
|
+
*/
|
|
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 });
|
|
146
|
+
};
|
|
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*/];
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}); };
|
|
171
|
+
/**
|
|
172
|
+
* This hook returns a flag object by its key. If no flag match the given key an empty flag will be returned.
|
|
173
|
+
* @param key
|
|
174
|
+
* @param defaultValue
|
|
175
|
+
* @returns
|
|
176
|
+
*/
|
|
177
|
+
export var useFsFlag = function (key, defaultValue) {
|
|
178
|
+
var state = useContext(FlagshipContext).state;
|
|
179
|
+
var visitor = state.visitor;
|
|
180
|
+
if (!visitor) {
|
|
181
|
+
return new Flag(defaultValue, key, state.modifications);
|
|
182
|
+
}
|
|
183
|
+
return visitor.getFlag(key, defaultValue);
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* Report this user has seen this modification. Report this user has seen these modifications.
|
|
187
|
+
* @param params
|
|
188
|
+
* @deprecated use useFsFlag instead
|
|
189
|
+
* @returns
|
|
190
|
+
*/
|
|
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';
|
|
211
|
+
if (!visitor) {
|
|
212
|
+
logError(config, noVisitorMessage, functionName);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
visitor.clearContext();
|
|
216
|
+
visitor.updateContext(context);
|
|
217
|
+
};
|
|
218
|
+
var fsClearContext = function () {
|
|
219
|
+
var functionName = 'cleanContext';
|
|
220
|
+
if (!visitor) {
|
|
221
|
+
logError(config, noVisitorMessage, functionName);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
visitor.clearContext();
|
|
225
|
+
};
|
|
226
|
+
var fsAuthenticate = function (visitorId) {
|
|
227
|
+
var functionName = 'authenticate';
|
|
228
|
+
if (!visitor) {
|
|
229
|
+
logError(config, noVisitorMessage, functionName);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
visitor.authenticate(visitorId);
|
|
233
|
+
};
|
|
234
|
+
var fsUnauthenticate = function () {
|
|
235
|
+
var functionName = 'unauthenticate';
|
|
236
|
+
if (!visitor) {
|
|
237
|
+
logError(config, noVisitorMessage, functionName);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
visitor.unauthenticate();
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* Send a Hit to Flagship servers for reporting.
|
|
244
|
+
*/
|
|
245
|
+
var fsSendHit = function (hit) {
|
|
246
|
+
var functionName = 'sendHit';
|
|
247
|
+
if (!visitor) {
|
|
248
|
+
logError(config, noVisitorMessage, functionName);
|
|
249
|
+
return Promise.resolve();
|
|
250
|
+
}
|
|
251
|
+
return visitor.sendHit(hit);
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* Send a Hit to Flagship servers for reporting.
|
|
255
|
+
*/
|
|
256
|
+
var fsSendHits = function (hit) {
|
|
257
|
+
var functionName = 'sendHits';
|
|
258
|
+
if (!visitor) {
|
|
259
|
+
logError(config, noVisitorMessage, functionName);
|
|
260
|
+
return Promise.resolve();
|
|
261
|
+
}
|
|
262
|
+
return visitor.sendHits(hit);
|
|
263
|
+
};
|
|
264
|
+
var modifications = visitor === null || visitor === void 0 ? void 0 : visitor.getModificationsArray();
|
|
265
|
+
if (!state.status.isSdkReady && state.modifications) {
|
|
266
|
+
// 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
|
+
});
|
|
271
|
+
}
|
|
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';
|
|
302
|
+
return fsModificationsSync({
|
|
303
|
+
functionName: functionName,
|
|
304
|
+
state: state,
|
|
305
|
+
visitor: visitor,
|
|
306
|
+
config: config,
|
|
307
|
+
params: params,
|
|
308
|
+
activateAll: activateAll
|
|
309
|
+
});
|
|
310
|
+
};
|
|
311
|
+
var getModificationInfo = function (key) {
|
|
312
|
+
return fsModificationInfoSync({ key: key, state: state, visitor: visitor });
|
|
313
|
+
};
|
|
314
|
+
function getFlag(key, defaultValue) {
|
|
315
|
+
if (!visitor) {
|
|
316
|
+
return new Flag(defaultValue, key, state.modifications);
|
|
317
|
+
}
|
|
318
|
+
return visitor.getFlag(key, defaultValue);
|
|
319
|
+
}
|
|
320
|
+
function fetchFlags() {
|
|
321
|
+
if (!visitor) {
|
|
322
|
+
logWarn(config, noVisitorMessage, 'fetchFlags');
|
|
323
|
+
return Promise.resolve();
|
|
324
|
+
}
|
|
325
|
+
return visitor.fetchFlags();
|
|
326
|
+
}
|
|
327
|
+
function setConsent(hasConsented) {
|
|
328
|
+
if (!visitor) {
|
|
329
|
+
logWarn(config, noVisitorMessage, 'setConsent');
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
visitor.setConsent(hasConsented);
|
|
333
|
+
}
|
|
334
|
+
return {
|
|
335
|
+
visitorId: visitor === null || visitor === void 0 ? void 0 : visitor.visitorId,
|
|
336
|
+
anonymousId: visitor === null || visitor === void 0 ? void 0 : visitor.anonymousId,
|
|
337
|
+
context: __assign({}, visitor === null || visitor === void 0 ? void 0 : visitor.context),
|
|
338
|
+
hasConsented: visitor === null || visitor === void 0 ? void 0 : visitor.hasConsented,
|
|
339
|
+
setConsent: setConsent,
|
|
340
|
+
updateContext: fsUpdateContext,
|
|
341
|
+
clearContext: fsClearContext,
|
|
342
|
+
authenticate: fsAuthenticate,
|
|
343
|
+
unauthenticate: fsUnauthenticate,
|
|
344
|
+
status: state.status,
|
|
345
|
+
activateModification: activateModification,
|
|
346
|
+
synchronizeModifications: synchronizeModifications,
|
|
347
|
+
getModifications: getModifications,
|
|
348
|
+
modifications: modifications || [],
|
|
349
|
+
flagsData: (visitor === null || visitor === void 0 ? void 0 : visitor.getFlagsDataArray()) || [],
|
|
350
|
+
getModificationInfo: getModificationInfo,
|
|
351
|
+
hit: {
|
|
352
|
+
send: fsSendHit,
|
|
353
|
+
sendMultiple: fsSendHits
|
|
354
|
+
},
|
|
355
|
+
getFlag: getFlag,
|
|
356
|
+
fetchFlags: fetchFlags
|
|
357
|
+
};
|
|
358
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const noVisitorMessage = "flagship Visitor not initialized.";
|
|
2
|
+
export declare const noVisitorDefault = "fsVisitor not initialized, returns default value";
|
|
3
|
+
export declare const GET_FLAG_CAST_ERROR = "Flag for key {0} has a different type. Default value is returned.";
|
|
4
|
+
export declare const GET_METADATA_CAST_ERROR = "Flag for key {0} has a different type with defaultValue, an empty metadata object is returned";
|
|
@@ -0,0 +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';
|