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
@@ -0,0 +1,203 @@
1
+ import Flagsmith from '../../sdk';
2
+ import { EnvironmentDataPollingManager } from '../../sdk/polling_manager';
3
+ import fetch, { Headers } from 'node-fetch';
4
+ import { environmentJSON, environmentModel, flagsJSON, flagsmith, identitiesJSON } from './utils';
5
+ import { DefaultFlag } from '../../sdk/models';
6
+
7
+ jest.mock('node-fetch');
8
+ jest.mock('../../sdk/polling_manager');
9
+ const { Response } = jest.requireActual('node-fetch');
10
+
11
+ beforeEach(() => {
12
+ // @ts-ignore
13
+ jest.clearAllMocks();
14
+ });
15
+
16
+ test('test_flagsmith_starts_polling_manager_on_init_if_enabled', () => {
17
+ // @ts-ignore
18
+ fetch.mockReturnValue(Promise.resolve(new Response(environmentJSON())));
19
+ new Flagsmith({
20
+ environmentKey: 'ser.key',
21
+ enableLocalEvaluation: true
22
+ });
23
+ expect(EnvironmentDataPollingManager).toBeCalled();
24
+ });
25
+
26
+ test('test_update_environment_sets_environment', async () => {
27
+ // @ts-ignore
28
+ fetch.mockReturnValue(Promise.resolve(new Response(environmentJSON())));
29
+ const flg = flagsmith();
30
+ await flg.updateEnvironment();
31
+ expect(flg.environment).toBeDefined();
32
+
33
+ // @ts-ignore
34
+ flg.environment.featureStates[0].featurestateUUID = undefined;
35
+ // @ts-ignore
36
+ flg.environment.project.segments[0].featureStates[0].featurestateUUID = undefined;
37
+ // @ts-ignore
38
+ const model = environmentModel(JSON.parse(environmentJSON()));
39
+ // @ts-ignore
40
+ model.featureStates[0].featurestateUUID = undefined;
41
+ // @ts-ignore
42
+ model.project.segments[0].featureStates[0].featurestateUUID = undefined;
43
+ expect(flg.environment).toStrictEqual(model);
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
+ });
58
+ test('test_get_environment_flags_calls_api_when_no_local_environment', async () => {
59
+ // @ts-ignore
60
+ fetch.mockReturnValue(Promise.resolve(new Response(flagsJSON())));
61
+
62
+ const flg = flagsmith();
63
+ const allFlags = await (await flg.getEnvironmentFlags()).allFlags();
64
+
65
+ expect(fetch).toBeCalledTimes(1);
66
+ expect(allFlags[0].enabled).toBe(true);
67
+ expect(allFlags[0].value).toBe('some-value');
68
+ expect(allFlags[0].featureName).toBe('some_feature');
69
+ });
70
+ test('test_get_environment_flags_uses_local_environment_when_available', async () => {
71
+ // @ts-ignore
72
+ fetch.mockReturnValue(Promise.resolve(new Response(flagsJSON())));
73
+
74
+ const flg = flagsmith();
75
+ const model = environmentModel(JSON.parse(environmentJSON()));
76
+ flg.environment = model;
77
+
78
+ const allFlags = await (await flg.getEnvironmentFlags()).allFlags();
79
+ expect(fetch).toBeCalledTimes(0);
80
+ expect(allFlags[0].enabled).toBe(model.featureStates[0].enabled);
81
+ expect(allFlags[0].value).toBe(model.featureStates[0].getValue());
82
+ expect(allFlags[0].featureName).toBe(model.featureStates[0].feature.name);
83
+ });
84
+ test('test_get_identity_flags_calls_api_when_no_local_environment_no_traits', async () => {
85
+ // @ts-ignore
86
+ fetch.mockReturnValue(Promise.resolve(new Response(identitiesJSON())));
87
+ const identifier = 'identifier';
88
+
89
+ const flg = flagsmith();
90
+
91
+ const identityFlags = (await flg.getIdentityFlags(identifier)).allFlags();
92
+
93
+ expect(identityFlags[0].enabled).toBe(true);
94
+ expect(identityFlags[0].value).toBe('some-value');
95
+ expect(identityFlags[0].featureName).toBe('some_feature');
96
+ });
97
+ test('test_get_identity_flags_calls_api_when_no_local_environment_with_traits', async () => {
98
+ // @ts-ignore
99
+ fetch.mockReturnValue(Promise.resolve(new Response(identitiesJSON())));
100
+ const identifier = 'identifier';
101
+ const traits = { some_trait: 'some_value' };
102
+ const flg = flagsmith();
103
+
104
+ const identityFlags = (await flg.getIdentityFlags(identifier, traits)).allFlags();
105
+
106
+ expect(identityFlags[0].enabled).toBe(true);
107
+ expect(identityFlags[0].value).toBe('some-value');
108
+ expect(identityFlags[0].featureName).toBe('some_feature');
109
+ });
110
+
111
+ test('test_default_flag_is_used_when_no_environment_flags_returned', async () => {
112
+ // @ts-ignore
113
+ fetch.mockReturnValue(Promise.resolve(new Response(JSON.stringify([]))));
114
+
115
+ const defaultFlag = new DefaultFlag('some-default-value', true);
116
+
117
+ const defaultFlagHandler = (featureName: string) => defaultFlag;
118
+
119
+ const flg = new Flagsmith({
120
+ environmentKey: 'key',
121
+ defaultFlagHandler: defaultFlagHandler
122
+ });
123
+
124
+ const flags = await flg.getEnvironmentFlags();
125
+ const flag = flags.getFlag('some_feature');
126
+ expect(flag.isDefault).toBe(true);
127
+ expect(flag.enabled).toBe(defaultFlag.enabled);
128
+ expect(flag.value).toBe(defaultFlag.value);
129
+ });
130
+
131
+ test('test_non_200_response_raises_flagsmith_api_error', async () => {
132
+ const errorResponse403 = new Response('403 Forbidden', {
133
+ status: 403
134
+ });
135
+ // @ts-ignore
136
+ fetch.mockReturnValue(Promise.resolve(errorResponse403));
137
+
138
+ const flg = new Flagsmith({
139
+ environmentKey: 'some'
140
+ });
141
+
142
+ await expect(flg.getEnvironmentFlags()).rejects.toThrow();
143
+ });
144
+ test('test_default_flag_is_not_used_when_environment_flags_returned', async () => {
145
+ // @ts-ignore
146
+ fetch.mockReturnValue(Promise.resolve(new Response(flagsJSON())));
147
+
148
+ const defaultFlag = new DefaultFlag('some-default-value', true);
149
+
150
+ const defaultFlagHandler = (featureName: string) => defaultFlag;
151
+
152
+ const flg = new Flagsmith({
153
+ environmentKey: 'key',
154
+ defaultFlagHandler: defaultFlagHandler
155
+ });
156
+
157
+ const flags = await flg.getEnvironmentFlags();
158
+ const flag = flags.getFlag('some_feature');
159
+
160
+ expect(flag.isDefault).toBe(false);
161
+ expect(flag.value).not.toBe(defaultFlag.value);
162
+ expect(flag.value).toBe('some-value');
163
+ });
164
+ test('test_default_flag_is_not_used_when_identity_flags_returned', async () => {
165
+ // @ts-ignore
166
+ fetch.mockReturnValue(Promise.resolve(new Response(identitiesJSON())));
167
+
168
+ const defaultFlag = new DefaultFlag('some-default-value', true);
169
+
170
+ const defaultFlagHandler = (featureName: string) => defaultFlag;
171
+
172
+ const flg = new Flagsmith({
173
+ environmentKey: 'key',
174
+ defaultFlagHandler: defaultFlagHandler
175
+ });
176
+
177
+ const flags = await flg.getIdentityFlags('identifier');
178
+ const flag = flags.getFlag('some_feature');
179
+
180
+ expect(flag.isDefault).toBe(false);
181
+ expect(flag.value).not.toBe(defaultFlag.value);
182
+ expect(flag.value).toBe('some-value');
183
+ });
184
+
185
+ test('test_default_flag_is_used_when_no_identity_flags_returned', async () => {
186
+ // @ts-ignore
187
+ fetch.mockReturnValue(Promise.resolve(new Response(JSON.stringify({ flags: [], traits: [] }))));
188
+
189
+ const defaultFlag = new DefaultFlag('some-default-value', true);
190
+ const defaultFlagHandler = (featureName: string) => defaultFlag;
191
+
192
+ const flg = new Flagsmith({
193
+ environmentKey: 'key',
194
+ defaultFlagHandler: defaultFlagHandler
195
+ });
196
+
197
+ const flags = await flg.getIdentityFlags('identifier');
198
+ const flag = flags.getFlag('some_feature');
199
+
200
+ expect(flag.isDefault).toBe(true);
201
+ expect(flag.value).toBe(defaultFlag.value);
202
+ expect(flag.enabled).toBe(defaultFlag.enabled);
203
+ });
@@ -0,0 +1,34 @@
1
+ import Flagsmith from '../../sdk';
2
+ import { EnvironmentDataPollingManager } from '../../sdk/polling_manager';
3
+ import { delay } from '../../sdk/utils';
4
+ jest.mock('../../sdk');
5
+ jest.mock('node-fetch');
6
+
7
+ beforeEach(() => {
8
+ // @ts-ignore
9
+ Flagsmith.mockClear();
10
+ });
11
+
12
+ test('test_polling_manager_calls_update_environment_on_start', async () => {
13
+ const flagsmith = new Flagsmith({
14
+ environmentKey: 'key'
15
+ });
16
+
17
+ const pollingManager = new EnvironmentDataPollingManager(flagsmith, 0.1);
18
+ pollingManager.start();
19
+ await delay(500);
20
+ pollingManager.stop();
21
+ expect(flagsmith.updateEnvironment).toHaveBeenCalled();
22
+ });
23
+
24
+ test('test_polling_manager_calls_update_environment_on_each_refresh', async () => {
25
+ const flagsmith = new Flagsmith({
26
+ environmentKey: 'key'
27
+ });
28
+
29
+ const pollingManager = new EnvironmentDataPollingManager(flagsmith, 0.1);
30
+ pollingManager.start();
31
+ await delay(450);
32
+ pollingManager.stop();
33
+ expect(flagsmith.updateEnvironment).toHaveBeenCalledTimes(5);
34
+ });
@@ -0,0 +1,39 @@
1
+ import { readFileSync } from 'fs';
2
+ import { buildEnvironmentModel } from '../../flagsmith-engine/environments/util';
3
+ import { AnalyticsProcessor } from '../../sdk/analytics';
4
+ import Flagsmith from '../../sdk';
5
+
6
+ const DATA_DIR = __dirname + '/data/';
7
+
8
+ export function analyticsProcessor() {
9
+ return new AnalyticsProcessor({
10
+ environmentKey: 'test-key',
11
+ baseApiUrl: 'http://testUrl'
12
+ });
13
+ }
14
+
15
+ export function apiKey(): string {
16
+ return 'sometestfakekey';
17
+ }
18
+
19
+ export function flagsmith() {
20
+ return new Flagsmith({
21
+ environmentKey: apiKey()
22
+ });
23
+ }
24
+
25
+ export function environmentJSON() {
26
+ return readFileSync(DATA_DIR + 'environment.json', 'utf-8');
27
+ }
28
+
29
+ export function environmentModel(environmentJSON: any) {
30
+ return buildEnvironmentModel(environmentJSON);
31
+ }
32
+
33
+ export function flagsJSON() {
34
+ return readFileSync(DATA_DIR + 'flags.json', 'utf-8');
35
+ }
36
+
37
+ export function identitiesJSON() {
38
+ return readFileSync(DATA_DIR + 'identities.json', 'utf-8');
39
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "include": [
3
+ "./flagsmith-engine",
4
+ "./sdk",
5
+ "./index.ts"
6
+ ],
7
+ "compilerOptions": {
8
+ "outDir": "./build",
9
+ "target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
10
+ /* Modules */
11
+ "module": "commonjs", /* Specify what module code is generated. */
12
+ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
13
+ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
14
+ "declaration": true,
15
+ "strict": true, /* Enable all strict type-checking options. */
16
+ "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
17
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */
18
+ }
19
+ }
package/flagsmith-core.js DELETED
@@ -1,243 +0,0 @@
1
- let fetch = require('node-fetch');
2
- // https://github.com/node-fetch/node-fetch/issues/450
3
- if (typeof fetch.default !== "undefined") fetch = fetch.default
4
-
5
- module.exports = class FlagsmithCore {
6
- normalizeFlags(flags) {
7
- const _flags = {};
8
-
9
- for (const { feature, enabled, feature_state_value } of flags) {
10
- const normalizedKey = feature.name.toLowerCase().replace(/ /g, '_');
11
- _flags[normalizedKey] = {
12
- enabled,
13
- value: feature_state_value
14
- };
15
- }
16
-
17
- return _flags;
18
- }
19
-
20
- normalizeTraits(traits) {
21
- const _traits = {};
22
-
23
- for (const { trait_key, trait_value } of traits) {
24
- const normalizedKey = trait_key.toLowerCase().replace(/ /g, '_');
25
- _traits[normalizedKey] = trait_value;
26
- }
27
-
28
- return _traits;
29
- }
30
-
31
- async getJSON(url, method, body) {
32
- const { environmentID } = this;
33
- const options = {
34
- method: method || 'GET',
35
- body,
36
- headers: {
37
- 'x-environment-key': environmentID
38
- }
39
- };
40
-
41
- if (method !== 'GET') {
42
- options.headers['Content-Type'] = 'application/json; charset=utf-8';
43
- }
44
-
45
- const res = await fetch(url, options);
46
- const result = await res.json();
47
-
48
- if (res.status >= 400) {
49
- throw new Error(result.detail);
50
- }
51
-
52
- return result;
53
- }
54
-
55
- init({ environmentID, api, onError, cache }) {
56
- if (!environmentID) {
57
- throw new Error('Please specify a environment id');
58
- }
59
-
60
- this.environmentID = environmentID;
61
-
62
- this.api = api || 'https://api.flagsmith.com/api/v1';
63
- this.onError = onError;
64
-
65
- if (cache) {
66
- const missingMethods = [];
67
-
68
- ['has', 'get', 'set'].forEach(method => {
69
- if (!cache[method]) missingMethods.push(method);
70
- });
71
-
72
- if (missingMethods.length > 0) {
73
- throw new Error(
74
- `Please implement the following methods in your cache: ${missingMethods.join(
75
- ', '
76
- )}`
77
- );
78
- }
79
- }
80
-
81
- this.cache = cache;
82
- }
83
-
84
- async getFlags() {
85
- if (this.cache && (await this.cache.has('flags'))) {
86
- return this.cache.get('flags');
87
- }
88
-
89
- const { onError, api } = this;
90
-
91
- try {
92
- const flags = await this.getJSON(`${api}/flags/`);
93
- const normalizedFlags = this.normalizeFlags(flags);
94
-
95
- if (this.cache) await this.cache.set('flags', normalizedFlags);
96
-
97
- return normalizedFlags;
98
- } catch (err) {
99
- onError && onError({ message: err.message });
100
- throw err;
101
- }
102
- }
103
-
104
- async getFlagsForUser(identity) {
105
- const cacheKey = `flags-${identity}`;
106
-
107
- if (this.cache && (await this.cache.has(cacheKey))) {
108
- return this.cache.get(cacheKey);
109
- }
110
-
111
- const { onError, api } = this;
112
-
113
- if (!identity) {
114
- const errMsg = 'getFlagsForUser() called without a user identity';
115
- onError && onError({ message: errMsg });
116
- throw new Error(errMsg);
117
- }
118
-
119
- try {
120
- const { flags } = await this.getJSON(`${api}/identities/?identifier=${identity}`);
121
- const normalizedFlags = this.normalizeFlags(flags);
122
-
123
- if (this.cache) await this.cache.set(cacheKey, normalizedFlags);
124
-
125
- return normalizedFlags;
126
- } catch (err) {
127
- onError && onError({ message: err.message });
128
- throw err;
129
- }
130
- }
131
-
132
- async getUserIdentity(identity) {
133
- const cacheKey = `flags_traits-${identity}`;
134
-
135
- if (this.cache && (await this.cache.has(cacheKey))) {
136
- return this.cache.get(cacheKey);
137
- }
138
-
139
- const { onError, api } = this;
140
-
141
- if (!identity) {
142
- const errMsg = 'getUserIdentity() called without a user identity';
143
- onError && onError({ message: errMsg });
144
- throw new Error(errMsg);
145
- }
146
-
147
- try {
148
- const { flags, traits } = await this.getJSON(
149
- `${api}/identities/?identifier=${identity}`
150
- );
151
-
152
- const normalizedFlags = this.normalizeFlags(flags);
153
- const normalizedTraits = this.normalizeTraits(traits);
154
- const res = { flags: normalizedFlags, traits: normalizedTraits };
155
-
156
- if (this.cache) await this.cache.set(cacheKey, res);
157
-
158
- return res;
159
- } catch (err) {
160
- onError && onError({ message: err.message });
161
- throw err;
162
- }
163
- }
164
-
165
- async getValue(key, userId) {
166
- const flags = userId ? await this.getFlagsForUser(userId) : await this.getFlags();
167
-
168
- return this.getValueFromFeatures(key, flags);
169
- }
170
-
171
- async hasFeature(key, userId) {
172
- const flags = userId ? await this.getFlagsForUser(userId) : await this.getFlags();
173
-
174
- return this.checkFeatureEnabled(key, flags);
175
- }
176
-
177
- getValueFromFeatures(key, flags) {
178
- if (!flags) return null;
179
-
180
- const flag = flags[key];
181
-
182
- //todo record check for value
183
- return flag ? flag.value : null;
184
- }
185
-
186
- checkFeatureEnabled(key, flags) {
187
- if (!flags) return false;
188
-
189
- const flag = flags[key];
190
- return flag && flag.enabled;
191
- }
192
-
193
- async getTrait(identity, key) {
194
- const { onError } = this;
195
-
196
- if (!identity || !key) {
197
- const errMsg = `getTrait() called without a ${
198
- !identity ? 'user identity' : 'trait key'
199
- }`;
200
- onError && onError({ message: errMsg });
201
- throw new Error(errMsg);
202
- }
203
-
204
- try {
205
- const { traits } = await this.getUserIdentity(identity);
206
- return traits[key];
207
- } catch (err) {
208
- onError && onError({ message: err.message });
209
- throw err;
210
- }
211
- }
212
-
213
- async setTrait(identity, key, value) {
214
- const { onError, api } = this;
215
-
216
- if (!identity || !key) {
217
- const errMsg = `setTrait() called without a ${
218
- !identity ? 'user identity' : 'trait key'
219
- }`;
220
- onError &&
221
- onError({
222
- message: errMsg
223
- });
224
- throw new Error(errMsg);
225
- }
226
-
227
- const body = {
228
- identity: {
229
- identifier: identity
230
- },
231
- trait_key: key,
232
- trait_value: value
233
- };
234
-
235
- try {
236
- await this.getJSON(`${api}/traits/`, 'POST', JSON.stringify(body));
237
- return await this.getUserIdentity(identity);
238
- } catch (err) {
239
- onError && onError({ message: err.message });
240
- throw err;
241
- }
242
- }
243
- };
package/index.d.ts DELETED
@@ -1,85 +0,0 @@
1
- declare module 'flagsmith-nodejs' {
2
- /**
3
- * Initialise the sdk against a particular environment
4
- */
5
- export function init(config: {
6
- environmentID: string;
7
- onError?: Function;
8
- defaultFlags?: string[];
9
- api?: string;
10
- cache?: ICache;
11
- }): void;
12
-
13
- /**
14
- * Get the whether a flag is enabled e.g. flagsmith.hasFeature("powerUserFeature")
15
- */
16
- export function hasFeature(key: string): Promise<boolean>;
17
-
18
- /**
19
- * Get the value of a whether a flag is enabled for a user e.g. flagsmith.hasFeature("powerUserFeature", 1234)
20
- */
21
- export function hasFeature(key: string, userId: string): Promise<boolean>;
22
-
23
- /**
24
- * Get the value of a particular remote config e.g. flagsmith.getValue("font_size")
25
- */
26
- export function getValue(key: string): Promise<string | number | boolean>;
27
-
28
- /**
29
- * Get the value of a particular remote config for a specified user e.g. flagsmith.getValue("font_size", 1234)
30
- */
31
- export function getValue(key: string, userId: string): Promise<string | number | boolean>;
32
-
33
- /**
34
- * Trigger a manual fetch of the environment features
35
- */
36
- export function getFlags(): Promise<IFlags>;
37
-
38
- /**
39
- * Trigger a manual fetch of the environment features for a given user id
40
- */
41
- export function getFlagsForUser(userId: string): Promise<IFlags>;
42
-
43
- /**
44
- * Trigger a manual fetch of both the environment features and users' traits for a given user id
45
- */
46
- export function getUserIdentity(userId: string): Promise<IUserIdentity>;
47
-
48
- /**
49
- * Trigger a manual fetch of a specific trait for a given user id
50
- */
51
- export function getTrait(userId: string, key: string): Promise<ITraits>;
52
-
53
- /**
54
- * Set a specific trait for a given user id
55
- */
56
- export function setTrait(
57
- userId: string,
58
- key: string,
59
- value: string | number | boolean
60
- ): Promise<IUserIdentity>;
61
-
62
- interface IFeature {
63
- enabled: boolean;
64
- value?: string | number | boolean;
65
- }
66
-
67
- interface IFlags {
68
- [key: string]: IFeature;
69
- }
70
-
71
- interface ITraits {
72
- [key: string]: string;
73
- }
74
-
75
- interface IUserIdentity {
76
- flags: IFeature;
77
- traits: ITraits;
78
- }
79
-
80
- interface ICache {
81
- has(key: string): boolean | Promise<boolean>;
82
- get(key: string): any | Promise<any>;
83
- set(key: string, val: any): void | Promise<void>;
84
- }
85
- }
package/index.js DELETED
@@ -1,3 +0,0 @@
1
- const FlagsmithCore = require('./flagsmith-core');
2
-
3
- module.exports = new FlagsmithCore();