flagsmith-nodejs 2.0.2 → 2.1.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.
@@ -20,5 +20,8 @@
20
20
  "ssg-node-express": "4.16.4",
21
21
  "ssg-util": "0.0.3"
22
22
  },
23
- "keywords": []
23
+ "keywords": [],
24
+ "devDependencies": {
25
+ "nodemon": "^2.0.19"
26
+ }
24
27
  }
@@ -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');
@@ -6,6 +6,14 @@ import { getIdentitySegments } from './segments/evaluators';
6
6
  import { SegmentModel } from './segments/models';
7
7
  import { FeatureStateNotFound } from './utils/errors';
8
8
 
9
+ export { EnvironmentModel } from './environments/models';
10
+ export { IntegrationModel } from './environments/integrations/models';
11
+ export { FeatureStateModel } from './features/models';
12
+ export { IdentityModel } from './identities/models';
13
+ export { TraitModel } from './identities/traits/models';
14
+ export { SegmentModel } from './segments/models';
15
+ export { OrganisationModel } from './organisations/models';
16
+
9
17
  function getIdentityFeatureStatesDict(
10
18
  environment: EnvironmentModel,
11
19
  identity: IdentityModel,
package/index.ts CHANGED
@@ -1,6 +1,23 @@
1
1
  import Flagsmith from './sdk';
2
2
 
3
- export { Flagsmith } from './sdk';
4
- // export default Flagsmith;
3
+ export {
4
+ AnalyticsProcessor,
5
+ FlagsmithAPIError,
6
+ FlagsmithClientError,
7
+ EnvironmentDataPollingManager,
8
+ FlagsmithCache,
9
+ DefaultFlag,
10
+ Flags
11
+ } from './sdk';
12
+
13
+ export {
14
+ EnvironmentModel,
15
+ IntegrationModel,
16
+ FeatureStateModel,
17
+ IdentityModel,
18
+ TraitModel,
19
+ SegmentModel,
20
+ OrganisationModel
21
+ } from './flagsmith-engine';
5
22
 
6
23
  module.exports = Flagsmith;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flagsmith-nodejs",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
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
@@ -14,6 +14,13 @@ import { SegmentModel } from '../flagsmith-engine/segments/models';
14
14
  import { getIdentitySegments } from '../flagsmith-engine/segments/evaluators';
15
15
  import { FlagsmithCache } from './types';
16
16
 
17
+ export { AnalyticsProcessor } from './analytics';
18
+ export { FlagsmithAPIError, FlagsmithClientError } from './errors';
19
+
20
+ export { DefaultFlag, Flags } from './models';
21
+ export { EnvironmentDataPollingManager } from './polling_manager';
22
+ export { FlagsmithCache } from './types';
23
+
17
24
  const DEFAULT_API_URL = 'https://edge.api.flagsmith.com/api/v1/';
18
25
 
19
26
  export class Flagsmith {
@@ -298,6 +305,7 @@ export class Flagsmith {
298
305
  defaultFlagHandler: this.defaultFlagHandler
299
306
  });
300
307
  if (!!this.cache) {
308
+ // @ts-ignore node-cache types are incorrect, ttl should be optional
301
309
  await this.cache.set('flags', flags);
302
310
  }
303
311
  return flags;
@@ -321,6 +329,7 @@ export class Flagsmith {
321
329
  });
322
330
 
323
331
  if (!!this.cache) {
332
+ // @ts-ignore node-cache types are incorrect, ttl should be optional
324
333
  await this.cache.set(`flags-${identifier}`, flags);
325
334
  }
326
335
 
@@ -336,6 +345,7 @@ export class Flagsmith {
336
345
  defaultFlagHandler: this.defaultFlagHandler
337
346
  });
338
347
  if (!!this.cache) {
348
+ // @ts-ignore node-cache types are incorrect, ttl should be optional
339
349
  await this.cache.set('flags', flags);
340
350
  }
341
351
  return flags;
@@ -361,6 +371,7 @@ export class Flagsmith {
361
371
  defaultFlagHandler: this.defaultFlagHandler
362
372
  });
363
373
  if (!!this.cache) {
374
+ // @ts-ignore node-cache types are incorrect, ttl should be optional
364
375
  await this.cache.set(`flags-${identifier}`, flags);
365
376
  }
366
377
  return flags;
@@ -383,3 +394,4 @@ export class Flagsmith {
383
394
  }
384
395
 
385
396
  export default Flagsmith;
397
+
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