flagsmith-nodejs 1.0.9 → 2.0.0-beta.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.
Files changed (127) hide show
  1. package/.github/workflows/pull_request.yaml +33 -0
  2. package/.gitmodules +3 -0
  3. package/.husky/pre-commit +6 -0
  4. package/.idea/flagsmith-nodejs-client.iml +12 -0
  5. package/.idea/modules.xml +8 -0
  6. package/.idea/vcs.xml +6 -0
  7. package/.prettierignore +1 -0
  8. package/.prettierrc.js +9 -0
  9. package/CONTRIBUTING.md +5 -4
  10. package/{LICENCE.md → LICENCE} +5 -6
  11. package/README.md +1 -1
  12. package/build/flagsmith-engine/environments/integrations/models.d.ts +4 -0
  13. package/build/flagsmith-engine/environments/integrations/models.js +8 -0
  14. package/build/flagsmith-engine/environments/models.d.ts +25 -0
  15. package/build/flagsmith-engine/environments/models.js +40 -0
  16. package/build/flagsmith-engine/environments/util.d.ts +3 -0
  17. package/build/flagsmith-engine/environments/util.js +19 -0
  18. package/build/flagsmith-engine/features/constants.d.ts +4 -0
  19. package/build/flagsmith-engine/features/constants.js +7 -0
  20. package/build/flagsmith-engine/features/models.d.ts +32 -0
  21. package/build/flagsmith-engine/features/models.js +85 -0
  22. package/build/flagsmith-engine/features/util.d.ts +3 -0
  23. package/build/flagsmith-engine/features/util.js +20 -0
  24. package/build/flagsmith-engine/identities/models.d.ts +15 -0
  25. package/build/flagsmith-engine/identities/models.js +47 -0
  26. package/build/flagsmith-engine/identities/traits/models.d.ts +5 -0
  27. package/build/flagsmith-engine/identities/traits/models.js +12 -0
  28. package/build/flagsmith-engine/identities/util.d.ts +4 -0
  29. package/build/flagsmith-engine/identities/util.js +22 -0
  30. package/build/flagsmith-engine/index.d.ts +8 -0
  31. package/build/flagsmith-engine/index.js +60 -0
  32. package/build/flagsmith-engine/organisations/models.d.ts +9 -0
  33. package/build/flagsmith-engine/organisations/models.js +21 -0
  34. package/build/flagsmith-engine/organisations/util.d.ts +2 -0
  35. package/build/flagsmith-engine/organisations/util.js +8 -0
  36. package/build/flagsmith-engine/projects/models.d.ts +10 -0
  37. package/build/flagsmith-engine/projects/models.js +18 -0
  38. package/build/flagsmith-engine/projects/util.d.ts +2 -0
  39. package/build/flagsmith-engine/projects/util.js +15 -0
  40. package/build/flagsmith-engine/segments/constants.d.ts +26 -0
  41. package/build/flagsmith-engine/segments/constants.js +31 -0
  42. package/build/flagsmith-engine/segments/evaluators.d.ts +6 -0
  43. package/build/flagsmith-engine/segments/evaluators.js +29 -0
  44. package/build/flagsmith-engine/segments/models.d.ts +31 -0
  45. package/build/flagsmith-engine/segments/models.js +83 -0
  46. package/build/flagsmith-engine/segments/util.d.ts +4 -0
  47. package/build/flagsmith-engine/segments/util.js +23 -0
  48. package/build/flagsmith-engine/utils/collections.d.ts +4 -0
  49. package/build/flagsmith-engine/utils/collections.js +16 -0
  50. package/build/flagsmith-engine/utils/hashing/index.d.ts +1 -0
  51. package/build/flagsmith-engine/utils/hashing/index.js +56 -0
  52. package/build/flagsmith-engine/utils/index.d.ts +1 -0
  53. package/build/flagsmith-engine/utils/index.js +14 -0
  54. package/build/index.d.ts +2 -0
  55. package/build/index.js +7 -0
  56. package/build/sdk/analytics.d.ts +28 -0
  57. package/build/sdk/analytics.js +81 -0
  58. package/build/sdk/errors.d.ts +4 -0
  59. package/build/sdk/errors.js +9 -0
  60. package/build/sdk/index.d.ts +99 -0
  61. package/build/sdk/index.js +221 -0
  62. package/build/sdk/models.d.ts +55 -0
  63. package/build/sdk/models.js +102 -0
  64. package/build/sdk/polling_manager.d.ts +9 -0
  65. package/build/sdk/polling_manager.js +31 -0
  66. package/build/sdk/utils.d.ts +12 -0
  67. package/build/sdk/utils.js +45 -0
  68. package/example/README.md +8 -14
  69. package/example/package-lock.json +12 -12
  70. package/example/package.json +4 -4
  71. package/example/server/api/index.js +19 -22
  72. package/example/server/index.js +4 -9
  73. package/flagsmith-engine/environments/integrations/models.ts +4 -0
  74. package/flagsmith-engine/environments/models.ts +50 -0
  75. package/flagsmith-engine/environments/util.ts +29 -0
  76. package/flagsmith-engine/features/constants.ts +4 -0
  77. package/flagsmith-engine/features/models.ts +105 -0
  78. package/flagsmith-engine/features/util.ts +38 -0
  79. package/flagsmith-engine/identities/models.ts +60 -0
  80. package/flagsmith-engine/identities/traits/models.ts +9 -0
  81. package/flagsmith-engine/identities/util.ts +30 -0
  82. package/flagsmith-engine/index.ts +92 -0
  83. package/flagsmith-engine/organisations/models.ts +25 -0
  84. package/flagsmith-engine/organisations/util.ts +11 -0
  85. package/flagsmith-engine/projects/models.ts +23 -0
  86. package/flagsmith-engine/projects/util.ts +18 -0
  87. package/flagsmith-engine/segments/constants.ts +31 -0
  88. package/flagsmith-engine/segments/evaluators.ts +72 -0
  89. package/flagsmith-engine/segments/models.ts +103 -0
  90. package/flagsmith-engine/segments/util.ts +29 -0
  91. package/flagsmith-engine/utils/collections.ts +14 -0
  92. package/flagsmith-engine/utils/hashing/index.ts +57 -0
  93. package/flagsmith-engine/utils/index.ts +10 -0
  94. package/index.ts +3 -0
  95. package/jest.config.js +5 -0
  96. package/package.json +24 -3
  97. package/sdk/analytics.ts +88 -0
  98. package/sdk/errors.ts +2 -0
  99. package/sdk/index.ts +282 -0
  100. package/sdk/models.ts +143 -0
  101. package/sdk/polling_manager.ts +31 -0
  102. package/sdk/utils.ts +45 -0
  103. package/tests/engine/e2e/engine.test.ts +51 -0
  104. package/tests/engine/engine-tests/engine-test-data/data/environment_n9fbf9h3v4fFgH3U3ngWhb.json +12393 -0
  105. package/tests/engine/engine-tests/engine-test-data/readme.md +30 -0
  106. package/tests/engine/unit/egine.test.ts +96 -0
  107. package/tests/engine/unit/environments/builder.test.ts +148 -0
  108. package/tests/engine/unit/environments/models.test.ts +49 -0
  109. package/tests/engine/unit/features/models.test.ts +72 -0
  110. package/tests/engine/unit/identities/identities_builders.test.ts +85 -0
  111. package/tests/engine/unit/identities/identities_models.test.ts +105 -0
  112. package/tests/engine/unit/organization/models.test.ts +12 -0
  113. package/tests/engine/unit/segments/segments_model.test.ts +101 -0
  114. package/tests/engine/unit/segments/util.ts +151 -0
  115. package/tests/engine/unit/utils.ts +114 -0
  116. package/tests/index.js +0 -0
  117. package/tests/sdk/analytics.test.ts +43 -0
  118. package/tests/sdk/data/environment.json +33 -0
  119. package/tests/sdk/data/flags.json +20 -0
  120. package/tests/sdk/data/identities.json +29 -0
  121. package/tests/sdk/flagsmith.test.ts +184 -0
  122. package/tests/sdk/polling.test.ts +34 -0
  123. package/tests/sdk/utils.ts +39 -0
  124. package/tsconfig.json +19 -0
  125. package/flagsmith-core.js +0 -238
  126. package/index.d.ts +0 -78
  127. package/index.js +0 -5
@@ -0,0 +1,103 @@
1
+ import { FeatureStateModel } from '../features/models';
2
+ import { getCastingFunction as getCastingFunction } from '../utils';
3
+ import {
4
+ ALL_RULE,
5
+ ANY_RULE,
6
+ NONE_RULE,
7
+ NOT_CONTAINS,
8
+ REGEX,
9
+ CONDITION_OPERATORS
10
+ } from './constants';
11
+
12
+ export const all = (iterable: Array<any>) => iterable.filter(e => !!e).length === iterable.length;
13
+ export const any = (iterable: Array<any>) => iterable.filter(e => !!e).length > 0;
14
+
15
+ export const matchingFunctions = {
16
+ [CONDITION_OPERATORS.EQUAL]: (thisValue: any, otherValue: any) => thisValue == otherValue,
17
+ [CONDITION_OPERATORS.GREATER_THAN]: (thisValue: any, otherValue: any) => otherValue > thisValue,
18
+ [CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: (thisValue: any, otherValue: any) =>
19
+ otherValue >= thisValue,
20
+ [CONDITION_OPERATORS.LESS_THAN]: (thisValue: any, otherValue: any) => thisValue > otherValue,
21
+ [CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (thisValue: any, otherValue: any) =>
22
+ thisValue >= otherValue,
23
+ [CONDITION_OPERATORS.NOT_EQUAL]: (thisValue: any, otherValue: any) => thisValue != otherValue,
24
+ [CONDITION_OPERATORS.CONTAINS]: (thisValue: any, otherValue: any) =>
25
+ otherValue.includes(thisValue),
26
+ [CONDITION_OPERATORS.NOT_CONTAINS]: (thisValue: any, otherValue: any) =>
27
+ !otherValue.includes(thisValue)
28
+ };
29
+
30
+ export class SegmentConditionModel {
31
+ EXCEPTION_OPERATOR_METHODS: { [key: string]: string } = {
32
+ [NOT_CONTAINS]: 'evaluateNotContains',
33
+ [REGEX]: 'evaluateRegex'
34
+ };
35
+
36
+ operator: string;
37
+ value: string;
38
+ property_: string | undefined;
39
+
40
+ constructor(operator: string, value: string, property?: string) {
41
+ this.operator = operator;
42
+ this.value = value;
43
+ this.property_ = property;
44
+ }
45
+
46
+ matchesTraitValue(traitValue: any) {
47
+ const evaluators: { [key: string]: CallableFunction } = {
48
+ evaluateNotContains: (traitValue: any) => {
49
+ return !traitValue.includes(this.value);
50
+ },
51
+ evaluateRegex: (traitValue: any) => {
52
+ return !!traitValue.match(new RegExp(this.value));
53
+ }
54
+ };
55
+
56
+ // TODO: move this logic to the evaluator module
57
+ if (this.EXCEPTION_OPERATOR_METHODS[this.operator]) {
58
+ const evaluatorFunction = evaluators[this.EXCEPTION_OPERATOR_METHODS[this.operator]];
59
+ return evaluatorFunction(traitValue);
60
+ }
61
+
62
+ const defaultFunction = (x: any, y: any) => false;
63
+
64
+ const matchingFunction = matchingFunctions[this.operator] || defaultFunction;
65
+
66
+ const castToTypeOfTraitValue = getCastingFunction(traitValue);
67
+ return matchingFunction(castToTypeOfTraitValue(this.value), traitValue);
68
+ }
69
+ }
70
+
71
+ export class SegmentRuleModel {
72
+ type: string;
73
+ rules: SegmentRuleModel[] = [];
74
+ conditions: SegmentConditionModel[] = [];
75
+
76
+ constructor(type: string) {
77
+ this.type = type;
78
+ }
79
+
80
+ static none(iterable: Array<any>) {
81
+ return iterable.filter(e => !!e).length === 0;
82
+ }
83
+
84
+ matchingFunction(): CallableFunction {
85
+ return {
86
+ [ANY_RULE]: any,
87
+ [ALL_RULE]: all,
88
+ [NONE_RULE]: SegmentRuleModel.none
89
+ }[this.type] as CallableFunction;
90
+ }
91
+ }
92
+
93
+ export class SegmentModel {
94
+ id: number;
95
+ name: string;
96
+ rules: SegmentRuleModel[] = [];
97
+ featureStates: FeatureStateModel[] = [];
98
+
99
+ constructor(id: number, name: string) {
100
+ this.id = id;
101
+ this.name = name;
102
+ }
103
+ }
@@ -0,0 +1,29 @@
1
+ import { buildFeatureStateModel } from '../features/util';
2
+ import { SegmentConditionModel, SegmentModel, SegmentRuleModel } from './models';
3
+
4
+ export function buildSegmentConditionModel(segmentConditionJSON: any): SegmentConditionModel {
5
+ return new SegmentConditionModel(
6
+ segmentConditionJSON.operator,
7
+ segmentConditionJSON.value,
8
+ segmentConditionJSON.property_
9
+ );
10
+ }
11
+
12
+ export function buildSegmentRuleModel(ruleModelJSON: any): SegmentRuleModel {
13
+ const ruleModel = new SegmentRuleModel(ruleModelJSON.type);
14
+
15
+ ruleModel.rules = ruleModelJSON.rules.map((r: any) => buildSegmentRuleModel(r));
16
+ ruleModel.conditions = ruleModelJSON.conditions.map((c: any) => buildSegmentConditionModel(c));
17
+ return ruleModel;
18
+ }
19
+
20
+ export function buildSegmentModel(segmentModelJSON: any): SegmentModel {
21
+ const model = new SegmentModel(segmentModelJSON.id, segmentModelJSON.name);
22
+
23
+ model.featureStates = segmentModelJSON['feature_states'].map((fs: any) =>
24
+ buildFeatureStateModel(fs)
25
+ );
26
+ model.rules = segmentModelJSON['rules'].map((r: any) => buildSegmentRuleModel(r));
27
+
28
+ return model;
29
+ }
@@ -0,0 +1,14 @@
1
+ import { FeatureStateModel } from '../features/models';
2
+
3
+ export class IdentityFeaturesList extends Array<FeatureStateModel> {
4
+ public push(...e: FeatureStateModel[]): number {
5
+ for (const [_, item] of e.entries()) {
6
+ for (const [k, v] of this.entries()) {
7
+ if (v.djangoID === item.djangoID) {
8
+ throw new Error('feature state for this feature already exists');
9
+ }
10
+ }
11
+ }
12
+ return super.push(...e);
13
+ }
14
+ }
@@ -0,0 +1,57 @@
1
+ import md5 from 'md5';
2
+ import bigInt from 'big-integer';
3
+
4
+ // def get_hashed_percentage_for_object_ids(
5
+ // object_ids: typing.Iterable[typing.Any], iterations: int = 1
6
+ // ) -> float:
7
+ // """
8
+ // Given a list of object ids, get a floating point number between 0 and 1 based on
9
+ // the hash of those ids. This should give the same value every time for any
10
+ // list of ids.
11
+
12
+ // :param object_ids: list of object ids to calculate the has for
13
+ // :param iterations: num times to include each id in the generated string to hash
14
+ // :return: (float) number between 0 (inclusive) and 100 (exclusive)
15
+ // """
16
+
17
+ const makeRepeated = (arr: Array<any>, repeats: number) =>
18
+ Array.from({ length: repeats }, () => arr).flat();
19
+
20
+ function h2d(s: any): string {
21
+ function add(x: any, y: any) {
22
+ var c = 0,
23
+ r = [];
24
+ var x = x.split('').map(Number);
25
+ var y = y.split('').map(Number);
26
+ while (x.length || y.length) {
27
+ var s = (x.pop() || 0) + (y.pop() || 0) + c;
28
+ r.unshift(s < 10 ? s : s - 10);
29
+ c = s < 10 ? 0 : 1;
30
+ }
31
+ if (c) r.unshift(c);
32
+ return r.join('');
33
+ }
34
+
35
+ var dec = '0';
36
+ s.split('').forEach(function (chr: any) {
37
+ var n = parseInt(chr, 16);
38
+ for (var t = 8; t; t >>= 1) {
39
+ dec = add(dec, dec);
40
+ if (n & t) dec = add(dec, '1');
41
+ }
42
+ });
43
+ return dec;
44
+ }
45
+
46
+ export function getHashedPercentateForObjIds(objectIds: Array<any>, iterations = 1): number {
47
+ let to_hash = makeRepeated(objectIds, iterations).join(',');
48
+ const hashedValue = md5(to_hash);
49
+ const hashedInt = bigInt(h2d(hashedValue));
50
+ const value = (hashedInt.mod(9999).toJSNumber() / 9998) * 100;
51
+
52
+ if (value === 100) {
53
+ return getHashedPercentateForObjIds(objectIds, iterations + 1);
54
+ }
55
+
56
+ return value;
57
+ }
@@ -0,0 +1,10 @@
1
+ export function getCastingFunction(input: any): CallableFunction {
2
+ switch (typeof input) {
3
+ case 'boolean':
4
+ return (x: any) => !['False', 'false'].includes(x);
5
+ case 'number':
6
+ return (x: any) => parseFloat(x);
7
+ default:
8
+ return (x: any) => String(x);
9
+ }
10
+ }
package/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import Flagsmith from './sdk';
2
+
3
+ export default Flagsmith;
package/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2
+ module.exports = {
3
+ preset: 'ts-jest',
4
+ testEnvironment: 'node'
5
+ };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "flagsmith-nodejs",
3
- "version": "1.0.9",
3
+ "version": "2.0.0-beta.1",
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
- "main": "index.js",
5
+ "main": "build/index.js",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/SolidStateGroup/bullet-train-nodejs-client"
@@ -39,7 +39,28 @@
39
39
  }
40
40
  ],
41
41
  "license": "MIT",
42
+ "scripts": {
43
+ "lint": "prettier --write .",
44
+ "test": "jest --coverage --coverageReporters='text'",
45
+ "build": "tsc",
46
+ "prepare": "husky install"
47
+ },
42
48
  "dependencies": {
43
- "node-fetch": "^2.1.2"
49
+ "big-integer": "^1.6.51",
50
+ "md5": "^2.3.0",
51
+ "node-fetch": "^2.1.2",
52
+ "uuid": "^8.3.2"
53
+ },
54
+ "devDependencies": {
55
+ "@types/jest": "^27.4.1",
56
+ "@types/md5": "^2.3.2",
57
+ "@types/node-fetch": "^2.6.1",
58
+ "@types/uuid": "^8.3.4",
59
+ "esbuild": "^0.14.25",
60
+ "husky": "^7.0.4",
61
+ "jest": "^27.5.1",
62
+ "prettier": "^2.2.1",
63
+ "ts-jest": "^27.1.3",
64
+ "typescript": "^4.6.2"
44
65
  }
45
66
  }
@@ -0,0 +1,88 @@
1
+ import fetch from 'node-fetch';
2
+
3
+ const ANALYTICS_ENDPOINT = 'analytics/flags/';
4
+
5
+ // Used to control how often we send data(in seconds)
6
+ const ANALYTICS_TIMER = 10;
7
+
8
+ const delay = (ms: number) => new Promise(resolve => setTimeout(() => resolve(undefined), ms));
9
+
10
+ const retryFetch = (
11
+ url: string,
12
+ fetchOptions = {},
13
+ retries = 3,
14
+ retryDelay = 1000,
15
+ timeout: number
16
+ ) => {
17
+ return new Promise((resolve, reject) => {
18
+ // check for timeout
19
+ if (timeout) setTimeout(() => reject('error: timeout'), timeout);
20
+
21
+ const wrapper = (n: number) => {
22
+ fetch(url, fetchOptions)
23
+ .then(res => resolve(res))
24
+ .catch(async err => {
25
+ if (n > 0) {
26
+ await delay(retryDelay);
27
+ wrapper(--n);
28
+ } else {
29
+ reject(err);
30
+ }
31
+ });
32
+ };
33
+
34
+ wrapper(retries);
35
+ });
36
+ };
37
+
38
+ export class AnalyticsProcessor {
39
+ private analyticsEndpoint: string;
40
+ private environmentKey: string;
41
+ private lastFlushed: number;
42
+ analyticsData: { [key: string]: any };
43
+ private timeout: number = 3;
44
+ /**
45
+ * AnalyticsProcessor is used to track how often individual Flags are evaluated within
46
+ * the Flagsmith SDK. Docs: https://docs.flagsmith.com/advanced-use/flag-analytics.
47
+ *
48
+ * @param data.environmentKey environment key obtained from the Flagsmith UI
49
+ * @param data.baseApiUrl base api url to override when using self hosted version
50
+ * @param data.timeout used to tell requests to stop waiting for a response after a
51
+ given number of seconds
52
+ */
53
+ constructor(data: { environmentKey: string; baseApiUrl: string; timeout?: number }) {
54
+ this.analyticsEndpoint = data.baseApiUrl + ANALYTICS_ENDPOINT;
55
+ this.environmentKey = data.environmentKey;
56
+ this.lastFlushed = Date.now();
57
+ this.analyticsData = {};
58
+ this.timeout = data.timeout || this.timeout;
59
+ }
60
+ /**
61
+ * Sends all the collected data to the api asynchronously and resets the timer
62
+ */
63
+ async flush() {
64
+ if (!Object.keys(this.analyticsData).length) {
65
+ return;
66
+ }
67
+
68
+ await fetch(this.analyticsEndpoint, {
69
+ method: 'POST',
70
+ body: JSON.stringify(this.analyticsData),
71
+ timeout: this.timeout,
72
+ headers: {
73
+ 'Content-Type': 'application/json',
74
+ 'X-Environment-Key': this.environmentKey
75
+ }
76
+ });
77
+
78
+ this.analyticsData = {};
79
+ this.lastFlushed = Date.now();
80
+ }
81
+
82
+ trackFeature(featureId: number) {
83
+ this.analyticsData[featureId] = (this.analyticsData[featureId] || 0) + 1;
84
+ if (Date.now() - this.lastFlushed > ANALYTICS_TIMER * 1000) {
85
+ this.flush();
86
+ }
87
+ }
88
+ }
package/sdk/errors.ts ADDED
@@ -0,0 +1,2 @@
1
+ export class FlagsmithClientError extends Error {}
2
+ export class FlagsmithAPIError extends Error {}
package/sdk/index.ts ADDED
@@ -0,0 +1,282 @@
1
+ import { getEnvironmentFeatureStates, getIdentityFeatureStates } from '../flagsmith-engine';
2
+ import { EnvironmentModel } from '../flagsmith-engine/environments/models';
3
+ import { buildEnvironmentModel } from '../flagsmith-engine/environments/util';
4
+ import { IdentityModel } from '../flagsmith-engine/identities/models';
5
+ import { TraitModel } from '../flagsmith-engine/identities/traits/models';
6
+
7
+ import { AnalyticsProcessor } from './analytics';
8
+ import { FlagsmithAPIError, FlagsmithClientError } from './errors';
9
+
10
+ import { DefaultFlag, Flags } from './models';
11
+ import { EnvironmentDataPollingManager } from './polling_manager';
12
+ import { generateIdentitiesData, retryFetch } from './utils';
13
+
14
+ const DEFAULT_API_URL = 'https://api.flagsmith.com/api/v1/';
15
+
16
+ export class Flagsmith {
17
+ environmentKey?: string;
18
+ apiUrl: string = DEFAULT_API_URL;
19
+ customHeaders?: { [key: string]: any };
20
+ requestTimeoutSeconds?: number;
21
+ enableLocalEvaluation?: boolean = false;
22
+ environmentRefreshIntervalSeconds: number = 60;
23
+ retries?: any;
24
+ enableAnalytics: boolean = false;
25
+ defaultFlagHandler?: (featureName: string) => DefaultFlag;
26
+
27
+ environmentFlagsUrl: string;
28
+ identitiesUrl: string;
29
+ environmentUrl: string;
30
+
31
+ environmentDataPollingManager?: EnvironmentDataPollingManager;
32
+ environment?: EnvironmentModel;
33
+ private analyticsProcessor?: AnalyticsProcessor;
34
+ /**
35
+ * A Flagsmith client.
36
+ *
37
+ * Provides an interface for interacting with the Flagsmith http API.
38
+ * Basic Usage::
39
+ *
40
+ * import flagsmith from Flagsmith
41
+ * const flagsmith = new Flagsmith({environmentKey: '<your API key>'});
42
+ * const environmentFlags = flagsmith.getEnvironmentFlags();
43
+ * const featureEnabled = environmentFlags.isFeatureEnabled('foo');
44
+ * const identityFlags = flagsmith.getIdentityFlags('identifier', {'foo': 'bar'});
45
+ * const featureEnabledForIdentity = identityFlags.isFeatureEnabled("foo")
46
+ *
47
+ * @param {string} data.environmentKey: The environment key obtained from Flagsmith interface
48
+ @param {string} data.apiUrl: Override the URL of the Flagsmith API to communicate with
49
+ @param data.customHeaders: Additional headers to add to requests made to the
50
+ Flagsmith API
51
+ @param {number} data.requestTimeoutSeconds: Number of seconds to wait for a request to
52
+ complete before terminating the request
53
+ @param {boolean} data.enableLocalEvaluation: Enables local evaluation of flags
54
+ @param {number} data.environmentRefreshIntervalSeconds: If using local evaluation,
55
+ specify the interval period between refreshes of local environment data
56
+ @param {number} data.retries: a urllib3.Retry object to use on all http requests to the
57
+ Flagsmith API
58
+ @param {boolean} data.enableAnalytics: if enabled, sends additional requests to the Flagsmith
59
+ API to power flag analytics charts
60
+ @param data.defaultFlagHandler: callable which will be used in the case where
61
+ flags cannot be retrieved from the API or a non existent feature is
62
+ requested
63
+ */
64
+ constructor(data: {
65
+ environmentKey: string;
66
+ apiUrl?: string;
67
+ customHeaders?: { [key: string]: any };
68
+ requestTimeoutSeconds?: number;
69
+ enableLocalEvaluation?: boolean;
70
+ environmentRefreshIntervalSeconds?: number;
71
+ retries?: any;
72
+ enableAnalytics?: boolean;
73
+ defaultFlagHandler?: (featureName: string) => DefaultFlag;
74
+ }) {
75
+ this.environmentKey = data.environmentKey;
76
+ this.apiUrl = data.apiUrl || this.apiUrl;
77
+ this.customHeaders = data.customHeaders;
78
+ this.requestTimeoutSeconds = data.requestTimeoutSeconds;
79
+ this.enableLocalEvaluation = data.enableLocalEvaluation;
80
+ this.environmentRefreshIntervalSeconds =
81
+ data.environmentRefreshIntervalSeconds || this.environmentRefreshIntervalSeconds;
82
+ this.retries = data.retries;
83
+ this.enableAnalytics = data.enableAnalytics || false;
84
+ this.defaultFlagHandler = data.defaultFlagHandler;
85
+
86
+ this.environmentFlagsUrl = `${this.apiUrl}flags/`;
87
+ this.identitiesUrl = `${this.apiUrl}identities/`;
88
+ this.environmentUrl = `${this.apiUrl}environment-document/`;
89
+
90
+ if (this.enableLocalEvaluation) {
91
+ this.environmentDataPollingManager = new EnvironmentDataPollingManager(
92
+ this,
93
+ this.environmentRefreshIntervalSeconds
94
+ );
95
+ this.environmentDataPollingManager.start();
96
+ }
97
+
98
+ this.analyticsProcessor = data.enableAnalytics
99
+ ? new AnalyticsProcessor({
100
+ environmentKey: this.environmentKey,
101
+ baseApiUrl: this.apiUrl,
102
+ timeout: this.requestTimeoutSeconds
103
+ })
104
+ : undefined;
105
+ }
106
+ /**
107
+ * Get all the default for flags for the current environment.
108
+ *
109
+ * @returns Flags object holding all the flags for the current environment.
110
+ */
111
+ async getEnvironmentFlags(): Promise<Flags> {
112
+ if (this.environment) {
113
+ return new Promise(resolve => resolve(this.getEnvironmentFlagsFromDocument()));
114
+ }
115
+
116
+ return this.getEnvironmentFlagsFromApi();
117
+ }
118
+ /**
119
+ * Get all the flags for the current environment for a given identity. Will also
120
+ upsert all traits to the Flagsmith API for future evaluations. Providing a
121
+ trait with a value of None will remove the trait from the identity if it exists.
122
+ *
123
+ * @param {string} identifier a unique identifier for the identity in the current
124
+ environment, e.g. email address, username, uuid
125
+ * @param {{[key:string]:any}} traits? a dictionary of traits to add / update on the identity in
126
+ Flagsmith, e.g. {"num_orders": 10}
127
+ * @returns Flags object holding all the flags for the given identity.
128
+ */
129
+ getIdentityFlags(identifier: string, traits?: { [key: string]: any }): Promise<Flags> {
130
+ traits = traits || {};
131
+ if (this.environment) {
132
+ return new Promise(resolve =>
133
+ resolve(this.getIdentityFlagsFromDocument(identifier, traits || {}))
134
+ );
135
+ }
136
+ return this.getIdentityFlagsFromApi(identifier, traits);
137
+ }
138
+
139
+ /**
140
+ * Updates the environment state for local flag evaluation.
141
+ * You only need to call this if you wish to bypass environmentRefreshIntervalSeconds.
142
+ */
143
+ async updateEnvironment() {
144
+ this.environment = await this.getEnvironmentFromApi();
145
+ }
146
+
147
+ private async getJSONResponse(
148
+ url: string,
149
+ method: string,
150
+ body?: { [key: string]: any }
151
+ ): Promise<any> {
152
+ const headers: { [key: string]: any } = {};
153
+ if (this.environmentKey) {
154
+ headers['X-Environment-Key'] = this.environmentKey as string;
155
+ }
156
+
157
+ if (this.customHeaders) {
158
+ for (const [k, v] of Object.entries(this.customHeaders)) {
159
+ headers[k] = v;
160
+ }
161
+ }
162
+
163
+ const data = await retryFetch(
164
+ url,
165
+ {
166
+ method: method,
167
+ timeout: this.requestTimeoutSeconds || undefined,
168
+ body: JSON.stringify(body),
169
+ headers: headers
170
+ },
171
+ this.retries,
172
+ 1000,
173
+ (this.requestTimeoutSeconds || 10) * 1000
174
+ );
175
+
176
+ if (data.status !== 200) {
177
+ throw new FlagsmithAPIError(
178
+ `Invalid request made to Flagsmith API. Response status code: ${data.status}`
179
+ );
180
+ }
181
+
182
+ return data.json();
183
+ }
184
+
185
+ private async getEnvironmentFromApi() {
186
+ const environment_data = await this.getJSONResponse(this.environmentUrl, 'GET');
187
+ return buildEnvironmentModel(environment_data);
188
+ }
189
+
190
+ private getEnvironmentFlagsFromDocument() {
191
+ if (!this.environment) {
192
+ throw new FlagsmithClientError(
193
+ 'Unable to build identity model when no local environment present.'
194
+ );
195
+ }
196
+
197
+ return Flags.fromFeatureStateModels({
198
+ featureStates: getEnvironmentFeatureStates(this.environment),
199
+ analyticsProcessor: this.analyticsProcessor,
200
+ defaultFlagHandler: this.defaultFlagHandler
201
+ });
202
+ }
203
+
204
+ private getIdentityFlagsFromDocument(identifier: string, traits: { [key: string]: any }) {
205
+ if (!this.environment) {
206
+ throw new FlagsmithClientError(
207
+ 'Unable to build identity model when no local environment present.'
208
+ );
209
+ }
210
+
211
+ const identityModel = this.buildIdentityModel(identifier, Object.keys(traits).map((key)=>(
212
+ {
213
+ key,
214
+ value: traits[key]
215
+ }
216
+ )));
217
+
218
+ const featureStates = getIdentityFeatureStates(this.environment, identityModel);
219
+
220
+ return Flags.fromFeatureStateModels({
221
+ featureStates: featureStates,
222
+ analyticsProcessor: this.analyticsProcessor,
223
+ defaultFlagHandler: this.defaultFlagHandler
224
+ });
225
+ }
226
+
227
+ private async getEnvironmentFlagsFromApi() {
228
+ try {
229
+ const apiFlags = await this.getJSONResponse(this.environmentFlagsUrl, 'GET');
230
+ return Flags.fromAPIFlags({
231
+ apiFlags: apiFlags,
232
+ analyticsProcessor: this.analyticsProcessor,
233
+ defaultFlagHandler: this.defaultFlagHandler
234
+ });
235
+ } catch (e) {
236
+ if (this.defaultFlagHandler) {
237
+ return new Flags({
238
+ flags: {},
239
+ defaultFlagHandler: this.defaultFlagHandler
240
+ });
241
+ }
242
+
243
+ throw e;
244
+ }
245
+ }
246
+
247
+ private async getIdentityFlagsFromApi(identifier: string, traits: { [key: string]: any }) {
248
+ try {
249
+ const data = generateIdentitiesData(identifier, traits);
250
+ const jsonResponse = await this.getJSONResponse(this.identitiesUrl, 'POST', data);
251
+ return Flags.fromAPIFlags({
252
+ apiFlags: jsonResponse['flags'],
253
+ analyticsProcessor: this.analyticsProcessor,
254
+ defaultFlagHandler: this.defaultFlagHandler
255
+ });
256
+ } catch (e) {
257
+ if (this.defaultFlagHandler) {
258
+ return new Flags({
259
+ flags: {},
260
+ defaultFlagHandler: this.defaultFlagHandler
261
+ });
262
+ }
263
+
264
+ throw e;
265
+ }
266
+ }
267
+
268
+ private buildIdentityModel(identifier: string, traits: { key:string, value:any }[]) {
269
+ if (!this.environment) {
270
+ throw new FlagsmithClientError(
271
+ 'Unable to build identity model when no local environment present.'
272
+ );
273
+ }
274
+
275
+ const traitModels = traits.map(trait => new TraitModel(trait.key, trait.value));
276
+ return new IdentityModel('0', traitModels, [], this.environment.apiKey, identifier);
277
+ }
278
+
279
+ stop() {}
280
+ }
281
+
282
+ export default Flagsmith;