flagsmith-nodejs 3.0.1 → 3.1.1

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.
@@ -83,10 +83,7 @@ var FeatureStateModel = /** @class */ (function () {
83
83
  };
84
84
  FeatureStateModel.prototype.getMultivariateValue = function (identityID) {
85
85
  var e_1, _a;
86
- var percentageValue = (0, hashing_1.getHashedPercentateForObjIds)([
87
- this.djangoID || this.featurestateUUID,
88
- identityID
89
- ]);
86
+ var percentageValue;
90
87
  var startPercentage = 0;
91
88
  var sortedF = this.multivariateFeatureStateValues.sort(function (a, b) {
92
89
  return a.id - b.id;
@@ -94,6 +91,19 @@ var FeatureStateModel = /** @class */ (function () {
94
91
  try {
95
92
  for (var sortedF_1 = __values(sortedF), sortedF_1_1 = sortedF_1.next(); !sortedF_1_1.done; sortedF_1_1 = sortedF_1.next()) {
96
93
  var myValue = sortedF_1_1.value;
94
+ switch (myValue.percentageAllocation) {
95
+ case 0:
96
+ continue;
97
+ case 100:
98
+ return myValue.multivariateFeatureOption.value;
99
+ default:
100
+ if (percentageValue === undefined) {
101
+ percentageValue = (0, hashing_1.getHashedPercentateForObjIds)([
102
+ this.djangoID || this.featurestateUUID,
103
+ identityID
104
+ ]);
105
+ }
106
+ }
97
107
  var limit = myValue.percentageAllocation + startPercentage;
98
108
  if (startPercentage <= percentageValue && percentageValue < limit) {
99
109
  return myValue.multivariateFeatureOption.value;
@@ -15,7 +15,6 @@ export declare class Flagsmith {
15
15
  customHeaders?: {
16
16
  [key: string]: any;
17
17
  };
18
- requestTimeoutSeconds?: number;
19
18
  agent: RequestInit['agent'];
20
19
  requestTimeoutMs?: number;
21
20
  enableLocalEvaluation?: boolean;
@@ -89,6 +89,7 @@ Object.defineProperty(exports, "Flags", { enumerable: true, get: function () { r
89
89
  var polling_manager_2 = require("./polling_manager");
90
90
  Object.defineProperty(exports, "EnvironmentDataPollingManager", { enumerable: true, get: function () { return polling_manager_2.EnvironmentDataPollingManager; } });
91
91
  var DEFAULT_API_URL = 'https://edge.api.flagsmith.com/api/v1/';
92
+ var DEFAULT_REQUEST_TIMEOUT_SECONDS = 10;
92
93
  var Flagsmith = /** @class */ (function () {
93
94
  /**
94
95
  * A Flagsmith client.
@@ -122,6 +123,7 @@ var Flagsmith = /** @class */ (function () {
122
123
  @param data.logger: an instance of the pino Logger class to use for logging
123
124
  */
124
125
  function Flagsmith(data) {
126
+ var _a;
125
127
  this.apiUrl = DEFAULT_API_URL;
126
128
  this.enableLocalEvaluation = false;
127
129
  this.environmentRefreshIntervalSeconds = 60;
@@ -130,8 +132,7 @@ var Flagsmith = /** @class */ (function () {
130
132
  this.environmentKey = data.environmentKey;
131
133
  this.apiUrl = data.apiUrl || this.apiUrl;
132
134
  this.customHeaders = data.customHeaders;
133
- this.requestTimeoutSeconds = data.requestTimeoutSeconds;
134
- this.requestTimeoutMs = data.requestTimeoutSeconds ? data.requestTimeoutSeconds * 1000 : undefined;
135
+ this.requestTimeoutMs = 1000 * ((_a = data.requestTimeoutSeconds) !== null && _a !== void 0 ? _a : DEFAULT_REQUEST_TIMEOUT_SECONDS);
135
136
  this.enableLocalEvaluation = data.enableLocalEvaluation;
136
137
  this.environmentRefreshIntervalSeconds =
137
138
  data.environmentRefreshIntervalSeconds || this.environmentRefreshIntervalSeconds;
@@ -9,4 +9,4 @@ export declare function generateIdentitiesData(identifier: string, traits: {
9
9
  }[];
10
10
  };
11
11
  export declare const delay: (ms: number) => Promise<unknown>;
12
- export declare const retryFetch: (url: string, fetchOptions: RequestInit, retries?: number, timeout?: number | undefined) => Promise<Response>;
12
+ export declare const retryFetch: (url: string, fetchOptions: RequestInit, retries?: number, timeout?: number) => Promise<Response>;
@@ -62,6 +62,7 @@ exports.delay = delay;
62
62
  var retryFetch = function (url, fetchOptions, retries, timeout // set an overall timeout for this function
63
63
  ) {
64
64
  if (retries === void 0) { retries = 3; }
65
+ if (timeout === void 0) { timeout = 10; }
65
66
  return new Promise(function (resolve, reject) {
66
67
  var retryWrapper = function (n) {
67
68
  requestWrapper()
@@ -96,18 +96,27 @@ export class FeatureStateModel {
96
96
  }
97
97
  return this.featureSegment.priority < other.featureSegment.priority;
98
98
  }
99
-
100
- getMultivariateValue(identityID: number | string) {
101
- const percentageValue = getHashedPercentateForObjIds([
102
- this.djangoID || this.featurestateUUID,
103
- identityID
104
- ]);
105
99
 
100
+ getMultivariateValue(identityID: number | string) {
101
+ let percentageValue: number | undefined;
106
102
  let startPercentage = 0;
107
- const sortedF = this.multivariateFeatureStateValues.sort((a, b) =>{
103
+ const sortedF = this.multivariateFeatureStateValues.sort((a, b) => {
108
104
  return a.id - b.id;
109
105
  });
110
106
  for (const myValue of sortedF) {
107
+ switch (myValue.percentageAllocation) {
108
+ case 0:
109
+ continue;
110
+ case 100:
111
+ return myValue.multivariateFeatureOption.value;
112
+ default:
113
+ if (percentageValue === undefined) {
114
+ percentageValue = getHashedPercentateForObjIds([
115
+ this.djangoID || this.featurestateUUID,
116
+ identityID
117
+ ]);
118
+ }
119
+ }
111
120
  const limit = myValue.percentageAllocation + startPercentage;
112
121
  if (startPercentage <= percentageValue && percentageValue < limit) {
113
122
  return myValue.multivariateFeatureOption.value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flagsmith-nodejs",
3
- "version": "3.0.1",
3
+ "version": "3.1.1",
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": {
package/sdk/index.ts CHANGED
@@ -24,13 +24,13 @@ export { EnvironmentDataPollingManager } from './polling_manager';
24
24
  export { FlagsmithCache, FlagsmithConfig } from './types';
25
25
 
26
26
  const DEFAULT_API_URL = 'https://edge.api.flagsmith.com/api/v1/';
27
+ const DEFAULT_REQUEST_TIMEOUT_SECONDS = 10;
27
28
 
28
29
 
29
30
  export class Flagsmith {
30
31
  environmentKey?: string;
31
32
  apiUrl: string = DEFAULT_API_URL;
32
33
  customHeaders?: { [key: string]: any };
33
- requestTimeoutSeconds?: number;
34
34
  agent: RequestInit['agent'];
35
35
  requestTimeoutMs?: number;
36
36
  enableLocalEvaluation?: boolean = false;
@@ -87,8 +87,7 @@ export class Flagsmith {
87
87
  this.environmentKey = data.environmentKey;
88
88
  this.apiUrl = data.apiUrl || this.apiUrl;
89
89
  this.customHeaders = data.customHeaders;
90
- this.requestTimeoutSeconds = data.requestTimeoutSeconds;
91
- this.requestTimeoutMs = data.requestTimeoutSeconds ? data.requestTimeoutSeconds * 1000 : undefined;
90
+ this.requestTimeoutMs = 1000 * (data.requestTimeoutSeconds ?? DEFAULT_REQUEST_TIMEOUT_SECONDS);
92
91
  this.enableLocalEvaluation = data.enableLocalEvaluation;
93
92
  this.environmentRefreshIntervalSeconds =
94
93
  data.environmentRefreshIntervalSeconds || this.environmentRefreshIntervalSeconds;
package/sdk/utils.ts CHANGED
@@ -19,8 +19,8 @@ export const delay = (ms: number) =>
19
19
  export const retryFetch = (
20
20
  url: string,
21
21
  fetchOptions: RequestInit,
22
- retries = 3,
23
- timeout?: number // set an overall timeout for this function
22
+ retries: number = 3,
23
+ timeout: number = 10// set an overall timeout for this function
24
24
  ): Promise<Response> => {
25
25
  return new Promise((resolve, reject) => {
26
26
  const retryWrapper = (n: number) => {
@@ -183,7 +183,7 @@ test('default flag handler used when timeout occurs', async () => {
183
183
  const flg = new Flagsmith({
184
184
  environmentKey: 'key',
185
185
  defaultFlagHandler: defaultFlagHandler,
186
- requestTimeoutSeconds: 0.1,
186
+ requestTimeoutSeconds: 0.001,
187
187
  });
188
188
 
189
189
  const flags = await flg.getEnvironmentFlags();
@@ -193,6 +193,15 @@ test('default flag handler used when timeout occurs', async () => {
193
193
  expect(flag.value).toBe(defaultFlag.value);
194
194
  })
195
195
 
196
+ test('request timeout uses default if not provided', async () => {
197
+
198
+ const flg = new Flagsmith({
199
+ environmentKey: 'key',
200
+ });
201
+
202
+ expect(flg.requestTimeoutMs).toBe(10000);
203
+ })
204
+
196
205
  test('test_throws_when_no_identity_flags_returned_due_to_error', async () => {
197
206
  // @ts-ignore
198
207
  fetch.mockReturnValue(Promise.resolve(new Response('bad data')));