flagsmith-nodejs 1.1.2 → 1.1.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 (62) hide show
  1. package/package.json +1 -1
  2. package/.idea/flagsmith-nodejs-client.iml +0 -12
  3. package/.idea/modules.xml +0 -8
  4. package/.idea/vcs.xml +0 -6
  5. package/build/flagsmith-engine/environments/integrations/models.d.ts +0 -4
  6. package/build/flagsmith-engine/environments/integrations/models.js +0 -8
  7. package/build/flagsmith-engine/environments/models.d.ts +0 -25
  8. package/build/flagsmith-engine/environments/models.js +0 -40
  9. package/build/flagsmith-engine/environments/util.d.ts +0 -3
  10. package/build/flagsmith-engine/environments/util.js +0 -19
  11. package/build/flagsmith-engine/features/constants.d.ts +0 -4
  12. package/build/flagsmith-engine/features/constants.js +0 -7
  13. package/build/flagsmith-engine/features/models.d.ts +0 -32
  14. package/build/flagsmith-engine/features/models.js +0 -85
  15. package/build/flagsmith-engine/features/util.d.ts +0 -3
  16. package/build/flagsmith-engine/features/util.js +0 -20
  17. package/build/flagsmith-engine/identities/models.d.ts +0 -15
  18. package/build/flagsmith-engine/identities/models.js +0 -47
  19. package/build/flagsmith-engine/identities/traits/models.d.ts +0 -5
  20. package/build/flagsmith-engine/identities/traits/models.js +0 -12
  21. package/build/flagsmith-engine/identities/util.d.ts +0 -4
  22. package/build/flagsmith-engine/identities/util.js +0 -22
  23. package/build/flagsmith-engine/index.d.ts +0 -8
  24. package/build/flagsmith-engine/index.js +0 -61
  25. package/build/flagsmith-engine/organisations/models.d.ts +0 -9
  26. package/build/flagsmith-engine/organisations/models.js +0 -21
  27. package/build/flagsmith-engine/organisations/util.d.ts +0 -2
  28. package/build/flagsmith-engine/organisations/util.js +0 -8
  29. package/build/flagsmith-engine/projects/models.d.ts +0 -10
  30. package/build/flagsmith-engine/projects/models.js +0 -17
  31. package/build/flagsmith-engine/projects/util.d.ts +0 -2
  32. package/build/flagsmith-engine/projects/util.js +0 -15
  33. package/build/flagsmith-engine/segments/constants.d.ts +0 -26
  34. package/build/flagsmith-engine/segments/constants.js +0 -31
  35. package/build/flagsmith-engine/segments/evaluators.d.ts +0 -6
  36. package/build/flagsmith-engine/segments/evaluators.js +0 -29
  37. package/build/flagsmith-engine/segments/models.d.ts +0 -31
  38. package/build/flagsmith-engine/segments/models.js +0 -83
  39. package/build/flagsmith-engine/segments/util.d.ts +0 -4
  40. package/build/flagsmith-engine/segments/util.js +0 -23
  41. package/build/flagsmith-engine/utils/collections.d.ts +0 -4
  42. package/build/flagsmith-engine/utils/collections.js +0 -16
  43. package/build/flagsmith-engine/utils/errors.d.ts +0 -2
  44. package/build/flagsmith-engine/utils/errors.js +0 -6
  45. package/build/flagsmith-engine/utils/hashing/index.d.ts +0 -9
  46. package/build/flagsmith-engine/utils/hashing/index.js +0 -54
  47. package/build/flagsmith-engine/utils/index.d.ts +0 -1
  48. package/build/flagsmith-engine/utils/index.js +0 -14
  49. package/build/index.d.ts +0 -1
  50. package/build/index.js +0 -11
  51. package/build/sdk/analytics.d.ts +0 -28
  52. package/build/sdk/analytics.js +0 -60
  53. package/build/sdk/errors.d.ts +0 -4
  54. package/build/sdk/errors.js +0 -9
  55. package/build/sdk/index.d.ts +0 -98
  56. package/build/sdk/index.js +0 -215
  57. package/build/sdk/models.d.ts +0 -55
  58. package/build/sdk/models.js +0 -102
  59. package/build/sdk/polling_manager.d.ts +0 -9
  60. package/build/sdk/polling_manager.js +0 -31
  61. package/build/sdk/utils.d.ts +0 -12
  62. package/build/sdk/utils.js +0 -45
@@ -1,215 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Flagsmith = void 0;
4
- const flagsmith_engine_1 = require("../flagsmith-engine");
5
- const util_1 = require("../flagsmith-engine/environments/util");
6
- const models_1 = require("../flagsmith-engine/identities/models");
7
- const models_2 = require("../flagsmith-engine/identities/traits/models");
8
- const analytics_1 = require("./analytics");
9
- const errors_1 = require("./errors");
10
- const models_3 = require("./models");
11
- const polling_manager_1 = require("./polling_manager");
12
- const utils_1 = require("./utils");
13
- const DEFAULT_API_URL = 'https://api.flagsmith.com/api/v1/';
14
- class Flagsmith {
15
- environmentKey;
16
- apiUrl = DEFAULT_API_URL;
17
- customHeaders;
18
- requestTimeoutSeconds;
19
- enableLocalEvaluation = false;
20
- environmentRefreshIntervalSeconds = 60;
21
- retries;
22
- enableAnalytics = false;
23
- defaultFlagHandler;
24
- environmentFlagsUrl;
25
- identitiesUrl;
26
- environmentUrl;
27
- environmentDataPollingManager;
28
- environment;
29
- analyticsProcessor;
30
- /**
31
- * A Flagsmith client.
32
- *
33
- * Provides an interface for interacting with the Flagsmith http API.
34
- * Basic Usage::
35
- *
36
- * import flagsmith from Flagsmith
37
- * const flagsmith = new Flagsmith({environmentKey: '<your API key>'});
38
- * const environmentFlags = flagsmith.getEnvironmentFlags();
39
- * const featureEnabled = environmentFlags.isFeatureEnabled('foo');
40
- * const identityFlags = flagsmith.getIdentityFlags('identifier', {'foo': 'bar'});
41
- * const featureEnabledForIdentity = identityFlags.isFeatureEnabled("foo")
42
- *
43
- * @param {string} data.environmentKey: The environment key obtained from Flagsmith interface
44
- @param {string} data.apiUrl: Override the URL of the Flagsmith API to communicate with
45
- @param data.customHeaders: Additional headers to add to requests made to the
46
- Flagsmith API
47
- @param {number} data.requestTimeoutSeconds: Number of seconds to wait for a request to
48
- complete before terminating the request
49
- @param {boolean} data.enableLocalEvaluation: Enables local evaluation of flags
50
- @param {number} data.environmentRefreshIntervalSeconds: If using local evaluation,
51
- specify the interval period between refreshes of local environment data
52
- @param {number} data.retries: a urllib3.Retry object to use on all http requests to the
53
- Flagsmith API
54
- @param {boolean} data.enableAnalytics: if enabled, sends additional requests to the Flagsmith
55
- API to power flag analytics charts
56
- @param data.defaultFlagHandler: callable which will be used in the case where
57
- flags cannot be retrieved from the API or a non existent feature is
58
- requested
59
- */
60
- constructor(data) {
61
- this.environmentKey = data.environmentKey;
62
- this.apiUrl = data.apiUrl || this.apiUrl;
63
- this.customHeaders = data.customHeaders;
64
- this.requestTimeoutSeconds = data.requestTimeoutSeconds;
65
- this.enableLocalEvaluation = data.enableLocalEvaluation;
66
- this.environmentRefreshIntervalSeconds =
67
- data.environmentRefreshIntervalSeconds || this.environmentRefreshIntervalSeconds;
68
- this.retries = data.retries;
69
- this.enableAnalytics = data.enableAnalytics || false;
70
- this.defaultFlagHandler = data.defaultFlagHandler;
71
- this.environmentFlagsUrl = `${this.apiUrl}flags/`;
72
- this.identitiesUrl = `${this.apiUrl}identities/`;
73
- this.environmentUrl = `${this.apiUrl}environment-document/`;
74
- if (this.enableLocalEvaluation) {
75
- this.environmentDataPollingManager = new polling_manager_1.EnvironmentDataPollingManager(this, this.environmentRefreshIntervalSeconds);
76
- this.environmentDataPollingManager.start();
77
- }
78
- this.analyticsProcessor = data.enableAnalytics
79
- ? new analytics_1.AnalyticsProcessor({
80
- environmentKey: this.environmentKey,
81
- baseApiUrl: this.apiUrl,
82
- timeout: this.requestTimeoutSeconds
83
- })
84
- : undefined;
85
- }
86
- /**
87
- * Get all the default for flags for the current environment.
88
- *
89
- * @returns Flags object holding all the flags for the current environment.
90
- */
91
- async getEnvironmentFlags() {
92
- if (this.environment) {
93
- return new Promise(resolve => resolve(this.getEnvironmentFlagsFromDocument()));
94
- }
95
- return this.getEnvironmentFlagsFromApi();
96
- }
97
- /**
98
- * Get all the flags for the current environment for a given identity. Will also
99
- upsert all traits to the Flagsmith API for future evaluations. Providing a
100
- trait with a value of None will remove the trait from the identity if it exists.
101
- *
102
- * @param {string} identifier a unique identifier for the identity in the current
103
- environment, e.g. email address, username, uuid
104
- * @param {{[key:string]:any}} traits? a dictionary of traits to add / update on the identity in
105
- Flagsmith, e.g. {"num_orders": 10}
106
- * @returns Flags object holding all the flags for the given identity.
107
- */
108
- getIdentityFlags(identifier, traits) {
109
- traits = traits || {};
110
- if (this.environment) {
111
- return new Promise(resolve => resolve(this.getIdentityFlagsFromDocument(identifier, traits || {})));
112
- }
113
- return this.getIdentityFlagsFromApi(identifier, traits);
114
- }
115
- /**
116
- * Updates the environment state for local flag evaluation.
117
- * You only need to call this if you wish to bypass environmentRefreshIntervalSeconds.
118
- */
119
- async updateEnvironment() {
120
- this.environment = await this.getEnvironmentFromApi();
121
- }
122
- async getJSONResponse(url, method, body) {
123
- const headers = { 'Content-Type': 'application/json' };
124
- if (this.environmentKey) {
125
- headers['X-Environment-Key'] = this.environmentKey;
126
- }
127
- if (this.customHeaders) {
128
- for (const [k, v] of Object.entries(this.customHeaders)) {
129
- headers[k] = v;
130
- }
131
- }
132
- const data = await (0, utils_1.retryFetch)(url, {
133
- method: method,
134
- timeout: this.requestTimeoutSeconds || undefined,
135
- body: JSON.stringify(body),
136
- headers: headers
137
- }, this.retries, 1000, (this.requestTimeoutSeconds || 10) * 1000);
138
- if (data.status !== 200) {
139
- throw new errors_1.FlagsmithAPIError(`Invalid request made to Flagsmith API. Response status code: ${data.status}`);
140
- }
141
- return data.json();
142
- }
143
- async getEnvironmentFromApi() {
144
- const environment_data = await this.getJSONResponse(this.environmentUrl, 'GET');
145
- return (0, util_1.buildEnvironmentModel)(environment_data);
146
- }
147
- getEnvironmentFlagsFromDocument() {
148
- return models_3.Flags.fromFeatureStateModels({
149
- featureStates: (0, flagsmith_engine_1.getEnvironmentFeatureStates)(this.environment),
150
- analyticsProcessor: this.analyticsProcessor,
151
- defaultFlagHandler: this.defaultFlagHandler
152
- });
153
- }
154
- getIdentityFlagsFromDocument(identifier, traits) {
155
- const identityModel = this.buildIdentityModel(identifier, Object.keys(traits).map(key => ({
156
- key,
157
- value: traits[key]
158
- })));
159
- const featureStates = (0, flagsmith_engine_1.getIdentityFeatureStates)(this.environment, identityModel);
160
- return models_3.Flags.fromFeatureStateModels({
161
- featureStates: featureStates,
162
- analyticsProcessor: this.analyticsProcessor,
163
- defaultFlagHandler: this.defaultFlagHandler
164
- });
165
- }
166
- async getEnvironmentFlagsFromApi() {
167
- try {
168
- const apiFlags = await this.getJSONResponse(this.environmentFlagsUrl, 'GET');
169
- return models_3.Flags.fromAPIFlags({
170
- apiFlags: apiFlags,
171
- analyticsProcessor: this.analyticsProcessor,
172
- defaultFlagHandler: this.defaultFlagHandler
173
- });
174
- }
175
- catch (e) {
176
- if (this.defaultFlagHandler) {
177
- return new models_3.Flags({
178
- flags: {},
179
- defaultFlagHandler: this.defaultFlagHandler
180
- });
181
- }
182
- throw e;
183
- }
184
- }
185
- async getIdentityFlagsFromApi(identifier, traits) {
186
- try {
187
- const data = (0, utils_1.generateIdentitiesData)(identifier, traits);
188
- const jsonResponse = await this.getJSONResponse(this.identitiesUrl, 'POST', data);
189
- return models_3.Flags.fromAPIFlags({
190
- apiFlags: jsonResponse['flags'],
191
- analyticsProcessor: this.analyticsProcessor,
192
- defaultFlagHandler: this.defaultFlagHandler
193
- });
194
- }
195
- catch (e) {
196
- if (this.defaultFlagHandler) {
197
- return new models_3.Flags({
198
- flags: {},
199
- defaultFlagHandler: this.defaultFlagHandler
200
- });
201
- }
202
- throw e;
203
- }
204
- }
205
- buildIdentityModel(identifier, traits) {
206
- if (!this.environment) {
207
- throw new errors_1.FlagsmithClientError('Unable to build identity model when no local environment present.');
208
- }
209
- const traitModels = traits.map(trait => new models_2.TraitModel(trait.key, trait.value));
210
- return new models_1.IdentityModel('0', traitModels, [], this.environment.apiKey, identifier);
211
- }
212
- }
213
- exports.Flagsmith = Flagsmith;
214
- exports.default = Flagsmith;
215
- // export = Flagsmith;
@@ -1,55 +0,0 @@
1
- import { FeatureStateModel } from '../flagsmith-engine/features/models';
2
- import { AnalyticsProcessor } from './analytics';
3
- export declare class BaseFlag {
4
- enabled: boolean;
5
- value: string | number | boolean | undefined;
6
- isDefault: boolean;
7
- constructor(value: string | number | boolean | undefined, enabled: boolean, isDefault: boolean);
8
- }
9
- export declare class DefaultFlag extends BaseFlag {
10
- constructor(value: string | number | boolean | undefined, enabled: boolean);
11
- }
12
- export declare class Flag extends BaseFlag {
13
- featureId: number;
14
- featureName: string;
15
- constructor(params: {
16
- value: string | number | boolean | undefined;
17
- enabled: boolean;
18
- isDefault?: boolean;
19
- featureId: number;
20
- featureName: string;
21
- });
22
- static fromFeatureStateModel(fsm: FeatureStateModel, identityId: number | string | undefined): Flag;
23
- static fromAPIFlag(flagData: any): Flag;
24
- }
25
- export declare class Flags {
26
- flags: {
27
- [key: string]: Flag;
28
- };
29
- defaultFlagHandler?: (featureName: string) => DefaultFlag;
30
- analyticsProcessor?: AnalyticsProcessor;
31
- constructor(data: {
32
- flags: {
33
- [key: string]: Flag;
34
- };
35
- defaultFlagHandler?: (v: string) => DefaultFlag;
36
- analyticsProcessor?: AnalyticsProcessor;
37
- });
38
- static fromFeatureStateModels(data: {
39
- featureStates: FeatureStateModel[];
40
- analyticsProcessor?: AnalyticsProcessor;
41
- defaultFlagHandler?: (f: string) => DefaultFlag;
42
- identityID?: string | number;
43
- }): Flags;
44
- static fromAPIFlags(data: {
45
- apiFlags: {
46
- [key: string]: any;
47
- }[];
48
- analyticsProcessor?: AnalyticsProcessor;
49
- defaultFlagHandler?: (v: string) => DefaultFlag;
50
- }): Flags;
51
- allFlags(): Flag[];
52
- getFlag(featureName: string): BaseFlag;
53
- getFeatureValue(featureName: string): any;
54
- isFeatureEnabled(featureName: string): boolean;
55
- }
@@ -1,102 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Flags = exports.Flag = exports.DefaultFlag = exports.BaseFlag = void 0;
4
- const errors_1 = require("./errors");
5
- class BaseFlag {
6
- enabled;
7
- value;
8
- isDefault;
9
- constructor(value, enabled, isDefault) {
10
- this.value = value;
11
- this.enabled = enabled;
12
- this.isDefault = isDefault;
13
- }
14
- }
15
- exports.BaseFlag = BaseFlag;
16
- class DefaultFlag extends BaseFlag {
17
- constructor(value, enabled) {
18
- super(value, enabled, true);
19
- }
20
- }
21
- exports.DefaultFlag = DefaultFlag;
22
- class Flag extends BaseFlag {
23
- featureId;
24
- featureName;
25
- constructor(params) {
26
- super(params.value, params.enabled, !!params.isDefault);
27
- this.featureId = params.featureId;
28
- this.featureName = params.featureName;
29
- }
30
- static fromFeatureStateModel(fsm, identityId) {
31
- return new Flag({
32
- value: fsm.getValue(identityId),
33
- enabled: fsm.enabled,
34
- featureId: fsm.feature.id,
35
- featureName: fsm.feature.name
36
- });
37
- }
38
- static fromAPIFlag(flagData) {
39
- return new Flag({
40
- enabled: flagData['enabled'],
41
- value: flagData['feature_state_value'] || flagData['value'],
42
- featureId: flagData['feature']['id'],
43
- featureName: flagData['feature']['name']
44
- });
45
- }
46
- }
47
- exports.Flag = Flag;
48
- class Flags {
49
- flags = {};
50
- defaultFlagHandler;
51
- analyticsProcessor;
52
- constructor(data) {
53
- this.flags = data.flags;
54
- this.defaultFlagHandler = data.defaultFlagHandler;
55
- this.analyticsProcessor = data.analyticsProcessor;
56
- }
57
- static fromFeatureStateModels(data) {
58
- const flags = {};
59
- for (const fs of data.featureStates) {
60
- flags[fs.feature.name] = Flag.fromFeatureStateModel(fs, data.identityID);
61
- }
62
- return new Flags({
63
- flags: flags,
64
- defaultFlagHandler: data.defaultFlagHandler,
65
- analyticsProcessor: data.analyticsProcessor
66
- });
67
- }
68
- static fromAPIFlags(data) {
69
- const flags = {};
70
- for (const flagData of data.apiFlags) {
71
- flags[flagData['feature']['name']] = Flag.fromAPIFlag(flagData);
72
- }
73
- return new Flags({
74
- flags: flags,
75
- defaultFlagHandler: data.defaultFlagHandler,
76
- analyticsProcessor: data.analyticsProcessor
77
- });
78
- }
79
- allFlags() {
80
- return Object.values(this.flags);
81
- }
82
- getFlag(featureName) {
83
- const flag = this.flags[featureName];
84
- if (!flag) {
85
- if (this.defaultFlagHandler) {
86
- return this.defaultFlagHandler(featureName);
87
- }
88
- throw new errors_1.FlagsmithClientError(`Feature does not exist: ${featureName}, implement defaultFlagHandler to handle this case.`);
89
- }
90
- if (this.analyticsProcessor && flag.featureId) {
91
- this.analyticsProcessor.trackFeature(flag.featureId);
92
- }
93
- return flag;
94
- }
95
- getFeatureValue(featureName) {
96
- return this.getFlag(featureName).value;
97
- }
98
- isFeatureEnabled(featureName) {
99
- return this.getFlag(featureName).enabled;
100
- }
101
- }
102
- exports.Flags = Flags;
@@ -1,9 +0,0 @@
1
- import Flagsmith from '.';
2
- export declare class EnvironmentDataPollingManager {
3
- private interval?;
4
- private main;
5
- private refreshIntervalSeconds;
6
- constructor(main: Flagsmith, refreshIntervalSeconds: number);
7
- start(): void;
8
- stop(): void;
9
- }
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EnvironmentDataPollingManager = void 0;
4
- class EnvironmentDataPollingManager {
5
- interval;
6
- main;
7
- refreshIntervalSeconds;
8
- constructor(main, refreshIntervalSeconds) {
9
- this.main = main;
10
- this.refreshIntervalSeconds = refreshIntervalSeconds;
11
- }
12
- start() {
13
- const updateEnvironment = () => {
14
- if (this.interval)
15
- 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
- stop() {
25
- if (!this.interval) {
26
- return;
27
- }
28
- clearInterval(this.interval);
29
- }
30
- }
31
- exports.EnvironmentDataPollingManager = EnvironmentDataPollingManager;
@@ -1,12 +0,0 @@
1
- import { Response } from 'node-fetch';
2
- export declare function generateIdentitiesData(identifier: string, traits?: {
3
- [key: string]: any;
4
- }): {
5
- identifier: string;
6
- traits: {
7
- trait_key: string;
8
- trait_value: any;
9
- }[];
10
- };
11
- export declare const delay: (ms: number) => Promise<unknown>;
12
- export declare const retryFetch: (url: string, fetchOptions: {} | undefined, retries: number | undefined, retryDelay: number | undefined, timeout: number) => Promise<Response>;
@@ -1,45 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.retryFetch = exports.delay = exports.generateIdentitiesData = void 0;
7
- const node_fetch_1 = __importDefault(require("node-fetch"));
8
- // @ts-ignore
9
- if (typeof node_fetch_1.default.default !== 'undefined')
10
- node_fetch_1.default = node_fetch_1.default.default;
11
- function generateIdentitiesData(identifier, traits) {
12
- const traitsGenerated = Object.entries(traits || {}).map(trait => ({
13
- trait_key: trait[0],
14
- trait_value: trait[1]
15
- }));
16
- return {
17
- identifier: identifier,
18
- traits: traitsGenerated
19
- };
20
- }
21
- exports.generateIdentitiesData = generateIdentitiesData;
22
- const delay = (ms) => new Promise(resolve => setTimeout(() => resolve(undefined), ms));
23
- exports.delay = delay;
24
- const retryFetch = (url, fetchOptions = {}, retries = 3, retryDelay = 1000, timeout) => {
25
- return new Promise((resolve, reject) => {
26
- // check for timeout
27
- if (timeout)
28
- setTimeout(() => reject('error: timeout'), timeout);
29
- const wrapper = (n) => {
30
- (0, node_fetch_1.default)(url, fetchOptions)
31
- .then(res => resolve(res))
32
- .catch(async (err) => {
33
- if (n > 0) {
34
- await (0, exports.delay)(retryDelay);
35
- wrapper(--n);
36
- }
37
- else {
38
- reject(err);
39
- }
40
- });
41
- };
42
- wrapper(retries);
43
- });
44
- };
45
- exports.retryFetch = retryFetch;