@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.
- package/README.md +25 -25
- package/dist/{types/Flag.d.ts → Flag.d.ts} +0 -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} +0 -0
- package/dist/FlagshipHooks.js +358 -0
- package/dist/{types/constants.d.ts → constants.d.ts} +0 -0
- package/dist/{esm/constants.js → constants.js} +1 -1
- package/dist/index.browser.js +19207 -1
- 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} +0 -0
- package/dist/utils.js +94 -0
- package/package.json +99 -98
- package/dist/esm/Flag.js +0 -105
- package/dist/esm/FlagshipContext.js +0 -269
- package/dist/esm/FlagshipHooks.js +0 -474
- package/dist/esm/index.js +0 -3
- package/dist/esm/utils.js +0 -90
- package/dist/index.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
## About Flagship
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
<img src="https://www.flagship.io/wp-content/uploads/Flagship-horizontal-black-wake-AB.png" alt="drawing" width="150"/>
|
|
5
|
-
|
|
6
|
-
[Flagship by AB Tasty](https://www.flagship.io/) is a feature flagging platform for modern engineering and product teams. It eliminates the risks of future releases by separating code deployments from these releases :bulb: With Flagship, you have full control over the release process. You can:
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
- Switch features on or off through remote config.
|
|
10
|
-
- Automatically roll-out your features gradually to monitor performance and gather feedback from your most relevant users.
|
|
11
|
-
- Roll back any feature should any issues arise while testing in production.
|
|
12
|
-
- Segment users by granting access to a feature based on certain user attributes.
|
|
13
|
-
- Carry out A/B tests by easily assigning feature variations to groups of users.
|
|
14
|
-
|
|
15
|
-
<img src="https://www.flagship.io/wp-content/uploads/demo-setup.png" alt="drawing" width="600"/>
|
|
16
|
-
|
|
17
|
-
Flagship also allows you to choose whatever implementation method works for you from our many available SDKs or directly through a REST API. Additionally, our architecture is based on multi-cloud providers that offer high performance and highly-scalable managed services.
|
|
18
|
-
|
|
19
|
-
**To learn more:**
|
|
20
|
-
|
|
21
|
-
- [Solution overview](https://www.flagship.io/#showvideo) - A 5mn video demo :movie_camera:
|
|
22
|
-
- [Documentation](https://docs.developers.flagship.io/) - Our dev portal with guides, how tos, API and SDK references
|
|
23
|
-
- [Sign up for a free trial](https://www.flagship.io/sign-up/) - Create your free account
|
|
24
|
-
- [Guide to feature flagging](https://www.flagship.io/feature-flags/) - Everyhting you need to know about feature flag related use cases
|
|
25
|
-
- [Blog](https://www.flagship.io/blog/) - Additional resources about release management
|
|
1
|
+
## About Flagship
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
<img src="https://www.flagship.io/wp-content/uploads/Flagship-horizontal-black-wake-AB.png" alt="drawing" width="150"/>
|
|
5
|
+
|
|
6
|
+
[Flagship by AB Tasty](https://www.flagship.io/) is a feature flagging platform for modern engineering and product teams. It eliminates the risks of future releases by separating code deployments from these releases :bulb: With Flagship, you have full control over the release process. You can:
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
- Switch features on or off through remote config.
|
|
10
|
+
- Automatically roll-out your features gradually to monitor performance and gather feedback from your most relevant users.
|
|
11
|
+
- Roll back any feature should any issues arise while testing in production.
|
|
12
|
+
- Segment users by granting access to a feature based on certain user attributes.
|
|
13
|
+
- Carry out A/B tests by easily assigning feature variations to groups of users.
|
|
14
|
+
|
|
15
|
+
<img src="https://www.flagship.io/wp-content/uploads/demo-setup.png" alt="drawing" width="600"/>
|
|
16
|
+
|
|
17
|
+
Flagship also allows you to choose whatever implementation method works for you from our many available SDKs or directly through a REST API. Additionally, our architecture is based on multi-cloud providers that offer high performance and highly-scalable managed services.
|
|
18
|
+
|
|
19
|
+
**To learn more:**
|
|
20
|
+
|
|
21
|
+
- [Solution overview](https://www.flagship.io/#showvideo) - A 5mn video demo :movie_camera:
|
|
22
|
+
- [Documentation](https://docs.developers.flagship.io/) - Our dev portal with guides, how tos, API and SDK references
|
|
23
|
+
- [Sign up for a free trial](https://www.flagship.io/sign-up/) - Create your free account
|
|
24
|
+
- [Guide to feature flagging](https://www.flagship.io/feature-flags/) - Everyhting you need to know about feature flag related use cases
|
|
25
|
+
- [Blog](https://www.flagship.io/blog/) - Additional resources about release management
|
|
File without changes
|
package/dist/Flag.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import Flagship, { FlagMetadata } from '@flagship.io/js-sdk';
|
|
2
|
+
import { GET_FLAG_CAST_ERROR, GET_METADATA_CAST_ERROR, noVisitorMessage } from './constants';
|
|
3
|
+
import { hasSameType, logInfo, logWarn, sprintf } from './utils';
|
|
4
|
+
var Flag = /** @class */ (function () {
|
|
5
|
+
function Flag(defaultValue, key, flagsData) {
|
|
6
|
+
if (!flagsData) {
|
|
7
|
+
logWarn(Flagship.getConfig(), noVisitorMessage, 'GetFlag');
|
|
8
|
+
}
|
|
9
|
+
this.defaultValue = defaultValue;
|
|
10
|
+
this.key = key;
|
|
11
|
+
this.flag = flagsData === null || flagsData === void 0 ? void 0 : flagsData.get(key);
|
|
12
|
+
}
|
|
13
|
+
Flag.prototype.NotSameType = function () {
|
|
14
|
+
var _a;
|
|
15
|
+
return this.defaultValue !== null && this.defaultValue !== undefined && !hasSameType((_a = this.flag) === null || _a === void 0 ? void 0 : _a.value, this.defaultValue);
|
|
16
|
+
};
|
|
17
|
+
Flag.prototype.getValue = function () {
|
|
18
|
+
if (!this.flag) {
|
|
19
|
+
logWarn(Flagship.getConfig(), noVisitorMessage, 'getValue');
|
|
20
|
+
return this.defaultValue;
|
|
21
|
+
}
|
|
22
|
+
if (this.NotSameType()) {
|
|
23
|
+
logInfo(Flagship.getConfig(), sprintf(GET_FLAG_CAST_ERROR, this.key), "getValue");
|
|
24
|
+
return this.defaultValue;
|
|
25
|
+
}
|
|
26
|
+
return this.flag.value;
|
|
27
|
+
};
|
|
28
|
+
Flag.prototype.exists = function () {
|
|
29
|
+
var _a;
|
|
30
|
+
if (!this.flag) {
|
|
31
|
+
logWarn(Flagship.getConfig(), noVisitorMessage, 'exists');
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return !!(((_a = this.flag) === null || _a === void 0 ? void 0 : _a.campaignId) && this.flag.variationId && this.flag.variationGroupId);
|
|
35
|
+
};
|
|
36
|
+
Flag.prototype.userExposed = function () {
|
|
37
|
+
if (!this.flag) {
|
|
38
|
+
logWarn(Flagship.getConfig(), noVisitorMessage, 'userExposed');
|
|
39
|
+
}
|
|
40
|
+
return Promise.resolve();
|
|
41
|
+
};
|
|
42
|
+
Object.defineProperty(Flag.prototype, "metadata", {
|
|
43
|
+
get: function () {
|
|
44
|
+
var _a, _b, _c, _d, _e, _f;
|
|
45
|
+
var functionName = 'metadata';
|
|
46
|
+
if (!this.flag) {
|
|
47
|
+
logWarn(Flagship.getConfig(), noVisitorMessage, functionName);
|
|
48
|
+
return FlagMetadata.Empty();
|
|
49
|
+
}
|
|
50
|
+
if (this.NotSameType()) {
|
|
51
|
+
logInfo(Flagship.getConfig(), sprintf(GET_METADATA_CAST_ERROR, this.key), functionName);
|
|
52
|
+
return FlagMetadata.Empty();
|
|
53
|
+
}
|
|
54
|
+
return new FlagMetadata({
|
|
55
|
+
campaignId: ((_a = this.flag) === null || _a === void 0 ? void 0 : _a.campaignId) || '',
|
|
56
|
+
variationGroupId: ((_b = this.flag) === null || _b === void 0 ? void 0 : _b.variationGroupId) || '',
|
|
57
|
+
variationId: ((_c = this.flag) === null || _c === void 0 ? void 0 : _c.variationId) || '',
|
|
58
|
+
isReference: !!((_d = this.flag) === null || _d === void 0 ? void 0 : _d.isReference),
|
|
59
|
+
campaignType: ((_e = this.flag) === null || _e === void 0 ? void 0 : _e.campaignType) || '',
|
|
60
|
+
slug: (_f = this.flag) === null || _f === void 0 ? void 0 : _f.slug
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
enumerable: false,
|
|
64
|
+
configurable: true
|
|
65
|
+
});
|
|
66
|
+
return Flag;
|
|
67
|
+
}());
|
|
68
|
+
export { Flag };
|
|
File without changes
|
|
@@ -0,0 +1,181 @@
|
|
|
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
|
+
// eslint-disable-next-line no-use-before-define
|
|
13
|
+
import React, { useState, useEffect, createContext, useRef, } from "react";
|
|
14
|
+
import { Flagship, FlagshipStatus, } from "@flagship.io/js-sdk";
|
|
15
|
+
import { getModificationsFromCampaigns, logError, useNonInitialEffect, } from "./utils";
|
|
16
|
+
var initStat = {
|
|
17
|
+
status: { isLoading: true, isSdkReady: false },
|
|
18
|
+
};
|
|
19
|
+
export var FlagshipContext = createContext({
|
|
20
|
+
state: __assign({}, initStat),
|
|
21
|
+
});
|
|
22
|
+
export var FlagshipProvider = function (_a) {
|
|
23
|
+
var children = _a.children, fetchNow = _a.fetchNow, envId = _a.envId, apiKey = _a.apiKey, decisionMode = _a.decisionMode, decisionApiUrl = _a.decisionApiUrl, timeout = _a.timeout, logLevel = _a.logLevel, statusChangedCallback = _a.statusChangedCallback, logManager = _a.logManager, pollingInterval = _a.pollingInterval, visitorData = _a.visitorData, onInitStart = _a.onInitStart, onInitDone = _a.onInitDone, onBucketingSuccess = _a.onBucketingSuccess, onBucketingFail = _a.onBucketingFail, loadingComponent = _a.loadingComponent, onBucketingUpdated = _a.onBucketingUpdated, onUpdate = _a.onUpdate, enableClientCache = _a.enableClientCache, initialBucketing = _a.initialBucketing, initialCampaigns = _a.initialCampaigns, initialModifications = _a.initialModifications, initialFlagsData = _a.initialFlagsData, fetchFlagsOnBucketingUpdated = _a.fetchFlagsOnBucketingUpdated, activateDeduplicationTime = _a.activateDeduplicationTime, hitDeduplicationTime = _a.hitDeduplicationTime, visitorCacheImplementation = _a.visitorCacheImplementation, hitCacheImplementation = _a.hitCacheImplementation, disableCache = _a.disableCache, language = _a.language;
|
|
24
|
+
var modifications = new Map();
|
|
25
|
+
if (initialFlagsData && initialFlagsData.forEach) {
|
|
26
|
+
initialFlagsData.forEach(function (flag) {
|
|
27
|
+
modifications.set(flag.key, flag);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
else if (initialModifications && initialModifications.forEach) {
|
|
31
|
+
initialModifications.forEach(function (modification) {
|
|
32
|
+
modifications.set(modification.key, modification);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
else if (initialCampaigns) {
|
|
36
|
+
modifications = getModificationsFromCampaigns(initialCampaigns);
|
|
37
|
+
}
|
|
38
|
+
var _b = useState(__assign(__assign({}, initStat), { modifications: modifications })), state = _b[0], setState = _b[1];
|
|
39
|
+
var _c = useState(), lastModified = _c[0], setLastModified = _c[1];
|
|
40
|
+
var stateRef = useRef();
|
|
41
|
+
stateRef.current = state;
|
|
42
|
+
useNonInitialEffect(function () {
|
|
43
|
+
var _a;
|
|
44
|
+
if (fetchFlagsOnBucketingUpdated) {
|
|
45
|
+
(_a = state.visitor) === null || _a === void 0 ? void 0 : _a.fetchFlags();
|
|
46
|
+
}
|
|
47
|
+
}, [lastModified]);
|
|
48
|
+
useNonInitialEffect(function () {
|
|
49
|
+
createVisitor(true);
|
|
50
|
+
}, [JSON.stringify(visitorData)]);
|
|
51
|
+
useEffect(function () {
|
|
52
|
+
initSdk();
|
|
53
|
+
}, [envId, apiKey, decisionMode]);
|
|
54
|
+
function initializeState(param) {
|
|
55
|
+
var newStatus = {
|
|
56
|
+
isSdkReady: param.isSdkReady,
|
|
57
|
+
isLoading: param.isLoading,
|
|
58
|
+
isVisitorDefined: !!param.fsVisitor,
|
|
59
|
+
lastRefresh: new Date().toISOString(),
|
|
60
|
+
};
|
|
61
|
+
setState(function (currentState) {
|
|
62
|
+
if (!currentState.status.firstInitSuccess) {
|
|
63
|
+
newStatus.firstInitSuccess = new Date().toISOString();
|
|
64
|
+
}
|
|
65
|
+
return __assign(__assign({}, currentState), { visitor: param.fsVisitor, modifications: param.fsVisitor.modifications, config: Flagship.getConfig(), status: __assign(__assign({}, currentState.status), newStatus) });
|
|
66
|
+
});
|
|
67
|
+
return newStatus;
|
|
68
|
+
}
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
70
|
+
var onVisitorReady = function (fsVisitor, error) {
|
|
71
|
+
if (error) {
|
|
72
|
+
logError(Flagship.getConfig(), error.message || error, "onReady");
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
var newStatus = initializeState({
|
|
76
|
+
fsVisitor: fsVisitor,
|
|
77
|
+
isSdkReady: true,
|
|
78
|
+
isLoading: false,
|
|
79
|
+
});
|
|
80
|
+
if (onUpdate) {
|
|
81
|
+
onUpdate({
|
|
82
|
+
fsModifications: fsVisitor.modifications,
|
|
83
|
+
config: Flagship.getConfig(),
|
|
84
|
+
status: newStatus,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
var createVisitor = function (fetchFlags) {
|
|
89
|
+
if (fetchFlags === void 0) { fetchFlags = false; }
|
|
90
|
+
if (!visitorData) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
var fsVisitor = Flagship.newVisitor({
|
|
94
|
+
visitorId: visitorData.id,
|
|
95
|
+
context: visitorData.context,
|
|
96
|
+
isAuthenticated: visitorData.isAuthenticated,
|
|
97
|
+
hasConsented: visitorData.hasConsented,
|
|
98
|
+
initialCampaigns: initialCampaigns,
|
|
99
|
+
initialModifications: initialModifications,
|
|
100
|
+
initialFlagsData: initialFlagsData,
|
|
101
|
+
});
|
|
102
|
+
fsVisitor === null || fsVisitor === void 0 ? void 0 : fsVisitor.on("ready", function (error) {
|
|
103
|
+
onVisitorReady(fsVisitor, error);
|
|
104
|
+
});
|
|
105
|
+
if (!fetchNow && fsVisitor && !fetchFlags) {
|
|
106
|
+
initializeState({
|
|
107
|
+
fsVisitor: fsVisitor,
|
|
108
|
+
isSdkReady: true,
|
|
109
|
+
isLoading: false,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
if (fetchFlags && !fetchNow) {
|
|
113
|
+
fsVisitor === null || fsVisitor === void 0 ? void 0 : fsVisitor.fetchFlags();
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
var statusChanged = function (status) {
|
|
117
|
+
if (statusChangedCallback) {
|
|
118
|
+
statusChangedCallback(status);
|
|
119
|
+
}
|
|
120
|
+
switch (status) {
|
|
121
|
+
case FlagshipStatus.STARTING:
|
|
122
|
+
if (status === FlagshipStatus.STARTING && onInitStart) {
|
|
123
|
+
onInitStart();
|
|
124
|
+
}
|
|
125
|
+
break;
|
|
126
|
+
case FlagshipStatus.READY_PANIC_ON:
|
|
127
|
+
case FlagshipStatus.READY:
|
|
128
|
+
if (onInitDone) {
|
|
129
|
+
onInitDone();
|
|
130
|
+
}
|
|
131
|
+
createVisitor();
|
|
132
|
+
break;
|
|
133
|
+
case FlagshipStatus.NOT_INITIALIZED:
|
|
134
|
+
setState(function (prev) { return (__assign(__assign({}, prev), { config: Flagship.getConfig() })); });
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
var onBucketingLastModified = function (lastUpdate) {
|
|
139
|
+
if (onBucketingUpdated) {
|
|
140
|
+
onBucketingUpdated(lastUpdate);
|
|
141
|
+
}
|
|
142
|
+
setLastModified(lastUpdate);
|
|
143
|
+
};
|
|
144
|
+
var initSdk = function () {
|
|
145
|
+
Flagship.start(envId, apiKey, {
|
|
146
|
+
decisionMode: decisionMode,
|
|
147
|
+
fetchNow: fetchNow,
|
|
148
|
+
timeout: timeout,
|
|
149
|
+
logLevel: logLevel,
|
|
150
|
+
statusChangedCallback: statusChanged,
|
|
151
|
+
logManager: logManager,
|
|
152
|
+
pollingInterval: pollingInterval,
|
|
153
|
+
onBucketingFail: onBucketingFail,
|
|
154
|
+
onBucketingSuccess: onBucketingSuccess,
|
|
155
|
+
enableClientCache: enableClientCache,
|
|
156
|
+
decisionApiUrl: decisionApiUrl,
|
|
157
|
+
onBucketingUpdated: onBucketingLastModified,
|
|
158
|
+
initialBucketing: initialBucketing,
|
|
159
|
+
activateDeduplicationTime: activateDeduplicationTime,
|
|
160
|
+
hitDeduplicationTime: hitDeduplicationTime,
|
|
161
|
+
visitorCacheImplementation: visitorCacheImplementation,
|
|
162
|
+
hitCacheImplementation: hitCacheImplementation,
|
|
163
|
+
disableCache: disableCache,
|
|
164
|
+
language: language,
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
var handleDisplay = function () {
|
|
168
|
+
var isFirstInit = !state.visitor;
|
|
169
|
+
if (state.status.isLoading && loadingComponent && isFirstInit && fetchNow) {
|
|
170
|
+
return React.createElement(React.Fragment, null, loadingComponent);
|
|
171
|
+
}
|
|
172
|
+
return React.createElement(React.Fragment, null, children);
|
|
173
|
+
};
|
|
174
|
+
return (React.createElement(FlagshipContext.Provider, { value: { state: state, setState: setState } }, handleDisplay()));
|
|
175
|
+
};
|
|
176
|
+
FlagshipProvider.defaultProps = {
|
|
177
|
+
activateDeduplicationTime: 2,
|
|
178
|
+
hitDeduplicationTime: 2,
|
|
179
|
+
fetchNow: true,
|
|
180
|
+
language: 1,
|
|
181
|
+
};
|
|
File without changes
|
|
@@ -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
|
+
};
|
|
File without changes
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export var noVisitorMessage = 'flagship Visitor not initialized.';
|
|
2
2
|
export var noVisitorDefault = 'fsVisitor not initialized, returns default value';
|
|
3
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';
|
|
4
|
+
export var GET_METADATA_CAST_ERROR = 'Flag for key {0} has a different type with defaultValue, an empty metadata object is returned';
|