flagsmith-nodejs 2.2.2 → 2.4.0

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 (66) hide show
  1. package/build/flagsmith-engine/segments/constants.d.ts +6 -0
  2. package/build/flagsmith-engine/segments/constants.js +8 -2
  3. package/build/flagsmith-engine/segments/evaluators.d.ts +2 -1
  4. package/build/flagsmith-engine/segments/evaluators.js +9 -2
  5. package/build/flagsmith-engine/segments/models.d.ts +3 -3
  6. package/build/flagsmith-engine/segments/models.js +26 -1
  7. package/build/sdk/index.d.ts +5 -17
  8. package/build/sdk/index.js +2 -0
  9. package/build/sdk/types.d.ts +19 -1
  10. package/build/sdk/utils.d.ts +2 -2
  11. package/{es6-example → examples/api-proxy}/.babelrc +0 -0
  12. package/{es6-example → examples/api-proxy}/.eslintrc +0 -0
  13. package/examples/api-proxy/README.md +12 -0
  14. package/examples/api-proxy/package-lock.json +10895 -0
  15. package/examples/api-proxy/package.json +57 -0
  16. package/examples/api-proxy/src/api/index.js +42 -0
  17. package/{es6-example → examples/api-proxy}/src/index.js +0 -0
  18. package/examples/basic/.babelrc +3 -0
  19. package/examples/basic/.eslintrc +8 -0
  20. package/examples/basic/README.md +10 -0
  21. package/{es6-example → examples/basic}/package-lock.json +25 -39
  22. package/{es6-example → examples/basic}/package.json +1 -1
  23. package/examples/basic/src/api/index.js +33 -0
  24. package/{example/server → examples/basic/src}/index.js +4 -4
  25. package/examples/caching/.babelrc +3 -0
  26. package/examples/caching/.eslintrc +8 -0
  27. package/examples/caching/README.md +9 -0
  28. package/examples/caching/package-lock.json +6670 -0
  29. package/examples/caching/package.json +56 -0
  30. package/{example/server → examples/caching/src}/api/index.js +7 -11
  31. package/examples/caching/src/index.js +29 -0
  32. package/examples/custom-fetch-agent/.babelrc +3 -0
  33. package/examples/custom-fetch-agent/.eslintrc +8 -0
  34. package/examples/custom-fetch-agent/README.md +12 -0
  35. package/examples/custom-fetch-agent/package-lock.json +6756 -0
  36. package/examples/custom-fetch-agent/package.json +56 -0
  37. package/examples/custom-fetch-agent/src/api/index.js +34 -0
  38. package/examples/custom-fetch-agent/src/index.js +29 -0
  39. package/examples/local-evaluation/.babelrc +3 -0
  40. package/examples/local-evaluation/.eslintrc +8 -0
  41. package/examples/local-evaluation/README.md +18 -0
  42. package/examples/local-evaluation/package-lock.json +6674 -0
  43. package/examples/local-evaluation/package.json +56 -0
  44. package/{es6-example → examples/local-evaluation}/src/api/index.js +2 -13
  45. package/examples/local-evaluation/src/index.js +29 -0
  46. package/flagsmith-engine/segments/constants.ts +7 -1
  47. package/flagsmith-engine/segments/evaluators.ts +10 -6
  48. package/flagsmith-engine/segments/models.ts +15 -5
  49. package/package.json +1 -1
  50. package/sdk/index.ts +8 -15
  51. package/sdk/types.ts +19 -2
  52. package/sdk/utils.ts +2 -2
  53. package/tests/engine/engine-tests/engine-test-data/data/environment_n9fbf9h3v4fFgH3U3ngWhb.json +12591 -0
  54. package/tests/engine/engine-tests/engine-test-data/readme.md +30 -0
  55. package/tests/engine/unit/segments/segment_evaluators.test.ts +27 -0
  56. package/tests/engine/unit/segments/segments_model.test.ts +8 -1
  57. package/tests/sdk/flagsmith.test.ts +25 -10
  58. package/.idea/flagsmith-nodejs-client.iml +0 -12
  59. package/.idea/modules.xml +0 -8
  60. package/.idea/vcs.xml +0 -6
  61. package/es6-example/README.md +0 -62
  62. package/example/LICENSE +0 -0
  63. package/example/Procfile +0 -1
  64. package/example/README.md +0 -15
  65. package/example/package-lock.json +0 -2387
  66. package/example/package.json +0 -27
@@ -0,0 +1,30 @@
1
+ # Engine Test Data
2
+
3
+ This repository contains a single directory containing json files that can be used to test that any Flagsmith engine
4
+ written in a given language is correct.
5
+
6
+ Each JSON file should consist of a single object in the following format.
7
+
8
+ ```json
9
+ {
10
+ "environment": {...}, // the environment document as found in DynamoDB
11
+ "identities_and_responses": [
12
+ {
13
+ "identity": {...}, // the identity as found in DynamoDB,
14
+ "response": {...}, // the response that was obtained from the current API
15
+ }
16
+ ]
17
+ }
18
+ ```
19
+
20
+ To use this data, you will need to write a test case in the repository which contains the engine code and include
21
+ this repository as a submodule to get access to the json files.
22
+
23
+ To add the git submodule:
24
+
25
+ ```bash
26
+ git submodule add git@github.com:Flagsmith/engine-test-data.git tests/engine_tests/engine-test-data
27
+ ```
28
+
29
+ An example of how to use the test data can be found in the flagsmith-flag-engine repository
30
+ [here](https://github.com/Flagsmith/flagsmith-engine/blob/main/tests/engine_tests/test_engine.py).
@@ -0,0 +1,27 @@
1
+ import {
2
+ CONDITION_OPERATORS,
3
+ } from '../../../../flagsmith-engine/segments/constants';
4
+ import {
5
+ SegmentConditionModel,
6
+ } from '../../../../flagsmith-engine/segments/models';
7
+ import {traitsMatchSegmentCondition} from "../../../../flagsmith-engine/segments/evaluators";
8
+ import {TraitModel} from "../../../../flagsmith-engine";
9
+
10
+ let traitExistenceTestCases: [string, string | null | undefined, string | null | undefined, TraitModel [],boolean][] = [
11
+ [CONDITION_OPERATORS.IS_SET,'foo', null,[] , false],
12
+ [CONDITION_OPERATORS.IS_SET, 'foo',undefined , [new TraitModel('foo','bar')], true],
13
+ [CONDITION_OPERATORS.IS_SET, 'foo',undefined , [new TraitModel('foo','bar'), new TraitModel('fooBaz','baz')], true],
14
+ [CONDITION_OPERATORS.IS_NOT_SET, 'foo', undefined, [], true],
15
+ [CONDITION_OPERATORS.IS_NOT_SET, 'foo', null, [new TraitModel('foo','bar')], false],
16
+ [CONDITION_OPERATORS.IS_NOT_SET, 'foo', null, [new TraitModel('foo','bar'), new TraitModel('fooBaz','baz')], false]
17
+ ];
18
+
19
+ test('test_traits_match_segment_condition_for_trait_existence_operators', () => {
20
+ for (const testCase of traitExistenceTestCases) {
21
+ const [operator, conditionProperty, conditionValue, traits, expectedResult] = testCase
22
+ let segmentModel = new SegmentConditionModel(operator, conditionValue, conditionProperty)
23
+ expect(
24
+ traitsMatchSegmentCondition (traits, segmentModel, 'any','any')
25
+ ).toBe(expectedResult);
26
+ }
27
+ });
@@ -82,6 +82,13 @@ const conditionMatchCases: [string, string | number | boolean, string, boolean][
82
82
  [CONDITION_OPERATORS.LESS_THAN_INCLUSIVE, "1.0.0", "1.0.1:semver", true],
83
83
  [CONDITION_OPERATORS.LESS_THAN_INCLUSIVE, "1.0.0", "1.0.0:semver", true],
84
84
  [CONDITION_OPERATORS.LESS_THAN_INCLUSIVE, "1.0.1", "1.0.0:semver", false],
85
+ [CONDITION_OPERATORS.MODULO, 1, "2|0", false],
86
+ [CONDITION_OPERATORS.MODULO, 2, "2|0", true],
87
+ [CONDITION_OPERATORS.MODULO, 3, "2|0", false],
88
+ [CONDITION_OPERATORS.MODULO, 34.2, "4|3", false],
89
+ [CONDITION_OPERATORS.MODULO, 35.0, "4|3", true],
90
+ [CONDITION_OPERATORS.MODULO, "foo", "4|3", false],
91
+ [CONDITION_OPERATORS.MODULO, 35.0, "foo|bar", false],
85
92
  ['BAD_OP', 'a', 'a', false]
86
93
  ];
87
94
 
@@ -119,4 +126,4 @@ test('test_segment_rule_matching_function', () => {
119
126
  for (const testCase of testCases) {
120
127
  expect(new SegmentRuleModel(testCase[0]).matchingFunction()).toBe(testCase[1]);
121
128
  }
122
- });
129
+ });
@@ -1,10 +1,12 @@
1
1
  import Flagsmith from '../../sdk';
2
2
  import { EnvironmentDataPollingManager } from '../../sdk/polling_manager';
3
- import fetch from 'node-fetch';
3
+ import fetch, {RequestInit} from 'node-fetch';
4
4
  import { environmentJSON, environmentModel, flagsJSON, flagsmith, identitiesJSON } from './utils';
5
5
  import { DefaultFlag } from '../../sdk/models';
6
- import { delay } from '../../sdk/utils';
6
+ import {delay, retryFetch} from '../../sdk/utils';
7
+ import * as utils from '../../sdk/utils';
7
8
  import { EnvironmentModel } from '../../flagsmith-engine/environments/models';
9
+ import https from 'https'
8
10
 
9
11
  jest.mock('node-fetch');
10
12
  jest.mock('../../sdk/polling_manager');
@@ -56,6 +58,26 @@ test('test_update_environment_sets_environment', async () => {
56
58
  expect(flg.environment).toStrictEqual(model);
57
59
  });
58
60
 
61
+ test('test_set_agent_options', async () => {
62
+ const agent = new https.Agent({})
63
+
64
+ // @ts-ignore
65
+ fetch.mockImplementation((url:string, options:RequestInit)=>{
66
+ if(options.agent!==agent) {
67
+ throw new Error("Agent has not been set on retry fetch")
68
+ }
69
+ return Promise.resolve(new Response(environmentJSON()))
70
+ });
71
+
72
+ const flg = flagsmith({
73
+ agent
74
+ });
75
+
76
+ await flg.updateEnvironment();
77
+ expect(flg.environment).toBeDefined();
78
+
79
+ });
80
+
59
81
  test('test_get_identity_segments', async () => {
60
82
  // @ts-ignore
61
83
  fetch.mockReturnValue(Promise.resolve(new Response(environmentJSON())));
@@ -81,11 +103,6 @@ test('test_get_identity_segments_empty_without_local_eval', async () => {
81
103
  expect(segments.length).toBe(0);
82
104
  });
83
105
 
84
-
85
-
86
-
87
-
88
-
89
106
  test('test_update_environment_uses_req_when_inited', async () => {
90
107
  // @ts-ignore
91
108
  fetch.mockReturnValue(Promise.resolve(new Response(environmentJSON())));
@@ -160,8 +177,6 @@ test('test_default_flag_used_after_multiple_API_errors', async () => {
160
177
  expect(flag.value).toBe(defaultFlag.value);
161
178
  });
162
179
 
163
-
164
-
165
180
  test('test_throws_when_no_identity_flags_returned_due_to_error', async () => {
166
181
  // @ts-ignore
167
182
  fetch.mockReturnValue(Promise.resolve(new Response('bad data')));
@@ -215,4 +230,4 @@ test('test onEnvironmentChange is called after error', async () => {
215
230
  await delay(200);
216
231
 
217
232
  expect(callbackSpy).toBeCalled();
218
- });
233
+ });
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$">
5
- <excludeFolder url="file://$MODULE_DIR$/temp" />
6
- <excludeFolder url="file://$MODULE_DIR$/.tmp" />
7
- <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
- </content>
9
- <orderEntry type="inheritedJdk" />
10
- <orderEntry type="sourceFolder" forTests="false" />
11
- </component>
12
- </module>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/flagsmith-nodejs-client.iml" filepath="$PROJECT_DIR$/.idea/flagsmith-nodejs-client.iml" />
6
- </modules>
7
- </component>
8
- </project>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
- </component>
6
- </project>
@@ -1,62 +0,0 @@
1
- # Nodejs-ES6-Boilerplate
2
- [![Made in Indonesia](https://made-in-indonesia.github.io/made-in-indonesia.svg)](https://github.com/made-in-indonesia/made-in-indonesia)
3
-
4
- A Node.js boilerplate with ES6, ESLint, and Prettier
5
-
6
- ## Background
7
-
8
- I created this boilerplate because I have several coding interviews that needs to be done using JavaScript.
9
-
10
- ## Scripts
11
-
12
- ```bash
13
- # Run the project without nodemon
14
- npm run start
15
-
16
- # Build the project
17
- npm run build
18
-
19
- # Clean build
20
- npm run clean
21
-
22
- # Check the lint errors
23
- npm run lint
24
-
25
- # Fix the lint errors
26
- npm run lint:fix
27
-
28
- # Run prettier
29
- npm run format
30
-
31
- # Check prettier errors
32
- npm run format:check
33
- ```
34
-
35
- ## Usage
36
-
37
- 1. Clone the repository and init new git project
38
-
39
- ```bash
40
- $ git clone git@github.com:vferdiansyah/nodejs-es6-boilerplate.git ./your/project/folder
41
- $ cd ./your/project/folder
42
- $ rm -rf .git
43
- $ git init
44
- ```
45
-
46
- 2. Change project specific information in the following places
47
-
48
- - [package.json](./package.json)
49
- - [README.md](./README.md)
50
-
51
- 3. Install dependencies
52
-
53
- ```bash
54
- $ npm i
55
- ```
56
-
57
- 4. Start coding
58
-
59
- ## License
60
-
61
- Copyright (c) 2020 Veri Ferdiansyah
62
- Licensed under the MIT license.
package/example/LICENSE DELETED
File without changes
package/example/Procfile DELETED
@@ -1 +0,0 @@
1
- web: npm start
package/example/README.md DELETED
@@ -1,15 +0,0 @@
1
- # Flagsmith example
2
-
3
- ## Getting Started
4
-
5
- # Run
6
-
7
- `$ npm start`
8
-
9
- # Nodemon (Restart server on changes)
10
-
11
- `npm run dev`
12
-
13
- # The project
14
-
15
- - `/server/api` contains a simple express api that interacts with Flagsmith