flagsmith-nodejs 2.2.2 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/build/flagsmith-engine/segments/constants.d.ts +6 -0
  2. package/build/flagsmith-engine/segments/constants.js +8 -2
  3. package/build/flagsmith-engine/segments/evaluators.d.ts +2 -1
  4. package/build/flagsmith-engine/segments/evaluators.js +9 -2
  5. package/build/flagsmith-engine/segments/models.d.ts +3 -3
  6. package/build/flagsmith-engine/segments/models.js +26 -1
  7. package/build/sdk/index.d.ts +5 -17
  8. package/build/sdk/index.js +2 -0
  9. package/build/sdk/types.d.ts +19 -1
  10. package/build/sdk/utils.d.ts +2 -2
  11. package/{es6-example → examples/api-proxy}/.babelrc +0 -0
  12. package/{es6-example → examples/api-proxy}/.eslintrc +0 -0
  13. package/examples/api-proxy/README.md +12 -0
  14. package/examples/api-proxy/package-lock.json +10895 -0
  15. package/examples/api-proxy/package.json +57 -0
  16. package/examples/api-proxy/src/api/index.js +42 -0
  17. package/{es6-example → examples/api-proxy}/src/index.js +0 -0
  18. package/examples/basic/.babelrc +3 -0
  19. package/examples/basic/.eslintrc +8 -0
  20. package/examples/basic/README.md +10 -0
  21. package/{es6-example → examples/basic}/package-lock.json +25 -39
  22. package/{es6-example → examples/basic}/package.json +1 -1
  23. package/examples/basic/src/api/index.js +33 -0
  24. package/{example/server → examples/basic/src}/index.js +4 -4
  25. package/examples/caching/.babelrc +3 -0
  26. package/examples/caching/.eslintrc +8 -0
  27. package/examples/caching/README.md +9 -0
  28. package/examples/caching/package-lock.json +6670 -0
  29. package/examples/caching/package.json +56 -0
  30. package/{example/server → examples/caching/src}/api/index.js +7 -11
  31. package/examples/caching/src/index.js +29 -0
  32. package/examples/custom-fetch-agent/.babelrc +3 -0
  33. package/examples/custom-fetch-agent/.eslintrc +8 -0
  34. package/examples/custom-fetch-agent/README.md +12 -0
  35. package/examples/custom-fetch-agent/package-lock.json +6756 -0
  36. package/examples/custom-fetch-agent/package.json +56 -0
  37. package/examples/custom-fetch-agent/src/api/index.js +34 -0
  38. package/examples/custom-fetch-agent/src/index.js +29 -0
  39. package/examples/local-evaluation/.babelrc +3 -0
  40. package/examples/local-evaluation/.eslintrc +8 -0
  41. package/examples/local-evaluation/README.md +18 -0
  42. package/examples/local-evaluation/package-lock.json +6674 -0
  43. package/examples/local-evaluation/package.json +56 -0
  44. package/{es6-example → examples/local-evaluation}/src/api/index.js +2 -13
  45. package/examples/local-evaluation/src/index.js +29 -0
  46. package/flagsmith-engine/segments/constants.ts +7 -1
  47. package/flagsmith-engine/segments/evaluators.ts +10 -6
  48. package/flagsmith-engine/segments/models.ts +15 -5
  49. package/package.json +1 -1
  50. package/sdk/index.ts +8 -15
  51. package/sdk/types.ts +19 -2
  52. package/sdk/utils.ts +2 -2
  53. package/tests/engine/engine-tests/engine-test-data/data/environment_n9fbf9h3v4fFgH3U3ngWhb.json +12591 -0
  54. package/tests/engine/engine-tests/engine-test-data/readme.md +30 -0
  55. package/tests/engine/unit/segments/segment_evaluators.test.ts +27 -0
  56. package/tests/engine/unit/segments/segments_model.test.ts +8 -1
  57. package/tests/sdk/flagsmith.test.ts +25 -10
  58. package/.idea/flagsmith-nodejs-client.iml +0 -12
  59. package/.idea/modules.xml +0 -8
  60. package/.idea/vcs.xml +0 -6
  61. package/es6-example/README.md +0 -62
  62. package/example/LICENSE +0 -0
  63. package/example/Procfile +0 -1
  64. package/example/README.md +0 -15
  65. package/example/package-lock.json +0 -2387
  66. package/example/package.json +0 -27
@@ -12,6 +12,9 @@ export declare const NOT_CONTAINS = "NOT_CONTAINS";
12
12
  export declare const NOT_EQUAL = "NOT_EQUAL";
13
13
  export declare const REGEX = "REGEX";
14
14
  export declare const PERCENTAGE_SPLIT = "PERCENTAGE_SPLIT";
15
+ export declare const IS_SET = "IS_SET";
16
+ export declare const IS_NOT_SET = "IS_NOT_SET";
17
+ export declare const MODULO = "MODULO";
15
18
  export declare const CONDITION_OPERATORS: {
16
19
  EQUAL: string;
17
20
  GREATER_THAN: string;
@@ -23,4 +26,7 @@ export declare const CONDITION_OPERATORS: {
23
26
  NOT_EQUAL: string;
24
27
  REGEX: string;
25
28
  PERCENTAGE_SPLIT: string;
29
+ IS_SET: string;
30
+ IS_NOT_SET: string;
31
+ MODULO: string;
26
32
  };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CONDITION_OPERATORS = exports.PERCENTAGE_SPLIT = exports.REGEX = exports.NOT_EQUAL = exports.NOT_CONTAINS = exports.GREATER_THAN_INCLUSIVE = exports.CONTAINS = exports.LESS_THAN_INCLUSIVE = exports.LESS_THAN = exports.GREATER_THAN = exports.EQUAL = exports.RULE_TYPES = exports.NONE_RULE = exports.ANY_RULE = exports.ALL_RULE = void 0;
3
+ exports.CONDITION_OPERATORS = exports.MODULO = exports.IS_NOT_SET = exports.IS_SET = exports.PERCENTAGE_SPLIT = exports.REGEX = exports.NOT_EQUAL = exports.NOT_CONTAINS = exports.GREATER_THAN_INCLUSIVE = exports.CONTAINS = exports.LESS_THAN_INCLUSIVE = exports.LESS_THAN = exports.GREATER_THAN = exports.EQUAL = exports.RULE_TYPES = exports.NONE_RULE = exports.ANY_RULE = exports.ALL_RULE = void 0;
4
4
  // Segment Rules
5
5
  exports.ALL_RULE = 'ALL';
6
6
  exports.ANY_RULE = 'ANY';
@@ -17,6 +17,9 @@ exports.NOT_CONTAINS = 'NOT_CONTAINS';
17
17
  exports.NOT_EQUAL = 'NOT_EQUAL';
18
18
  exports.REGEX = 'REGEX';
19
19
  exports.PERCENTAGE_SPLIT = 'PERCENTAGE_SPLIT';
20
+ exports.IS_SET = 'IS_SET';
21
+ exports.IS_NOT_SET = 'IS_NOT_SET';
22
+ exports.MODULO = 'MODULO';
20
23
  exports.CONDITION_OPERATORS = {
21
24
  EQUAL: exports.EQUAL,
22
25
  GREATER_THAN: exports.GREATER_THAN,
@@ -27,5 +30,8 @@ exports.CONDITION_OPERATORS = {
27
30
  NOT_CONTAINS: exports.NOT_CONTAINS,
28
31
  NOT_EQUAL: exports.NOT_EQUAL,
29
32
  REGEX: exports.REGEX,
30
- PERCENTAGE_SPLIT: exports.PERCENTAGE_SPLIT
33
+ PERCENTAGE_SPLIT: exports.PERCENTAGE_SPLIT,
34
+ IS_SET: exports.IS_SET,
35
+ IS_NOT_SET: exports.IS_NOT_SET,
36
+ MODULO: exports.MODULO
31
37
  };
@@ -1,6 +1,7 @@
1
1
  import { EnvironmentModel } from '../environments/models';
2
2
  import { IdentityModel } from '../identities/models';
3
3
  import { TraitModel } from '../identities/traits/models';
4
- import { SegmentModel } from './models';
4
+ import { SegmentConditionModel, SegmentModel } from './models';
5
5
  export declare function getIdentitySegments(environment: EnvironmentModel, identity: IdentityModel, overrideTraits?: TraitModel[]): SegmentModel[];
6
6
  export declare function evaluateIdentityInSegment(identity: IdentityModel, segment: SegmentModel, overrideTraits?: TraitModel[]): boolean;
7
+ export declare function traitsMatchSegmentCondition(identityTraits: TraitModel[], condition: SegmentConditionModel, segmentId: number | string, identityId: number | string): boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.evaluateIdentityInSegment = exports.getIdentitySegments = void 0;
3
+ exports.traitsMatchSegmentCondition = exports.evaluateIdentityInSegment = exports.getIdentitySegments = void 0;
4
4
  var hashing_1 = require("../utils/hashing");
5
5
  var constants_1 = require("./constants");
6
6
  function getIdentitySegments(environment, identity, overrideTraits) {
@@ -29,9 +29,16 @@ function traitsMatchSegmentRule(identityTraits, rule, segmentId, identityId) {
29
29
  }
30
30
  function traitsMatchSegmentCondition(identityTraits, condition, segmentId, identityId) {
31
31
  if (condition.operator == constants_1.PERCENTAGE_SPLIT) {
32
- return (0, hashing_1.getHashedPercentateForObjIds)([segmentId, identityId]) <= parseFloat(condition.value);
32
+ return (0, hashing_1.getHashedPercentateForObjIds)([segmentId, identityId]) <= parseFloat(String(condition.value));
33
33
  }
34
34
  var traits = identityTraits.filter(function (t) { return t.traitKey === condition.property_; });
35
35
  var trait = traits.length > 0 ? traits[0] : undefined;
36
+ if (condition.operator === constants_1.IS_SET) {
37
+ return !!trait;
38
+ }
39
+ else if (condition.operator === constants_1.IS_NOT_SET) {
40
+ return trait == undefined;
41
+ }
36
42
  return trait ? condition.matchesTraitValue(trait.traitValue) : false;
37
43
  }
44
+ exports.traitsMatchSegmentCondition = traitsMatchSegmentCondition;
@@ -15,9 +15,9 @@ export declare class SegmentConditionModel {
15
15
  [key: string]: string;
16
16
  };
17
17
  operator: string;
18
- value: string;
19
- property_: string | undefined;
20
- constructor(operator: string, value: string, property?: string);
18
+ value: string | null | undefined;
19
+ property_: string | null | undefined;
20
+ constructor(operator: string, value?: string | null | undefined, property?: string | null | undefined);
21
21
  matchesTraitValue(traitValue: any): any;
22
22
  }
23
23
  export declare class SegmentRuleModel {
@@ -10,6 +10,22 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
17
+ try {
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
+ }
20
+ catch (error) { e = { error: error }; }
21
+ finally {
22
+ try {
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
24
+ }
25
+ finally { if (e) throw e.error; }
26
+ }
27
+ return ar;
28
+ };
13
29
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
30
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
31
  };
@@ -52,6 +68,7 @@ var SegmentConditionModel = /** @class */ (function () {
52
68
  this.EXCEPTION_OPERATOR_METHODS = (_a = {},
53
69
  _a[constants_1.NOT_CONTAINS] = 'evaluateNotContains',
54
70
  _a[constants_1.REGEX] = 'evaluateRegex',
71
+ _a[constants_1.MODULO] = 'evaluateModulo',
55
72
  _a);
56
73
  this.operator = operator;
57
74
  this.value = value;
@@ -64,7 +81,15 @@ var SegmentConditionModel = /** @class */ (function () {
64
81
  return !traitValue.includes(_this.value);
65
82
  },
66
83
  evaluateRegex: function (traitValue) {
67
- return !!traitValue.match(new RegExp(_this.value));
84
+ return !!_this.value && !!traitValue.match(new RegExp(_this.value));
85
+ },
86
+ evaluateModulo: function (traitValue) {
87
+ if (isNaN(parseFloat(traitValue)) || !_this.value) {
88
+ return false;
89
+ }
90
+ var parts = (_this.value).split("|");
91
+ var _a = __read([parseFloat(parts[0]), parseFloat(parts[1])], 2), divisor = _a[0], reminder = _a[1];
92
+ return traitValue % divisor === reminder;
68
93
  }
69
94
  };
70
95
  // TODO: move this logic to the evaluator module
@@ -1,13 +1,14 @@
1
+ import { RequestInit } from "node-fetch";
1
2
  import { EnvironmentModel } from '../flagsmith-engine/environments/models';
2
3
  import { DefaultFlag, Flags } from './models';
3
4
  import { EnvironmentDataPollingManager } from './polling_manager';
4
5
  import { SegmentModel } from '../flagsmith-engine/segments/models';
5
- import { FlagsmithCache } from './types';
6
+ import { FlagsmithConfig } from './types';
6
7
  export { AnalyticsProcessor } from './analytics';
7
8
  export { FlagsmithAPIError, FlagsmithClientError } from './errors';
8
9
  export { DefaultFlag, Flags } from './models';
9
10
  export { EnvironmentDataPollingManager } from './polling_manager';
10
- export { FlagsmithCache } from './types';
11
+ export { FlagsmithCache, FlagsmithConfig } from './types';
11
12
  export declare class Flagsmith {
12
13
  environmentKey?: string;
13
14
  apiUrl: string;
@@ -15,6 +16,7 @@ export declare class Flagsmith {
15
16
  [key: string]: any;
16
17
  };
17
18
  requestTimeoutSeconds?: number;
19
+ agent: RequestInit['agent'];
18
20
  requestTimeoutMs?: number;
19
21
  enableLocalEvaluation?: boolean;
20
22
  environmentRefreshIntervalSeconds: number;
@@ -59,21 +61,7 @@ export declare class Flagsmith {
59
61
  flags cannot be retrieved from the API or a non existent feature is
60
62
  requested
61
63
  */
62
- constructor(data: {
63
- environmentKey: string;
64
- apiUrl?: string;
65
- customHeaders?: {
66
- [key: string]: any;
67
- };
68
- requestTimeoutSeconds?: number;
69
- enableLocalEvaluation?: boolean;
70
- environmentRefreshIntervalSeconds?: number;
71
- retries?: number;
72
- enableAnalytics?: boolean;
73
- defaultFlagHandler?: (featureName: string) => DefaultFlag;
74
- cache?: FlagsmithCache;
75
- onEnvironmentChange?: (error: Error | null, result: EnvironmentModel) => void;
76
- });
64
+ constructor(data: FlagsmithConfig);
77
65
  /**
78
66
  * Get all the default for flags for the current environment.
79
67
  *
@@ -121,6 +121,7 @@ var Flagsmith = /** @class */ (function () {
121
121
  this.enableLocalEvaluation = false;
122
122
  this.environmentRefreshIntervalSeconds = 60;
123
123
  this.enableAnalytics = false;
124
+ this.agent = data.agent;
124
125
  this.environmentKey = data.environmentKey;
125
126
  this.apiUrl = data.apiUrl || this.apiUrl;
126
127
  this.customHeaders = data.customHeaders;
@@ -348,6 +349,7 @@ var Flagsmith = /** @class */ (function () {
348
349
  }
349
350
  }
350
351
  return [4 /*yield*/, (0, utils_1.retryFetch)(url, {
352
+ agent: this.agent,
351
353
  method: method,
352
354
  timeout: this.requestTimeoutMs || undefined,
353
355
  body: JSON.stringify(body),
@@ -1,7 +1,25 @@
1
- import { Flags } from "./models";
1
+ import { DefaultFlag, Flags } from "./models";
2
+ import { EnvironmentModel } from "../flagsmith-engine";
3
+ import { RequestInit } from "node-fetch";
2
4
  export interface FlagsmithCache {
3
5
  get(key: string): Promise<Flags | undefined> | undefined;
4
6
  set(key: string, value: Flags, ttl: string | number): boolean | Promise<boolean>;
5
7
  has(key: string): boolean | Promise<boolean>;
6
8
  [key: string]: any;
7
9
  }
10
+ export interface FlagsmithConfig {
11
+ environmentKey: string;
12
+ apiUrl?: string;
13
+ agent?: RequestInit['agent'];
14
+ customHeaders?: {
15
+ [key: string]: any;
16
+ };
17
+ requestTimeoutSeconds?: number;
18
+ enableLocalEvaluation?: boolean;
19
+ environmentRefreshIntervalSeconds?: number;
20
+ retries?: number;
21
+ enableAnalytics?: boolean;
22
+ defaultFlagHandler?: (featureName: string) => DefaultFlag;
23
+ cache?: FlagsmithCache;
24
+ onEnvironmentChange?: (error: Error | null, result: EnvironmentModel) => void;
25
+ }
@@ -1,4 +1,4 @@
1
- import { Response } from 'node-fetch';
1
+ import { RequestInit, Response } from 'node-fetch';
2
2
  export declare function generateIdentitiesData(identifier: string, traits: {
3
3
  [key: string]: any;
4
4
  }): {
@@ -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: any, retries?: number, timeout?: number | undefined) => Promise<Response>;
12
+ export declare const retryFetch: (url: string, fetchOptions: RequestInit, retries?: number, timeout?: number | undefined) => Promise<Response>;
File without changes
File without changes
@@ -0,0 +1,12 @@
1
+ <img width="100%" src="https://github.com/Flagsmith/flagsmith/raw/main/static-files/hero.png"/>
2
+
3
+ # Flagsmith Node.js example with custom fetch agent
4
+
5
+ This is a simple Flagsmith Node.js example in local evaluation modes with a custom fetch agent. Using a custom fetch agent lets allows you to specify networking related options such as keep alive and socket timeout, read more on this [here](https://github.com/node-fetch/node-fetch#custom-agent).
6
+
7
+ ## Running the example
8
+
9
+ ```shell
10
+ npm i
11
+ npm run dev
12
+ ```