flagsmith-nodejs 2.0.0-beta.2 → 2.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import { EnvironmentModel } from '../flagsmith-engine/environments/models';
2
2
  import { DefaultFlag, Flags } from './models';
3
3
  import { EnvironmentDataPollingManager } from './polling_manager';
4
+ import { SegmentModel } from '../flagsmith-engine/segments/models';
4
5
  export declare class Flagsmith {
5
6
  environmentKey?: string;
6
7
  apiUrl: string;
@@ -10,7 +11,7 @@ export declare class Flagsmith {
10
11
  requestTimeoutSeconds?: number;
11
12
  enableLocalEvaluation?: boolean;
12
13
  environmentRefreshIntervalSeconds: number;
13
- retries?: any;
14
+ retries?: number;
14
15
  enableAnalytics: boolean;
15
16
  defaultFlagHandler?: (featureName: string) => DefaultFlag;
16
17
  environmentFlagsUrl: string;
@@ -58,7 +59,7 @@ export declare class Flagsmith {
58
59
  requestTimeoutSeconds?: number;
59
60
  enableLocalEvaluation?: boolean;
60
61
  environmentRefreshIntervalSeconds?: number;
61
- retries?: any;
62
+ retries?: number;
62
63
  enableAnalytics?: boolean;
63
64
  defaultFlagHandler?: (featureName: string) => DefaultFlag;
64
65
  });
@@ -82,12 +83,31 @@ export declare class Flagsmith {
82
83
  getIdentityFlags(identifier: string, traits?: {
83
84
  [key: string]: any;
84
85
  }): Promise<Flags>;
86
+ /**
87
+ * Get the segments for the current environment for a given identity. Will also
88
+ upsert all traits to the Flagsmith API for future evaluations. Providing a
89
+ trait with a value of None will remove the trait from the identity if it exists.
90
+ *
91
+ * @param {string} identifier a unique identifier for the identity in the current
92
+ environment, e.g. email address, username, uuid
93
+ * @param {{[key:string]:any}} traits? a dictionary of traits to add / update on the identity in
94
+ Flagsmith, e.g. {"num_orders": 10}
95
+ * @returns Segments that the given identity belongs to.
96
+ */
97
+ getIdentitySegments(identifier: string, traits?: {
98
+ [key: string]: any;
99
+ }): Promise<SegmentModel[]>;
85
100
  /**
86
101
  * Updates the environment state for local flag evaluation.
102
+ * Sets a local promise to prevent race conditions in getIdentityFlags / getIdentitySegments.
87
103
  * You only need to call this if you wish to bypass environmentRefreshIntervalSeconds.
88
104
  */
89
105
  updateEnvironment(): Promise<void>;
90
106
  private getJSONResponse;
107
+ /**
108
+ * This promise ensures that the environment is retrieved before attempting to locally evaluate.
109
+ */
110
+ private environmentPromise;
91
111
  private getEnvironmentFromApi;
92
112
  private getEnvironmentFlagsFromDocument;
93
113
  private getIdentityFlagsFromDocument;
@@ -10,6 +10,7 @@ const errors_1 = require("./errors");
10
10
  const models_3 = require("./models");
11
11
  const polling_manager_1 = require("./polling_manager");
12
12
  const utils_1 = require("./utils");
13
+ const evaluators_1 = require("../flagsmith-engine/segments/evaluators");
13
14
  const DEFAULT_API_URL = 'https://api.flagsmith.com/api/v1/';
14
15
  class Flagsmith {
15
16
  environmentKey;
@@ -72,8 +73,12 @@ class Flagsmith {
72
73
  this.identitiesUrl = `${this.apiUrl}identities/`;
73
74
  this.environmentUrl = `${this.apiUrl}environment-document/`;
74
75
  if (this.enableLocalEvaluation) {
76
+ if (!this.environmentKey.startsWith('ser.')) {
77
+ console.error('In order to use local evaluation, please generate a server key in the environment settings page.');
78
+ }
75
79
  this.environmentDataPollingManager = new polling_manager_1.EnvironmentDataPollingManager(this, this.environmentRefreshIntervalSeconds);
76
80
  this.environmentDataPollingManager.start();
81
+ this.updateEnvironment();
77
82
  }
78
83
  this.analyticsProcessor = data.enableAnalytics
79
84
  ? new analytics_1.AnalyticsProcessor({
@@ -107,17 +112,57 @@ class Flagsmith {
107
112
  */
108
113
  getIdentityFlags(identifier, traits) {
109
114
  traits = traits || {};
110
- if (this.environment) {
111
- return new Promise(resolve => resolve(this.getIdentityFlagsFromDocument(identifier, traits || {})));
115
+ if (this.enableLocalEvaluation) {
116
+ return new Promise(resolve => this.environmentPromise.then(() => {
117
+ resolve(this.getIdentityFlagsFromDocument(identifier, traits || {}));
118
+ }));
112
119
  }
113
120
  return this.getIdentityFlagsFromApi(identifier, traits);
114
121
  }
122
+ /**
123
+ * Get the segments for the current environment for a given identity. Will also
124
+ upsert all traits to the Flagsmith API for future evaluations. Providing a
125
+ trait with a value of None will remove the trait from the identity if it exists.
126
+ *
127
+ * @param {string} identifier a unique identifier for the identity in the current
128
+ environment, e.g. email address, username, uuid
129
+ * @param {{[key:string]:any}} traits? a dictionary of traits to add / update on the identity in
130
+ Flagsmith, e.g. {"num_orders": 10}
131
+ * @returns Segments that the given identity belongs to.
132
+ */
133
+ getIdentitySegments(identifier, traits) {
134
+ traits = traits || {};
135
+ if (this.enableLocalEvaluation) {
136
+ return this.environmentPromise.then(() => {
137
+ return new Promise(resolve => {
138
+ const identityModel = this.buildIdentityModel(identifier, Object.keys(traits || {}).map(key => ({
139
+ key,
140
+ value: traits?.[key]
141
+ })));
142
+ const segments = (0, evaluators_1.getIdentitySegments)(this.environment, identityModel);
143
+ return resolve(segments);
144
+ });
145
+ });
146
+ }
147
+ console.error('This function is only permitted with local evaluation.');
148
+ return Promise.resolve([]);
149
+ }
115
150
  /**
116
151
  * Updates the environment state for local flag evaluation.
152
+ * Sets a local promise to prevent race conditions in getIdentityFlags / getIdentitySegments.
117
153
  * You only need to call this if you wish to bypass environmentRefreshIntervalSeconds.
118
154
  */
119
155
  async updateEnvironment() {
120
- this.environment = await this.getEnvironmentFromApi();
156
+ const request = this.getEnvironmentFromApi();
157
+ if (!this.environmentPromise) {
158
+ this.environmentPromise = request.then(res => {
159
+ this.environment = res;
160
+ });
161
+ await this.environmentPromise;
162
+ }
163
+ else {
164
+ this.environment = await request;
165
+ }
121
166
  }
122
167
  async getJSONResponse(url, method, body) {
123
168
  const headers = { 'Content-Type': 'application/json' };
@@ -140,6 +185,10 @@ class Flagsmith {
140
185
  }
141
186
  return data.json();
142
187
  }
188
+ /**
189
+ * This promise ensures that the environment is retrieved before attempting to locally evaluate.
190
+ */
191
+ environmentPromise;
143
192
  async getEnvironmentFromApi() {
144
193
  const environment_data = await this.getJSONResponse(this.environmentUrl, 'GET');
145
194
  return (0, util_1.buildEnvironmentModel)(environment_data);
@@ -212,4 +261,3 @@ class Flagsmith {
212
261
  }
213
262
  exports.Flagsmith = Flagsmith;
214
263
  exports.default = Flagsmith;
215
- // export = Flagsmith;
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Flags = exports.Flag = exports.DefaultFlag = exports.BaseFlag = void 0;
4
- const errors_1 = require("./errors");
5
4
  class BaseFlag {
6
5
  enabled;
7
6
  value;
@@ -85,7 +84,7 @@ class Flags {
85
84
  if (this.defaultFlagHandler) {
86
85
  return this.defaultFlagHandler(featureName);
87
86
  }
88
- throw new errors_1.FlagsmithClientError(`Feature does not exist: ${featureName}, implement defaultFlagHandler to handle this case.`);
87
+ return { enabled: false, isDefault: true, value: undefined };
89
88
  }
90
89
  if (this.analyticsProcessor && flag.featureId) {
91
90
  this.analyticsProcessor.trackFeature(flag.featureId);
@@ -29,5 +29,10 @@ module.exports = () => {
29
29
  res.json({ fontSize, checkoutV2 });
30
30
  });
31
31
 
32
+ api.get('/:user/segments', async (req, res) => {
33
+ const segments = await flagsmith.getIdentitySegments(req.params.user, { checkout_v2: 1 });
34
+ res.json(segments.map(v => v.name));
35
+ });
36
+
32
37
  return api;
33
38
  };
@@ -22,5 +22,8 @@ console.log('To get an example response for getFlags');
22
22
  console.log();
23
23
  console.log('Go to http://localhost:' + PORT + '/api/flagsmith_sample_user');
24
24
  console.log('To get an example feature state for a user');
25
+ console.log();
26
+ console.log('Go to http://localhost:' + PORT + '/api/flagsmith_sample_user/segments');
27
+ console.log('To get the segments which the user belongs to');
25
28
 
26
29
  module.exports = app;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flagsmith-nodejs",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.0.0-beta.5",
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": {
@@ -43,6 +43,8 @@
43
43
  "scripts": {
44
44
  "lint": "prettier --write .",
45
45
  "test": "jest --coverage --coverageReporters='text'",
46
+ "test:watch": "jest --coverage --watch --coverageReporters='text'",
47
+ "test:debug": "node --inspect-brk node_modules/.bin/jest --coverage --watch --coverageReporters='text'",
46
48
  "build": "tsc",
47
49
  "prepare": "husky install"
48
50
  },
package/sdk/index.ts CHANGED
@@ -10,6 +10,8 @@ import { FlagsmithAPIError, FlagsmithClientError } from './errors';
10
10
  import { DefaultFlag, Flags } from './models';
11
11
  import { EnvironmentDataPollingManager } from './polling_manager';
12
12
  import { generateIdentitiesData, retryFetch } from './utils';
13
+ import { SegmentModel } from '../flagsmith-engine/segments/models';
14
+ import { getIdentitySegments } from '../flagsmith-engine/segments/evaluators';
13
15
 
14
16
  const DEFAULT_API_URL = 'https://api.flagsmith.com/api/v1/';
15
17
 
@@ -20,7 +22,7 @@ export class Flagsmith {
20
22
  requestTimeoutSeconds?: number;
21
23
  enableLocalEvaluation?: boolean = false;
22
24
  environmentRefreshIntervalSeconds: number = 60;
23
- retries?: any;
25
+ retries?: number;
24
26
  enableAnalytics: boolean = false;
25
27
  defaultFlagHandler?: (featureName: string) => DefaultFlag;
26
28
 
@@ -68,7 +70,7 @@ export class Flagsmith {
68
70
  requestTimeoutSeconds?: number;
69
71
  enableLocalEvaluation?: boolean;
70
72
  environmentRefreshIntervalSeconds?: number;
71
- retries?: any;
73
+ retries?: number;
72
74
  enableAnalytics?: boolean;
73
75
  defaultFlagHandler?: (featureName: string) => DefaultFlag;
74
76
  }) {
@@ -88,11 +90,17 @@ export class Flagsmith {
88
90
  this.environmentUrl = `${this.apiUrl}environment-document/`;
89
91
 
90
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
+ }
91
98
  this.environmentDataPollingManager = new EnvironmentDataPollingManager(
92
99
  this,
93
100
  this.environmentRefreshIntervalSeconds
94
101
  );
95
102
  this.environmentDataPollingManager.start();
103
+ this.updateEnvironment();
96
104
  }
97
105
 
98
106
  this.analyticsProcessor = data.enableAnalytics
@@ -128,20 +136,67 @@ export class Flagsmith {
128
136
  */
129
137
  getIdentityFlags(identifier: string, traits?: { [key: string]: any }): Promise<Flags> {
130
138
  traits = traits || {};
131
- if (this.environment) {
139
+ if (this.enableLocalEvaluation) {
132
140
  return new Promise(resolve =>
133
- resolve(this.getIdentityFlagsFromDocument(identifier, traits || {}))
141
+ this.environmentPromise!.then(() => {
142
+ resolve(this.getIdentityFlagsFromDocument(identifier, traits || {}));
143
+ })
134
144
  );
135
145
  }
136
146
  return this.getIdentityFlagsFromApi(identifier, traits);
137
147
  }
138
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
+
139
185
  /**
140
186
  * Updates the environment state for local flag evaluation.
187
+ * Sets a local promise to prevent race conditions in getIdentityFlags / getIdentitySegments.
141
188
  * You only need to call this if you wish to bypass environmentRefreshIntervalSeconds.
142
189
  */
143
190
  async updateEnvironment() {
144
- this.environment = await this.getEnvironmentFromApi();
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
+ }
145
200
  }
146
201
 
147
202
  private async getJSONResponse(
@@ -149,7 +204,7 @@ export class Flagsmith {
149
204
  method: string,
150
205
  body?: { [key: string]: any }
151
206
  ): Promise<any> {
152
- const headers: { [key: string]: any } = {'Content-Type': 'application/json'};
207
+ const headers: { [key: string]: any } = { 'Content-Type': 'application/json' };
153
208
  if (this.environmentKey) {
154
209
  headers['X-Environment-Key'] = this.environmentKey as string;
155
210
  }
@@ -182,6 +237,11 @@ export class Flagsmith {
182
237
  return data.json();
183
238
  }
184
239
 
240
+ /**
241
+ * This promise ensures that the environment is retrieved before attempting to locally evaluate.
242
+ */
243
+ private environmentPromise: Promise<any> | undefined;
244
+
185
245
  private async getEnvironmentFromApi() {
186
246
  const environment_data = await this.getJSONResponse(this.environmentUrl, 'GET');
187
247
  return buildEnvironmentModel(environment_data);
@@ -267,4 +327,3 @@ export class Flagsmith {
267
327
  }
268
328
 
269
329
  export default Flagsmith;
270
- // export = Flagsmith;
package/sdk/models.ts CHANGED
@@ -123,9 +123,9 @@ export class Flags {
123
123
  if (this.defaultFlagHandler) {
124
124
  return this.defaultFlagHandler(featureName);
125
125
  }
126
- throw new FlagsmithClientError(
127
- `Feature does not exist: ${featureName}, implement defaultFlagHandler to handle this case.`
128
- );
126
+
127
+ return { enabled: false, isDefault: true, value: undefined };
128
+
129
129
  }
130
130
 
131
131
  if (this.analyticsProcessor && flag.featureId) {
@@ -11,7 +11,44 @@
11
11
  },
12
12
  "id": 1,
13
13
  "hide_disabled_flags": false,
14
- "segments": []
14
+ "segments": [
15
+ {
16
+ "name": "regular_segment",
17
+ "feature_states": [
18
+ {
19
+ "feature_state_value": "segment_override",
20
+ "multivariate_feature_state_values": [],
21
+ "django_id": 81027,
22
+ "feature": {
23
+ "name": "some_feature",
24
+ "type": "STANDARD",
25
+ "id": 1
26
+ },
27
+ "enabled": false
28
+ }
29
+ ],
30
+ "id": 1,
31
+ "rules": [
32
+ {
33
+ "type": "ALL",
34
+ "conditions": [],
35
+ "rules": [
36
+ {
37
+ "type": "ANY",
38
+ "conditions": [
39
+ {
40
+ "value": "40",
41
+ "property_": "age",
42
+ "operator": "LESS_THAN"
43
+ }
44
+ ],
45
+ "rules": []
46
+ }
47
+ ]
48
+ }
49
+ ]
50
+ }
51
+ ]
15
52
  },
16
53
  "segment_overrides": [],
17
54
  "id": 1,
@@ -30,4 +67,4 @@
30
67
  "enabled": true
31
68
  }
32
69
  ]
33
- }
70
+ }
@@ -14,8 +14,10 @@ beforeEach(() => {
14
14
  });
15
15
 
16
16
  test('test_flagsmith_starts_polling_manager_on_init_if_enabled', () => {
17
+ // @ts-ignore
18
+ fetch.mockReturnValue(Promise.resolve(new Response(environmentJSON())));
17
19
  new Flagsmith({
18
- environmentKey: 'key',
20
+ environmentKey: 'ser.key',
19
21
  enableLocalEvaluation: true
20
22
  });
21
23
  expect(EnvironmentDataPollingManager).toBeCalled();
@@ -31,11 +33,28 @@ test('test_update_environment_sets_environment', async () => {
31
33
  // @ts-ignore
32
34
  flg.environment.featureStates[0].featurestateUUID = undefined;
33
35
  // @ts-ignore
36
+ flg.environment.project.segments[0].featureStates[0].featurestateUUID = undefined;
37
+ // @ts-ignore
34
38
  const model = environmentModel(JSON.parse(environmentJSON()));
35
39
  // @ts-ignore
36
40
  model.featureStates[0].featurestateUUID = undefined;
41
+ // @ts-ignore
42
+ model.project.segments[0].featureStates[0].featurestateUUID = undefined;
37
43
  expect(flg.environment).toStrictEqual(model);
38
44
  });
45
+
46
+ test('test_get_identity_segments', async () => {
47
+ // @ts-ignore
48
+ fetch.mockReturnValue(Promise.resolve(new Response(environmentJSON())));
49
+ const flg = new Flagsmith({
50
+ environmentKey: 'ser.key',
51
+ enableLocalEvaluation: true
52
+ });
53
+ const segments = await flg.getIdentitySegments('user', { age: 21 });
54
+ expect(segments[0].name).toEqual('regular_segment');
55
+ const segments2 = await flg.getIdentitySegments('user', { age: 41 });
56
+ expect(segments2.length).toEqual(0);
57
+ });
39
58
  test('test_get_environment_flags_calls_api_when_no_local_environment', async () => {
40
59
  // @ts-ignore
41
60
  fetch.mockReturnValue(Promise.resolve(new Response(flagsJSON())));