@flagship.io/react-sdk 3.1.1-alpha.1 → 3.1.1
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 +27 -27
- package/dist/index.node.js +1072 -1
- package/dist/index.node.js.map +1 -1
- package/dist/src/FlagshipContext.d.ts +1 -1
- package/dist/src/FlagshipContext.js +7 -4
- package/dist/src/FlagshipHooks.d.ts +3 -3
- package/dist/src/sdkVersion.d.ts +1 -1
- package/dist/src/sdkVersion.js +1 -1
- package/package.json +108 -109
package/dist/index.node.js
CHANGED
|
@@ -1,2 +1,1073 @@
|
|
|
1
|
-
(()=>{"use strict";var e={161:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Flag=void 0,i(117);var a=function(e,t){if(e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var i=s(t);if(i&&i.has(e))return i.get(e);var a={},o=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var n in e)if("default"!==n&&Object.prototype.hasOwnProperty.call(e,n)){var r=o?Object.getOwnPropertyDescriptor(e,n):null;r&&(r.get||r.set)?Object.defineProperty(a,n,r):a[n]=e[n]}return a.default=e,i&&i.set(e,a),a}(i(4)),o=i(147),n=i(370);function s(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,i=new WeakMap;return(s=function(e){return e?i:t})(e)}function r(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}t.Flag=class{constructor(e,t,i){r(this,"defaultValue",void 0),r(this,"flagsData",void 0),r(this,"key",void 0),r(this,"flag",void 0),i||(0,n.logWarn)(a.default.getConfig(),o.noVisitorMessage,"GetFlag"),this.defaultValue=e,this.key=t,this.flag=null==i?void 0:i.get(t)}NotSameType(){var e;return null!==this.defaultValue&&void 0!==this.defaultValue&&!(0,n.hasSameType)(null===(e=this.flag)||void 0===e?void 0:e.value,this.defaultValue)}getValue(){return this.flag?this.NotSameType()?((0,n.logInfo)(a.default.getConfig(),(0,n.sprintf)(o.GET_FLAG_CAST_ERROR,this.key),"getValue"),this.defaultValue):this.flag.value:((0,n.logWarn)(a.default.getConfig(),o.noVisitorMessage,"getValue"),this.defaultValue)}exists(){return this.flag?!!(this.flag.campaignId&&this.flag.variationId&&this.flag.variationGroupId):((0,n.logWarn)(a.default.getConfig(),o.noVisitorMessage,"exists"),!1)}userExposed(){return this.flag||(0,n.logWarn)(a.default.getConfig(),o.noVisitorMessage,"userExposed"),Promise.resolve()}get metadata(){const e="metadata";return this.flag?this.NotSameType()?((0,n.logInfo)(a.default.getConfig(),(0,n.sprintf)(o.GET_METADATA_CAST_ERROR,this.key),e),a.FlagMetadata.Empty()):new a.FlagMetadata({campaignId:this.flag.campaignId,variationGroupId:this.flag.variationGroupId,variationId:this.flag.variationId,isReference:!!this.flag.isReference,campaignType:this.flag.campaignType,slug:this.flag.slug}):((0,n.logWarn)(a.default.getConfig(),o.noVisitorMessage,e),a.FlagMetadata.Empty())}}},821:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.FlagshipProvider=t.FlagshipContext=void 0;var a=function(e,t){if(e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var i=r(t);if(i&&i.has(e))return i.get(e);var a={},o=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var n in e)if("default"!==n&&Object.prototype.hasOwnProperty.call(e,n)){var s=o?Object.getOwnPropertyDescriptor(e,n):null;s&&(s.get||s.set)?Object.defineProperty(a,n,s):a[n]=e[n]}return a.default=e,i&&i.set(e,a),a}(i(689)),o=i(4),n=i(370),s=i(359);function r(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,i=new WeakMap;return(r=function(e){return e?i:t})(e)}const l={status:{isLoading:!0,isSdkReady:!1}},u=(0,a.createContext)({state:{...l}});t.FlagshipContext=u;const f=({children:e,fetchNow:t,envId:i,apiKey:s,decisionMode:r,visitorData:f,onInitStart:c,onInitDone:d,loadingComponent:g,statusChangedCallback:p,onBucketingUpdated:v,onUpdate:y,initialCampaigns:h,initialModifications:M,initialFlagsData:x,fetchFlagsOnBucketingUpdated:m,...F})=>{let C=new Map;x&&x.forEach?x.forEach((e=>{C.set(e.key,e)})):M&&M.forEach?M.forEach((e=>{C.set(e.key,e)})):h&&(C=(0,n.getModificationsFromCampaigns)(h));const[E,V]=(0,a.useState)({...l,modifications:C}),[_,A]=(0,a.useState)();function k(e){const t={isSdkReady:e.isSdkReady,isLoading:e.isLoading,isVisitorDefined:!!e.fsVisitor,lastRefresh:(new Date).toISOString()};return V((i=>(i.status.firstInitSuccess||(t.firstInitSuccess=(new Date).toISOString()),{...i,visitor:e.fsVisitor,modifications:e.fsVisitor.modifications,config:o.Flagship.getConfig(),status:{...i.status,...t}}))),t}(0,a.useRef)().current=E,(0,n.useNonInitialEffect)((()=>{var e;m&&(null===(e=E.visitor)||void 0===e||e.fetchFlags())}),[_]),(0,n.useNonInitialEffect)((()=>{I(!0)}),[JSON.stringify(f)]),(0,a.useEffect)((()=>{S()}),[i,s,r]);const I=(e=!1)=>{if(!f)return;const i=o.Flagship.newVisitor({visitorId:f.id,context:f.context,isAuthenticated:f.isAuthenticated,hasConsented:f.hasConsented,initialCampaigns:h,initialModifications:M,initialFlagsData:x});null==i||i.on("ready",(e=>{((e,t)=>{if(t)return void(0,n.logError)(o.Flagship.getConfig(),t.message||t,"onReady");const i=k({fsVisitor:e,isSdkReady:!0,isLoading:!1});y&&y({fsModifications:e.modifications,config:o.Flagship.getConfig(),status:i})})(i,e)})),t||!i||e||k({fsVisitor:i,isSdkReady:!0,isLoading:!1}),e&&!t&&(null==i||i.fetchFlags())},O=e=>{switch(p&&p(e),e){case o.FlagshipStatus.STARTING:e===o.FlagshipStatus.STARTING&&c&&c();break;case o.FlagshipStatus.READY_PANIC_ON:case o.FlagshipStatus.READY:d&&d(),I();break;case o.FlagshipStatus.NOT_INITIALIZED:V((e=>({...e,status:{...e.status,isLoading:!1},config:o.Flagship.getConfig()})))}},R=e=>{v&&v(e),A(e)},S=()=>{o.Flagship.start(i,s,{decisionMode:r,fetchNow:t,statusChangedCallback:O,onBucketingUpdated:R,...F})};return a.default.createElement(u.Provider,{value:{state:E,setState:V}},(()=>{const i=!E.visitor;return E.status.isLoading&&g&&i&&t?a.default.createElement(a.default.Fragment,null,g):a.default.createElement(a.default.Fragment,null,e)})())};t.FlagshipProvider=f,f.defaultProps={hitDeduplicationTime:2,fetchNow:!0,language:1,sdkVersion:s.version}},460:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useFlagship=t.useFsActivate=t.useFsFlag=t.useFsModificationInfo=t.useFsModification=t.useFsModifications=void 0,i(117);var a,o=i(689),n=(a=i(4))&&a.__esModule?a:{default:a},s=i(821),r=i(370),l=i(161),u=i(147);const f=(e,t)=>"object"==typeof e&&"object"==typeof t&&Array.isArray(e)===Array.isArray(t)||typeof e==typeof t,c=e=>{const{visitor:t,params:i,activateAll:a,state:o,functionName:n,config:s}=e;if(t)return t.getModificationsSync(i,a);const l=!o.status.isSdkReady&&!!o.modifications&&o.modifications.size>0,c={};return l?(i.forEach((e=>{var t;const i=null===(t=o.modifications)||void 0===t?void 0:t.get(e.key);i&&f(null==i?void 0:i.value,e.defaultValue)?c[e.key]=i.value:c[e.key]=e.defaultValue})),c):((0,r.logWarn)(s,u.noVisitorDefault,n),i.forEach((e=>{c[e.key]=e.defaultValue})),c)};t.useFsModifications=(e,t)=>{const{state:i}=(0,o.useContext)(s.FlagshipContext),{visitor:a,config:n}=i;return c({functionName:"useFsModifications",state:i,visitor:a,config:n,params:e,activateAll:t})},t.useFsModification=e=>{var t;const{state:i}=(0,o.useContext)(s.FlagshipContext),{visitor:a,config:n}=i;if(a)return a.getModificationSync(e);const l=null===(t=i.modifications)||void 0===t?void 0:t.get(e.key);return!i.status.isSdkReady&&l&&f(null==l?void 0:l.value,e.defaultValue)?l.value:((0,r.logWarn)(n,u.noVisitorDefault,"useFsModifications"),e.defaultValue)};const d=e=>{var t;const{key:i,visitor:a,state:o}=e;if(a)return a.getModificationInfoSync(i);const n=null===(t=o.modifications)||void 0===t?void 0:t.get(i);return!o.status.isSdkReady&&n?n:null};t.useFsModificationInfo=e=>{const{state:t}=(0,o.useContext)(s.FlagshipContext),{visitor:i}=t;return d({key:e,state:t,visitor:i})};const g=async(e,t,i,a)=>{try{if(!i)return void(0,r.logWarn)(a,u.noVisitorMessage,t);await i.activateModifications(e)}catch(e){(0,r.logWarn)(a,e.message||e,t)}};t.useFsFlag=(e,t)=>{const{state:i}=(0,o.useContext)(s.FlagshipContext),{visitor:a}=i;return a?a.getFlag(e,t):new l.Flag(t,e,i.modifications)},t.useFsActivate=async e=>{const{state:t}=(0,o.useContext)(s.FlagshipContext),{visitor:i,config:a}=t;await g(e,"useFsModifications",i,a)},t.useFlagship=()=>{const{state:e}=(0,o.useContext)(s.FlagshipContext),{visitor:t,config:i}=e;let a=null==t?void 0:t.getModificationsArray();return!e.status.isSdkReady&&e.modifications&&(a=Array.from(e.modifications,(([e,t])=>t))),{visitorId:null==t?void 0:t.visitorId,anonymousId:null==t?void 0:t.anonymousId,context:{...null==t?void 0:t.context},hasConsented:null==t?void 0:t.hasConsented,setConsent:function(e){t?t.setConsent(e):(0,r.logWarn)(i,u.noVisitorMessage,"setConsent")},updateContext:e=>{t?(t.clearContext(),t.updateContext(e)):(0,r.logError)(i,u.noVisitorMessage,"updateContext")},clearContext:()=>{t?t.clearContext():(0,r.logError)(i,u.noVisitorMessage,"cleanContext")},authenticate:e=>{t?t.authenticate(e):(0,r.logError)(i,u.noVisitorMessage,"authenticate")},unauthenticate:()=>{t?t.unauthenticate():(0,r.logError)(i,u.noVisitorMessage,"unauthenticate")},status:e.status,activateModification:async e=>{await g(e,"activateModification",t,i)},synchronizeModifications:async()=>{t?await t.synchronizeModifications():(0,r.logWarn)(i,u.noVisitorMessage,"synchronizeModifications")},getModifications:(a,o)=>c({functionName:"getModifications",state:e,visitor:t,config:i,params:a,activateAll:o}),modifications:a||[],flagsData:(null==t?void 0:t.getFlagsDataArray())||[],getModificationInfo:i=>d({key:i,state:e,visitor:t}),hit:{send:e=>t?t.sendHit(e):((0,r.logError)(i,u.noVisitorMessage,"sendHit"),Promise.resolve()),sendMultiple:e=>t?t.sendHits(e):((0,r.logError)(i,u.noVisitorMessage,"sendHits"),Promise.resolve())},getFlag:function(i,a){return t?t.getFlag(i,a):new l.Flag(a,i,e.modifications)},fetchFlags:function(){return t?t.fetchFlags():((0,r.logWarn)(i,u.noVisitorMessage,"fetchFlags"),Promise.resolve())},close:async function(){await n.default.close()}}}},147:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.GET_METADATA_CAST_ERROR=t.GET_FLAG_CAST_ERROR=t.noVisitorDefault=t.noVisitorMessage=void 0,t.noVisitorMessage="flagship Visitor not initialized.",t.noVisitorDefault="fsVisitor not initialized, returns default value",t.GET_FLAG_CAST_ERROR="Flag for key {0} has a different type. Default value is returned.",t.GET_METADATA_CAST_ERROR="Flag for key {0} has a different type with defaultValue, an empty metadata object is returned"},231:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0});var a=i(821);Object.keys(a).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===a[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return a[e]}}))}));var o=i(4);Object.keys(o).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===o[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return o[e]}}))}));var n=i(460);Object.keys(n).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===n[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return n[e]}}))}))},359:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.version=void 0,t.version="3.1.0-alpha.1"},370:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.logError=function(e,t,i){e&&e.logManager&&"function"==typeof e.logManager.error&&e.logLevel&&!(e.logLevel<a.LogLevel.ERROR)&&e.logManager.error(t,i)},t.logInfo=function(e,t,i){e&&e.logManager&&"function"==typeof e.logManager.info&&e.logLevel&&!(e.logLevel<a.LogLevel.INFO)&&e.logManager.info(t,i)},t.logWarn=function(e,t,i){e&&e.logManager&&"function"==typeof e.logManager.warning&&e.logLevel&&!(e.logLevel<a.LogLevel.WARNING)&&e.logManager.warning(t,i)},t.uuidV4=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){const t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)}))},t.useNonInitialEffect=function(e,t){const i=(0,o.useRef)(!0);(0,o.useEffect)((()=>{if(!i.current)return"function"==typeof e?e():void 0;i.current=!1}),t)},t.hasSameType=function(e,t){return typeof e==typeof t&&("object"!=typeof e||"object"!=typeof t||Array.isArray(e)===Array.isArray(t))},t.sprintf=function(e,...t){let i=e;for(let e=0;e<t.length;e++){const a=t[e];i=i.replace(new RegExp(`\\{${e}\\}`,"g"),a)}return i},t.getModificationsFromCampaigns=void 0;var a=i(4),o=i(689);t.getModificationsFromCampaigns=e=>{const t=new Map;return e&&Array.isArray(e)?(e.forEach((e=>{const i=e.variation.modifications.value;for(const a in i){const o=i[a];t.set(a,{key:a,campaignId:e.id,variationGroupId:e.variationGroupId,variationId:e.variation.id,isReference:e.variation.reference,value:o})}})),t):t}},4:e=>{e.exports=require("@flagship.io/js-sdk")},117:e=>{e.exports=require("core-js/modules/es.promise.js")},689:e=>{e.exports=require("react")}},t={},i=function i(a){var o=t[a];if(void 0!==o)return o.exports;var n=t[a]={exports:{}};return e[a](n,n.exports,i),n.exports}(231);module.exports=i})();
|
|
1
|
+
/******/ (() => { // webpackBootstrap
|
|
2
|
+
/******/ "use strict";
|
|
3
|
+
/******/ var __webpack_modules__ = ({
|
|
4
|
+
|
|
5
|
+
/***/ "./src/Flag.ts":
|
|
6
|
+
/*!*********************!*\
|
|
7
|
+
!*** ./src/Flag.ts ***!
|
|
8
|
+
\*********************/
|
|
9
|
+
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, "__esModule", ({
|
|
14
|
+
value: true
|
|
15
|
+
}));
|
|
16
|
+
exports.Flag = void 0;
|
|
17
|
+
|
|
18
|
+
__webpack_require__(/*! core-js/modules/es.promise.js */ "core-js/modules/es.promise.js");
|
|
19
|
+
|
|
20
|
+
var _jsSdk = _interopRequireWildcard(__webpack_require__(/*! @flagship.io/js-sdk */ "@flagship.io/js-sdk"));
|
|
21
|
+
|
|
22
|
+
var _constants = __webpack_require__(/*! ./constants */ "./src/constants.ts");
|
|
23
|
+
|
|
24
|
+
var _utils = __webpack_require__(/*! ./utils */ "./src/utils.tsx");
|
|
25
|
+
|
|
26
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
27
|
+
|
|
28
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
29
|
+
|
|
30
|
+
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; }
|
|
31
|
+
|
|
32
|
+
class Flag {
|
|
33
|
+
constructor(defaultValue, key, flagsData) {
|
|
34
|
+
_defineProperty(this, "defaultValue", void 0);
|
|
35
|
+
|
|
36
|
+
_defineProperty(this, "flagsData", void 0);
|
|
37
|
+
|
|
38
|
+
_defineProperty(this, "key", void 0);
|
|
39
|
+
|
|
40
|
+
_defineProperty(this, "flag", void 0);
|
|
41
|
+
|
|
42
|
+
if (!flagsData) {
|
|
43
|
+
(0, _utils.logWarn)(_jsSdk.default.getConfig(), _constants.noVisitorMessage, 'GetFlag');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.defaultValue = defaultValue;
|
|
47
|
+
this.key = key;
|
|
48
|
+
this.flag = flagsData === null || flagsData === void 0 ? void 0 : flagsData.get(key);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
NotSameType() {
|
|
52
|
+
var _this$flag;
|
|
53
|
+
|
|
54
|
+
return this.defaultValue !== null && this.defaultValue !== undefined && !(0, _utils.hasSameType)((_this$flag = this.flag) === null || _this$flag === void 0 ? void 0 : _this$flag.value, this.defaultValue);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
getValue() {
|
|
58
|
+
if (!this.flag) {
|
|
59
|
+
(0, _utils.logWarn)(_jsSdk.default.getConfig(), _constants.noVisitorMessage, 'getValue');
|
|
60
|
+
return this.defaultValue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (this.NotSameType()) {
|
|
64
|
+
(0, _utils.logInfo)(_jsSdk.default.getConfig(), (0, _utils.sprintf)(_constants.GET_FLAG_CAST_ERROR, this.key), 'getValue');
|
|
65
|
+
return this.defaultValue;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return this.flag.value;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
exists() {
|
|
72
|
+
if (!this.flag) {
|
|
73
|
+
(0, _utils.logWarn)(_jsSdk.default.getConfig(), _constants.noVisitorMessage, 'exists');
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return !!(this.flag.campaignId && this.flag.variationId && this.flag.variationGroupId);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
userExposed() {
|
|
81
|
+
if (!this.flag) {
|
|
82
|
+
(0, _utils.logWarn)(_jsSdk.default.getConfig(), _constants.noVisitorMessage, 'userExposed');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return Promise.resolve();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
get metadata() {
|
|
89
|
+
const functionName = 'metadata';
|
|
90
|
+
|
|
91
|
+
if (!this.flag) {
|
|
92
|
+
(0, _utils.logWarn)(_jsSdk.default.getConfig(), _constants.noVisitorMessage, functionName);
|
|
93
|
+
return _jsSdk.FlagMetadata.Empty();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (this.NotSameType()) {
|
|
97
|
+
(0, _utils.logInfo)(_jsSdk.default.getConfig(), (0, _utils.sprintf)(_constants.GET_METADATA_CAST_ERROR, this.key), functionName);
|
|
98
|
+
return _jsSdk.FlagMetadata.Empty();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return new _jsSdk.FlagMetadata({
|
|
102
|
+
campaignId: this.flag.campaignId,
|
|
103
|
+
variationGroupId: this.flag.variationGroupId,
|
|
104
|
+
variationId: this.flag.variationId,
|
|
105
|
+
isReference: !!this.flag.isReference,
|
|
106
|
+
campaignType: this.flag.campaignType,
|
|
107
|
+
slug: this.flag.slug
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
exports.Flag = Flag;
|
|
114
|
+
|
|
115
|
+
/***/ }),
|
|
116
|
+
|
|
117
|
+
/***/ "./src/FlagshipContext.tsx":
|
|
118
|
+
/*!*********************************!*\
|
|
119
|
+
!*** ./src/FlagshipContext.tsx ***!
|
|
120
|
+
\*********************************/
|
|
121
|
+
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
Object.defineProperty(exports, "__esModule", ({
|
|
126
|
+
value: true
|
|
127
|
+
}));
|
|
128
|
+
exports.FlagshipProvider = exports.FlagshipContext = void 0;
|
|
129
|
+
|
|
130
|
+
var _react = _interopRequireWildcard(__webpack_require__(/*! react */ "react"));
|
|
131
|
+
|
|
132
|
+
var _jsSdk = __webpack_require__(/*! @flagship.io/js-sdk */ "@flagship.io/js-sdk");
|
|
133
|
+
|
|
134
|
+
var _utils = __webpack_require__(/*! ./utils */ "./src/utils.tsx");
|
|
135
|
+
|
|
136
|
+
var _sdkVersion = __webpack_require__(/*! ./sdkVersion */ "./src/sdkVersion.ts");
|
|
137
|
+
|
|
138
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
139
|
+
|
|
140
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
141
|
+
|
|
142
|
+
// eslint-disable-next-line no-use-before-define
|
|
143
|
+
const initStat = {
|
|
144
|
+
status: {
|
|
145
|
+
isLoading: true,
|
|
146
|
+
isSdkReady: false
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const FlagshipContext = /*#__PURE__*/(0, _react.createContext)({
|
|
150
|
+
state: { ...initStat
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
exports.FlagshipContext = FlagshipContext;
|
|
154
|
+
|
|
155
|
+
const FlagshipProvider = ({
|
|
156
|
+
children,
|
|
157
|
+
envId,
|
|
158
|
+
apiKey,
|
|
159
|
+
decisionMode = _jsSdk.DecisionMode.DECISION_API,
|
|
160
|
+
visitorData,
|
|
161
|
+
onInitStart,
|
|
162
|
+
onInitDone,
|
|
163
|
+
loadingComponent,
|
|
164
|
+
statusChangedCallback,
|
|
165
|
+
onBucketingUpdated,
|
|
166
|
+
onUpdate,
|
|
167
|
+
initialCampaigns,
|
|
168
|
+
initialModifications,
|
|
169
|
+
initialFlagsData,
|
|
170
|
+
fetchFlagsOnBucketingUpdated,
|
|
171
|
+
hitDeduplicationTime = 2,
|
|
172
|
+
fetchNow = true,
|
|
173
|
+
language = 1,
|
|
174
|
+
sdkVersion = _sdkVersion.version,
|
|
175
|
+
...props
|
|
176
|
+
}) => {
|
|
177
|
+
let modifications = new Map();
|
|
178
|
+
|
|
179
|
+
if (initialFlagsData && initialFlagsData.forEach) {
|
|
180
|
+
initialFlagsData.forEach(flag => {
|
|
181
|
+
modifications.set(flag.key, flag);
|
|
182
|
+
});
|
|
183
|
+
} else if (initialModifications && initialModifications.forEach) {
|
|
184
|
+
initialModifications.forEach(modification => {
|
|
185
|
+
modifications.set(modification.key, modification);
|
|
186
|
+
});
|
|
187
|
+
} else if (initialCampaigns) {
|
|
188
|
+
modifications = (0, _utils.getModificationsFromCampaigns)(initialCampaigns);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const [state, setState] = (0, _react.useState)({ ...initStat,
|
|
192
|
+
modifications
|
|
193
|
+
});
|
|
194
|
+
const [lastModified, setLastModified] = (0, _react.useState)();
|
|
195
|
+
const stateRef = (0, _react.useRef)();
|
|
196
|
+
stateRef.current = state;
|
|
197
|
+
(0, _utils.useNonInitialEffect)(() => {
|
|
198
|
+
if (fetchFlagsOnBucketingUpdated) {
|
|
199
|
+
var _state$visitor;
|
|
200
|
+
|
|
201
|
+
(_state$visitor = state.visitor) === null || _state$visitor === void 0 ? void 0 : _state$visitor.fetchFlags();
|
|
202
|
+
}
|
|
203
|
+
}, [lastModified]);
|
|
204
|
+
(0, _utils.useNonInitialEffect)(() => {
|
|
205
|
+
createVisitor(true);
|
|
206
|
+
}, [JSON.stringify(visitorData)]);
|
|
207
|
+
(0, _react.useEffect)(() => {
|
|
208
|
+
initSdk();
|
|
209
|
+
}, [envId, apiKey, decisionMode]);
|
|
210
|
+
|
|
211
|
+
function initializeState(param) {
|
|
212
|
+
const newStatus = {
|
|
213
|
+
isSdkReady: param.isSdkReady,
|
|
214
|
+
isLoading: param.isLoading,
|
|
215
|
+
isVisitorDefined: !!param.fsVisitor,
|
|
216
|
+
lastRefresh: new Date().toISOString()
|
|
217
|
+
};
|
|
218
|
+
setState(currentState => {
|
|
219
|
+
if (!currentState.status.firstInitSuccess) {
|
|
220
|
+
newStatus.firstInitSuccess = new Date().toISOString();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return { ...currentState,
|
|
224
|
+
visitor: param.fsVisitor,
|
|
225
|
+
modifications: param.fsVisitor.modifications,
|
|
226
|
+
config: _jsSdk.Flagship.getConfig(),
|
|
227
|
+
status: { ...currentState.status,
|
|
228
|
+
...newStatus
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
});
|
|
232
|
+
return newStatus;
|
|
233
|
+
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
const onVisitorReady = (fsVisitor, error) => {
|
|
237
|
+
if (error) {
|
|
238
|
+
(0, _utils.logError)(_jsSdk.Flagship.getConfig(), error.message || error, 'onReady');
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const newStatus = initializeState({
|
|
243
|
+
fsVisitor,
|
|
244
|
+
isSdkReady: true,
|
|
245
|
+
isLoading: false
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
if (onUpdate) {
|
|
249
|
+
onUpdate({
|
|
250
|
+
fsModifications: fsVisitor.modifications,
|
|
251
|
+
config: _jsSdk.Flagship.getConfig(),
|
|
252
|
+
status: newStatus
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const createVisitor = (fetchFlags = false) => {
|
|
258
|
+
if (!visitorData) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const fsVisitor = _jsSdk.Flagship.newVisitor({
|
|
263
|
+
visitorId: visitorData.id,
|
|
264
|
+
context: visitorData.context,
|
|
265
|
+
isAuthenticated: visitorData.isAuthenticated,
|
|
266
|
+
hasConsented: visitorData.hasConsented,
|
|
267
|
+
initialCampaigns,
|
|
268
|
+
initialModifications,
|
|
269
|
+
initialFlagsData
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
fsVisitor === null || fsVisitor === void 0 ? void 0 : fsVisitor.on('ready', error => {
|
|
273
|
+
onVisitorReady(fsVisitor, error);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
if (!fetchNow && fsVisitor && !fetchFlags) {
|
|
277
|
+
initializeState({
|
|
278
|
+
fsVisitor,
|
|
279
|
+
isSdkReady: true,
|
|
280
|
+
isLoading: false
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (fetchFlags && !fetchNow) {
|
|
285
|
+
fsVisitor === null || fsVisitor === void 0 ? void 0 : fsVisitor.fetchFlags();
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const statusChanged = status => {
|
|
290
|
+
if (statusChangedCallback) {
|
|
291
|
+
statusChangedCallback(status);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
switch (status) {
|
|
295
|
+
case _jsSdk.FlagshipStatus.STARTING:
|
|
296
|
+
if (status === _jsSdk.FlagshipStatus.STARTING && onInitStart) {
|
|
297
|
+
onInitStart();
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
break;
|
|
301
|
+
|
|
302
|
+
case _jsSdk.FlagshipStatus.READY_PANIC_ON:
|
|
303
|
+
case _jsSdk.FlagshipStatus.READY:
|
|
304
|
+
if (onInitDone) {
|
|
305
|
+
onInitDone();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
createVisitor();
|
|
309
|
+
break;
|
|
310
|
+
|
|
311
|
+
case _jsSdk.FlagshipStatus.NOT_INITIALIZED:
|
|
312
|
+
setState(prev => ({ ...prev,
|
|
313
|
+
status: { ...prev.status,
|
|
314
|
+
isLoading: false
|
|
315
|
+
},
|
|
316
|
+
config: _jsSdk.Flagship.getConfig()
|
|
317
|
+
}));
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
const onBucketingLastModified = lastUpdate => {
|
|
323
|
+
if (onBucketingUpdated) {
|
|
324
|
+
onBucketingUpdated(lastUpdate);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
setLastModified(lastUpdate);
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
const initSdk = () => {
|
|
331
|
+
_jsSdk.Flagship.start(envId, apiKey, {
|
|
332
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
333
|
+
decisionMode: decisionMode,
|
|
334
|
+
fetchNow,
|
|
335
|
+
statusChangedCallback: statusChanged,
|
|
336
|
+
onBucketingUpdated: onBucketingLastModified,
|
|
337
|
+
hitDeduplicationTime,
|
|
338
|
+
language,
|
|
339
|
+
sdkVersion,
|
|
340
|
+
...props
|
|
341
|
+
});
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const handleDisplay = () => {
|
|
345
|
+
const isFirstInit = !state.visitor;
|
|
346
|
+
|
|
347
|
+
if (state.status.isLoading && loadingComponent && isFirstInit && fetchNow) {
|
|
348
|
+
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, loadingComponent);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, children);
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
return /*#__PURE__*/_react.default.createElement(FlagshipContext.Provider, {
|
|
355
|
+
value: {
|
|
356
|
+
state,
|
|
357
|
+
setState
|
|
358
|
+
}
|
|
359
|
+
}, handleDisplay());
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
exports.FlagshipProvider = FlagshipProvider;
|
|
363
|
+
FlagshipProvider.defaultProps = {
|
|
364
|
+
hitDeduplicationTime: 2,
|
|
365
|
+
fetchNow: true,
|
|
366
|
+
language: 1,
|
|
367
|
+
sdkVersion: _sdkVersion.version
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
/***/ }),
|
|
371
|
+
|
|
372
|
+
/***/ "./src/FlagshipHooks.tsx":
|
|
373
|
+
/*!*******************************!*\
|
|
374
|
+
!*** ./src/FlagshipHooks.tsx ***!
|
|
375
|
+
\*******************************/
|
|
376
|
+
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
Object.defineProperty(exports, "__esModule", ({
|
|
381
|
+
value: true
|
|
382
|
+
}));
|
|
383
|
+
exports.useFlagship = exports.useFsActivate = exports.useFsFlag = exports.useFsModificationInfo = exports.useFsModification = exports.useFsModifications = void 0;
|
|
384
|
+
|
|
385
|
+
__webpack_require__(/*! core-js/modules/es.promise.js */ "core-js/modules/es.promise.js");
|
|
386
|
+
|
|
387
|
+
var _react = __webpack_require__(/*! react */ "react");
|
|
388
|
+
|
|
389
|
+
var _jsSdk = _interopRequireDefault(__webpack_require__(/*! @flagship.io/js-sdk */ "@flagship.io/js-sdk"));
|
|
390
|
+
|
|
391
|
+
var _FlagshipContext = __webpack_require__(/*! ./FlagshipContext */ "./src/FlagshipContext.tsx");
|
|
392
|
+
|
|
393
|
+
var _utils = __webpack_require__(/*! ./utils */ "./src/utils.tsx");
|
|
394
|
+
|
|
395
|
+
var _Flag = __webpack_require__(/*! ./Flag */ "./src/Flag.ts");
|
|
396
|
+
|
|
397
|
+
var _constants = __webpack_require__(/*! ./constants */ "./src/constants.ts");
|
|
398
|
+
|
|
399
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
400
|
+
|
|
401
|
+
const checkType = (value, defaultValue) => typeof value === 'object' && typeof defaultValue === 'object' && Array.isArray(value) === Array.isArray(defaultValue) || typeof value === typeof defaultValue;
|
|
402
|
+
|
|
403
|
+
const fsModificationsSync = args => {
|
|
404
|
+
const {
|
|
405
|
+
visitor,
|
|
406
|
+
params,
|
|
407
|
+
activateAll,
|
|
408
|
+
state,
|
|
409
|
+
functionName,
|
|
410
|
+
config
|
|
411
|
+
} = args;
|
|
412
|
+
|
|
413
|
+
if (visitor) {
|
|
414
|
+
return visitor.getModificationsSync(params, activateAll);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const check = !state.status.isSdkReady && !!state.modifications && state.modifications.size > 0;
|
|
418
|
+
const flags = {};
|
|
419
|
+
|
|
420
|
+
if (check) {
|
|
421
|
+
params.forEach(item => {
|
|
422
|
+
var _state$modifications;
|
|
423
|
+
|
|
424
|
+
const modification = (_state$modifications = state.modifications) === null || _state$modifications === void 0 ? void 0 : _state$modifications.get(item.key);
|
|
425
|
+
|
|
426
|
+
if (modification && checkType(modification === null || modification === void 0 ? void 0 : modification.value, item.defaultValue)) {
|
|
427
|
+
flags[item.key] = modification.value;
|
|
428
|
+
} else {
|
|
429
|
+
flags[item.key] = item.defaultValue;
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
return flags;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
(0, _utils.logWarn)(config, _constants.noVisitorDefault, functionName);
|
|
436
|
+
params.forEach(item => {
|
|
437
|
+
flags[item.key] = item.defaultValue;
|
|
438
|
+
});
|
|
439
|
+
return flags;
|
|
440
|
+
};
|
|
441
|
+
/**
|
|
442
|
+
* 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.
|
|
443
|
+
* @deprecated use useFsFlag instead
|
|
444
|
+
*/
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
const useFsModifications = (params, activateAll) => {
|
|
448
|
+
const {
|
|
449
|
+
state
|
|
450
|
+
} = (0, _react.useContext)(_FlagshipContext.FlagshipContext);
|
|
451
|
+
const {
|
|
452
|
+
visitor,
|
|
453
|
+
config
|
|
454
|
+
} = state;
|
|
455
|
+
const functionName = 'useFsModifications';
|
|
456
|
+
return fsModificationsSync({
|
|
457
|
+
functionName,
|
|
458
|
+
state,
|
|
459
|
+
visitor,
|
|
460
|
+
config,
|
|
461
|
+
params,
|
|
462
|
+
activateAll
|
|
463
|
+
});
|
|
464
|
+
};
|
|
465
|
+
/**
|
|
466
|
+
* 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.
|
|
467
|
+
* @deprecated use useFsFlag instead
|
|
468
|
+
*/
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
exports.useFsModifications = useFsModifications;
|
|
472
|
+
|
|
473
|
+
const useFsModification = params => {
|
|
474
|
+
var _state$modifications2;
|
|
475
|
+
|
|
476
|
+
const {
|
|
477
|
+
state
|
|
478
|
+
} = (0, _react.useContext)(_FlagshipContext.FlagshipContext);
|
|
479
|
+
const {
|
|
480
|
+
visitor,
|
|
481
|
+
config
|
|
482
|
+
} = state;
|
|
483
|
+
const functionName = 'useFsModifications';
|
|
484
|
+
|
|
485
|
+
if (visitor) {
|
|
486
|
+
return visitor.getModificationSync(params);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const modification = (_state$modifications2 = state.modifications) === null || _state$modifications2 === void 0 ? void 0 : _state$modifications2.get(params.key);
|
|
490
|
+
|
|
491
|
+
if (!state.status.isSdkReady && modification && checkType(modification === null || modification === void 0 ? void 0 : modification.value, params.defaultValue)) {
|
|
492
|
+
return modification.value;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
(0, _utils.logWarn)(config, _constants.noVisitorDefault, functionName);
|
|
496
|
+
return params.defaultValue;
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
exports.useFsModification = useFsModification;
|
|
500
|
+
|
|
501
|
+
const fsModificationInfoSync = args => {
|
|
502
|
+
var _state$modifications3;
|
|
503
|
+
|
|
504
|
+
const {
|
|
505
|
+
key,
|
|
506
|
+
visitor,
|
|
507
|
+
state
|
|
508
|
+
} = args;
|
|
509
|
+
|
|
510
|
+
if (visitor) {
|
|
511
|
+
return visitor.getModificationInfoSync(key);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
const modification = (_state$modifications3 = state.modifications) === null || _state$modifications3 === void 0 ? void 0 : _state$modifications3.get(key);
|
|
515
|
+
|
|
516
|
+
if (!state.status.isSdkReady && modification) {
|
|
517
|
+
return modification;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
return null;
|
|
521
|
+
};
|
|
522
|
+
/**
|
|
523
|
+
* Get the campaign modification information value matching the given key.
|
|
524
|
+
* @param {string} key key which identify the modification.
|
|
525
|
+
* @deprecated use useFsFlag instead
|
|
526
|
+
*/
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
const useFsModificationInfo = key => {
|
|
530
|
+
const {
|
|
531
|
+
state
|
|
532
|
+
} = (0, _react.useContext)(_FlagshipContext.FlagshipContext);
|
|
533
|
+
const {
|
|
534
|
+
visitor
|
|
535
|
+
} = state;
|
|
536
|
+
return fsModificationInfoSync({
|
|
537
|
+
key,
|
|
538
|
+
state,
|
|
539
|
+
visitor
|
|
540
|
+
});
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
exports.useFsModificationInfo = useFsModificationInfo;
|
|
544
|
+
|
|
545
|
+
const fsActivate = async (params, functionName, visitor, config) => {
|
|
546
|
+
try {
|
|
547
|
+
if (!visitor) {
|
|
548
|
+
(0, _utils.logWarn)(config, _constants.noVisitorMessage, functionName);
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
await visitor.activateModifications(params); // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
553
|
+
} catch (error) {
|
|
554
|
+
(0, _utils.logWarn)(config, error.message || error, functionName);
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
/**
|
|
558
|
+
* This hook returns a flag object by its key. If no flag match the given key an empty flag will be returned.
|
|
559
|
+
* @param key
|
|
560
|
+
* @param defaultValue
|
|
561
|
+
* @returns
|
|
562
|
+
*/
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
const useFsFlag = (key, defaultValue) => {
|
|
566
|
+
const {
|
|
567
|
+
state
|
|
568
|
+
} = (0, _react.useContext)(_FlagshipContext.FlagshipContext);
|
|
569
|
+
const {
|
|
570
|
+
visitor
|
|
571
|
+
} = state;
|
|
572
|
+
|
|
573
|
+
if (!visitor) {
|
|
574
|
+
return new _Flag.Flag(defaultValue, key, state.modifications);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
return visitor.getFlag(key, defaultValue);
|
|
578
|
+
};
|
|
579
|
+
/**
|
|
580
|
+
* Report this user has seen this modification. Report this user has seen these modifications.
|
|
581
|
+
* @param params
|
|
582
|
+
* @deprecated use useFsFlag instead
|
|
583
|
+
* @returns
|
|
584
|
+
*/
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
exports.useFsFlag = useFsFlag;
|
|
588
|
+
|
|
589
|
+
const useFsActivate = async params => {
|
|
590
|
+
const {
|
|
591
|
+
state
|
|
592
|
+
} = (0, _react.useContext)(_FlagshipContext.FlagshipContext);
|
|
593
|
+
const {
|
|
594
|
+
visitor,
|
|
595
|
+
config
|
|
596
|
+
} = state;
|
|
597
|
+
const functionName = 'useFsModifications';
|
|
598
|
+
await fsActivate(params, functionName, visitor, config);
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
exports.useFsActivate = useFsActivate;
|
|
602
|
+
|
|
603
|
+
const useFlagship = () => {
|
|
604
|
+
const {
|
|
605
|
+
state
|
|
606
|
+
} = (0, _react.useContext)(_FlagshipContext.FlagshipContext);
|
|
607
|
+
const {
|
|
608
|
+
visitor,
|
|
609
|
+
config
|
|
610
|
+
} = state;
|
|
611
|
+
|
|
612
|
+
const fsUpdateContext = context => {
|
|
613
|
+
const functionName = 'updateContext';
|
|
614
|
+
|
|
615
|
+
if (!visitor) {
|
|
616
|
+
(0, _utils.logError)(config, _constants.noVisitorMessage, functionName);
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
visitor.clearContext();
|
|
621
|
+
visitor.updateContext(context);
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
const fsClearContext = () => {
|
|
625
|
+
const functionName = 'cleanContext';
|
|
626
|
+
|
|
627
|
+
if (!visitor) {
|
|
628
|
+
(0, _utils.logError)(config, _constants.noVisitorMessage, functionName);
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
visitor.clearContext();
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
const fsAuthenticate = visitorId => {
|
|
636
|
+
const functionName = 'authenticate';
|
|
637
|
+
|
|
638
|
+
if (!visitor) {
|
|
639
|
+
(0, _utils.logError)(config, _constants.noVisitorMessage, functionName);
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
visitor.authenticate(visitorId);
|
|
644
|
+
};
|
|
645
|
+
|
|
646
|
+
const fsUnauthenticate = () => {
|
|
647
|
+
const functionName = 'unauthenticate';
|
|
648
|
+
|
|
649
|
+
if (!visitor) {
|
|
650
|
+
(0, _utils.logError)(config, _constants.noVisitorMessage, functionName);
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
visitor.unauthenticate();
|
|
655
|
+
};
|
|
656
|
+
/**
|
|
657
|
+
* Send a Hit to Flagship servers for reporting.
|
|
658
|
+
*/
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
const fsSendHit = hit => {
|
|
662
|
+
const functionName = 'sendHit';
|
|
663
|
+
|
|
664
|
+
if (!visitor) {
|
|
665
|
+
(0, _utils.logError)(config, _constants.noVisitorMessage, functionName);
|
|
666
|
+
return Promise.resolve();
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
return visitor.sendHit(hit);
|
|
670
|
+
};
|
|
671
|
+
/**
|
|
672
|
+
* Send a Hit to Flagship servers for reporting.
|
|
673
|
+
*/
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
const fsSendHits = hit => {
|
|
677
|
+
const functionName = 'sendHits';
|
|
678
|
+
|
|
679
|
+
if (!visitor) {
|
|
680
|
+
(0, _utils.logError)(config, _constants.noVisitorMessage, functionName);
|
|
681
|
+
return Promise.resolve();
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
return visitor.sendHits(hit);
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
let modifications = visitor === null || visitor === void 0 ? void 0 : visitor.getModificationsArray();
|
|
688
|
+
|
|
689
|
+
if (!state.status.isSdkReady && state.modifications) {
|
|
690
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
691
|
+
modifications = Array.from(state.modifications, ([_key, item]) => item);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
const activateModification = async params => {
|
|
695
|
+
const functionName = 'activateModification';
|
|
696
|
+
await fsActivate(params, functionName, visitor, config);
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
const synchronizeModifications = async () => {
|
|
700
|
+
if (!visitor) {
|
|
701
|
+
(0, _utils.logWarn)(config, _constants.noVisitorMessage, 'synchronizeModifications');
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
await visitor.synchronizeModifications();
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
const getModifications = (params, activateAll) => {
|
|
709
|
+
const functionName = 'getModifications';
|
|
710
|
+
return fsModificationsSync({
|
|
711
|
+
functionName,
|
|
712
|
+
state,
|
|
713
|
+
visitor,
|
|
714
|
+
config,
|
|
715
|
+
params,
|
|
716
|
+
activateAll
|
|
717
|
+
});
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
const getModificationInfo = key => {
|
|
721
|
+
return fsModificationInfoSync({
|
|
722
|
+
key,
|
|
723
|
+
state,
|
|
724
|
+
visitor
|
|
725
|
+
});
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
function getFlag(key, defaultValue) {
|
|
729
|
+
if (!visitor) {
|
|
730
|
+
return new _Flag.Flag(defaultValue, key, state.modifications);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
return visitor.getFlag(key, defaultValue);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
function fetchFlags() {
|
|
737
|
+
if (!visitor) {
|
|
738
|
+
(0, _utils.logWarn)(config, _constants.noVisitorMessage, 'fetchFlags');
|
|
739
|
+
return Promise.resolve();
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
return visitor.fetchFlags();
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
function setConsent(hasConsented) {
|
|
746
|
+
if (!visitor) {
|
|
747
|
+
(0, _utils.logWarn)(config, _constants.noVisitorMessage, 'setConsent');
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
visitor.setConsent(hasConsented);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
async function close() {
|
|
755
|
+
await _jsSdk.default.close();
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
return {
|
|
759
|
+
visitorId: visitor === null || visitor === void 0 ? void 0 : visitor.visitorId,
|
|
760
|
+
anonymousId: visitor === null || visitor === void 0 ? void 0 : visitor.anonymousId,
|
|
761
|
+
context: { ...(visitor === null || visitor === void 0 ? void 0 : visitor.context)
|
|
762
|
+
},
|
|
763
|
+
hasConsented: visitor === null || visitor === void 0 ? void 0 : visitor.hasConsented,
|
|
764
|
+
setConsent,
|
|
765
|
+
updateContext: fsUpdateContext,
|
|
766
|
+
clearContext: fsClearContext,
|
|
767
|
+
authenticate: fsAuthenticate,
|
|
768
|
+
unauthenticate: fsUnauthenticate,
|
|
769
|
+
status: state.status,
|
|
770
|
+
activateModification,
|
|
771
|
+
synchronizeModifications,
|
|
772
|
+
getModifications,
|
|
773
|
+
modifications: modifications || [],
|
|
774
|
+
flagsData: (visitor === null || visitor === void 0 ? void 0 : visitor.getFlagsDataArray()) || [],
|
|
775
|
+
getModificationInfo,
|
|
776
|
+
hit: {
|
|
777
|
+
send: fsSendHit,
|
|
778
|
+
sendMultiple: fsSendHits
|
|
779
|
+
},
|
|
780
|
+
getFlag,
|
|
781
|
+
fetchFlags,
|
|
782
|
+
close
|
|
783
|
+
};
|
|
784
|
+
};
|
|
785
|
+
|
|
786
|
+
exports.useFlagship = useFlagship;
|
|
787
|
+
|
|
788
|
+
/***/ }),
|
|
789
|
+
|
|
790
|
+
/***/ "./src/constants.ts":
|
|
791
|
+
/*!**************************!*\
|
|
792
|
+
!*** ./src/constants.ts ***!
|
|
793
|
+
\**************************/
|
|
794
|
+
/***/ ((__unused_webpack_module, exports) => {
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
Object.defineProperty(exports, "__esModule", ({
|
|
799
|
+
value: true
|
|
800
|
+
}));
|
|
801
|
+
exports.GET_METADATA_CAST_ERROR = exports.GET_FLAG_CAST_ERROR = exports.noVisitorDefault = exports.noVisitorMessage = void 0;
|
|
802
|
+
const noVisitorMessage = 'flagship Visitor not initialized.';
|
|
803
|
+
exports.noVisitorMessage = noVisitorMessage;
|
|
804
|
+
const noVisitorDefault = 'fsVisitor not initialized, returns default value';
|
|
805
|
+
exports.noVisitorDefault = noVisitorDefault;
|
|
806
|
+
const GET_FLAG_CAST_ERROR = 'Flag for key {0} has a different type. Default value is returned.';
|
|
807
|
+
exports.GET_FLAG_CAST_ERROR = GET_FLAG_CAST_ERROR;
|
|
808
|
+
const GET_METADATA_CAST_ERROR = 'Flag for key {0} has a different type with defaultValue, an empty metadata object is returned';
|
|
809
|
+
exports.GET_METADATA_CAST_ERROR = GET_METADATA_CAST_ERROR;
|
|
810
|
+
|
|
811
|
+
/***/ }),
|
|
812
|
+
|
|
813
|
+
/***/ "./src/index.tsx":
|
|
814
|
+
/*!***********************!*\
|
|
815
|
+
!*** ./src/index.tsx ***!
|
|
816
|
+
\***********************/
|
|
817
|
+
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
Object.defineProperty(exports, "__esModule", ({
|
|
822
|
+
value: true
|
|
823
|
+
}));
|
|
824
|
+
|
|
825
|
+
var _FlagshipContext = __webpack_require__(/*! ./FlagshipContext */ "./src/FlagshipContext.tsx");
|
|
826
|
+
|
|
827
|
+
Object.keys(_FlagshipContext).forEach(function (key) {
|
|
828
|
+
if (key === "default" || key === "__esModule") return;
|
|
829
|
+
if (key in exports && exports[key] === _FlagshipContext[key]) return;
|
|
830
|
+
Object.defineProperty(exports, key, {
|
|
831
|
+
enumerable: true,
|
|
832
|
+
get: function () {
|
|
833
|
+
return _FlagshipContext[key];
|
|
834
|
+
}
|
|
835
|
+
});
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
var _jsSdk = __webpack_require__(/*! @flagship.io/js-sdk */ "@flagship.io/js-sdk");
|
|
839
|
+
|
|
840
|
+
Object.keys(_jsSdk).forEach(function (key) {
|
|
841
|
+
if (key === "default" || key === "__esModule") return;
|
|
842
|
+
if (key in exports && exports[key] === _jsSdk[key]) return;
|
|
843
|
+
Object.defineProperty(exports, key, {
|
|
844
|
+
enumerable: true,
|
|
845
|
+
get: function () {
|
|
846
|
+
return _jsSdk[key];
|
|
847
|
+
}
|
|
848
|
+
});
|
|
849
|
+
});
|
|
850
|
+
|
|
851
|
+
var _FlagshipHooks = __webpack_require__(/*! ./FlagshipHooks */ "./src/FlagshipHooks.tsx");
|
|
852
|
+
|
|
853
|
+
Object.keys(_FlagshipHooks).forEach(function (key) {
|
|
854
|
+
if (key === "default" || key === "__esModule") return;
|
|
855
|
+
if (key in exports && exports[key] === _FlagshipHooks[key]) return;
|
|
856
|
+
Object.defineProperty(exports, key, {
|
|
857
|
+
enumerable: true,
|
|
858
|
+
get: function () {
|
|
859
|
+
return _FlagshipHooks[key];
|
|
860
|
+
}
|
|
861
|
+
});
|
|
862
|
+
});
|
|
863
|
+
|
|
864
|
+
/***/ }),
|
|
865
|
+
|
|
866
|
+
/***/ "./src/sdkVersion.ts":
|
|
867
|
+
/*!***************************!*\
|
|
868
|
+
!*** ./src/sdkVersion.ts ***!
|
|
869
|
+
\***************************/
|
|
870
|
+
/***/ ((__unused_webpack_module, exports) => {
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
Object.defineProperty(exports, "__esModule", ({
|
|
875
|
+
value: true
|
|
876
|
+
}));
|
|
877
|
+
exports.version = void 0;
|
|
878
|
+
// Generated by genversion.
|
|
879
|
+
const version = '3.1.1';
|
|
880
|
+
exports.version = version;
|
|
881
|
+
|
|
882
|
+
/***/ }),
|
|
883
|
+
|
|
884
|
+
/***/ "./src/utils.tsx":
|
|
885
|
+
/*!***********************!*\
|
|
886
|
+
!*** ./src/utils.tsx ***!
|
|
887
|
+
\***********************/
|
|
888
|
+
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
Object.defineProperty(exports, "__esModule", ({
|
|
893
|
+
value: true
|
|
894
|
+
}));
|
|
895
|
+
exports.logError = logError;
|
|
896
|
+
exports.logInfo = logInfo;
|
|
897
|
+
exports.logWarn = logWarn;
|
|
898
|
+
exports.uuidV4 = uuidV4;
|
|
899
|
+
exports.useNonInitialEffect = useNonInitialEffect;
|
|
900
|
+
exports.hasSameType = hasSameType;
|
|
901
|
+
exports.sprintf = sprintf;
|
|
902
|
+
exports.getModificationsFromCampaigns = void 0;
|
|
903
|
+
|
|
904
|
+
var _jsSdk = __webpack_require__(/*! @flagship.io/js-sdk */ "@flagship.io/js-sdk");
|
|
905
|
+
|
|
906
|
+
var _react = __webpack_require__(/*! react */ "react");
|
|
907
|
+
|
|
908
|
+
function logError(config, message, tag) {
|
|
909
|
+
if (!config || !config.logManager || typeof config.logManager.error !== 'function' || !config.logLevel || config.logLevel < _jsSdk.LogLevel.ERROR) {
|
|
910
|
+
return;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
config.logManager.error(message, tag);
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
function logInfo(config, message, tag) {
|
|
917
|
+
if (!config || !config.logManager || typeof config.logManager.info !== 'function' || !config.logLevel || config.logLevel < _jsSdk.LogLevel.INFO) {
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
config.logManager.info(message, tag);
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
function logWarn(config, message, tag) {
|
|
925
|
+
if (!config || !config.logManager || typeof config.logManager.warning !== 'function' || !config.logLevel || config.logLevel < _jsSdk.LogLevel.WARNING) {
|
|
926
|
+
return;
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
config.logManager.warning(message, tag);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
const getModificationsFromCampaigns = campaigns => {
|
|
933
|
+
const modifications = new Map();
|
|
934
|
+
|
|
935
|
+
if (!campaigns || !Array.isArray(campaigns)) {
|
|
936
|
+
return modifications;
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
campaigns.forEach(campaign => {
|
|
940
|
+
const object = campaign.variation.modifications.value;
|
|
941
|
+
|
|
942
|
+
for (const key in object) {
|
|
943
|
+
const value = object[key];
|
|
944
|
+
modifications.set(key, {
|
|
945
|
+
key,
|
|
946
|
+
campaignId: campaign.id,
|
|
947
|
+
variationGroupId: campaign.variationGroupId,
|
|
948
|
+
variationId: campaign.variation.id,
|
|
949
|
+
isReference: campaign.variation.reference,
|
|
950
|
+
value
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
});
|
|
954
|
+
return modifications;
|
|
955
|
+
};
|
|
956
|
+
|
|
957
|
+
exports.getModificationsFromCampaigns = getModificationsFromCampaigns;
|
|
958
|
+
|
|
959
|
+
function uuidV4() {
|
|
960
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (char) {
|
|
961
|
+
const rand = Math.random() * 16 | 0;
|
|
962
|
+
const value = char === 'x' ? rand : rand & 0x3 | 0x8;
|
|
963
|
+
return value.toString(16);
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
function useNonInitialEffect(effect, deps) {
|
|
968
|
+
const initialRender = (0, _react.useRef)(true);
|
|
969
|
+
(0, _react.useEffect)(() => {
|
|
970
|
+
if (initialRender.current) {
|
|
971
|
+
initialRender.current = false;
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
if (typeof effect === 'function') {
|
|
976
|
+
return effect();
|
|
977
|
+
}
|
|
978
|
+
}, deps);
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
function hasSameType(flagValue, defaultValue) {
|
|
982
|
+
if (typeof flagValue !== typeof defaultValue) {
|
|
983
|
+
return false;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
if (typeof flagValue === 'object' && typeof defaultValue === 'object' && Array.isArray(flagValue) !== Array.isArray(defaultValue)) {
|
|
987
|
+
return false;
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
return true;
|
|
991
|
+
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
function sprintf(format, ...value) {
|
|
995
|
+
let formatted = format;
|
|
996
|
+
|
|
997
|
+
for (let i = 0; i < value.length; i++) {
|
|
998
|
+
const element = value[i];
|
|
999
|
+
formatted = formatted.replace(new RegExp(`\\{${i}\\}`, 'g'), element);
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
return formatted;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
/***/ }),
|
|
1006
|
+
|
|
1007
|
+
/***/ "@flagship.io/js-sdk":
|
|
1008
|
+
/*!**************************************!*\
|
|
1009
|
+
!*** external "@flagship.io/js-sdk" ***!
|
|
1010
|
+
\**************************************/
|
|
1011
|
+
/***/ ((module) => {
|
|
1012
|
+
|
|
1013
|
+
module.exports = require("@flagship.io/js-sdk");
|
|
1014
|
+
|
|
1015
|
+
/***/ }),
|
|
1016
|
+
|
|
1017
|
+
/***/ "core-js/modules/es.promise.js":
|
|
1018
|
+
/*!************************************************!*\
|
|
1019
|
+
!*** external "core-js/modules/es.promise.js" ***!
|
|
1020
|
+
\************************************************/
|
|
1021
|
+
/***/ ((module) => {
|
|
1022
|
+
|
|
1023
|
+
module.exports = require("core-js/modules/es.promise.js");
|
|
1024
|
+
|
|
1025
|
+
/***/ }),
|
|
1026
|
+
|
|
1027
|
+
/***/ "react":
|
|
1028
|
+
/*!************************!*\
|
|
1029
|
+
!*** external "react" ***!
|
|
1030
|
+
\************************/
|
|
1031
|
+
/***/ ((module) => {
|
|
1032
|
+
|
|
1033
|
+
module.exports = require("react");
|
|
1034
|
+
|
|
1035
|
+
/***/ })
|
|
1036
|
+
|
|
1037
|
+
/******/ });
|
|
1038
|
+
/************************************************************************/
|
|
1039
|
+
/******/ // The module cache
|
|
1040
|
+
/******/ var __webpack_module_cache__ = {};
|
|
1041
|
+
/******/
|
|
1042
|
+
/******/ // The require function
|
|
1043
|
+
/******/ function __webpack_require__(moduleId) {
|
|
1044
|
+
/******/ // Check if module is in cache
|
|
1045
|
+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
|
|
1046
|
+
/******/ if (cachedModule !== undefined) {
|
|
1047
|
+
/******/ return cachedModule.exports;
|
|
1048
|
+
/******/ }
|
|
1049
|
+
/******/ // Create a new module (and put it into the cache)
|
|
1050
|
+
/******/ var module = __webpack_module_cache__[moduleId] = {
|
|
1051
|
+
/******/ // no module.id needed
|
|
1052
|
+
/******/ // no module.loaded needed
|
|
1053
|
+
/******/ exports: {}
|
|
1054
|
+
/******/ };
|
|
1055
|
+
/******/
|
|
1056
|
+
/******/ // Execute the module function
|
|
1057
|
+
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
1058
|
+
/******/
|
|
1059
|
+
/******/ // Return the exports of the module
|
|
1060
|
+
/******/ return module.exports;
|
|
1061
|
+
/******/ }
|
|
1062
|
+
/******/
|
|
1063
|
+
/************************************************************************/
|
|
1064
|
+
/******/
|
|
1065
|
+
/******/ // startup
|
|
1066
|
+
/******/ // Load entry module and return exports
|
|
1067
|
+
/******/ // This entry module is referenced by other modules so it can't be inlined
|
|
1068
|
+
/******/ var __webpack_exports__ = __webpack_require__("./src/index.tsx");
|
|
1069
|
+
/******/ module.exports = __webpack_exports__;
|
|
1070
|
+
/******/
|
|
1071
|
+
/******/ })()
|
|
1072
|
+
;
|
|
2
1073
|
//# sourceMappingURL=index.node.js.map
|