@flagship.io/react-sdk 3.0.4-alpha.3 → 3.0.5

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 CHANGED
@@ -1,14 +1,25 @@
1
- <p align="center">
2
- <img src="https://mk0abtastybwtpirqi5t.kinstacdn.com/wp-content/uploads/picture-solutions-persona-product-flagship.jpg" width="211" height="182" alt="flagship-java" />
3
- </p>
4
- <h3 align="center">Bring your features to life</h3>
5
-
6
- **Visit [https://developers.flagship.io/](https://developers.flagship.io/) to get started with Flagship.**
7
-
8
- ## Docs
9
-
10
- - [docs](https://docs.developers.flagship.io/docs/react-v3-0-x)
11
-
12
- ## Licence
13
-
14
- [Apache License.](https://github.com/flagship-io/flagship-react-sdk/blob/master/LICENSE)
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
package/dist/esm/Flag.js CHANGED
@@ -7,46 +7,96 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
7
7
  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; }
8
8
 
9
9
  import Flagship, { FlagMetadata } from '@flagship.io/js-sdk';
10
- import { noVisitorMessage } from './constants';
11
- import { logWarn } from './utils';
10
+ import { GET_FLAG_CAST_ERROR, GET_METADATA_CAST_ERROR, noVisitorMessage } from './constants';
11
+ import { hasSameType, logInfo, logWarn, sprintf } from './utils';
12
12
  export var Flag = /*#__PURE__*/function () {
13
- function Flag(defaultValue) {
13
+ function Flag(defaultValue, key, flagsData) {
14
14
  _classCallCheck(this, Flag);
15
15
 
16
- _defineProperty(this, "_defaultValue", void 0);
16
+ _defineProperty(this, "defaultValue", void 0);
17
17
 
18
- logWarn(Flagship.getConfig(), noVisitorMessage, 'GetFlag');
19
- this._defaultValue = defaultValue;
18
+ _defineProperty(this, "flagsData", void 0);
19
+
20
+ _defineProperty(this, "key", void 0);
21
+
22
+ _defineProperty(this, "flag", void 0);
23
+
24
+ if (!flagsData) {
25
+ logWarn(Flagship.getConfig(), noVisitorMessage, 'GetFlag');
26
+ }
27
+
28
+ this.defaultValue = defaultValue;
29
+ this.key = key;
30
+ this.flag = flagsData === null || flagsData === void 0 ? void 0 : flagsData.get(key);
20
31
  }
21
32
 
22
33
  _createClass(Flag, [{
34
+ key: "NotSameType",
35
+ value: function NotSameType() {
36
+ var _this$flag;
37
+
38
+ return this.defaultValue !== null && this.defaultValue !== undefined && !hasSameType((_this$flag = this.flag) === null || _this$flag === void 0 ? void 0 : _this$flag.value, this.defaultValue);
39
+ }
40
+ }, {
23
41
  key: "getValue",
24
42
  value: function getValue() {
25
- logWarn(Flagship.getConfig(), noVisitorMessage, 'getValue');
26
- return this._defaultValue;
43
+ if (!this.flag) {
44
+ logWarn(Flagship.getConfig(), noVisitorMessage, 'getValue');
45
+ return this.defaultValue;
46
+ }
47
+
48
+ if (this.NotSameType()) {
49
+ logInfo(Flagship.getConfig(), sprintf(GET_FLAG_CAST_ERROR, this.key), "getValue");
50
+ return this.defaultValue;
51
+ }
52
+
53
+ return this.flag.value;
27
54
  }
28
55
  }, {
29
56
  key: "exists",
30
57
  value: function exists() {
31
- logWarn(Flagship.getConfig(), noVisitorMessage, 'exists');
32
- return false;
58
+ var _this$flag2;
59
+
60
+ if (!this.flag) {
61
+ logWarn(Flagship.getConfig(), noVisitorMessage, 'exists');
62
+ return false;
63
+ }
64
+
65
+ return !!((_this$flag2 = this.flag) !== null && _this$flag2 !== void 0 && _this$flag2.campaignId && this.flag.variationId && this.flag.variationGroupId);
33
66
  }
34
67
  }, {
35
68
  key: "userExposed",
36
69
  value: function userExposed() {
37
- logWarn(Flagship.getConfig(), noVisitorMessage, 'userExposed');
70
+ if (!this.flag) {
71
+ logWarn(Flagship.getConfig(), noVisitorMessage, 'userExposed');
72
+ }
73
+
38
74
  return Promise.resolve();
39
75
  }
40
76
  }, {
41
77
  key: "metadata",
42
78
  get: function get() {
43
- logWarn(Flagship.getConfig(), noVisitorMessage, 'metadata');
79
+ var _this$flag3, _this$flag4, _this$flag5, _this$flag6, _this$flag7, _this$flag8;
80
+
81
+ var functionName = 'metadata';
82
+
83
+ if (!this.flag) {
84
+ logWarn(Flagship.getConfig(), noVisitorMessage, functionName);
85
+ return FlagMetadata.Empty();
86
+ }
87
+
88
+ if (this.NotSameType()) {
89
+ logInfo(Flagship.getConfig(), sprintf(GET_METADATA_CAST_ERROR, this.key), functionName);
90
+ return FlagMetadata.Empty();
91
+ }
92
+
44
93
  return new FlagMetadata({
45
- campaignId: '',
46
- campaignType: '',
47
- isReference: false,
48
- variationGroupId: '',
49
- variationId: ''
94
+ campaignId: ((_this$flag3 = this.flag) === null || _this$flag3 === void 0 ? void 0 : _this$flag3.campaignId) || '',
95
+ variationGroupId: ((_this$flag4 = this.flag) === null || _this$flag4 === void 0 ? void 0 : _this$flag4.variationGroupId) || '',
96
+ variationId: ((_this$flag5 = this.flag) === null || _this$flag5 === void 0 ? void 0 : _this$flag5.variationId) || '',
97
+ isReference: !!((_this$flag6 = this.flag) !== null && _this$flag6 !== void 0 && _this$flag6.isReference),
98
+ campaignType: ((_this$flag7 = this.flag) === null || _this$flag7 === void 0 ? void 0 : _this$flag7.campaignType) || '',
99
+ slug: (_this$flag8 = this.flag) === null || _this$flag8 === void 0 ? void 0 : _this$flag8.slug
50
100
  });
51
101
  }
52
102
  }]);
@@ -68,9 +68,9 @@ var fsModificationsSync = function fsModificationsSync(args) {
68
68
  });
69
69
  return flags;
70
70
  };
71
- /**
72
- * Retrieve a modification value by its key. If no modification match the given key or if the stored value type and default value type do not match, default value will be returned.
73
- * @deprecated use useFsGetFlag instead
71
+ /**
72
+ * Retrieve a modification value by its key. If no modification match the given key or if the stored value type and default value type do not match, default value will be returned.
73
+ * @deprecated use useFsFlag instead
74
74
  */
75
75
 
76
76
 
@@ -90,9 +90,9 @@ export var useFsModifications = function useFsModifications(params, activateAll)
90
90
  activateAll: activateAll
91
91
  });
92
92
  };
93
- /**
94
- * Retrieve a modification value by its key. If no modification match the given key or if the stored value type and default value type do not match, default value will be returned.
95
- * @deprecated use useFsGetFlag instead
93
+ /**
94
+ * Retrieve a modification value by its key. If no modification match the given key or if the stored value type and default value type do not match, default value will be returned.
95
+ * @deprecated use useFsFlag instead
96
96
  */
97
97
 
98
98
  export var useFsModification = function useFsModification(params) {
@@ -138,10 +138,10 @@ var fsModificationInfoSync = function fsModificationInfoSync(args) {
138
138
 
139
139
  return null;
140
140
  };
141
- /**
142
- * Get the campaign modification information value matching the given key.
143
- * @param {string} key key which identify the modification.
144
- * @deprecated use useFsGetFlag instead
141
+ /**
142
+ * Get the campaign modification information value matching the given key.
143
+ * @param {string} key key which identify the modification.
144
+ * @deprecated use useFsFlag instead
145
145
  */
146
146
 
147
147
 
@@ -198,11 +198,11 @@ var fsActivate = /*#__PURE__*/function () {
198
198
  return _ref.apply(this, arguments);
199
199
  };
200
200
  }();
201
- /**
202
- * This hook returns a flag object by its key. If no flag match the given key an empty flag will be returned.
203
- * @param key
204
- * @param defaultValue
205
- * @returns
201
+ /**
202
+ * This hook returns a flag object by its key. If no flag match the given key an empty flag will be returned.
203
+ * @param key
204
+ * @param defaultValue
205
+ * @returns
206
206
  */
207
207
 
208
208
 
@@ -213,16 +213,16 @@ export var useFsFlag = function useFsFlag(key, defaultValue) {
213
213
  var visitor = state.visitor;
214
214
 
215
215
  if (!visitor) {
216
- return new Flag(defaultValue);
216
+ return new Flag(defaultValue, key, state.modifications);
217
217
  }
218
218
 
219
219
  return visitor.getFlag(key, defaultValue);
220
220
  };
221
- /**
222
- * Report this user has seen this modification. Report this user has seen these modifications.
223
- * @param params
224
- * @deprecated use useFsGetFlag instead
225
- * @returns
221
+ /**
222
+ * Report this user has seen this modification. Report this user has seen these modifications.
223
+ * @param params
224
+ * @deprecated use useFsFlag instead
225
+ * @returns
226
226
  */
227
227
 
228
228
  export var useFsActivate = /*#__PURE__*/function () {
@@ -302,8 +302,8 @@ export var useFlagship = function useFlagship() {
302
302
 
303
303
  visitor.unauthenticate();
304
304
  };
305
- /**
306
- * Send a Hit to Flagship servers for reporting.
305
+ /**
306
+ * Send a Hit to Flagship servers for reporting.
307
307
  */
308
308
 
309
309
 
@@ -317,8 +317,8 @@ export var useFlagship = function useFlagship() {
317
317
 
318
318
  return visitor.sendHit(hit);
319
319
  };
320
- /**
321
- * Send a Hit to Flagship servers for reporting.
320
+ /**
321
+ * Send a Hit to Flagship servers for reporting.
322
322
  */
323
323
 
324
324
 
@@ -423,7 +423,7 @@ export var useFlagship = function useFlagship() {
423
423
 
424
424
  function getFlag(key, defaultValue) {
425
425
  if (!visitor) {
426
- return new Flag(defaultValue);
426
+ return new Flag(defaultValue, key, state.modifications);
427
427
  }
428
428
 
429
429
  return visitor.getFlag(key, defaultValue);
@@ -462,7 +462,7 @@ export var useFlagship = function useFlagship() {
462
462
  synchronizeModifications: synchronizeModifications,
463
463
  getModifications: getModifications,
464
464
  modifications: modifications || [],
465
- FlagsData: (visitor === null || visitor === void 0 ? void 0 : visitor.getFlagsDataArray()) || [],
465
+ flagsData: (visitor === null || visitor === void 0 ? void 0 : visitor.getFlagsDataArray()) || [],
466
466
  getModificationInfo: getModificationInfo,
467
467
  hit: {
468
468
  send: fsSendHit,
@@ -1,2 +1,4 @@
1
1
  export var noVisitorMessage = 'flagship Visitor not initialized.';
2
- export var noVisitorDefault = 'fsVisitor not initialized, returns default value';
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';
package/dist/esm/utils.js CHANGED
@@ -1,3 +1,5 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
2
+
1
3
  import { LogLevel } from '@flagship.io/js-sdk';
2
4
  import { useEffect, useRef } from 'react';
3
5
  export function logError(config, message, tag) {
@@ -64,4 +66,25 @@ export function useNonInitialEffect(effect, deps) {
64
66
  return effect();
65
67
  }
66
68
  }, deps);
69
+ }
70
+ export function hasSameType(flagValue, defaultValue) {
71
+ if (_typeof(flagValue) !== _typeof(defaultValue)) {
72
+ return false;
73
+ }
74
+
75
+ if (_typeof(flagValue) === 'object' && _typeof(defaultValue) === 'object' && Array.isArray(flagValue) !== Array.isArray(defaultValue)) {
76
+ return false;
77
+ }
78
+
79
+ return true;
80
+ }
81
+ export function sprintf(format) {
82
+ var formatted = format;
83
+
84
+ for (var i = 0; i < (arguments.length <= 1 ? 0 : arguments.length - 1); i++) {
85
+ var element = i + 1 < 1 || arguments.length <= i + 1 ? undefined : arguments[i + 1];
86
+ formatted = formatted.replace(new RegExp("\\{".concat(i, "\\}"), 'g'), element);
87
+ }
88
+
89
+ return formatted;
67
90
  }