flagsmith-nodejs 2.0.1 → 2.0.4

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 (74) hide show
  1. package/README.md +1 -1
  2. package/build/flagsmith-engine/environments/integrations/models.d.ts +4 -0
  3. package/build/flagsmith-engine/environments/integrations/models.js +11 -0
  4. package/build/flagsmith-engine/environments/models.d.ts +25 -0
  5. package/build/flagsmith-engine/environments/models.js +29 -0
  6. package/build/flagsmith-engine/environments/util.d.ts +3 -0
  7. package/build/flagsmith-engine/environments/util.js +21 -0
  8. package/build/flagsmith-engine/features/constants.d.ts +4 -0
  9. package/build/flagsmith-engine/features/constants.js +7 -0
  10. package/build/flagsmith-engine/features/models.d.ts +37 -0
  11. package/build/flagsmith-engine/features/models.js +122 -0
  12. package/build/flagsmith-engine/features/util.d.ts +4 -0
  13. package/build/flagsmith-engine/features/util.js +27 -0
  14. package/build/flagsmith-engine/identities/models.d.ts +15 -0
  15. package/build/flagsmith-engine/identities/models.js +112 -0
  16. package/build/flagsmith-engine/identities/traits/models.d.ts +5 -0
  17. package/build/flagsmith-engine/identities/traits/models.js +11 -0
  18. package/build/flagsmith-engine/identities/util.d.ts +4 -0
  19. package/build/flagsmith-engine/identities/util.js +46 -0
  20. package/build/flagsmith-engine/index.d.ts +8 -0
  21. package/build/flagsmith-engine/index.js +115 -0
  22. package/build/flagsmith-engine/organisations/models.d.ts +9 -0
  23. package/build/flagsmith-engine/organisations/models.js +21 -0
  24. package/build/flagsmith-engine/organisations/util.d.ts +2 -0
  25. package/build/flagsmith-engine/organisations/util.js +8 -0
  26. package/build/flagsmith-engine/projects/models.d.ts +10 -0
  27. package/build/flagsmith-engine/projects/models.js +14 -0
  28. package/build/flagsmith-engine/projects/util.d.ts +2 -0
  29. package/build/flagsmith-engine/projects/util.js +15 -0
  30. package/build/flagsmith-engine/segments/constants.d.ts +26 -0
  31. package/build/flagsmith-engine/segments/constants.js +31 -0
  32. package/build/flagsmith-engine/segments/evaluators.d.ts +6 -0
  33. package/build/flagsmith-engine/segments/evaluators.js +37 -0
  34. package/build/flagsmith-engine/segments/models.d.ts +37 -0
  35. package/build/flagsmith-engine/segments/models.js +114 -0
  36. package/build/flagsmith-engine/segments/util.d.ts +6 -0
  37. package/build/flagsmith-engine/segments/util.js +33 -0
  38. package/build/flagsmith-engine/utils/collections.d.ts +3 -0
  39. package/build/flagsmith-engine/utils/collections.js +26 -0
  40. package/build/flagsmith-engine/utils/errors.d.ts +2 -0
  41. package/build/flagsmith-engine/utils/errors.js +26 -0
  42. package/build/flagsmith-engine/utils/hashing/index.d.ts +9 -0
  43. package/build/flagsmith-engine/utils/hashing/index.js +60 -0
  44. package/build/flagsmith-engine/utils/index.d.ts +1 -0
  45. package/build/flagsmith-engine/utils/index.js +17 -0
  46. package/build/index.d.ts +1 -0
  47. package/build/index.js +11 -0
  48. package/build/sdk/analytics.d.ts +28 -0
  49. package/build/sdk/analytics.js +102 -0
  50. package/build/sdk/errors.d.ts +4 -0
  51. package/build/sdk/errors.js +34 -0
  52. package/build/sdk/index.d.ts +123 -0
  53. package/build/sdk/index.js +492 -0
  54. package/build/sdk/models.d.ts +55 -0
  55. package/build/sdk/models.js +149 -0
  56. package/build/sdk/polling_manager.d.ts +9 -0
  57. package/build/sdk/polling_manager.js +72 -0
  58. package/build/sdk/types.d.ts +7 -0
  59. package/build/sdk/types.js +2 -0
  60. package/build/sdk/utils.d.ts +12 -0
  61. package/build/sdk/utils.js +92 -0
  62. package/example/package-lock.json +997 -21
  63. package/example/server/api/index.js +6 -3
  64. package/example/server/index.js +1 -1
  65. package/package.json +3 -2
  66. package/sdk/analytics.ts +2 -2
  67. package/sdk/index.ts +4 -0
  68. package/sdk/models.ts +1 -1
  69. package/sdk/types.ts +4 -4
  70. package/tests/sdk/analytics.test.ts +10 -10
  71. package/tests/sdk/utils.ts +2 -1
  72. package/.idea/flagsmith-nodejs-client.iml +0 -12
  73. package/.idea/modules.xml +0 -8
  74. package/.idea/vcs.xml +0 -6
@@ -1,6 +1,8 @@
1
1
  const Router = require('express').Router;
2
2
  const Flagsmith = require('../../../build');
3
3
  const environmentKey = '';
4
+ const nodecache = require("node-cache");
5
+
4
6
  if (!environmentKey) {
5
7
  throw new Error(
6
8
  'Please generate a Server Side SDK Key in environment settings to run the example'
@@ -9,9 +11,10 @@ if (!environmentKey) {
9
11
  const flagsmith = new Flagsmith({
10
12
  environmentKey,
11
13
  enableLocalEvaluation: true,
12
- defaultFlagHandler: str => {
13
- return { enabled: false, isDefault: true, value: null };
14
- }
14
+ cache: new nodecache({
15
+ stdTTL: 10,
16
+ checkperiod: 10,
17
+ }),
15
18
  });
16
19
 
17
20
  module.exports = () => {
@@ -15,7 +15,7 @@ app.use(bodyParser.json());
15
15
  app.use('/api', api());
16
16
 
17
17
  app.server.listen(PORT);
18
- console.log('Server started on port ' + app.server.address().port);
18
+ console.log('Server started on port ' + PORT);
19
19
  console.log();
20
20
  console.log('Go to http://localhost:' + PORT + '/api');
21
21
  console.log('To get an example response for getFlags');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flagsmith-nodejs",
3
- "version": "2.0.1",
3
+ "version": "2.0.4",
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": {
@@ -46,7 +46,8 @@
46
46
  "test:watch": "jest --coverage --watch --coverageReporters='text'",
47
47
  "test:debug": "node --inspect-brk node_modules/.bin/jest --coverage --watch --coverageReporters='text'",
48
48
  "build": "tsc",
49
- "prepublish": "npm i && npm run build",
49
+ "deploy": "npm i && npm run build && npm publish",
50
+ "deploy:beta": "npm i && npm run build && npm publish --tag beta",
50
51
  "prepare": "husky install"
51
52
  },
52
53
  "dependencies": {
package/sdk/analytics.ts CHANGED
@@ -49,8 +49,8 @@ export class AnalyticsProcessor {
49
49
  this.lastFlushed = Date.now();
50
50
  }
51
51
 
52
- trackFeature(featureId: number) {
53
- this.analyticsData[featureId] = (this.analyticsData[featureId] || 0) + 1;
52
+ trackFeature(featureName: string) {
53
+ this.analyticsData[featureName] = (this.analyticsData[featureName] || 0) + 1;
54
54
  if (Date.now() - this.lastFlushed > ANALYTICS_TIMER * 1000) {
55
55
  this.flush();
56
56
  }
package/sdk/index.ts CHANGED
@@ -298,6 +298,7 @@ export class Flagsmith {
298
298
  defaultFlagHandler: this.defaultFlagHandler
299
299
  });
300
300
  if (!!this.cache) {
301
+ // @ts-ignore node-cache types are incorrect, ttl should be optional
301
302
  await this.cache.set('flags', flags);
302
303
  }
303
304
  return flags;
@@ -321,6 +322,7 @@ export class Flagsmith {
321
322
  });
322
323
 
323
324
  if (!!this.cache) {
325
+ // @ts-ignore node-cache types are incorrect, ttl should be optional
324
326
  await this.cache.set(`flags-${identifier}`, flags);
325
327
  }
326
328
 
@@ -336,6 +338,7 @@ export class Flagsmith {
336
338
  defaultFlagHandler: this.defaultFlagHandler
337
339
  });
338
340
  if (!!this.cache) {
341
+ // @ts-ignore node-cache types are incorrect, ttl should be optional
339
342
  await this.cache.set('flags', flags);
340
343
  }
341
344
  return flags;
@@ -361,6 +364,7 @@ export class Flagsmith {
361
364
  defaultFlagHandler: this.defaultFlagHandler
362
365
  });
363
366
  if (!!this.cache) {
367
+ // @ts-ignore node-cache types are incorrect, ttl should be optional
364
368
  await this.cache.set(`flags-${identifier}`, flags);
365
369
  }
366
370
  return flags;
package/sdk/models.ts CHANGED
@@ -129,7 +129,7 @@ export class Flags {
129
129
  }
130
130
 
131
131
  if (this.analyticsProcessor && flag.featureId) {
132
- this.analyticsProcessor.trackFeature(flag.featureId);
132
+ this.analyticsProcessor.trackFeature(flag.featureName);
133
133
  }
134
134
 
135
135
  return flag;
package/sdk/types.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { Flags } from "./models";
2
2
 
3
3
  export interface FlagsmithCache {
4
- get(key: string): Promise<Flags>;
5
- set(key: string, value: Flags): Promise<void>;
6
- has(key: string): Promise<boolean>;
4
+ get(key: string): Promise<Flags|undefined> | undefined;
5
+ set(key: string, value: Flags, ttl: string | number): boolean | Promise<boolean>;
6
+ has(key: string): boolean | Promise<boolean>;
7
7
  [key: string]: any;
8
- }
8
+ }
@@ -8,28 +8,28 @@ afterEach(() => {
8
8
 
9
9
  test('test_analytics_processor_track_feature_updates_analytics_data', () => {
10
10
  const aP = analyticsProcessor();
11
- aP.trackFeature(1);
12
- expect(aP.analyticsData[1]).toBe(1);
11
+ aP.trackFeature("myFeature");
12
+ expect(aP.analyticsData["myFeature"]).toBe(1);
13
13
 
14
- aP.trackFeature(1);
15
- expect(aP.analyticsData[1]).toBe(2);
14
+ aP.trackFeature("myFeature");
15
+ expect(aP.analyticsData["myFeature"]).toBe(2);
16
16
  });
17
17
 
18
18
  test('test_analytics_processor_flush_clears_analytics_data', async () => {
19
19
  const aP = analyticsProcessor();
20
- aP.trackFeature(1);
20
+ aP.trackFeature("myFeature");
21
21
  await aP.flush();
22
22
  expect(aP.analyticsData).toStrictEqual({});
23
23
  });
24
24
 
25
25
  test('test_analytics_processor_flush_post_request_data_match_ananlytics_data', async () => {
26
26
  const aP = analyticsProcessor();
27
- aP.trackFeature(1);
28
- aP.trackFeature(2);
27
+ aP.trackFeature("myFeature1");
28
+ aP.trackFeature("myFeature2");
29
29
  await aP.flush();
30
30
  expect(fetch).toHaveBeenCalledTimes(1);
31
31
  expect(fetch).toHaveBeenCalledWith('http://testUrlanalytics/flags/', {
32
- body: '{"1":1,"2":1}',
32
+ body: '{"myFeature1":1,"myFeature2":1}',
33
33
  headers: { 'Content-Type': 'application/json', 'X-Environment-Key': 'test-key' },
34
34
  method: 'POST',
35
35
  timeout: 3
@@ -39,9 +39,9 @@ test('test_analytics_processor_flush_post_request_data_match_ananlytics_data', a
39
39
  jest.useFakeTimers()
40
40
  test('test_analytics_processor_flush_post_request_data_match_ananlytics_data_test', async () => {
41
41
  const aP = analyticsProcessor();
42
- aP.trackFeature(1);
42
+ aP.trackFeature("myFeature1");
43
43
  setTimeout(() => {
44
- aP.trackFeature(2);
44
+ aP.trackFeature("myFeature2");
45
45
  expect(fetch).toHaveBeenCalledTimes(1);
46
46
  }, 15000);
47
47
  jest.runOnlyPendingTimers();
@@ -18,8 +18,9 @@ export class TestCache implements FlagsmithCache {
18
18
  return !!this.cache[name];
19
19
  }
20
20
 
21
- async set(name: string, value: Flags) {
21
+ async set(name: string, value: Flags, ttl: number|string) {
22
22
  this.cache[name] = value;
23
+ return true
23
24
  }
24
25
  }
25
26
 
@@ -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>