@flipfeatureflag/core 0.1.11 → 0.1.13

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/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  type FlagValue = boolean | string | number | Record<string, unknown>;
2
2
  type SdkReason = "rule_match" | "fallthrough" | "default" | "disabled";
3
3
  interface SdkContext {
4
- userId?: string;
4
+ userId: string;
5
5
  attributes?: Record<string, unknown>;
6
6
  }
7
7
  interface SdkEvaluateRequest {
@@ -28,7 +28,7 @@ interface SdkMetricEvent {
28
28
  variationKey: string;
29
29
  reason: SdkReason | string;
30
30
  ts: string;
31
- context?: SdkContext;
31
+ context: SdkContext;
32
32
  }
33
33
  interface SdkMetricsRequest {
34
34
  env?: string;
@@ -78,7 +78,7 @@ interface FlipFlagClientOptions {
78
78
  metricsIntervalMs?: number;
79
79
  disableRefresh?: boolean;
80
80
  disableMetrics?: boolean;
81
- context?: SdkContext;
81
+ context: SdkContext;
82
82
  fetch?: typeof fetch;
83
83
  }
84
84
  interface FlagsStatus {
@@ -98,6 +98,7 @@ declare class FlipFlagClient {
98
98
  private emitter;
99
99
  private status;
100
100
  private evaluations;
101
+ private knownFlagKeys;
101
102
  private refreshTimer?;
102
103
  private metricsTimer?;
103
104
  private metricsBuffer;
@@ -115,6 +116,7 @@ declare class FlipFlagClient {
115
116
  getContext(): SdkContext;
116
117
  updateContext(partial: SdkContext): void;
117
118
  getAllFlags(): Record<string, SdkFlagEvaluation>;
119
+ hasFlag(flagKey: string): boolean;
118
120
  isEnabled(flagKey: string, defaultValue?: boolean): boolean;
119
121
  getVariant(flagKey: string, defaultValue?: FlagValue): SdkFlagEvaluation;
120
122
  private evaluate;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  type FlagValue = boolean | string | number | Record<string, unknown>;
2
2
  type SdkReason = "rule_match" | "fallthrough" | "default" | "disabled";
3
3
  interface SdkContext {
4
- userId?: string;
4
+ userId: string;
5
5
  attributes?: Record<string, unknown>;
6
6
  }
7
7
  interface SdkEvaluateRequest {
@@ -28,7 +28,7 @@ interface SdkMetricEvent {
28
28
  variationKey: string;
29
29
  reason: SdkReason | string;
30
30
  ts: string;
31
- context?: SdkContext;
31
+ context: SdkContext;
32
32
  }
33
33
  interface SdkMetricsRequest {
34
34
  env?: string;
@@ -78,7 +78,7 @@ interface FlipFlagClientOptions {
78
78
  metricsIntervalMs?: number;
79
79
  disableRefresh?: boolean;
80
80
  disableMetrics?: boolean;
81
- context?: SdkContext;
81
+ context: SdkContext;
82
82
  fetch?: typeof fetch;
83
83
  }
84
84
  interface FlagsStatus {
@@ -98,6 +98,7 @@ declare class FlipFlagClient {
98
98
  private emitter;
99
99
  private status;
100
100
  private evaluations;
101
+ private knownFlagKeys;
101
102
  private refreshTimer?;
102
103
  private metricsTimer?;
103
104
  private metricsBuffer;
@@ -115,6 +116,7 @@ declare class FlipFlagClient {
115
116
  getContext(): SdkContext;
116
117
  updateContext(partial: SdkContext): void;
117
118
  getAllFlags(): Record<string, SdkFlagEvaluation>;
119
+ hasFlag(flagKey: string): boolean;
118
120
  isEnabled(flagKey: string, defaultValue?: boolean): boolean;
119
121
  getVariant(flagKey: string, defaultValue?: FlagValue): SdkFlagEvaluation;
120
122
  private evaluate;
package/dist/index.js CHANGED
@@ -138,6 +138,7 @@ var FlipFlagClient = class {
138
138
  this.emitter = new Emitter();
139
139
  this.status = { ready: false };
140
140
  this.evaluations = /* @__PURE__ */ new Map();
141
+ this.knownFlagKeys = /* @__PURE__ */ new Set();
141
142
  this.metricsBuffer = new MetricsBuffer();
142
143
  this.started = false;
143
144
  this.pendingFlagKeys = /* @__PURE__ */ new Set();
@@ -153,7 +154,10 @@ var FlipFlagClient = class {
153
154
  metricsIntervalMs: DEFAULT_METRICS_MS,
154
155
  ...options
155
156
  };
156
- this.context = options.context ?? {};
157
+ if (!options.context?.userId) {
158
+ throw new Error("flipFeatureFlag SDK requires context.userId");
159
+ }
160
+ this.context = options.context;
157
161
  const fetchFn = options.fetch ?? fetch;
158
162
  this.http = new HttpClient({
159
163
  baseUrl: this.options.url,
@@ -219,6 +223,9 @@ var FlipFlagClient = class {
219
223
  }
220
224
  return result;
221
225
  }
226
+ hasFlag(flagKey) {
227
+ return this.knownFlagKeys.has(flagKey);
228
+ }
222
229
  isEnabled(flagKey, defaultValue = false) {
223
230
  const evaluation = this.evaluate(flagKey, defaultValue);
224
231
  return Boolean(evaluation.value);
@@ -269,7 +276,7 @@ var FlipFlagClient = class {
269
276
  context: this.context,
270
277
  flagKeys: keys
271
278
  });
272
- this.applyEvaluate(response);
279
+ this.applyEvaluate(response, keys);
273
280
  if (!this.status.ready) {
274
281
  this.status.ready = true;
275
282
  this.emitter.emit("ready");
@@ -287,9 +294,22 @@ var FlipFlagClient = class {
287
294
  })();
288
295
  return this.refreshInFlight;
289
296
  }
290
- applyEvaluate(response) {
297
+ applyEvaluate(response, requestedKeys) {
298
+ if (!requestedKeys) {
299
+ this.knownFlagKeys.clear();
300
+ }
301
+ const returnedKeys = /* @__PURE__ */ new Set();
291
302
  for (const [key, evaluation] of Object.entries(response.flags)) {
292
303
  this.evaluations.set(key, evaluation);
304
+ this.knownFlagKeys.add(key);
305
+ returnedKeys.add(key);
306
+ }
307
+ if (requestedKeys) {
308
+ for (const key of requestedKeys) {
309
+ if (!returnedKeys.has(key)) {
310
+ this.knownFlagKeys.delete(key);
311
+ }
312
+ }
293
313
  }
294
314
  this.status.updatedAt = response.updatedAt;
295
315
  }
package/dist/index.mjs CHANGED
@@ -112,6 +112,7 @@ var FlipFlagClient = class {
112
112
  this.emitter = new Emitter();
113
113
  this.status = { ready: false };
114
114
  this.evaluations = /* @__PURE__ */ new Map();
115
+ this.knownFlagKeys = /* @__PURE__ */ new Set();
115
116
  this.metricsBuffer = new MetricsBuffer();
116
117
  this.started = false;
117
118
  this.pendingFlagKeys = /* @__PURE__ */ new Set();
@@ -127,7 +128,10 @@ var FlipFlagClient = class {
127
128
  metricsIntervalMs: DEFAULT_METRICS_MS,
128
129
  ...options
129
130
  };
130
- this.context = options.context ?? {};
131
+ if (!options.context?.userId) {
132
+ throw new Error("flipFeatureFlag SDK requires context.userId");
133
+ }
134
+ this.context = options.context;
131
135
  const fetchFn = options.fetch ?? fetch;
132
136
  this.http = new HttpClient({
133
137
  baseUrl: this.options.url,
@@ -193,6 +197,9 @@ var FlipFlagClient = class {
193
197
  }
194
198
  return result;
195
199
  }
200
+ hasFlag(flagKey) {
201
+ return this.knownFlagKeys.has(flagKey);
202
+ }
196
203
  isEnabled(flagKey, defaultValue = false) {
197
204
  const evaluation = this.evaluate(flagKey, defaultValue);
198
205
  return Boolean(evaluation.value);
@@ -243,7 +250,7 @@ var FlipFlagClient = class {
243
250
  context: this.context,
244
251
  flagKeys: keys
245
252
  });
246
- this.applyEvaluate(response);
253
+ this.applyEvaluate(response, keys);
247
254
  if (!this.status.ready) {
248
255
  this.status.ready = true;
249
256
  this.emitter.emit("ready");
@@ -261,9 +268,22 @@ var FlipFlagClient = class {
261
268
  })();
262
269
  return this.refreshInFlight;
263
270
  }
264
- applyEvaluate(response) {
271
+ applyEvaluate(response, requestedKeys) {
272
+ if (!requestedKeys) {
273
+ this.knownFlagKeys.clear();
274
+ }
275
+ const returnedKeys = /* @__PURE__ */ new Set();
265
276
  for (const [key, evaluation] of Object.entries(response.flags)) {
266
277
  this.evaluations.set(key, evaluation);
278
+ this.knownFlagKeys.add(key);
279
+ returnedKeys.add(key);
280
+ }
281
+ if (requestedKeys) {
282
+ for (const key of requestedKeys) {
283
+ if (!returnedKeys.has(key)) {
284
+ this.knownFlagKeys.delete(key);
285
+ }
286
+ }
267
287
  }
268
288
  this.status.updatedAt = response.updatedAt;
269
289
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flipfeatureflag/core",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "flipFeatureFlag core SDK",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",