flagsmith-nodejs 1.1.3 → 2.0.0-beta.3

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 (65) 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/.tool-versions +1 -0
  5. package/example/README.md +0 -6
  6. package/example/package-lock.json +1070 -2
  7. package/example/package.json +4 -3
  8. package/example/server/api/index.js +23 -18
  9. package/example/server/index.js +7 -6
  10. package/flagsmith-engine/environments/integrations/models.ts +4 -0
  11. package/flagsmith-engine/environments/models.ts +50 -0
  12. package/flagsmith-engine/environments/util.ts +29 -0
  13. package/flagsmith-engine/features/constants.ts +4 -0
  14. package/flagsmith-engine/features/models.ts +105 -0
  15. package/flagsmith-engine/features/util.ts +38 -0
  16. package/flagsmith-engine/identities/models.ts +60 -0
  17. package/flagsmith-engine/identities/traits/models.ts +9 -0
  18. package/flagsmith-engine/identities/util.ts +30 -0
  19. package/flagsmith-engine/index.ts +93 -0
  20. package/flagsmith-engine/organisations/models.ts +25 -0
  21. package/flagsmith-engine/organisations/util.ts +11 -0
  22. package/flagsmith-engine/projects/models.ts +22 -0
  23. package/flagsmith-engine/projects/util.ts +18 -0
  24. package/flagsmith-engine/segments/constants.ts +31 -0
  25. package/flagsmith-engine/segments/evaluators.ts +72 -0
  26. package/flagsmith-engine/segments/models.ts +103 -0
  27. package/flagsmith-engine/segments/util.ts +29 -0
  28. package/flagsmith-engine/utils/collections.ts +14 -0
  29. package/flagsmith-engine/utils/errors.ts +1 -0
  30. package/flagsmith-engine/utils/hashing/index.ts +52 -0
  31. package/flagsmith-engine/utils/index.ts +10 -0
  32. package/index.ts +6 -0
  33. package/jest.config.js +5 -0
  34. package/package.json +30 -12
  35. package/sdk/analytics.ts +60 -0
  36. package/sdk/errors.ts +2 -0
  37. package/sdk/index.ts +329 -0
  38. package/sdk/models.ts +145 -0
  39. package/sdk/polling_manager.ts +31 -0
  40. package/sdk/utils.ts +45 -0
  41. package/tests/engine/e2e/engine.test.ts +51 -0
  42. package/tests/engine/engine-tests/engine-test-data/data/environment_n9fbf9h3v4fFgH3U3ngWhb.json +12393 -0
  43. package/tests/engine/engine-tests/engine-test-data/readme.md +30 -0
  44. package/tests/engine/unit/egine.test.ts +96 -0
  45. package/tests/engine/unit/environments/builder.test.ts +148 -0
  46. package/tests/engine/unit/environments/models.test.ts +49 -0
  47. package/tests/engine/unit/features/models.test.ts +72 -0
  48. package/tests/engine/unit/identities/identities_builders.test.ts +85 -0
  49. package/tests/engine/unit/identities/identities_models.test.ts +105 -0
  50. package/tests/engine/unit/organization/models.test.ts +12 -0
  51. package/tests/engine/unit/segments/segments_model.test.ts +101 -0
  52. package/tests/engine/unit/segments/util.ts +151 -0
  53. package/tests/engine/unit/utils.ts +114 -0
  54. package/tests/index.js +0 -0
  55. package/tests/sdk/analytics.test.ts +43 -0
  56. package/tests/sdk/data/environment.json +70 -0
  57. package/tests/sdk/data/flags.json +20 -0
  58. package/tests/sdk/data/identities.json +29 -0
  59. package/tests/sdk/flagsmith.test.ts +203 -0
  60. package/tests/sdk/polling.test.ts +34 -0
  61. package/tests/sdk/utils.ts +39 -0
  62. package/tsconfig.json +19 -0
  63. package/flagsmith-core.js +0 -243
  64. package/index.d.ts +0 -85
  65. package/index.js +0 -3
package/sdk/index.ts ADDED
@@ -0,0 +1,329 @@
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
+ import { SegmentModel } from '../flagsmith-engine/segments/models';
14
+ import { getIdentitySegments } from '../flagsmith-engine/segments/evaluators';
15
+
16
+ const DEFAULT_API_URL = 'https://api.flagsmith.com/api/v1/';
17
+
18
+ export class Flagsmith {
19
+ environmentKey?: string;
20
+ apiUrl: string = DEFAULT_API_URL;
21
+ customHeaders?: { [key: string]: any };
22
+ requestTimeoutSeconds?: number;
23
+ enableLocalEvaluation?: boolean = false;
24
+ environmentRefreshIntervalSeconds: number = 60;
25
+ retries?: number;
26
+ enableAnalytics: boolean = false;
27
+ defaultFlagHandler?: (featureName: string) => DefaultFlag;
28
+
29
+ environmentFlagsUrl: string;
30
+ identitiesUrl: string;
31
+ environmentUrl: string;
32
+
33
+ environmentDataPollingManager?: EnvironmentDataPollingManager;
34
+ environment!: EnvironmentModel;
35
+ private analyticsProcessor?: AnalyticsProcessor;
36
+ /**
37
+ * A Flagsmith client.
38
+ *
39
+ * Provides an interface for interacting with the Flagsmith http API.
40
+ * Basic Usage::
41
+ *
42
+ * import flagsmith from Flagsmith
43
+ * const flagsmith = new Flagsmith({environmentKey: '<your API key>'});
44
+ * const environmentFlags = flagsmith.getEnvironmentFlags();
45
+ * const featureEnabled = environmentFlags.isFeatureEnabled('foo');
46
+ * const identityFlags = flagsmith.getIdentityFlags('identifier', {'foo': 'bar'});
47
+ * const featureEnabledForIdentity = identityFlags.isFeatureEnabled("foo")
48
+ *
49
+ * @param {string} data.environmentKey: The environment key obtained from Flagsmith interface
50
+ @param {string} data.apiUrl: Override the URL of the Flagsmith API to communicate with
51
+ @param data.customHeaders: Additional headers to add to requests made to the
52
+ Flagsmith API
53
+ @param {number} data.requestTimeoutSeconds: Number of seconds to wait for a request to
54
+ complete before terminating the request
55
+ @param {boolean} data.enableLocalEvaluation: Enables local evaluation of flags
56
+ @param {number} data.environmentRefreshIntervalSeconds: If using local evaluation,
57
+ specify the interval period between refreshes of local environment data
58
+ @param {number} data.retries: a urllib3.Retry object to use on all http requests to the
59
+ Flagsmith API
60
+ @param {boolean} data.enableAnalytics: if enabled, sends additional requests to the Flagsmith
61
+ API to power flag analytics charts
62
+ @param data.defaultFlagHandler: callable which will be used in the case where
63
+ flags cannot be retrieved from the API or a non existent feature is
64
+ requested
65
+ */
66
+ constructor(data: {
67
+ environmentKey: string;
68
+ apiUrl?: string;
69
+ customHeaders?: { [key: string]: any };
70
+ requestTimeoutSeconds?: number;
71
+ enableLocalEvaluation?: boolean;
72
+ environmentRefreshIntervalSeconds?: number;
73
+ retries?: number;
74
+ enableAnalytics?: boolean;
75
+ defaultFlagHandler?: (featureName: string) => DefaultFlag;
76
+ }) {
77
+ this.environmentKey = data.environmentKey;
78
+ this.apiUrl = data.apiUrl || this.apiUrl;
79
+ this.customHeaders = data.customHeaders;
80
+ this.requestTimeoutSeconds = data.requestTimeoutSeconds;
81
+ this.enableLocalEvaluation = data.enableLocalEvaluation;
82
+ this.environmentRefreshIntervalSeconds =
83
+ data.environmentRefreshIntervalSeconds || this.environmentRefreshIntervalSeconds;
84
+ this.retries = data.retries;
85
+ this.enableAnalytics = data.enableAnalytics || false;
86
+ this.defaultFlagHandler = data.defaultFlagHandler;
87
+
88
+ this.environmentFlagsUrl = `${this.apiUrl}flags/`;
89
+ this.identitiesUrl = `${this.apiUrl}identities/`;
90
+ this.environmentUrl = `${this.apiUrl}environment-document/`;
91
+
92
+ if (this.enableLocalEvaluation) {
93
+ if (!this.environmentKey.startsWith('ser.')) {
94
+ console.error(
95
+ 'In order to use local evaluation, please generate a server key in the environment settings page.'
96
+ );
97
+ }
98
+ this.environmentDataPollingManager = new EnvironmentDataPollingManager(
99
+ this,
100
+ this.environmentRefreshIntervalSeconds
101
+ );
102
+ this.environmentDataPollingManager.start();
103
+ this.updateEnvironment();
104
+ }
105
+
106
+ this.analyticsProcessor = data.enableAnalytics
107
+ ? new AnalyticsProcessor({
108
+ environmentKey: this.environmentKey,
109
+ baseApiUrl: this.apiUrl,
110
+ timeout: this.requestTimeoutSeconds
111
+ })
112
+ : undefined;
113
+ }
114
+ /**
115
+ * Get all the default for flags for the current environment.
116
+ *
117
+ * @returns Flags object holding all the flags for the current environment.
118
+ */
119
+ async getEnvironmentFlags(): Promise<Flags> {
120
+ if (this.environment) {
121
+ return new Promise(resolve => resolve(this.getEnvironmentFlagsFromDocument()));
122
+ }
123
+
124
+ return this.getEnvironmentFlagsFromApi();
125
+ }
126
+ /**
127
+ * Get all the flags for the current environment for a given identity. Will also
128
+ upsert all traits to the Flagsmith API for future evaluations. Providing a
129
+ trait with a value of None will remove the trait from the identity if it exists.
130
+ *
131
+ * @param {string} identifier a unique identifier for the identity in the current
132
+ environment, e.g. email address, username, uuid
133
+ * @param {{[key:string]:any}} traits? a dictionary of traits to add / update on the identity in
134
+ Flagsmith, e.g. {"num_orders": 10}
135
+ * @returns Flags object holding all the flags for the given identity.
136
+ */
137
+ getIdentityFlags(identifier: string, traits?: { [key: string]: any }): Promise<Flags> {
138
+ traits = traits || {};
139
+ if (this.enableLocalEvaluation) {
140
+ return new Promise(resolve =>
141
+ this.environmentPromise!.then(() => {
142
+ resolve(this.getIdentityFlagsFromDocument(identifier, traits || {}));
143
+ })
144
+ );
145
+ }
146
+ return this.getIdentityFlagsFromApi(identifier, traits);
147
+ }
148
+
149
+ /**
150
+ * Get the segments for the current environment for a given identity. Will also
151
+ upsert all traits to the Flagsmith API for future evaluations. Providing a
152
+ trait with a value of None will remove the trait from the identity if it exists.
153
+ *
154
+ * @param {string} identifier a unique identifier for the identity in the current
155
+ environment, e.g. email address, username, uuid
156
+ * @param {{[key:string]:any}} traits? a dictionary of traits to add / update on the identity in
157
+ Flagsmith, e.g. {"num_orders": 10}
158
+ * @returns Segments that the given identity belongs to.
159
+ */
160
+ getIdentitySegments(
161
+ identifier: string,
162
+ traits?: { [key: string]: any }
163
+ ): Promise<SegmentModel[]> {
164
+ traits = traits || {};
165
+ if (this.enableLocalEvaluation) {
166
+ return this.environmentPromise!.then(() => {
167
+ return new Promise(resolve => {
168
+ const identityModel = this.buildIdentityModel(
169
+ identifier,
170
+ Object.keys(traits || {}).map(key => ({
171
+ key,
172
+ value: traits?.[key]
173
+ }))
174
+ );
175
+
176
+ const segments = getIdentitySegments(this.environment, identityModel);
177
+ return resolve(segments);
178
+ });
179
+ });
180
+ }
181
+ console.error('This function is only permitted with local evaluation.');
182
+ return Promise.resolve([]);
183
+ }
184
+
185
+ /**
186
+ * Updates the environment state for local flag evaluation.
187
+ * Sets a local promise to prevent race conditions in getIdentityFlags / getIdentitySegments.
188
+ * You only need to call this if you wish to bypass environmentRefreshIntervalSeconds.
189
+ */
190
+ async updateEnvironment() {
191
+ const request = this.getEnvironmentFromApi();
192
+ if (!this.environmentPromise) {
193
+ this.environmentPromise = request.then(res => {
194
+ this.environment = res;
195
+ });
196
+ await this.environmentPromise;
197
+ } else {
198
+ this.environment = await request;
199
+ }
200
+ }
201
+
202
+ private async getJSONResponse(
203
+ url: string,
204
+ method: string,
205
+ body?: { [key: string]: any }
206
+ ): Promise<any> {
207
+ const headers: { [key: string]: any } = { 'Content-Type': 'application/json' };
208
+ if (this.environmentKey) {
209
+ headers['X-Environment-Key'] = this.environmentKey as string;
210
+ }
211
+
212
+ if (this.customHeaders) {
213
+ for (const [k, v] of Object.entries(this.customHeaders)) {
214
+ headers[k] = v;
215
+ }
216
+ }
217
+
218
+ const data = await retryFetch(
219
+ url,
220
+ {
221
+ method: method,
222
+ timeout: this.requestTimeoutSeconds || undefined,
223
+ body: JSON.stringify(body),
224
+ headers: headers
225
+ },
226
+ this.retries,
227
+ 1000,
228
+ (this.requestTimeoutSeconds || 10) * 1000
229
+ );
230
+
231
+ if (data.status !== 200) {
232
+ throw new FlagsmithAPIError(
233
+ `Invalid request made to Flagsmith API. Response status code: ${data.status}`
234
+ );
235
+ }
236
+
237
+ return data.json();
238
+ }
239
+
240
+ /**
241
+ * This promise ensures that the environment is retrieved before attempting to locally evaluate.
242
+ */
243
+ private environmentPromise: Promise<any> | undefined;
244
+
245
+ private async getEnvironmentFromApi() {
246
+ const environment_data = await this.getJSONResponse(this.environmentUrl, 'GET');
247
+ return buildEnvironmentModel(environment_data);
248
+ }
249
+
250
+ private getEnvironmentFlagsFromDocument() {
251
+ return Flags.fromFeatureStateModels({
252
+ featureStates: getEnvironmentFeatureStates(this.environment),
253
+ analyticsProcessor: this.analyticsProcessor,
254
+ defaultFlagHandler: this.defaultFlagHandler
255
+ });
256
+ }
257
+
258
+ private getIdentityFlagsFromDocument(identifier: string, traits: { [key: string]: any }) {
259
+ const identityModel = this.buildIdentityModel(
260
+ identifier,
261
+ Object.keys(traits).map(key => ({
262
+ key,
263
+ value: traits[key]
264
+ }))
265
+ );
266
+
267
+ const featureStates = getIdentityFeatureStates(this.environment, identityModel);
268
+
269
+ return Flags.fromFeatureStateModels({
270
+ featureStates: featureStates,
271
+ analyticsProcessor: this.analyticsProcessor,
272
+ defaultFlagHandler: this.defaultFlagHandler
273
+ });
274
+ }
275
+
276
+ private async getEnvironmentFlagsFromApi() {
277
+ try {
278
+ const apiFlags = await this.getJSONResponse(this.environmentFlagsUrl, 'GET');
279
+ return Flags.fromAPIFlags({
280
+ apiFlags: apiFlags,
281
+ analyticsProcessor: this.analyticsProcessor,
282
+ defaultFlagHandler: this.defaultFlagHandler
283
+ });
284
+ } catch (e) {
285
+ if (this.defaultFlagHandler) {
286
+ return new Flags({
287
+ flags: {},
288
+ defaultFlagHandler: this.defaultFlagHandler
289
+ });
290
+ }
291
+
292
+ throw e;
293
+ }
294
+ }
295
+
296
+ private async getIdentityFlagsFromApi(identifier: string, traits: { [key: string]: any }) {
297
+ try {
298
+ const data = generateIdentitiesData(identifier, traits);
299
+ const jsonResponse = await this.getJSONResponse(this.identitiesUrl, 'POST', data);
300
+ return Flags.fromAPIFlags({
301
+ apiFlags: jsonResponse['flags'],
302
+ analyticsProcessor: this.analyticsProcessor,
303
+ defaultFlagHandler: this.defaultFlagHandler
304
+ });
305
+ } catch (e) {
306
+ if (this.defaultFlagHandler) {
307
+ return new Flags({
308
+ flags: {},
309
+ defaultFlagHandler: this.defaultFlagHandler
310
+ });
311
+ }
312
+
313
+ throw e;
314
+ }
315
+ }
316
+
317
+ private buildIdentityModel(identifier: string, traits: { key: string; value: any }[]) {
318
+ if (!this.environment) {
319
+ throw new FlagsmithClientError(
320
+ 'Unable to build identity model when no local environment present.'
321
+ );
322
+ }
323
+
324
+ const traitModels = traits.map(trait => new TraitModel(trait.key, trait.value));
325
+ return new IdentityModel('0', traitModels, [], this.environment.apiKey, identifier);
326
+ }
327
+ }
328
+
329
+ export default Flagsmith;
package/sdk/models.ts ADDED
@@ -0,0 +1,145 @@
1
+ import { FeatureStateModel } from '../flagsmith-engine/features/models';
2
+ import { AnalyticsProcessor } from './analytics';
3
+ import { FlagsmithClientError } from './errors';
4
+
5
+ export class BaseFlag {
6
+ enabled: boolean;
7
+ value: string | number | boolean | undefined;
8
+ isDefault: boolean;
9
+
10
+ constructor(
11
+ value: string | number | boolean | undefined,
12
+ enabled: boolean,
13
+ isDefault: boolean
14
+ ) {
15
+ this.value = value;
16
+ this.enabled = enabled;
17
+ this.isDefault = isDefault;
18
+ }
19
+ }
20
+
21
+ export class DefaultFlag extends BaseFlag {
22
+ constructor(value: string | number | boolean | undefined, enabled: boolean) {
23
+ super(value, enabled, true);
24
+ }
25
+ }
26
+
27
+ export class Flag extends BaseFlag {
28
+ featureId: number;
29
+ featureName: string;
30
+
31
+ constructor(params: {
32
+ value: string | number | boolean | undefined;
33
+ enabled: boolean;
34
+ isDefault?: boolean;
35
+ featureId: number;
36
+ featureName: string;
37
+ }) {
38
+ super(params.value, params.enabled, !!params.isDefault);
39
+ this.featureId = params.featureId;
40
+ this.featureName = params.featureName;
41
+ }
42
+
43
+ static fromFeatureStateModel(
44
+ fsm: FeatureStateModel,
45
+ identityId: number | string | undefined
46
+ ): Flag {
47
+ return new Flag({
48
+ value: fsm.getValue(identityId),
49
+ enabled: fsm.enabled,
50
+ featureId: fsm.feature.id,
51
+ featureName: fsm.feature.name
52
+ });
53
+ }
54
+
55
+ static fromAPIFlag(flagData: any): Flag {
56
+ return new Flag({
57
+ enabled: flagData['enabled'],
58
+ value: flagData['feature_state_value'] || flagData['value'],
59
+ featureId: flagData['feature']['id'],
60
+ featureName: flagData['feature']['name']
61
+ });
62
+ }
63
+ }
64
+
65
+ export class Flags {
66
+ flags: { [key: string]: Flag } = {};
67
+ defaultFlagHandler?: (featureName: string) => DefaultFlag;
68
+ analyticsProcessor?: AnalyticsProcessor;
69
+
70
+ constructor(data: {
71
+ flags: { [key: string]: Flag };
72
+ defaultFlagHandler?: (v: string) => DefaultFlag;
73
+ analyticsProcessor?: AnalyticsProcessor;
74
+ }) {
75
+ this.flags = data.flags;
76
+ this.defaultFlagHandler = data.defaultFlagHandler;
77
+ this.analyticsProcessor = data.analyticsProcessor;
78
+ }
79
+
80
+ static fromFeatureStateModels(data: {
81
+ featureStates: FeatureStateModel[];
82
+ analyticsProcessor?: AnalyticsProcessor;
83
+ defaultFlagHandler?: (f: string) => DefaultFlag;
84
+ identityID?: string | number;
85
+ }): Flags {
86
+ const flags: { [key: string]: any } = {};
87
+ for (const fs of data.featureStates) {
88
+ flags[fs.feature.name] = Flag.fromFeatureStateModel(fs, data.identityID);
89
+ }
90
+ return new Flags({
91
+ flags: flags,
92
+ defaultFlagHandler: data.defaultFlagHandler,
93
+ analyticsProcessor: data.analyticsProcessor
94
+ });
95
+ }
96
+
97
+ static fromAPIFlags(data: {
98
+ apiFlags: { [key: string]: any }[];
99
+ analyticsProcessor?: AnalyticsProcessor;
100
+ defaultFlagHandler?: (v: string) => DefaultFlag;
101
+ }): Flags {
102
+ const flags: { [key: string]: any } = {};
103
+
104
+ for (const flagData of data.apiFlags) {
105
+ flags[flagData['feature']['name']] = Flag.fromAPIFlag(flagData);
106
+ }
107
+
108
+ return new Flags({
109
+ flags: flags,
110
+ defaultFlagHandler: data.defaultFlagHandler,
111
+ analyticsProcessor: data.analyticsProcessor
112
+ });
113
+ }
114
+
115
+ allFlags(): Flag[] {
116
+ return Object.values(this.flags);
117
+ }
118
+
119
+ getFlag(featureName: string): BaseFlag {
120
+ const flag = this.flags[featureName];
121
+
122
+ if (!flag) {
123
+ if (this.defaultFlagHandler) {
124
+ return this.defaultFlagHandler(featureName);
125
+ }
126
+
127
+ return { enabled: false, isDefault: true, value: undefined };
128
+
129
+ }
130
+
131
+ if (this.analyticsProcessor && flag.featureId) {
132
+ this.analyticsProcessor.trackFeature(flag.featureId);
133
+ }
134
+
135
+ return flag;
136
+ }
137
+
138
+ getFeatureValue(featureName: string): any {
139
+ return this.getFlag(featureName).value;
140
+ }
141
+
142
+ isFeatureEnabled(featureName: string): boolean {
143
+ return this.getFlag(featureName).enabled;
144
+ }
145
+ }
@@ -0,0 +1,31 @@
1
+ import Flagsmith from '.';
2
+
3
+ export class EnvironmentDataPollingManager {
4
+ private interval?: NodeJS.Timer;
5
+ private main: Flagsmith;
6
+ private refreshIntervalSeconds: number;
7
+
8
+ constructor(main: Flagsmith, refreshIntervalSeconds: number) {
9
+ this.main = main;
10
+ this.refreshIntervalSeconds = refreshIntervalSeconds;
11
+ }
12
+
13
+ start() {
14
+ const updateEnvironment = () => {
15
+ if (this.interval) clearInterval(this.interval);
16
+ this.interval = setInterval(async () => {
17
+ await this.main.updateEnvironment();
18
+ }, this.refreshIntervalSeconds * 1000);
19
+ };
20
+ // todo: this call should be awaited for getIdentityFlags/getEnvironmentFlags when enableLocalEvaluation is true
21
+ this.main.updateEnvironment();
22
+ updateEnvironment();
23
+ }
24
+
25
+ stop() {
26
+ if (!this.interval) {
27
+ return;
28
+ }
29
+ clearInterval(this.interval);
30
+ }
31
+ }
package/sdk/utils.ts ADDED
@@ -0,0 +1,45 @@
1
+ import fetch, { Response } from 'node-fetch';
2
+ // @ts-ignore
3
+ if (typeof fetch.default !== 'undefined') fetch = fetch.default;
4
+
5
+ export function generateIdentitiesData(identifier: string, traits?: { [key: string]: any }) {
6
+ const traitsGenerated = Object.entries(traits || {}).map(trait => ({
7
+ trait_key: trait[0],
8
+ trait_value: trait[1]
9
+ }));
10
+ return {
11
+ identifier: identifier,
12
+ traits: traitsGenerated
13
+ };
14
+ }
15
+
16
+ export const delay = (ms: number) =>
17
+ new Promise(resolve => setTimeout(() => resolve(undefined), ms));
18
+
19
+ export const retryFetch = (
20
+ url: string,
21
+ fetchOptions = {},
22
+ retries = 3,
23
+ retryDelay = 1000,
24
+ timeout: number
25
+ ): Promise<Response> => {
26
+ return new Promise((resolve, reject) => {
27
+ // check for timeout
28
+ if (timeout) setTimeout(() => reject('error: timeout'), timeout);
29
+
30
+ const wrapper = (n: number) => {
31
+ fetch(url, fetchOptions)
32
+ .then(res => resolve(res))
33
+ .catch(async err => {
34
+ if (n > 0) {
35
+ await delay(retryDelay);
36
+ wrapper(--n);
37
+ } else {
38
+ reject(err);
39
+ }
40
+ });
41
+ };
42
+
43
+ wrapper(retries);
44
+ });
45
+ };
@@ -0,0 +1,51 @@
1
+ import { readFileSync } from 'fs';
2
+ import { getIdentityFeatureStates } from '../../../flagsmith-engine';
3
+ import { EnvironmentModel } from '../../../flagsmith-engine/environments/models';
4
+ import { buildEnvironmentModel } from '../../../flagsmith-engine/environments/util';
5
+ import { IdentityModel } from '../../../flagsmith-engine/identities/models';
6
+ import { buildIdentityModel } from '../../../flagsmith-engine/identities/util';
7
+
8
+ function extractTestCases(
9
+ filePath: string
10
+ ): {
11
+ environment: EnvironmentModel;
12
+ identity: IdentityModel;
13
+ response: any;
14
+ }[] {
15
+ const data = JSON.parse(readFileSync(filePath, 'utf-8'));
16
+ const environmentModel = buildEnvironmentModel(data['environment']);
17
+ const test_data = data['identities_and_responses'].map((test_case: any) => {
18
+ const identity = buildIdentityModel(test_case['identity']);
19
+
20
+ return {
21
+ environment: environmentModel,
22
+ identity: identity,
23
+ response: test_case['response']
24
+ };
25
+ });
26
+ return test_data;
27
+ }
28
+
29
+ test('Test Engine', () => {
30
+ const testCases = extractTestCases(
31
+ __dirname + '/../engine-tests/engine-test-data/data/environment_n9fbf9h3v4fFgH3U3ngWhb.json'
32
+ );
33
+ for (const testCase of testCases) {
34
+ const engine_response = getIdentityFeatureStates(testCase.environment, testCase.identity);
35
+ const sortedEngineFlags = engine_response.sort((a, b) =>
36
+ a.feature.name > b.feature.name ? 1 : -1
37
+ );
38
+ const sortedAPIFlags = testCase.response['flags'].sort((a: any, b: any) =>
39
+ a.feature.name > b.feature.name ? 1 : -1
40
+ );
41
+
42
+ expect(sortedEngineFlags.length).toBe(sortedAPIFlags.length);
43
+
44
+ for (let i = 0; i < sortedEngineFlags.length; i++) {
45
+ expect(sortedEngineFlags[i].getValue(testCase.identity.djangoID)).toBe(
46
+ sortedAPIFlags[i]['feature_state_value']
47
+ );
48
+ expect(sortedEngineFlags[i].enabled).toBe(sortedAPIFlags[i]['enabled']);
49
+ }
50
+ }
51
+ });