flagsmith-nodejs 1.1.0 → 2.0.0-beta.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.
- package/.github/workflows/pull_request.yaml +33 -0
- package/.gitmodules +3 -0
- package/.husky/pre-commit +6 -0
- package/{LICENCE.md → LICENCE} +4 -5
- package/README.md +1 -1
- package/build/flagsmith-engine/environments/integrations/models.d.ts +4 -0
- package/build/flagsmith-engine/environments/integrations/models.js +8 -0
- package/build/flagsmith-engine/environments/models.d.ts +25 -0
- package/build/flagsmith-engine/environments/models.js +40 -0
- package/build/flagsmith-engine/environments/util.d.ts +3 -0
- package/build/flagsmith-engine/environments/util.js +19 -0
- package/build/flagsmith-engine/features/constants.d.ts +4 -0
- package/build/flagsmith-engine/features/constants.js +7 -0
- package/build/flagsmith-engine/features/models.d.ts +32 -0
- package/build/flagsmith-engine/features/models.js +85 -0
- package/build/flagsmith-engine/features/util.d.ts +3 -0
- package/build/flagsmith-engine/features/util.js +20 -0
- package/build/flagsmith-engine/identities/models.d.ts +15 -0
- package/build/flagsmith-engine/identities/models.js +47 -0
- package/build/flagsmith-engine/identities/traits/models.d.ts +5 -0
- package/build/flagsmith-engine/identities/traits/models.js +12 -0
- package/build/flagsmith-engine/identities/util.d.ts +4 -0
- package/build/flagsmith-engine/identities/util.js +22 -0
- package/build/flagsmith-engine/index.d.ts +8 -0
- package/build/flagsmith-engine/index.js +61 -0
- package/build/flagsmith-engine/organisations/models.d.ts +9 -0
- package/build/flagsmith-engine/organisations/models.js +21 -0
- package/build/flagsmith-engine/organisations/util.d.ts +2 -0
- package/build/flagsmith-engine/organisations/util.js +8 -0
- package/build/flagsmith-engine/projects/models.d.ts +10 -0
- package/build/flagsmith-engine/projects/models.js +17 -0
- package/build/flagsmith-engine/projects/util.d.ts +2 -0
- package/build/flagsmith-engine/projects/util.js +15 -0
- package/build/flagsmith-engine/segments/constants.d.ts +26 -0
- package/build/flagsmith-engine/segments/constants.js +31 -0
- package/build/flagsmith-engine/segments/evaluators.d.ts +6 -0
- package/build/flagsmith-engine/segments/evaluators.js +29 -0
- package/build/flagsmith-engine/segments/models.d.ts +31 -0
- package/build/flagsmith-engine/segments/models.js +83 -0
- package/build/flagsmith-engine/segments/util.d.ts +4 -0
- package/build/flagsmith-engine/segments/util.js +23 -0
- package/build/flagsmith-engine/utils/collections.d.ts +4 -0
- package/build/flagsmith-engine/utils/collections.js +16 -0
- package/build/flagsmith-engine/utils/errors.d.ts +2 -0
- package/build/flagsmith-engine/utils/errors.js +6 -0
- package/build/flagsmith-engine/utils/hashing/index.d.ts +9 -0
- package/build/flagsmith-engine/utils/hashing/index.js +54 -0
- package/build/flagsmith-engine/utils/index.d.ts +1 -0
- package/build/flagsmith-engine/utils/index.js +14 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +11 -0
- package/build/sdk/analytics.d.ts +28 -0
- package/build/sdk/analytics.js +60 -0
- package/build/sdk/errors.d.ts +4 -0
- package/build/sdk/errors.js +9 -0
- package/build/sdk/index.d.ts +98 -0
- package/build/sdk/index.js +215 -0
- package/build/sdk/models.d.ts +55 -0
- package/build/sdk/models.js +102 -0
- package/build/sdk/polling_manager.d.ts +9 -0
- package/build/sdk/polling_manager.js +31 -0
- package/build/sdk/utils.d.ts +12 -0
- package/build/sdk/utils.js +45 -0
- package/example/README.md +0 -6
- package/example/package-lock.json +1070 -2
- package/example/package.json +4 -3
- package/example/server/api/index.js +18 -18
- package/example/server/index.js +4 -6
- package/flagsmith-engine/environments/integrations/models.ts +4 -0
- package/flagsmith-engine/environments/models.ts +50 -0
- package/flagsmith-engine/environments/util.ts +29 -0
- package/flagsmith-engine/features/constants.ts +4 -0
- package/flagsmith-engine/features/models.ts +105 -0
- package/flagsmith-engine/features/util.ts +38 -0
- package/flagsmith-engine/identities/models.ts +60 -0
- package/flagsmith-engine/identities/traits/models.ts +9 -0
- package/flagsmith-engine/identities/util.ts +30 -0
- package/flagsmith-engine/index.ts +93 -0
- package/flagsmith-engine/organisations/models.ts +25 -0
- package/flagsmith-engine/organisations/util.ts +11 -0
- package/flagsmith-engine/projects/models.ts +22 -0
- package/flagsmith-engine/projects/util.ts +18 -0
- package/flagsmith-engine/segments/constants.ts +31 -0
- package/flagsmith-engine/segments/evaluators.ts +72 -0
- package/flagsmith-engine/segments/models.ts +103 -0
- package/flagsmith-engine/segments/util.ts +29 -0
- package/flagsmith-engine/utils/collections.ts +14 -0
- package/flagsmith-engine/utils/errors.ts +1 -0
- package/flagsmith-engine/utils/hashing/index.ts +52 -0
- package/flagsmith-engine/utils/index.ts +10 -0
- package/index.ts +6 -0
- package/jest.config.js +5 -0
- package/package.json +28 -12
- package/sdk/analytics.ts +60 -0
- package/sdk/errors.ts +2 -0
- package/sdk/index.ts +270 -0
- package/sdk/models.ts +145 -0
- package/sdk/polling_manager.ts +31 -0
- package/sdk/utils.ts +45 -0
- package/tests/engine/e2e/engine.test.ts +51 -0
- package/tests/engine/engine-tests/engine-test-data/data/environment_n9fbf9h3v4fFgH3U3ngWhb.json +12393 -0
- package/tests/engine/engine-tests/engine-test-data/readme.md +30 -0
- package/tests/engine/unit/egine.test.ts +96 -0
- package/tests/engine/unit/environments/builder.test.ts +148 -0
- package/tests/engine/unit/environments/models.test.ts +49 -0
- package/tests/engine/unit/features/models.test.ts +72 -0
- package/tests/engine/unit/identities/identities_builders.test.ts +85 -0
- package/tests/engine/unit/identities/identities_models.test.ts +105 -0
- package/tests/engine/unit/organization/models.test.ts +12 -0
- package/tests/engine/unit/segments/segments_model.test.ts +101 -0
- package/tests/engine/unit/segments/util.ts +151 -0
- package/tests/engine/unit/utils.ts +114 -0
- package/tests/index.js +0 -0
- package/tests/sdk/analytics.test.ts +43 -0
- package/tests/sdk/data/environment.json +33 -0
- package/tests/sdk/data/flags.json +20 -0
- package/tests/sdk/data/identities.json +29 -0
- package/tests/sdk/flagsmith.test.ts +184 -0
- package/tests/sdk/polling.test.ts +34 -0
- package/tests/sdk/utils.ts +39 -0
- package/tsconfig.json +19 -0
- package/.idea/codeStyles/Project.xml +0 -52
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/flagsmith-core.js +0 -241
- package/index.d.ts +0 -85
- package/index.js +0 -3
package/sdk/index.ts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
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 } = {'Content-Type': 'application/json'};
|
|
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
|
+
return Flags.fromFeatureStateModels({
|
|
192
|
+
featureStates: getEnvironmentFeatureStates(this.environment),
|
|
193
|
+
analyticsProcessor: this.analyticsProcessor,
|
|
194
|
+
defaultFlagHandler: this.defaultFlagHandler
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private getIdentityFlagsFromDocument(identifier: string, traits: { [key: string]: any }) {
|
|
199
|
+
const identityModel = this.buildIdentityModel(
|
|
200
|
+
identifier,
|
|
201
|
+
Object.keys(traits).map(key => ({
|
|
202
|
+
key,
|
|
203
|
+
value: traits[key]
|
|
204
|
+
}))
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
const featureStates = getIdentityFeatureStates(this.environment, identityModel);
|
|
208
|
+
|
|
209
|
+
return Flags.fromFeatureStateModels({
|
|
210
|
+
featureStates: featureStates,
|
|
211
|
+
analyticsProcessor: this.analyticsProcessor,
|
|
212
|
+
defaultFlagHandler: this.defaultFlagHandler
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
private async getEnvironmentFlagsFromApi() {
|
|
217
|
+
try {
|
|
218
|
+
const apiFlags = await this.getJSONResponse(this.environmentFlagsUrl, 'GET');
|
|
219
|
+
return Flags.fromAPIFlags({
|
|
220
|
+
apiFlags: apiFlags,
|
|
221
|
+
analyticsProcessor: this.analyticsProcessor,
|
|
222
|
+
defaultFlagHandler: this.defaultFlagHandler
|
|
223
|
+
});
|
|
224
|
+
} catch (e) {
|
|
225
|
+
if (this.defaultFlagHandler) {
|
|
226
|
+
return new Flags({
|
|
227
|
+
flags: {},
|
|
228
|
+
defaultFlagHandler: this.defaultFlagHandler
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
throw e;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private async getIdentityFlagsFromApi(identifier: string, traits: { [key: string]: any }) {
|
|
237
|
+
try {
|
|
238
|
+
const data = generateIdentitiesData(identifier, traits);
|
|
239
|
+
const jsonResponse = await this.getJSONResponse(this.identitiesUrl, 'POST', data);
|
|
240
|
+
return Flags.fromAPIFlags({
|
|
241
|
+
apiFlags: jsonResponse['flags'],
|
|
242
|
+
analyticsProcessor: this.analyticsProcessor,
|
|
243
|
+
defaultFlagHandler: this.defaultFlagHandler
|
|
244
|
+
});
|
|
245
|
+
} catch (e) {
|
|
246
|
+
if (this.defaultFlagHandler) {
|
|
247
|
+
return new Flags({
|
|
248
|
+
flags: {},
|
|
249
|
+
defaultFlagHandler: this.defaultFlagHandler
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
throw e;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private buildIdentityModel(identifier: string, traits: { key: string; value: any }[]) {
|
|
258
|
+
if (!this.environment) {
|
|
259
|
+
throw new FlagsmithClientError(
|
|
260
|
+
'Unable to build identity model when no local environment present.'
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const traitModels = traits.map(trait => new TraitModel(trait.key, trait.value));
|
|
265
|
+
return new IdentityModel('0', traitModels, [], this.environment.apiKey, identifier);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export default Flagsmith;
|
|
270
|
+
// export = 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
|
+
throw new FlagsmithClientError(
|
|
127
|
+
`Feature does not exist: ${featureName}, implement defaultFlagHandler to handle this case.`
|
|
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
|
+
});
|