flagsmith-nodejs 2.0.0-beta.7 → 2.0.2

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.
@@ -22,10 +22,16 @@ export declare class FeatureStateModel {
22
22
  enabled: boolean;
23
23
  djangoID: number;
24
24
  featurestateUUID: string;
25
+ featureSegment?: FeatureSegment;
25
26
  private value;
26
27
  multivariateFeatureStateValues: MultivariateFeatureStateValueModel[];
27
28
  constructor(feature: FeatureModel, enabled: boolean, djangoID: number, value?: any, featurestateUuid?: string);
28
29
  setValue(value: any): void;
29
30
  getValue(identityId?: number | string): any;
31
+ isHigherSegmentPriority(other: FeatureStateModel): boolean;
30
32
  getMultivariateValue(identityID: number | string): any;
31
33
  }
34
+ export declare class FeatureSegment {
35
+ priority: number;
36
+ constructor(priority: number);
37
+ }
@@ -11,7 +11,7 @@ var __values = (this && this.__values) || function(o) {
11
11
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.FeatureStateModel = exports.MultivariateFeatureStateValueModel = exports.MultivariateFeatureOptionModel = exports.FeatureModel = void 0;
14
+ exports.FeatureSegment = exports.FeatureStateModel = exports.MultivariateFeatureStateValueModel = exports.MultivariateFeatureOptionModel = exports.FeatureModel = void 0;
15
15
  var uuid_1 = require("uuid");
16
16
  var hashing_1 = require("../utils/hashing");
17
17
  var FeatureModel = /** @class */ (function () {
@@ -65,6 +65,22 @@ var FeatureStateModel = /** @class */ (function () {
65
65
  }
66
66
  return this.value;
67
67
  };
68
+ /*
69
+ Returns `True` if `this` is higher segment priority than `other`
70
+ (i.e. has lower value for featureSegment.priority)
71
+ NOTE:
72
+ A segment will be considered higher priority only if:
73
+ 1. `other` does not have a feature segment(i.e: it is an environment feature state or it's a
74
+ feature state with feature segment but from an old document that does not have `featureSegment.priority`)
75
+ but `this` does.
76
+ 2. `other` have a feature segment with high priority
77
+ */
78
+ FeatureStateModel.prototype.isHigherSegmentPriority = function (other) {
79
+ if (!other.featureSegment || !this.featureSegment) {
80
+ return !!this.featureSegment && !other.featureSegment;
81
+ }
82
+ return this.featureSegment.priority < other.featureSegment.priority;
83
+ };
68
84
  FeatureStateModel.prototype.getMultivariateValue = function (identityID) {
69
85
  var e_1, _a;
70
86
  var percentageValue = (0, hashing_1.getHashedPercentateForObjIds)([
@@ -73,7 +89,7 @@ var FeatureStateModel = /** @class */ (function () {
73
89
  ]);
74
90
  var startPercentage = 0;
75
91
  var sortedF = this.multivariateFeatureStateValues.sort(function (a, b) {
76
- return !!(a.id && b.id) ? a.id - b.id : a.mvFsValueUuid > b.mvFsValueUuid ? -1 : 1;
92
+ return a.id - b.id;
77
93
  });
78
94
  try {
79
95
  for (var sortedF_1 = __values(sortedF), sortedF_1_1 = sortedF_1.next(); !sortedF_1_1.done; sortedF_1_1 = sortedF_1.next()) {
@@ -97,3 +113,10 @@ var FeatureStateModel = /** @class */ (function () {
97
113
  return FeatureStateModel;
98
114
  }());
99
115
  exports.FeatureStateModel = FeatureStateModel;
116
+ var FeatureSegment = /** @class */ (function () {
117
+ function FeatureSegment(priority) {
118
+ this.priority = priority;
119
+ }
120
+ return FeatureSegment;
121
+ }());
122
+ exports.FeatureSegment = FeatureSegment;
@@ -1,3 +1,4 @@
1
- import { FeatureModel, FeatureStateModel } from './models';
1
+ import { FeatureModel, FeatureSegment, FeatureStateModel } from './models';
2
2
  export declare function buildFeatureModel(featuresModelJSON: any): FeatureModel;
3
3
  export declare function buildFeatureStateModel(featuresStateModelJSON: any): FeatureStateModel;
4
+ export declare function buildFeatureSegment(featureSegmentJSON: any): FeatureSegment;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildFeatureStateModel = exports.buildFeatureModel = void 0;
3
+ exports.buildFeatureSegment = exports.buildFeatureStateModel = exports.buildFeatureModel = void 0;
4
4
  var models_1 = require("./models");
5
5
  function buildFeatureModel(featuresModelJSON) {
6
6
  return new models_1.FeatureModel(featuresModelJSON.id, featuresModelJSON.name, featuresModelJSON.type);
@@ -8,6 +8,9 @@ function buildFeatureModel(featuresModelJSON) {
8
8
  exports.buildFeatureModel = buildFeatureModel;
9
9
  function buildFeatureStateModel(featuresStateModelJSON) {
10
10
  var featureStateModel = new models_1.FeatureStateModel(buildFeatureModel(featuresStateModelJSON.feature), featuresStateModelJSON.enabled, featuresStateModelJSON.django_id, featuresStateModelJSON.feature_state_value, featuresStateModelJSON.uuid);
11
+ featureStateModel.featureSegment = featuresStateModelJSON.feature_segment ?
12
+ buildFeatureSegment(featuresStateModelJSON.feature_segment) :
13
+ undefined;
11
14
  var multivariateFeatureStateValues = featuresStateModelJSON.multivariate_feature_state_values
12
15
  ? featuresStateModelJSON.multivariate_feature_state_values.map(function (fsv) {
13
16
  var featureOption = new models_1.MultivariateFeatureOptionModel(fsv.multivariate_feature_option.value, fsv.multivariate_feature_option.id);
@@ -18,3 +21,7 @@ function buildFeatureStateModel(featuresStateModelJSON) {
18
21
  return featureStateModel;
19
22
  }
20
23
  exports.buildFeatureStateModel = buildFeatureStateModel;
24
+ function buildFeatureSegment(featureSegmentJSON) {
25
+ return new models_1.FeatureSegment(featureSegmentJSON.priority);
26
+ }
27
+ exports.buildFeatureSegment = buildFeatureSegment;
@@ -39,9 +39,11 @@ function getIdentityFeatureStatesDict(environment, identity, overrideTraits) {
39
39
  try {
40
40
  for (var _g = (e_3 = void 0, __values(matchingSegment.featureStates)), _h = _g.next(); !_h.done; _h = _g.next()) {
41
41
  var featureState = _h.value;
42
- // note that feature states are stored on the segment in descending priority
43
- // order so we only care that the last one is added
44
- // TODO: can we optimise this?
42
+ if (featureStates[featureState.feature.id]) {
43
+ if (featureStates[featureState.feature.id].isHigherSegmentPriority(featureState)) {
44
+ continue;
45
+ }
46
+ }
45
47
  featureStates[featureState.feature.id] = featureState;
46
48
  }
47
49
  }
@@ -63,7 +65,7 @@ function getIdentityFeatureStatesDict(environment, identity, overrideTraits) {
63
65
  }
64
66
  try {
65
67
  // Override with any feature states defined directly the identity
66
- for (var _j = __values(identity.identityFeatures || []), _k = _j.next(); !_k.done; _k = _j.next()) {
68
+ for (var _j = __values(identity.identityFeatures), _k = _j.next(); !_k.done; _k = _j.next()) {
67
69
  var fs = _k.value;
68
70
  if (featureStates[fs.feature.id]) {
69
71
  featureStates[fs.feature.id] = fs;
@@ -4,6 +4,12 @@ export declare const any: (iterable: Array<any>) => boolean;
4
4
  export declare const matchingFunctions: {
5
5
  [x: string]: (thisValue: any, otherValue: any) => any;
6
6
  };
7
+ export declare const semverMatchingFunction: {
8
+ [x: string]: ((thisValue: any, otherValue: any) => any) | ((thisValue: any, otherValue: any) => boolean);
9
+ };
10
+ export declare const getMatchingFunctions: (semver: boolean) => {
11
+ [x: string]: (thisValue: any, otherValue: any) => any;
12
+ };
7
13
  export declare class SegmentConditionModel {
8
14
  EXCEPTION_OPERATOR_METHODS: {
9
15
  [key: string]: string;
@@ -1,9 +1,25 @@
1
1
  "use strict";
2
- var _a;
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ var _a, _b;
3
17
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.SegmentModel = exports.SegmentRuleModel = exports.SegmentConditionModel = exports.matchingFunctions = exports.any = exports.all = void 0;
18
+ exports.SegmentModel = exports.SegmentRuleModel = exports.SegmentConditionModel = exports.getMatchingFunctions = exports.semverMatchingFunction = exports.matchingFunctions = exports.any = exports.all = void 0;
19
+ var semver_1 = __importDefault(require("semver"));
5
20
  var utils_1 = require("../utils");
6
21
  var constants_1 = require("./constants");
22
+ var util_1 = require("./util");
7
23
  var all = function (iterable) { return iterable.filter(function (e) { return !!e; }).length === iterable.length; };
8
24
  exports.all = all;
9
25
  var any = function (iterable) { return iterable.filter(function (e) { return !!e; }).length > 0; };
@@ -23,6 +39,13 @@ exports.matchingFunctions = (_a = {},
23
39
  return otherValue.includes(thisValue);
24
40
  },
25
41
  _a);
42
+ exports.semverMatchingFunction = __assign(__assign({}, exports.matchingFunctions), (_b = {}, _b[constants_1.CONDITION_OPERATORS.EQUAL] = function (thisValue, otherValue) { return semver_1.default.eq(thisValue, otherValue); }, _b[constants_1.CONDITION_OPERATORS.GREATER_THAN] = function (thisValue, otherValue) { return semver_1.default.gt(otherValue, thisValue); }, _b[constants_1.CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE] = function (thisValue, otherValue) {
43
+ return semver_1.default.gte(otherValue, thisValue);
44
+ }, _b[constants_1.CONDITION_OPERATORS.LESS_THAN] = function (thisValue, otherValue) { return semver_1.default.gt(thisValue, otherValue); }, _b[constants_1.CONDITION_OPERATORS.LESS_THAN_INCLUSIVE] = function (thisValue, otherValue) {
45
+ return semver_1.default.gte(thisValue, otherValue);
46
+ }, _b));
47
+ var getMatchingFunctions = function (semver) { return (semver ? exports.semverMatchingFunction : exports.matchingFunctions); };
48
+ exports.getMatchingFunctions = getMatchingFunctions;
26
49
  var SegmentConditionModel = /** @class */ (function () {
27
50
  function SegmentConditionModel(operator, value, property) {
28
51
  var _a;
@@ -50,8 +73,10 @@ var SegmentConditionModel = /** @class */ (function () {
50
73
  return evaluatorFunction(traitValue);
51
74
  }
52
75
  var defaultFunction = function (x, y) { return false; };
53
- var matchingFunction = exports.matchingFunctions[this.operator] || defaultFunction;
54
- var castToTypeOfTraitValue = (0, utils_1.getCastingFunction)(traitValue);
76
+ var matchingFunctionSet = (0, exports.getMatchingFunctions)((0, util_1.isSemver)(this.value));
77
+ var matchingFunction = matchingFunctionSet[this.operator] || defaultFunction;
78
+ var traitType = (0, util_1.isSemver)(this.value) ? 'semver' : typeof traitValue;
79
+ var castToTypeOfTraitValue = (0, utils_1.getCastingFunction)(traitType);
55
80
  return matchingFunction(castToTypeOfTraitValue(this.value), traitValue);
56
81
  };
57
82
  return SegmentConditionModel;
@@ -2,3 +2,5 @@ import { SegmentConditionModel, SegmentModel, SegmentRuleModel } from './models'
2
2
  export declare function buildSegmentConditionModel(segmentConditionJSON: any): SegmentConditionModel;
3
3
  export declare function buildSegmentRuleModel(ruleModelJSON: any): SegmentRuleModel;
4
4
  export declare function buildSegmentModel(segmentModelJSON: any): SegmentModel;
5
+ export declare function isSemver(value: any): boolean;
6
+ export declare function removeSemverSuffix(value: string): string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildSegmentModel = exports.buildSegmentRuleModel = exports.buildSegmentConditionModel = void 0;
3
+ exports.removeSemverSuffix = exports.isSemver = exports.buildSegmentModel = exports.buildSegmentRuleModel = exports.buildSegmentConditionModel = void 0;
4
4
  var util_1 = require("../features/util");
5
5
  var models_1 = require("./models");
6
6
  function buildSegmentConditionModel(segmentConditionJSON) {
@@ -23,3 +23,11 @@ function buildSegmentModel(segmentModelJSON) {
23
23
  return model;
24
24
  }
25
25
  exports.buildSegmentModel = buildSegmentModel;
26
+ function isSemver(value) {
27
+ return typeof value == 'string' && value.endsWith(':semver');
28
+ }
29
+ exports.isSemver = isSemver;
30
+ function removeSemverSuffix(value) {
31
+ return value.replace(':semver', '');
32
+ }
33
+ exports.removeSemverSuffix = removeSemverSuffix;
@@ -1 +1 @@
1
- export declare function getCastingFunction(input: any): CallableFunction;
1
+ export declare function getCastingFunction(traitType: 'boolean' | 'string' | 'number' | 'semver' | any): CallableFunction;
@@ -1,12 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getCastingFunction = void 0;
4
- function getCastingFunction(input) {
5
- switch (typeof input) {
4
+ var util_1 = require("../segments/util");
5
+ function getCastingFunction(traitType) {
6
+ switch (traitType) {
6
7
  case 'boolean':
7
8
  return function (x) { return !['False', 'false'].includes(x); };
8
9
  case 'number':
9
10
  return function (x) { return parseFloat(x); };
11
+ case 'semver':
12
+ return function (x) { return (0, util_1.removeSemverSuffix)(x); };
10
13
  default:
11
14
  return function (x) { return String(x); };
12
15
  }
@@ -74,7 +74,7 @@ var models_3 = require("./models");
74
74
  var polling_manager_1 = require("./polling_manager");
75
75
  var utils_1 = require("./utils");
76
76
  var evaluators_1 = require("../flagsmith-engine/segments/evaluators");
77
- var DEFAULT_API_URL = 'https://api.flagsmith.com/api/v1/';
77
+ var DEFAULT_API_URL = 'https://edge.api.flagsmith.com/api/v1/';
78
78
  var Flagsmith = /** @class */ (function () {
79
79
  /**
80
80
  * A Flagsmith client.
@@ -332,7 +332,7 @@ var Flagsmith = /** @class */ (function () {
332
332
  timeout: this.requestTimeoutSeconds || undefined,
333
333
  body: JSON.stringify(body),
334
334
  headers: headers
335
- }, this.retries, 1000, (this.requestTimeoutSeconds || 10) * 1000)];
335
+ }, this.retries, (this.requestTimeoutSeconds || 10) * 1000)];
336
336
  case 1:
337
337
  data = _e.sent();
338
338
  if (data.status !== 200) {
@@ -1,5 +1,5 @@
1
1
  import { Response } from 'node-fetch';
2
- export declare function generateIdentitiesData(identifier: string, traits?: {
2
+ export declare function generateIdentitiesData(identifier: string, traits: {
3
3
  [key: string]: any;
4
4
  }): {
5
5
  identifier: string;
@@ -9,4 +9,4 @@ export declare function generateIdentitiesData(identifier: string, traits?: {
9
9
  }[];
10
10
  };
11
11
  export declare const delay: (ms: number) => Promise<unknown>;
12
- export declare const retryFetch: (url: string, fetchOptions: {} | undefined, retries: number | undefined, retryDelay: number | undefined, timeout: number) => Promise<Response>;
12
+ export declare const retryFetch: (url: string, fetchOptions: any, retries: number | undefined, timeout: number) => Promise<Response>;
@@ -45,7 +45,7 @@ var node_fetch_1 = __importDefault(require("node-fetch"));
45
45
  if (typeof node_fetch_1.default.default !== 'undefined')
46
46
  node_fetch_1.default = node_fetch_1.default.default;
47
47
  function generateIdentitiesData(identifier, traits) {
48
- var traitsGenerated = Object.entries(traits || {}).map(function (trait) { return ({
48
+ var traitsGenerated = Object.entries(traits).map(function (trait) { return ({
49
49
  trait_key: trait[0],
50
50
  trait_value: trait[1]
51
51
  }); });
@@ -59,10 +59,8 @@ var delay = function (ms) {
59
59
  return new Promise(function (resolve) { return setTimeout(function () { return resolve(undefined); }, ms); });
60
60
  };
61
61
  exports.delay = delay;
62
- var retryFetch = function (url, fetchOptions, retries, retryDelay, timeout) {
63
- if (fetchOptions === void 0) { fetchOptions = {}; }
62
+ var retryFetch = function (url, fetchOptions, retries, timeout) {
64
63
  if (retries === void 0) { retries = 3; }
65
- if (retryDelay === void 0) { retryDelay = 1000; }
66
64
  return new Promise(function (resolve, reject) {
67
65
  // check for timeout
68
66
  if (timeout)
@@ -75,7 +73,7 @@ var retryFetch = function (url, fetchOptions, retries, retryDelay, timeout) {
75
73
  switch (_a.label) {
76
74
  case 0:
77
75
  if (!(n > 0)) return [3 /*break*/, 2];
78
- return [4 /*yield*/, (0, exports.delay)(retryDelay)];
76
+ return [4 /*yield*/, (0, exports.delay)(1000)];
79
77
  case 1:
80
78
  _a.sent();
81
79
  wrapper(--n);
@@ -51,6 +51,7 @@ export class FeatureStateModel {
51
51
  enabled: boolean;
52
52
  djangoID: number;
53
53
  featurestateUUID: string = uuidv4();
54
+ featureSegment?: FeatureSegment;
54
55
  private value: any;
55
56
  multivariateFeatureStateValues: MultivariateFeatureStateValueModel[] = [];
56
57
 
@@ -79,6 +80,23 @@ export class FeatureStateModel {
79
80
  return this.value;
80
81
  }
81
82
 
83
+ /*
84
+ Returns `True` if `this` is higher segment priority than `other`
85
+ (i.e. has lower value for featureSegment.priority)
86
+ NOTE:
87
+ A segment will be considered higher priority only if:
88
+ 1. `other` does not have a feature segment(i.e: it is an environment feature state or it's a
89
+ feature state with feature segment but from an old document that does not have `featureSegment.priority`)
90
+ but `this` does.
91
+ 2. `other` have a feature segment with high priority
92
+ */
93
+ isHigherSegmentPriority(other: FeatureStateModel): boolean {
94
+ if (!other.featureSegment || !this.featureSegment) {
95
+ return !!this.featureSegment && !other.featureSegment;
96
+ }
97
+ return this.featureSegment.priority < other.featureSegment.priority;
98
+ }
99
+
82
100
  getMultivariateValue(identityID: number | string) {
83
101
  const percentageValue = getHashedPercentateForObjIds([
84
102
  this.djangoID || this.featurestateUUID,
@@ -86,9 +104,9 @@ export class FeatureStateModel {
86
104
  ]);
87
105
 
88
106
  let startPercentage = 0;
89
- const sortedF = this.multivariateFeatureStateValues.sort((a, b) =>
90
- !!(a.id && b.id) ? a.id - b.id : a.mvFsValueUuid > b.mvFsValueUuid ? -1 : 1
91
- );
107
+ const sortedF = this.multivariateFeatureStateValues.sort((a, b) =>{
108
+ return a.id - b.id;
109
+ });
92
110
  for (const myValue of sortedF) {
93
111
  const limit = myValue.percentageAllocation + startPercentage;
94
112
  if (startPercentage <= percentageValue && percentageValue < limit) {
@@ -99,3 +117,11 @@ export class FeatureStateModel {
99
117
  return this.value;
100
118
  }
101
119
  }
120
+
121
+ export class FeatureSegment {
122
+ priority: number;
123
+
124
+ constructor(priority: number) {
125
+ this.priority = priority;
126
+ }
127
+ }
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  FeatureModel,
3
+ FeatureSegment,
3
4
  FeatureStateModel,
4
5
  MultivariateFeatureOptionModel,
5
6
  MultivariateFeatureStateValueModel
@@ -18,6 +19,10 @@ export function buildFeatureStateModel(featuresStateModelJSON: any): FeatureStat
18
19
  featuresStateModelJSON.uuid
19
20
  );
20
21
 
22
+ featureStateModel.featureSegment = featuresStateModelJSON.feature_segment ?
23
+ buildFeatureSegment(featuresStateModelJSON.feature_segment) :
24
+ undefined;
25
+
21
26
  const multivariateFeatureStateValues = featuresStateModelJSON.multivariate_feature_state_values
22
27
  ? featuresStateModelJSON.multivariate_feature_state_values.map((fsv: any) => {
23
28
  const featureOption = new MultivariateFeatureOptionModel(
@@ -36,3 +41,7 @@ export function buildFeatureStateModel(featuresStateModelJSON: any): FeatureStat
36
41
 
37
42
  return featureStateModel;
38
43
  }
44
+
45
+ export function buildFeatureSegment(featureSegmentJSON: any): FeatureSegment {
46
+ return new FeatureSegment(featureSegmentJSON.priority);
47
+ }
@@ -25,15 +25,17 @@ function getIdentityFeatureStatesDict(
25
25
  );
26
26
  for (const matchingSegment of identitySegments) {
27
27
  for (const featureState of matchingSegment.featureStates) {
28
- // note that feature states are stored on the segment in descending priority
29
- // order so we only care that the last one is added
30
- // TODO: can we optimise this?
28
+ if (featureStates[featureState.feature.id]) {
29
+ if (featureStates[featureState.feature.id].isHigherSegmentPriority(featureState)) {
30
+ continue;
31
+ }
32
+ }
31
33
  featureStates[featureState.feature.id] = featureState;
32
34
  }
33
35
  }
34
36
 
35
37
  // Override with any feature states defined directly the identity
36
- for (const fs of identity.identityFeatures || []) {
38
+ for (const fs of identity.identityFeatures) {
37
39
  if (featureStates[fs.feature.id]) {
38
40
  featureStates[fs.feature.id] = fs;
39
41
  }
@@ -1,3 +1,5 @@
1
+ import semver from 'semver';
2
+
1
3
  import { FeatureStateModel } from '../features/models';
2
4
  import { getCastingFunction as getCastingFunction } from '../utils';
3
5
  import {
@@ -8,6 +10,7 @@ import {
8
10
  REGEX,
9
11
  CONDITION_OPERATORS
10
12
  } from './constants';
13
+ import { isSemver } from './util';
11
14
 
12
15
  export const all = (iterable: Array<any>) => iterable.filter(e => !!e).length === iterable.length;
13
16
  export const any = (iterable: Array<any>) => iterable.filter(e => !!e).length > 0;
@@ -25,6 +28,19 @@ export const matchingFunctions = {
25
28
  otherValue.includes(thisValue),
26
29
  };
27
30
 
31
+ export const semverMatchingFunction = {
32
+ ...matchingFunctions,
33
+ [CONDITION_OPERATORS.EQUAL]: (thisValue: any, otherValue: any) => semver.eq(thisValue, otherValue),
34
+ [CONDITION_OPERATORS.GREATER_THAN]: (thisValue: any, otherValue: any) => semver.gt(otherValue, thisValue),
35
+ [CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: (thisValue: any, otherValue: any) =>
36
+ semver.gte(otherValue, thisValue),
37
+ [CONDITION_OPERATORS.LESS_THAN]: (thisValue: any, otherValue: any) => semver.gt(thisValue, otherValue),
38
+ [CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (thisValue: any, otherValue: any) =>
39
+ semver.gte(thisValue, otherValue),
40
+ }
41
+
42
+ export const getMatchingFunctions = (semver: boolean) => (semver ? semverMatchingFunction : matchingFunctions);
43
+
28
44
  export class SegmentConditionModel {
29
45
  EXCEPTION_OPERATOR_METHODS: { [key: string]: string } = {
30
46
  [NOT_CONTAINS]: 'evaluateNotContains',
@@ -59,9 +75,12 @@ export class SegmentConditionModel {
59
75
 
60
76
  const defaultFunction = (x: any, y: any) => false;
61
77
 
62
- const matchingFunction = matchingFunctions[this.operator] || defaultFunction;
78
+ const matchingFunctionSet = getMatchingFunctions(isSemver(this.value));
79
+ const matchingFunction = matchingFunctionSet[this.operator] || defaultFunction;
80
+
81
+ const traitType = isSemver(this.value) ? 'semver' : typeof traitValue;
82
+ const castToTypeOfTraitValue = getCastingFunction(traitType);
63
83
 
64
- const castToTypeOfTraitValue = getCastingFunction(traitValue);
65
84
  return matchingFunction(castToTypeOfTraitValue(this.value), traitValue);
66
85
  }
67
86
  }
@@ -27,3 +27,11 @@ export function buildSegmentModel(segmentModelJSON: any): SegmentModel {
27
27
 
28
28
  return model;
29
29
  }
30
+
31
+ export function isSemver(value: any) {
32
+ return typeof value == 'string' && value.endsWith(':semver');
33
+ }
34
+
35
+ export function removeSemverSuffix(value: string) {
36
+ return value.replace(':semver', '');
37
+ }
@@ -1,9 +1,13 @@
1
- export function getCastingFunction(input: any): CallableFunction {
2
- switch (typeof input) {
1
+ import { removeSemverSuffix } from "../segments/util";
2
+
3
+ export function getCastingFunction(traitType: 'boolean' | 'string' | 'number' | 'semver' | any): CallableFunction {
4
+ switch (traitType) {
3
5
  case 'boolean':
4
6
  return (x: any) => !['False', 'false'].includes(x);
5
7
  case 'number':
6
8
  return (x: any) => parseFloat(x);
9
+ case 'semver':
10
+ return (x: any) => removeSemverSuffix(x);
7
11
  default:
8
12
  return (x: any) => String(x);
9
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flagsmith-nodejs",
3
- "version": "2.0.0-beta.7",
3
+ "version": "2.0.2",
4
4
  "description": "Flagsmith lets you manage features flags and remote config across web, mobile and server side applications. Deliver true Continuous Integration. Get builds out faster. Control who has access to new features.",
5
5
  "main": "build/index.js",
6
6
  "repository": {
@@ -46,19 +46,21 @@
46
46
  "test:watch": "jest --coverage --watch --coverageReporters='text'",
47
47
  "test:debug": "node --inspect-brk node_modules/.bin/jest --coverage --watch --coverageReporters='text'",
48
48
  "build": "tsc",
49
- "prepublish": "npm run build",
49
+ "prepublish": "npm i && npm run build",
50
50
  "prepare": "husky install"
51
51
  },
52
52
  "dependencies": {
53
53
  "big-integer": "^1.6.51",
54
54
  "md5": "^2.3.0",
55
55
  "node-fetch": "^2.1.2",
56
+ "semver": "^7.3.7",
56
57
  "uuid": "^8.3.2"
57
58
  },
58
59
  "devDependencies": {
59
60
  "@types/jest": "^27.4.1",
60
61
  "@types/md5": "^2.3.2",
61
62
  "@types/node-fetch": "^2.6.1",
63
+ "@types/semver": "^7.3.9",
62
64
  "@types/uuid": "^8.3.4",
63
65
  "esbuild": "^0.14.25",
64
66
  "husky": "^7.0.4",
@@ -67,4 +69,4 @@
67
69
  "ts-jest": "^27.1.3",
68
70
  "typescript": "^4.6.2"
69
71
  }
70
- }
72
+ }
package/sdk/index.ts CHANGED
@@ -14,7 +14,7 @@ import { SegmentModel } from '../flagsmith-engine/segments/models';
14
14
  import { getIdentitySegments } from '../flagsmith-engine/segments/evaluators';
15
15
  import { FlagsmithCache } from './types';
16
16
 
17
- const DEFAULT_API_URL = 'https://api.flagsmith.com/api/v1/';
17
+ const DEFAULT_API_URL = 'https://edge.api.flagsmith.com/api/v1/';
18
18
 
19
19
  export class Flagsmith {
20
20
  environmentKey?: string;
@@ -269,7 +269,6 @@ export class Flagsmith {
269
269
  headers: headers
270
270
  },
271
271
  this.retries,
272
- 1000,
273
272
  (this.requestTimeoutSeconds || 10) * 1000
274
273
  );
275
274
 
package/sdk/utils.ts CHANGED
@@ -2,8 +2,8 @@ import fetch, { Response } from 'node-fetch';
2
2
  // @ts-ignore
3
3
  if (typeof fetch.default !== 'undefined') fetch = fetch.default;
4
4
 
5
- export function generateIdentitiesData(identifier: string, traits?: { [key: string]: any }) {
6
- const traitsGenerated = Object.entries(traits || {}).map(trait => ({
5
+ export function generateIdentitiesData(identifier: string, traits: { [key: string]: any }) {
6
+ const traitsGenerated = Object.entries(traits).map(trait => ({
7
7
  trait_key: trait[0],
8
8
  trait_value: trait[1]
9
9
  }));
@@ -18,9 +18,8 @@ export const delay = (ms: number) =>
18
18
 
19
19
  export const retryFetch = (
20
20
  url: string,
21
- fetchOptions = {},
21
+ fetchOptions: any,
22
22
  retries = 3,
23
- retryDelay = 1000,
24
23
  timeout: number
25
24
  ): Promise<Response> => {
26
25
  return new Promise((resolve, reject) => {
@@ -32,7 +31,7 @@ export const retryFetch = (
32
31
  .then(res => resolve(res))
33
32
  .catch(async err => {
34
33
  if (n > 0) {
35
- await delay(retryDelay);
34
+ await delay(1000);
36
35
  wrapper(--n);
37
36
  } else {
38
37
  reject(err);
@@ -66,7 +66,7 @@ test('test_feature_state_get_value_mv_values', () => {
66
66
  const mvFeatureState = new FeatureStateModel(myFeature, true, 1);
67
67
  mvFeatureState.multivariateFeatureStateValues = [
68
68
  mvFeatureStateValue1,
69
- mvFeatureStateValue2
69
+ mvFeatureStateValue2,
70
70
  ];
71
71
 
72
72
  mvFeatureState.setValue(mvFeatureControlValue);
@@ -62,6 +62,26 @@ const conditionMatchCases: [string, string | number | boolean, string, boolean][
62
62
  [CONDITION_OPERATORS.NOT_CONTAINS, 'bar', 'baz', true],
63
63
  [CONDITION_OPERATORS.REGEX, 'foo', '[a-z]+', true],
64
64
  [CONDITION_OPERATORS.REGEX, 'FOO', '[a-z]+', false],
65
+ [CONDITION_OPERATORS.EQUAL, "1.0.0", "1.0.0:semver", true],
66
+ [CONDITION_OPERATORS.EQUAL, "1.0.0", "1.0.0:semver", true],
67
+ [CONDITION_OPERATORS.EQUAL, "1.0.0", "1.0.1:semver", false],
68
+ [CONDITION_OPERATORS.NOT_EQUAL, "1.0.0", "1.0.0:semver", false],
69
+ [CONDITION_OPERATORS.NOT_EQUAL, "1.0.0", "1.0.1:semver", true],
70
+ [CONDITION_OPERATORS.GREATER_THAN, "1.0.1", "1.0.0:semver", true],
71
+ [CONDITION_OPERATORS.GREATER_THAN, "1.0.0", "1.0.0-beta:semver", true],
72
+ [CONDITION_OPERATORS.GREATER_THAN, "1.0.1", "1.2.0:semver", false],
73
+ [CONDITION_OPERATORS.GREATER_THAN, "1.0.1", "1.0.1:semver", false],
74
+ [CONDITION_OPERATORS.GREATER_THAN, "1.2.4", "1.2.3-pre.2+build.4:semver", true],
75
+ [CONDITION_OPERATORS.LESS_THAN, "1.0.0", "1.0.1:semver", true],
76
+ [CONDITION_OPERATORS.LESS_THAN, "1.0.0", "1.0.0:semver", false],
77
+ [CONDITION_OPERATORS.LESS_THAN, "1.0.1", "1.0.0:semver", false],
78
+ [CONDITION_OPERATORS.LESS_THAN, "1.0.0-rc.2", "1.0.0-rc.3:semver", true],
79
+ [CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE, "1.0.1", "1.0.0:semver", true],
80
+ [CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE, "1.0.1", "1.2.0:semver", false],
81
+ [CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE, "1.0.1", "1.0.1:semver", true],
82
+ [CONDITION_OPERATORS.LESS_THAN_INCLUSIVE, "1.0.0", "1.0.1:semver", true],
83
+ [CONDITION_OPERATORS.LESS_THAN_INCLUSIVE, "1.0.0", "1.0.0:semver", true],
84
+ [CONDITION_OPERATORS.LESS_THAN_INCLUSIVE, "1.0.1", "1.0.0:semver", false],
65
85
  ['BAD_OP', 'a', 'a', false]
66
86
  ];
67
87