flagsmith-nodejs 2.0.0 → 2.0.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.
- package/.idea/flagsmith-nodejs-client.iml +12 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/README.md +1 -1
- package/build/flagsmith-engine/environments/integrations/models.d.ts +4 -0
- package/build/flagsmith-engine/environments/integrations/models.js +11 -0
- package/build/flagsmith-engine/environments/models.d.ts +25 -0
- package/build/flagsmith-engine/environments/models.js +29 -0
- package/build/flagsmith-engine/environments/util.d.ts +3 -0
- package/build/flagsmith-engine/environments/util.js +21 -0
- package/build/flagsmith-engine/features/constants.d.ts +4 -0
- package/build/flagsmith-engine/features/constants.js +7 -0
- package/build/flagsmith-engine/features/models.d.ts +37 -0
- package/build/flagsmith-engine/features/models.js +122 -0
- package/build/flagsmith-engine/features/util.d.ts +4 -0
- package/build/flagsmith-engine/features/util.js +27 -0
- package/build/flagsmith-engine/identities/models.d.ts +15 -0
- package/build/flagsmith-engine/identities/models.js +112 -0
- package/build/flagsmith-engine/identities/traits/models.d.ts +5 -0
- package/build/flagsmith-engine/identities/traits/models.js +11 -0
- package/build/flagsmith-engine/identities/util.d.ts +4 -0
- package/build/flagsmith-engine/identities/util.js +46 -0
- package/build/flagsmith-engine/index.d.ts +8 -0
- package/build/flagsmith-engine/index.js +115 -0
- package/build/flagsmith-engine/organisations/models.d.ts +9 -0
- package/build/flagsmith-engine/organisations/models.js +21 -0
- package/build/flagsmith-engine/organisations/util.d.ts +2 -0
- package/build/flagsmith-engine/organisations/util.js +8 -0
- package/build/flagsmith-engine/projects/models.d.ts +10 -0
- package/build/flagsmith-engine/projects/models.js +14 -0
- package/build/flagsmith-engine/projects/util.d.ts +2 -0
- package/build/flagsmith-engine/projects/util.js +15 -0
- package/build/flagsmith-engine/segments/constants.d.ts +26 -0
- package/build/flagsmith-engine/segments/constants.js +31 -0
- package/build/flagsmith-engine/segments/evaluators.d.ts +6 -0
- package/build/flagsmith-engine/segments/evaluators.js +37 -0
- package/build/flagsmith-engine/segments/models.d.ts +37 -0
- package/build/flagsmith-engine/segments/models.js +114 -0
- package/build/flagsmith-engine/segments/util.d.ts +6 -0
- package/build/flagsmith-engine/segments/util.js +33 -0
- package/build/flagsmith-engine/utils/collections.d.ts +3 -0
- package/build/flagsmith-engine/utils/collections.js +26 -0
- package/build/flagsmith-engine/utils/errors.d.ts +2 -0
- package/build/flagsmith-engine/utils/errors.js +26 -0
- package/build/flagsmith-engine/utils/hashing/index.d.ts +9 -0
- package/build/flagsmith-engine/utils/hashing/index.js +60 -0
- package/build/flagsmith-engine/utils/index.d.ts +1 -0
- package/build/flagsmith-engine/utils/index.js +17 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +11 -0
- package/build/sdk/analytics.d.ts +28 -0
- package/build/sdk/analytics.js +102 -0
- package/build/sdk/errors.d.ts +4 -0
- package/build/sdk/errors.js +34 -0
- package/build/sdk/index.d.ts +123 -0
- package/build/sdk/index.js +492 -0
- package/build/sdk/models.d.ts +55 -0
- package/build/sdk/models.js +149 -0
- package/build/sdk/polling_manager.d.ts +9 -0
- package/build/sdk/polling_manager.js +72 -0
- package/build/sdk/types.d.ts +7 -0
- package/build/sdk/types.js +2 -0
- package/build/sdk/utils.d.ts +12 -0
- package/build/sdk/utils.js +92 -0
- package/example/package-lock.json +996 -1
- package/example/server/api/index.js +6 -3
- package/example/server/index.js +1 -1
- package/package.json +3 -2
- package/sdk/index.ts +4 -0
- package/sdk/types.ts +4 -4
- package/tests/sdk/utils.ts +2 -1
- package/.tool-versions +0 -1
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __values = (this && this.__values) || function(o) {
|
|
39
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
40
|
+
if (m) return m.call(o);
|
|
41
|
+
if (o && typeof o.length === "number") return {
|
|
42
|
+
next: function () {
|
|
43
|
+
if (o && i >= o.length) o = void 0;
|
|
44
|
+
return { value: o && o[i++], done: !o };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
48
|
+
};
|
|
49
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
50
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
+
if (!m) return o;
|
|
52
|
+
var i = m.call(o), r, ar = [], e;
|
|
53
|
+
try {
|
|
54
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
+
}
|
|
56
|
+
catch (error) { e = { error: error }; }
|
|
57
|
+
finally {
|
|
58
|
+
try {
|
|
59
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
+
}
|
|
61
|
+
finally { if (e) throw e.error; }
|
|
62
|
+
}
|
|
63
|
+
return ar;
|
|
64
|
+
};
|
|
65
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
66
|
+
exports.Flagsmith = void 0;
|
|
67
|
+
var flagsmith_engine_1 = require("../flagsmith-engine");
|
|
68
|
+
var util_1 = require("../flagsmith-engine/environments/util");
|
|
69
|
+
var models_1 = require("../flagsmith-engine/identities/models");
|
|
70
|
+
var models_2 = require("../flagsmith-engine/identities/traits/models");
|
|
71
|
+
var analytics_1 = require("./analytics");
|
|
72
|
+
var errors_1 = require("./errors");
|
|
73
|
+
var models_3 = require("./models");
|
|
74
|
+
var polling_manager_1 = require("./polling_manager");
|
|
75
|
+
var utils_1 = require("./utils");
|
|
76
|
+
var evaluators_1 = require("../flagsmith-engine/segments/evaluators");
|
|
77
|
+
var DEFAULT_API_URL = 'https://edge.api.flagsmith.com/api/v1/';
|
|
78
|
+
var Flagsmith = /** @class */ (function () {
|
|
79
|
+
/**
|
|
80
|
+
* A Flagsmith client.
|
|
81
|
+
*
|
|
82
|
+
* Provides an interface for interacting with the Flagsmith http API.
|
|
83
|
+
* Basic Usage::
|
|
84
|
+
*
|
|
85
|
+
* import flagsmith from Flagsmith
|
|
86
|
+
* const flagsmith = new Flagsmith({environmentKey: '<your API key>'});
|
|
87
|
+
* const environmentFlags = flagsmith.getEnvironmentFlags();
|
|
88
|
+
* const featureEnabled = environmentFlags.isFeatureEnabled('foo');
|
|
89
|
+
* const identityFlags = flagsmith.getIdentityFlags('identifier', {'foo': 'bar'});
|
|
90
|
+
* const featureEnabledForIdentity = identityFlags.isFeatureEnabled("foo")
|
|
91
|
+
*
|
|
92
|
+
* @param {string} data.environmentKey: The environment key obtained from Flagsmith interface
|
|
93
|
+
@param {string} data.apiUrl: Override the URL of the Flagsmith API to communicate with
|
|
94
|
+
@param data.customHeaders: Additional headers to add to requests made to the
|
|
95
|
+
Flagsmith API
|
|
96
|
+
@param {number} data.requestTimeoutSeconds: Number of seconds to wait for a request to
|
|
97
|
+
complete before terminating the request
|
|
98
|
+
@param {boolean} data.enableLocalEvaluation: Enables local evaluation of flags
|
|
99
|
+
@param {number} data.environmentRefreshIntervalSeconds: If using local evaluation,
|
|
100
|
+
specify the interval period between refreshes of local environment data
|
|
101
|
+
@param {number} data.retries: a urllib3.Retry object to use on all http requests to the
|
|
102
|
+
Flagsmith API
|
|
103
|
+
@param {boolean} data.enableAnalytics: if enabled, sends additional requests to the Flagsmith
|
|
104
|
+
API to power flag analytics charts
|
|
105
|
+
@param data.defaultFlagHandler: callable which will be used in the case where
|
|
106
|
+
flags cannot be retrieved from the API or a non existent feature is
|
|
107
|
+
requested
|
|
108
|
+
*/
|
|
109
|
+
function Flagsmith(data) {
|
|
110
|
+
this.apiUrl = DEFAULT_API_URL;
|
|
111
|
+
this.enableLocalEvaluation = false;
|
|
112
|
+
this.environmentRefreshIntervalSeconds = 60;
|
|
113
|
+
this.enableAnalytics = false;
|
|
114
|
+
this.environmentKey = data.environmentKey;
|
|
115
|
+
this.apiUrl = data.apiUrl || this.apiUrl;
|
|
116
|
+
this.customHeaders = data.customHeaders;
|
|
117
|
+
this.requestTimeoutSeconds = data.requestTimeoutSeconds;
|
|
118
|
+
this.enableLocalEvaluation = data.enableLocalEvaluation;
|
|
119
|
+
this.environmentRefreshIntervalSeconds =
|
|
120
|
+
data.environmentRefreshIntervalSeconds || this.environmentRefreshIntervalSeconds;
|
|
121
|
+
this.retries = data.retries;
|
|
122
|
+
this.enableAnalytics = data.enableAnalytics || false;
|
|
123
|
+
this.defaultFlagHandler = data.defaultFlagHandler;
|
|
124
|
+
this.environmentFlagsUrl = "".concat(this.apiUrl, "flags/");
|
|
125
|
+
this.identitiesUrl = "".concat(this.apiUrl, "identities/");
|
|
126
|
+
this.environmentUrl = "".concat(this.apiUrl, "environment-document/");
|
|
127
|
+
this.onEnvironmentChange = data.onEnvironmentChange;
|
|
128
|
+
if (!!data.cache) {
|
|
129
|
+
var missingMethods = ['has', 'get', 'set'].filter(function (method) { return data.cache && !data.cache[method]; });
|
|
130
|
+
if (missingMethods.length > 0) {
|
|
131
|
+
throw new Error("Please implement the following methods in your cache: ".concat(missingMethods.join(', ')));
|
|
132
|
+
}
|
|
133
|
+
this.cache = data.cache;
|
|
134
|
+
}
|
|
135
|
+
if (this.enableLocalEvaluation) {
|
|
136
|
+
if (!this.environmentKey.startsWith('ser.')) {
|
|
137
|
+
console.error('In order to use local evaluation, please generate a server key in the environment settings page.');
|
|
138
|
+
}
|
|
139
|
+
this.environmentDataPollingManager = new polling_manager_1.EnvironmentDataPollingManager(this, this.environmentRefreshIntervalSeconds);
|
|
140
|
+
this.environmentDataPollingManager.start();
|
|
141
|
+
this.updateEnvironment();
|
|
142
|
+
}
|
|
143
|
+
this.analyticsProcessor = data.enableAnalytics
|
|
144
|
+
? new analytics_1.AnalyticsProcessor({
|
|
145
|
+
environmentKey: this.environmentKey,
|
|
146
|
+
baseApiUrl: this.apiUrl,
|
|
147
|
+
timeout: this.requestTimeoutSeconds
|
|
148
|
+
})
|
|
149
|
+
: undefined;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get all the default for flags for the current environment.
|
|
153
|
+
*
|
|
154
|
+
* @returns Flags object holding all the flags for the current environment.
|
|
155
|
+
*/
|
|
156
|
+
Flagsmith.prototype.getEnvironmentFlags = function () {
|
|
157
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
158
|
+
var cachedItem, _a;
|
|
159
|
+
var _this = this;
|
|
160
|
+
return __generator(this, function (_b) {
|
|
161
|
+
switch (_b.label) {
|
|
162
|
+
case 0:
|
|
163
|
+
_a = !!this.cache;
|
|
164
|
+
if (!_a) return [3 /*break*/, 2];
|
|
165
|
+
return [4 /*yield*/, this.cache.get("flags")];
|
|
166
|
+
case 1:
|
|
167
|
+
_a = (_b.sent());
|
|
168
|
+
_b.label = 2;
|
|
169
|
+
case 2:
|
|
170
|
+
cachedItem = _a;
|
|
171
|
+
if (!!cachedItem) {
|
|
172
|
+
return [2 /*return*/, cachedItem];
|
|
173
|
+
}
|
|
174
|
+
if (this.enableLocalEvaluation) {
|
|
175
|
+
return [2 /*return*/, new Promise(function (resolve) {
|
|
176
|
+
return _this.environmentPromise.then(function () {
|
|
177
|
+
resolve(_this.getEnvironmentFlagsFromDocument());
|
|
178
|
+
});
|
|
179
|
+
})];
|
|
180
|
+
}
|
|
181
|
+
if (this.environment) {
|
|
182
|
+
return [2 /*return*/, this.getEnvironmentFlagsFromDocument()];
|
|
183
|
+
}
|
|
184
|
+
return [2 /*return*/, this.getEnvironmentFlagsFromApi()];
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
/**
|
|
190
|
+
* Get all the flags for the current environment for a given identity. Will also
|
|
191
|
+
upsert all traits to the Flagsmith API for future evaluations. Providing a
|
|
192
|
+
trait with a value of None will remove the trait from the identity if it exists.
|
|
193
|
+
*
|
|
194
|
+
* @param {string} identifier a unique identifier for the identity in the current
|
|
195
|
+
environment, e.g. email address, username, uuid
|
|
196
|
+
* @param {{[key:string]:any}} traits? a dictionary of traits to add / update on the identity in
|
|
197
|
+
Flagsmith, e.g. {"num_orders": 10}
|
|
198
|
+
* @returns Flags object holding all the flags for the given identity.
|
|
199
|
+
*/
|
|
200
|
+
Flagsmith.prototype.getIdentityFlags = function (identifier, traits) {
|
|
201
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
202
|
+
var cachedItem, _a;
|
|
203
|
+
var _this = this;
|
|
204
|
+
return __generator(this, function (_b) {
|
|
205
|
+
switch (_b.label) {
|
|
206
|
+
case 0:
|
|
207
|
+
_a = !!this.cache;
|
|
208
|
+
if (!_a) return [3 /*break*/, 2];
|
|
209
|
+
return [4 /*yield*/, this.cache.get("flags-".concat(identifier))];
|
|
210
|
+
case 1:
|
|
211
|
+
_a = (_b.sent());
|
|
212
|
+
_b.label = 2;
|
|
213
|
+
case 2:
|
|
214
|
+
cachedItem = _a;
|
|
215
|
+
if (!!cachedItem) {
|
|
216
|
+
return [2 /*return*/, cachedItem];
|
|
217
|
+
}
|
|
218
|
+
traits = traits || {};
|
|
219
|
+
if (this.enableLocalEvaluation) {
|
|
220
|
+
return [2 /*return*/, new Promise(function (resolve) {
|
|
221
|
+
return _this.environmentPromise.then(function () {
|
|
222
|
+
resolve(_this.getIdentityFlagsFromDocument(identifier, traits || {}));
|
|
223
|
+
});
|
|
224
|
+
})];
|
|
225
|
+
}
|
|
226
|
+
return [2 /*return*/, this.getIdentityFlagsFromApi(identifier, traits)];
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
};
|
|
231
|
+
/**
|
|
232
|
+
* Get the segments for the current environment for a given identity. Will also
|
|
233
|
+
upsert all traits to the Flagsmith API for future evaluations. Providing a
|
|
234
|
+
trait with a value of None will remove the trait from the identity if it exists.
|
|
235
|
+
*
|
|
236
|
+
* @param {string} identifier a unique identifier for the identity in the current
|
|
237
|
+
environment, e.g. email address, username, uuid
|
|
238
|
+
* @param {{[key:string]:any}} traits? a dictionary of traits to add / update on the identity in
|
|
239
|
+
Flagsmith, e.g. {"num_orders": 10}
|
|
240
|
+
* @returns Segments that the given identity belongs to.
|
|
241
|
+
*/
|
|
242
|
+
Flagsmith.prototype.getIdentitySegments = function (identifier, traits) {
|
|
243
|
+
var _this = this;
|
|
244
|
+
traits = traits || {};
|
|
245
|
+
if (this.enableLocalEvaluation) {
|
|
246
|
+
return this.environmentPromise.then(function () {
|
|
247
|
+
return new Promise(function (resolve) {
|
|
248
|
+
var identityModel = _this.buildIdentityModel(identifier, Object.keys(traits || {}).map(function (key) { return ({
|
|
249
|
+
key: key,
|
|
250
|
+
value: traits === null || traits === void 0 ? void 0 : traits[key]
|
|
251
|
+
}); }));
|
|
252
|
+
var segments = (0, evaluators_1.getIdentitySegments)(_this.environment, identityModel);
|
|
253
|
+
return resolve(segments);
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
console.error('This function is only permitted with local evaluation.');
|
|
258
|
+
return Promise.resolve([]);
|
|
259
|
+
};
|
|
260
|
+
/**
|
|
261
|
+
* Updates the environment state for local flag evaluation.
|
|
262
|
+
* Sets a local promise to prevent race conditions in getIdentityFlags / getIdentitySegments.
|
|
263
|
+
* You only need to call this if you wish to bypass environmentRefreshIntervalSeconds.
|
|
264
|
+
*/
|
|
265
|
+
Flagsmith.prototype.updateEnvironment = function () {
|
|
266
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
267
|
+
var request, _a, e_1;
|
|
268
|
+
var _this = this;
|
|
269
|
+
return __generator(this, function (_b) {
|
|
270
|
+
switch (_b.label) {
|
|
271
|
+
case 0:
|
|
272
|
+
_b.trys.push([0, 5, , 6]);
|
|
273
|
+
request = this.getEnvironmentFromApi();
|
|
274
|
+
if (!!this.environmentPromise) return [3 /*break*/, 2];
|
|
275
|
+
this.environmentPromise = request.then(function (res) {
|
|
276
|
+
_this.environment = res;
|
|
277
|
+
});
|
|
278
|
+
return [4 /*yield*/, this.environmentPromise];
|
|
279
|
+
case 1:
|
|
280
|
+
_b.sent();
|
|
281
|
+
return [3 /*break*/, 4];
|
|
282
|
+
case 2:
|
|
283
|
+
_a = this;
|
|
284
|
+
return [4 /*yield*/, request];
|
|
285
|
+
case 3:
|
|
286
|
+
_a.environment = _b.sent();
|
|
287
|
+
_b.label = 4;
|
|
288
|
+
case 4:
|
|
289
|
+
if (this.onEnvironmentChange) {
|
|
290
|
+
this.onEnvironmentChange(null, this.environment);
|
|
291
|
+
}
|
|
292
|
+
return [3 /*break*/, 6];
|
|
293
|
+
case 5:
|
|
294
|
+
e_1 = _b.sent();
|
|
295
|
+
if (this.onEnvironmentChange) {
|
|
296
|
+
this.onEnvironmentChange(e_1, this.environment);
|
|
297
|
+
}
|
|
298
|
+
return [3 /*break*/, 6];
|
|
299
|
+
case 6: return [2 /*return*/];
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
};
|
|
304
|
+
Flagsmith.prototype.getJSONResponse = function (url, method, body) {
|
|
305
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
306
|
+
var headers, _a, _b, _c, k, v, data;
|
|
307
|
+
var e_2, _d;
|
|
308
|
+
return __generator(this, function (_e) {
|
|
309
|
+
switch (_e.label) {
|
|
310
|
+
case 0:
|
|
311
|
+
headers = { 'Content-Type': 'application/json' };
|
|
312
|
+
if (this.environmentKey) {
|
|
313
|
+
headers['X-Environment-Key'] = this.environmentKey;
|
|
314
|
+
}
|
|
315
|
+
if (this.customHeaders) {
|
|
316
|
+
try {
|
|
317
|
+
for (_a = __values(Object.entries(this.customHeaders)), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
318
|
+
_c = __read(_b.value, 2), k = _c[0], v = _c[1];
|
|
319
|
+
headers[k] = v;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
323
|
+
finally {
|
|
324
|
+
try {
|
|
325
|
+
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
326
|
+
}
|
|
327
|
+
finally { if (e_2) throw e_2.error; }
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return [4 /*yield*/, (0, utils_1.retryFetch)(url, {
|
|
331
|
+
method: method,
|
|
332
|
+
timeout: this.requestTimeoutSeconds || undefined,
|
|
333
|
+
body: JSON.stringify(body),
|
|
334
|
+
headers: headers
|
|
335
|
+
}, this.retries, (this.requestTimeoutSeconds || 10) * 1000)];
|
|
336
|
+
case 1:
|
|
337
|
+
data = _e.sent();
|
|
338
|
+
if (data.status !== 200) {
|
|
339
|
+
throw new errors_1.FlagsmithAPIError("Invalid request made to Flagsmith API. Response status code: ".concat(data.status));
|
|
340
|
+
}
|
|
341
|
+
return [2 /*return*/, data.json()];
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
};
|
|
346
|
+
Flagsmith.prototype.getEnvironmentFromApi = function () {
|
|
347
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
348
|
+
var environment_data;
|
|
349
|
+
return __generator(this, function (_a) {
|
|
350
|
+
switch (_a.label) {
|
|
351
|
+
case 0: return [4 /*yield*/, this.getJSONResponse(this.environmentUrl, 'GET')];
|
|
352
|
+
case 1:
|
|
353
|
+
environment_data = _a.sent();
|
|
354
|
+
return [2 /*return*/, (0, util_1.buildEnvironmentModel)(environment_data)];
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
};
|
|
359
|
+
Flagsmith.prototype.getEnvironmentFlagsFromDocument = function () {
|
|
360
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
361
|
+
var flags;
|
|
362
|
+
return __generator(this, function (_a) {
|
|
363
|
+
switch (_a.label) {
|
|
364
|
+
case 0:
|
|
365
|
+
flags = models_3.Flags.fromFeatureStateModels({
|
|
366
|
+
featureStates: (0, flagsmith_engine_1.getEnvironmentFeatureStates)(this.environment),
|
|
367
|
+
analyticsProcessor: this.analyticsProcessor,
|
|
368
|
+
defaultFlagHandler: this.defaultFlagHandler
|
|
369
|
+
});
|
|
370
|
+
if (!!!this.cache) return [3 /*break*/, 2];
|
|
371
|
+
// @ts-ignore node-cache types are incorrect, ttl should be optional
|
|
372
|
+
return [4 /*yield*/, this.cache.set('flags', flags)];
|
|
373
|
+
case 1:
|
|
374
|
+
// @ts-ignore node-cache types are incorrect, ttl should be optional
|
|
375
|
+
_a.sent();
|
|
376
|
+
_a.label = 2;
|
|
377
|
+
case 2: return [2 /*return*/, flags];
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
};
|
|
382
|
+
Flagsmith.prototype.getIdentityFlagsFromDocument = function (identifier, traits) {
|
|
383
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
384
|
+
var identityModel, featureStates, flags;
|
|
385
|
+
return __generator(this, function (_a) {
|
|
386
|
+
switch (_a.label) {
|
|
387
|
+
case 0:
|
|
388
|
+
identityModel = this.buildIdentityModel(identifier, Object.keys(traits).map(function (key) { return ({
|
|
389
|
+
key: key,
|
|
390
|
+
value: traits[key]
|
|
391
|
+
}); }));
|
|
392
|
+
featureStates = (0, flagsmith_engine_1.getIdentityFeatureStates)(this.environment, identityModel);
|
|
393
|
+
flags = models_3.Flags.fromFeatureStateModels({
|
|
394
|
+
featureStates: featureStates,
|
|
395
|
+
analyticsProcessor: this.analyticsProcessor,
|
|
396
|
+
defaultFlagHandler: this.defaultFlagHandler
|
|
397
|
+
});
|
|
398
|
+
if (!!!this.cache) return [3 /*break*/, 2];
|
|
399
|
+
// @ts-ignore node-cache types are incorrect, ttl should be optional
|
|
400
|
+
return [4 /*yield*/, this.cache.set("flags-".concat(identifier), flags)];
|
|
401
|
+
case 1:
|
|
402
|
+
// @ts-ignore node-cache types are incorrect, ttl should be optional
|
|
403
|
+
_a.sent();
|
|
404
|
+
_a.label = 2;
|
|
405
|
+
case 2: return [2 /*return*/, flags];
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
});
|
|
409
|
+
};
|
|
410
|
+
Flagsmith.prototype.getEnvironmentFlagsFromApi = function () {
|
|
411
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
412
|
+
var apiFlags, flags, e_3;
|
|
413
|
+
return __generator(this, function (_a) {
|
|
414
|
+
switch (_a.label) {
|
|
415
|
+
case 0:
|
|
416
|
+
_a.trys.push([0, 4, , 5]);
|
|
417
|
+
return [4 /*yield*/, this.getJSONResponse(this.environmentFlagsUrl, 'GET')];
|
|
418
|
+
case 1:
|
|
419
|
+
apiFlags = _a.sent();
|
|
420
|
+
flags = models_3.Flags.fromAPIFlags({
|
|
421
|
+
apiFlags: apiFlags,
|
|
422
|
+
analyticsProcessor: this.analyticsProcessor,
|
|
423
|
+
defaultFlagHandler: this.defaultFlagHandler
|
|
424
|
+
});
|
|
425
|
+
if (!!!this.cache) return [3 /*break*/, 3];
|
|
426
|
+
// @ts-ignore node-cache types are incorrect, ttl should be optional
|
|
427
|
+
return [4 /*yield*/, this.cache.set('flags', flags)];
|
|
428
|
+
case 2:
|
|
429
|
+
// @ts-ignore node-cache types are incorrect, ttl should be optional
|
|
430
|
+
_a.sent();
|
|
431
|
+
_a.label = 3;
|
|
432
|
+
case 3: return [2 /*return*/, flags];
|
|
433
|
+
case 4:
|
|
434
|
+
e_3 = _a.sent();
|
|
435
|
+
if (this.defaultFlagHandler) {
|
|
436
|
+
return [2 /*return*/, new models_3.Flags({
|
|
437
|
+
flags: {},
|
|
438
|
+
defaultFlagHandler: this.defaultFlagHandler
|
|
439
|
+
})];
|
|
440
|
+
}
|
|
441
|
+
throw e_3;
|
|
442
|
+
case 5: return [2 /*return*/];
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
});
|
|
446
|
+
};
|
|
447
|
+
Flagsmith.prototype.getIdentityFlagsFromApi = function (identifier, traits) {
|
|
448
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
449
|
+
var data, jsonResponse, flags, e_4;
|
|
450
|
+
return __generator(this, function (_a) {
|
|
451
|
+
switch (_a.label) {
|
|
452
|
+
case 0:
|
|
453
|
+
_a.trys.push([0, 4, , 5]);
|
|
454
|
+
data = (0, utils_1.generateIdentitiesData)(identifier, traits);
|
|
455
|
+
return [4 /*yield*/, this.getJSONResponse(this.identitiesUrl, 'POST', data)];
|
|
456
|
+
case 1:
|
|
457
|
+
jsonResponse = _a.sent();
|
|
458
|
+
flags = models_3.Flags.fromAPIFlags({
|
|
459
|
+
apiFlags: jsonResponse['flags'],
|
|
460
|
+
analyticsProcessor: this.analyticsProcessor,
|
|
461
|
+
defaultFlagHandler: this.defaultFlagHandler
|
|
462
|
+
});
|
|
463
|
+
if (!!!this.cache) return [3 /*break*/, 3];
|
|
464
|
+
// @ts-ignore node-cache types are incorrect, ttl should be optional
|
|
465
|
+
return [4 /*yield*/, this.cache.set("flags-".concat(identifier), flags)];
|
|
466
|
+
case 2:
|
|
467
|
+
// @ts-ignore node-cache types are incorrect, ttl should be optional
|
|
468
|
+
_a.sent();
|
|
469
|
+
_a.label = 3;
|
|
470
|
+
case 3: return [2 /*return*/, flags];
|
|
471
|
+
case 4:
|
|
472
|
+
e_4 = _a.sent();
|
|
473
|
+
if (this.defaultFlagHandler) {
|
|
474
|
+
return [2 /*return*/, new models_3.Flags({
|
|
475
|
+
flags: {},
|
|
476
|
+
defaultFlagHandler: this.defaultFlagHandler
|
|
477
|
+
})];
|
|
478
|
+
}
|
|
479
|
+
throw e_4;
|
|
480
|
+
case 5: return [2 /*return*/];
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
};
|
|
485
|
+
Flagsmith.prototype.buildIdentityModel = function (identifier, traits) {
|
|
486
|
+
var traitModels = traits.map(function (trait) { return new models_2.TraitModel(trait.key, trait.value); });
|
|
487
|
+
return new models_1.IdentityModel('0', traitModels, [], this.environment.apiKey, identifier);
|
|
488
|
+
};
|
|
489
|
+
return Flagsmith;
|
|
490
|
+
}());
|
|
491
|
+
exports.Flagsmith = Flagsmith;
|
|
492
|
+
exports.default = Flagsmith;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { FeatureStateModel } from '../flagsmith-engine/features/models';
|
|
2
|
+
import { AnalyticsProcessor } from './analytics';
|
|
3
|
+
export declare class BaseFlag {
|
|
4
|
+
enabled: boolean;
|
|
5
|
+
value: string | number | boolean | undefined;
|
|
6
|
+
isDefault: boolean;
|
|
7
|
+
constructor(value: string | number | boolean | undefined, enabled: boolean, isDefault: boolean);
|
|
8
|
+
}
|
|
9
|
+
export declare class DefaultFlag extends BaseFlag {
|
|
10
|
+
constructor(value: string | number | boolean | undefined, enabled: boolean);
|
|
11
|
+
}
|
|
12
|
+
export declare class Flag extends BaseFlag {
|
|
13
|
+
featureId: number;
|
|
14
|
+
featureName: string;
|
|
15
|
+
constructor(params: {
|
|
16
|
+
value: string | number | boolean | undefined;
|
|
17
|
+
enabled: boolean;
|
|
18
|
+
isDefault?: boolean;
|
|
19
|
+
featureId: number;
|
|
20
|
+
featureName: string;
|
|
21
|
+
});
|
|
22
|
+
static fromFeatureStateModel(fsm: FeatureStateModel, identityId: number | string | undefined): Flag;
|
|
23
|
+
static fromAPIFlag(flagData: any): Flag;
|
|
24
|
+
}
|
|
25
|
+
export declare class Flags {
|
|
26
|
+
flags: {
|
|
27
|
+
[key: string]: Flag;
|
|
28
|
+
};
|
|
29
|
+
defaultFlagHandler?: (featureName: string) => DefaultFlag;
|
|
30
|
+
analyticsProcessor?: AnalyticsProcessor;
|
|
31
|
+
constructor(data: {
|
|
32
|
+
flags: {
|
|
33
|
+
[key: string]: Flag;
|
|
34
|
+
};
|
|
35
|
+
defaultFlagHandler?: (v: string) => DefaultFlag;
|
|
36
|
+
analyticsProcessor?: AnalyticsProcessor;
|
|
37
|
+
});
|
|
38
|
+
static fromFeatureStateModels(data: {
|
|
39
|
+
featureStates: FeatureStateModel[];
|
|
40
|
+
analyticsProcessor?: AnalyticsProcessor;
|
|
41
|
+
defaultFlagHandler?: (f: string) => DefaultFlag;
|
|
42
|
+
identityID?: string | number;
|
|
43
|
+
}): Flags;
|
|
44
|
+
static fromAPIFlags(data: {
|
|
45
|
+
apiFlags: {
|
|
46
|
+
[key: string]: any;
|
|
47
|
+
}[];
|
|
48
|
+
analyticsProcessor?: AnalyticsProcessor;
|
|
49
|
+
defaultFlagHandler?: (v: string) => DefaultFlag;
|
|
50
|
+
}): Flags;
|
|
51
|
+
allFlags(): Flag[];
|
|
52
|
+
getFlag(featureName: string): BaseFlag;
|
|
53
|
+
getFeatureValue(featureName: string): any;
|
|
54
|
+
isFeatureEnabled(featureName: string): boolean;
|
|
55
|
+
}
|