@plyaz/core 1.0.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.
Files changed (82) hide show
  1. package/README.md +439 -0
  2. package/dist/backend/featureFlags/feature-flag.controller.d.ts +135 -0
  3. package/dist/backend/featureFlags/feature-flag.controller.d.ts.map +1 -0
  4. package/dist/backend/featureFlags/feature-flag.module.d.ts +114 -0
  5. package/dist/backend/featureFlags/feature-flag.module.d.ts.map +1 -0
  6. package/dist/backend/featureFlags/feature-flag.repository.d.ts +85 -0
  7. package/dist/backend/featureFlags/feature-flag.repository.d.ts.map +1 -0
  8. package/dist/backend/featureFlags/feature-flag.service.d.ts +123 -0
  9. package/dist/backend/featureFlags/feature-flag.service.d.ts.map +1 -0
  10. package/dist/backend/featureFlags/index.d.ts +49 -0
  11. package/dist/backend/featureFlags/index.d.ts.map +1 -0
  12. package/dist/backend/index.d.ts +5 -0
  13. package/dist/backend/index.d.ts.map +1 -0
  14. package/dist/cache/index.d.ts +98 -0
  15. package/dist/cache/index.d.ts.map +1 -0
  16. package/dist/cache/strategies/memory.d.ts +103 -0
  17. package/dist/cache/strategies/memory.d.ts.map +1 -0
  18. package/dist/cache/strategies/redis.d.ts +105 -0
  19. package/dist/cache/strategies/redis.d.ts.map +1 -0
  20. package/dist/domain/featureFlags/index.d.ts +49 -0
  21. package/dist/domain/featureFlags/index.d.ts.map +1 -0
  22. package/dist/domain/featureFlags/provider.d.ts +166 -0
  23. package/dist/domain/featureFlags/provider.d.ts.map +1 -0
  24. package/dist/domain/featureFlags/providers/api.d.ts +78 -0
  25. package/dist/domain/featureFlags/providers/api.d.ts.map +1 -0
  26. package/dist/domain/featureFlags/providers/database.d.ts +102 -0
  27. package/dist/domain/featureFlags/providers/database.d.ts.map +1 -0
  28. package/dist/domain/featureFlags/providers/factory.d.ts +116 -0
  29. package/dist/domain/featureFlags/providers/factory.d.ts.map +1 -0
  30. package/dist/domain/featureFlags/providers/file.d.ts +84 -0
  31. package/dist/domain/featureFlags/providers/file.d.ts.map +1 -0
  32. package/dist/domain/featureFlags/providers/memory.d.ts +179 -0
  33. package/dist/domain/featureFlags/providers/memory.d.ts.map +1 -0
  34. package/dist/domain/featureFlags/providers/redis.d.ts +80 -0
  35. package/dist/domain/featureFlags/providers/redis.d.ts.map +1 -0
  36. package/dist/domain/index.d.ts +6 -0
  37. package/dist/domain/index.d.ts.map +1 -0
  38. package/dist/domain/types.d.ts +18 -0
  39. package/dist/domain/types.d.ts.map +1 -0
  40. package/dist/engine/featureFlags/engine.d.ts +193 -0
  41. package/dist/engine/featureFlags/engine.d.ts.map +1 -0
  42. package/dist/engine/featureFlags/index.d.ts +10 -0
  43. package/dist/engine/featureFlags/index.d.ts.map +1 -0
  44. package/dist/engine/index.d.ts +5 -0
  45. package/dist/engine/index.d.ts.map +1 -0
  46. package/dist/frontend/featureFlags/hooks/useFeatureFlag.d.ts +103 -0
  47. package/dist/frontend/featureFlags/hooks/useFeatureFlag.d.ts.map +1 -0
  48. package/dist/frontend/featureFlags/hooks/useFeatureFlagActions.d.ts +35 -0
  49. package/dist/frontend/featureFlags/hooks/useFeatureFlagActions.d.ts.map +1 -0
  50. package/dist/frontend/featureFlags/hooks/useFeatureFlagHelpers.d.ts +55 -0
  51. package/dist/frontend/featureFlags/hooks/useFeatureFlagHelpers.d.ts.map +1 -0
  52. package/dist/frontend/featureFlags/hooks/useFeatureFlagProvider.d.ts +57 -0
  53. package/dist/frontend/featureFlags/hooks/useFeatureFlagProvider.d.ts.map +1 -0
  54. package/dist/frontend/featureFlags/index.d.ts +14 -0
  55. package/dist/frontend/featureFlags/index.d.ts.map +1 -0
  56. package/dist/frontend/featureFlags/providers/FeatureFlagProvider.d.ts +99 -0
  57. package/dist/frontend/featureFlags/providers/FeatureFlagProvider.d.ts.map +1 -0
  58. package/dist/frontend/featureFlags/providers/FeatureFlagProviderHelpers.d.ts +45 -0
  59. package/dist/frontend/featureFlags/providers/FeatureFlagProviderHelpers.d.ts.map +1 -0
  60. package/dist/frontend/index.d.ts +2 -0
  61. package/dist/frontend/index.d.ts.map +1 -0
  62. package/dist/index.cjs +3951 -0
  63. package/dist/index.cjs.map +1 -0
  64. package/dist/index.d.ts +7 -0
  65. package/dist/index.d.ts.map +1 -0
  66. package/dist/index.mjs +3902 -0
  67. package/dist/index.mjs.map +1 -0
  68. package/dist/utils/common/hash.d.ts +81 -0
  69. package/dist/utils/common/hash.d.ts.map +1 -0
  70. package/dist/utils/common/index.d.ts +11 -0
  71. package/dist/utils/common/index.d.ts.map +1 -0
  72. package/dist/utils/common/values.d.ts +48 -0
  73. package/dist/utils/common/values.d.ts.map +1 -0
  74. package/dist/utils/featureFlags/conditions.d.ts +114 -0
  75. package/dist/utils/featureFlags/conditions.d.ts.map +1 -0
  76. package/dist/utils/featureFlags/context.d.ts +241 -0
  77. package/dist/utils/featureFlags/context.d.ts.map +1 -0
  78. package/dist/utils/featureFlags/index.d.ts +11 -0
  79. package/dist/utils/featureFlags/index.d.ts.map +1 -0
  80. package/dist/utils/index.d.ts +6 -0
  81. package/dist/utils/index.d.ts.map +1 -0
  82. package/package.json +144 -0
package/dist/index.mjs ADDED
@@ -0,0 +1,3902 @@
1
+ import { FEATURES } from '@plyaz/config';
2
+ import { Post, Param, Body, Put, Delete, Get, Query, Controller, Injectable, Global, Module, HttpException, HttpStatus, Logger } from '@nestjs/common';
3
+ import React, { createContext, useState, useRef, useCallback, useEffect, useContext, useMemo } from 'react';
4
+ import { jsx } from 'react/jsx-runtime';
5
+
6
+ // @plyaz package - Built with tsup
7
+ var __defProp = Object.defineProperty;
8
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
9
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
11
+ var __decorateClass = (decorators, target, key, kind) => {
12
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
13
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
14
+ if (decorator = decorators[i])
15
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
16
+ if (kind && result) __defProp(target, key, result);
17
+ return result;
18
+ };
19
+ var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
20
+ var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
21
+
22
+ // src/utils/common/hash.ts
23
+ function hashString(str) {
24
+ const HASH_SHIFT = 5;
25
+ let hash = 0;
26
+ for (let i = 0; i < str.length; i++) {
27
+ const char = str.charCodeAt(i);
28
+ hash = (hash << HASH_SHIFT) - hash + char;
29
+ hash = hash & hash;
30
+ }
31
+ return Math.abs(hash);
32
+ }
33
+ __name(hashString, "hashString");
34
+ function isInRollout(identifier, percentage) {
35
+ if (percentage >= 100) return true;
36
+ if (percentage <= 0) return false;
37
+ const hash = hashString(identifier);
38
+ return hash % 100 < percentage;
39
+ }
40
+ __name(isInRollout, "isInRollout");
41
+ function createRolloutIdentifier(featureKey, userId) {
42
+ const trimmedUserId = userId?.trim();
43
+ const effectiveUserId = trimmedUserId && trimmedUserId.length > 0 ? trimmedUserId : "anonymous";
44
+ return `${featureKey}:${effectiveUserId}`;
45
+ }
46
+ __name(createRolloutIdentifier, "createRolloutIdentifier");
47
+ var HashUtils = {
48
+ /**
49
+ * Generates a hash-based bucket for load balancing or distribution.
50
+ *
51
+ * @param identifier - Unique identifier
52
+ * @param bucketCount - Number of buckets (default: 10)
53
+ * @returns Bucket number (0 to bucketCount-1)
54
+ */
55
+ getBucket: /* @__PURE__ */ __name((identifier, bucketCount = 10) => {
56
+ return hashString(identifier) % bucketCount;
57
+ }, "getBucket"),
58
+ /**
59
+ * Checks if an identifier falls within a specific bucket range.
60
+ *
61
+ * @param identifier - Unique identifier
62
+ * @param startBucket - Starting bucket (inclusive)
63
+ * @param endBucket - Ending bucket (inclusive)
64
+ * @param totalBuckets - Total number of buckets (default: 100)
65
+ * @returns true if identifier is in the bucket range
66
+ */
67
+ isInBucketRange: /* @__PURE__ */ __name((identifier, startBucket, endBucket, totalBuckets = 100) => {
68
+ const bucket = hashString(identifier) % totalBuckets;
69
+ return bucket >= startBucket && bucket <= endBucket;
70
+ }, "isInBucketRange"),
71
+ /**
72
+ * Creates a deterministic random seed from a string.
73
+ *
74
+ * @param str - String to convert to seed
75
+ * @returns Deterministic seed value
76
+ */
77
+ createSeed: /* @__PURE__ */ __name((str) => {
78
+ const SAFE_INT = 2147483647;
79
+ return hashString(str) % SAFE_INT;
80
+ }, "createSeed")
81
+ };
82
+
83
+ // src/utils/common/values.ts
84
+ function isStringFalsy(value) {
85
+ if (value === "") return true;
86
+ const lower = value.toLowerCase().trim();
87
+ return ["false", "no", "0", "off", "disabled"].includes(lower);
88
+ }
89
+ __name(isStringFalsy, "isStringFalsy");
90
+ function isObjectTruthy(value) {
91
+ if (Array.isArray(value)) return value.length > 0;
92
+ if (value instanceof Map || value instanceof Set) return value.size > 0;
93
+ if (value.constructor === Object) return Object.keys(value).length > 0;
94
+ return true;
95
+ }
96
+ __name(isObjectTruthy, "isObjectTruthy");
97
+ function isTruthy(value) {
98
+ if (value === null || value === void 0) return false;
99
+ switch (typeof value) {
100
+ case "boolean":
101
+ return value;
102
+ case "string":
103
+ return !isStringFalsy(value);
104
+ case "number":
105
+ return value !== 0 && !isNaN(value);
106
+ case "function":
107
+ case "symbol":
108
+ return true;
109
+ case "object":
110
+ return isObjectTruthy(value);
111
+ default:
112
+ return false;
113
+ }
114
+ }
115
+ __name(isTruthy, "isTruthy");
116
+ function parseStringToBoolean(value, defaultValue) {
117
+ const lower = value.toLowerCase();
118
+ if (["true", "yes", "1", "on", "enabled"].includes(lower)) return true;
119
+ if (["false", "no", "0", "off", "disabled"].includes(lower)) return false;
120
+ return defaultValue;
121
+ }
122
+ __name(parseStringToBoolean, "parseStringToBoolean");
123
+ function toBoolean(value, defaultValue = false) {
124
+ if (value === null || value === void 0) return false;
125
+ switch (typeof value) {
126
+ case "boolean":
127
+ return value;
128
+ case "string":
129
+ return parseStringToBoolean(value, defaultValue);
130
+ case "number":
131
+ return value !== 0 && !isNaN(value);
132
+ case "object":
133
+ case "function":
134
+ return true;
135
+ default:
136
+ return defaultValue;
137
+ }
138
+ }
139
+ __name(toBoolean, "toBoolean");
140
+ var ValueUtils = {
141
+ /**
142
+ * Checks if a value is a valid percentage (0-100).
143
+ *
144
+ * @param value - Value to check
145
+ * @returns true if valid percentage
146
+ */
147
+ isValidPercentage: /* @__PURE__ */ __name((value) => {
148
+ if (typeof value !== "number") return false;
149
+ return !isNaN(value) && isFinite(value) && value >= 0 && value <= 100;
150
+ }, "isValidPercentage"),
151
+ /**
152
+ * Clamps a number to a specific range.
153
+ *
154
+ * @param value - Value to clamp
155
+ * @param min - Minimum value
156
+ * @param max - Maximum value
157
+ * @returns Clamped value
158
+ */
159
+ clamp: /* @__PURE__ */ __name((value, min, max) => {
160
+ return Math.min(Math.max(value, min), max);
161
+ }, "clamp"),
162
+ /**
163
+ * Checks if a value is empty (null, undefined, empty string, empty array).
164
+ *
165
+ * @param value - Value to check
166
+ * @returns true if empty
167
+ */
168
+ isEmpty: /* @__PURE__ */ __name((value) => {
169
+ if (value === null || value === void 0) return true;
170
+ if (typeof value === "string") return value.trim() === "";
171
+ if (Array.isArray(value)) return value.length === 0;
172
+ if (typeof value === "object") return Object.keys(value).length === 0;
173
+ return false;
174
+ }, "isEmpty"),
175
+ /**
176
+ * Safely gets a nested property from an object.
177
+ *
178
+ * @param obj - Object to query
179
+ * @param path - Dot-separated path (e.g., 'user.profile.name')
180
+ * @param defaultValue - Default if path doesn't exist
181
+ * @returns Property value or default
182
+ */
183
+ getNestedProperty: /* @__PURE__ */ __name((obj, path, defaultValue) => {
184
+ if (!obj || typeof obj !== "object") return defaultValue;
185
+ const keys = path.split(".");
186
+ let current = obj;
187
+ for (const key of keys) {
188
+ if (current == null || typeof current !== "object") return defaultValue;
189
+ current = current[key];
190
+ if (current === void 0) return defaultValue;
191
+ }
192
+ return current;
193
+ }, "getNestedProperty")
194
+ };
195
+
196
+ // src/utils/featureFlags/context.ts
197
+ var FeatureFlagContextBuilder = class _FeatureFlagContextBuilder {
198
+ static {
199
+ __name(this, "FeatureFlagContextBuilder");
200
+ }
201
+ context = {};
202
+ /**
203
+ * Sets the user ID in the context.
204
+ *
205
+ * @param userId - User identifier
206
+ * @returns Builder instance for chaining
207
+ */
208
+ setUserId(userId) {
209
+ this.context.userId = userId;
210
+ return this;
211
+ }
212
+ /**
213
+ * Sets the user email in the context.
214
+ *
215
+ * @param userEmail - User email address
216
+ * @returns Builder instance for chaining
217
+ */
218
+ setUserEmail(userEmail) {
219
+ this.context.userEmail = userEmail;
220
+ return this;
221
+ }
222
+ /**
223
+ * Sets the user role in the context.
224
+ *
225
+ * @param userRole - User role or permission level
226
+ * @returns Builder instance for chaining
227
+ */
228
+ setUserRole(userRole) {
229
+ this.context.userRole = userRole;
230
+ return this;
231
+ }
232
+ /**
233
+ * Sets the country in the context.
234
+ *
235
+ * @param country - Country code (ISO 3166-1 alpha-2)
236
+ * @returns Builder instance for chaining
237
+ */
238
+ setCountry(country) {
239
+ this.context.country = country;
240
+ return this;
241
+ }
242
+ /**
243
+ * Sets the platform in the context.
244
+ *
245
+ * @param platform - Platform type
246
+ * @returns Builder instance for chaining
247
+ */
248
+ setPlatform(platform) {
249
+ this.context.platform = platform;
250
+ return this;
251
+ }
252
+ /**
253
+ * Sets the version in the context.
254
+ *
255
+ * @param version - Application version
256
+ * @returns Builder instance for chaining
257
+ */
258
+ setVersion(version) {
259
+ this.context.version = version;
260
+ return this;
261
+ }
262
+ /**
263
+ * Sets the environment in the context.
264
+ *
265
+ * @param environment - Current environment
266
+ * @returns Builder instance for chaining
267
+ */
268
+ setEnvironment(environment) {
269
+ this.context.environment = environment;
270
+ return this;
271
+ }
272
+ /**
273
+ * Sets custom context data.
274
+ *
275
+ * @param custom - Custom context properties
276
+ * @returns Builder instance for chaining
277
+ */
278
+ setCustom(custom) {
279
+ this.context.custom = { ...this.context.custom, ...custom };
280
+ return this;
281
+ }
282
+ /**
283
+ * Adds a single custom property to the context.
284
+ *
285
+ * @param key - Custom property key
286
+ * @param value - Custom property value
287
+ * @returns Builder instance for chaining
288
+ */
289
+ addCustomProperty(key, value) {
290
+ this.context.custom ??= {};
291
+ this.context.custom[key] = value;
292
+ return this;
293
+ }
294
+ /**
295
+ * Builds the final context object.
296
+ * Validates required fields and returns the context.
297
+ *
298
+ * @returns Complete feature flag context
299
+ * @throws Error if required environment is not set
300
+ */
301
+ build() {
302
+ return {
303
+ environment: this.context.environment ?? "development",
304
+ userId: this.context.userId,
305
+ userEmail: this.context.userEmail,
306
+ userRole: this.context.userRole,
307
+ country: this.context.country,
308
+ platform: this.context.platform,
309
+ version: this.context.version,
310
+ custom: this.context.custom
311
+ };
312
+ }
313
+ /**
314
+ * Clears all context data and resets the builder.
315
+ *
316
+ * @returns Builder instance for chaining
317
+ */
318
+ clear() {
319
+ this.context = {};
320
+ return this;
321
+ }
322
+ /**
323
+ * Creates a copy of the current builder state.
324
+ *
325
+ * @returns New builder instance with copied context
326
+ */
327
+ clone() {
328
+ const cloned = new _FeatureFlagContextBuilder();
329
+ cloned.context = { ...this.context };
330
+ if (this.context.custom) {
331
+ cloned.context.custom = { ...this.context.custom };
332
+ }
333
+ return cloned;
334
+ }
335
+ };
336
+ var ContextUtils = {
337
+ /**
338
+ * Creates a basic context for anonymous users using the builder.
339
+ *
340
+ * @param environment - Target environment
341
+ * @param platform - User platform
342
+ * @returns Basic anonymous context
343
+ */
344
+ createAnonymousContext(environment, platform = "web") {
345
+ return new FeatureFlagContextBuilder().setEnvironment(environment).setPlatform(platform).build();
346
+ },
347
+ /**
348
+ * Creates a context for authenticated users using the builder.
349
+ *
350
+ * @param params - User context parameters
351
+ * @returns User context
352
+ */
353
+ createUserContext(params) {
354
+ const builder = new FeatureFlagContextBuilder().setUserId(params.userId).setEnvironment(params.environment);
355
+ if (params.userEmail) builder.setUserEmail(params.userEmail);
356
+ if (params.userRole) builder.setUserRole(params.userRole);
357
+ if (params.platform) builder.setPlatform(params.platform);
358
+ if (params.country) builder.setCountry(params.country);
359
+ if (params.version) builder.setVersion(params.version);
360
+ if (params.custom) builder.setCustom(params.custom);
361
+ return builder.build();
362
+ },
363
+ /**
364
+ * Creates a testing context with minimal required fields.
365
+ *
366
+ * @param overrides - Optional context overrides
367
+ * @returns Testing context
368
+ */
369
+ createTestingContext(overrides = {}) {
370
+ return {
371
+ environment: "development",
372
+ platform: "web",
373
+ ...overrides
374
+ };
375
+ },
376
+ /**
377
+ * Validates if a context object is complete and valid.
378
+ *
379
+ * @param context - Context to validate
380
+ * @returns Validation result
381
+ */
382
+ validateContext(context) {
383
+ const errors = [];
384
+ if (!context.environment) {
385
+ errors.push("Environment is required");
386
+ } else if (!["development", "staging", "production"].includes(context.environment)) {
387
+ errors.push("Environment must be development, staging, or production");
388
+ }
389
+ if (context.platform && !["web", "mobile", "desktop"].includes(context.platform)) {
390
+ errors.push("Platform must be web, mobile, or desktop");
391
+ }
392
+ if (context.country && context.country.length !== 2) {
393
+ errors.push("Country must be a 2-letter ISO country code");
394
+ }
395
+ return {
396
+ isValid: errors.length === 0,
397
+ errors
398
+ };
399
+ },
400
+ /**
401
+ * Merges multiple context objects, with later contexts taking precedence.
402
+ *
403
+ * @param contexts - Array of contexts to merge
404
+ * @returns Merged context
405
+ */
406
+ mergeContexts(...contexts) {
407
+ const merged = contexts.reduce((acc, context) => {
408
+ const result = {
409
+ ...acc,
410
+ ...context
411
+ };
412
+ if (acc.custom || context.custom) {
413
+ result.custom = {
414
+ ...acc.custom,
415
+ ...context.custom
416
+ };
417
+ }
418
+ return result;
419
+ }, {});
420
+ merged.environment ??= "development";
421
+ return merged;
422
+ },
423
+ /**
424
+ * Extracts a specific field value from a context.
425
+ *
426
+ * @param field - Field name to extract
427
+ * @param context - Context object
428
+ * @returns Field value or undefined
429
+ */
430
+ getContextValue(field, context) {
431
+ const standardFields = {
432
+ userId: /* @__PURE__ */ __name((ctx) => ctx.userId, "userId"),
433
+ userEmail: /* @__PURE__ */ __name((ctx) => ctx.userEmail, "userEmail"),
434
+ userRole: /* @__PURE__ */ __name((ctx) => ctx.userRole, "userRole"),
435
+ country: /* @__PURE__ */ __name((ctx) => ctx.country, "country"),
436
+ platform: /* @__PURE__ */ __name((ctx) => ctx.platform, "platform"),
437
+ version: /* @__PURE__ */ __name((ctx) => ctx.version, "version"),
438
+ environment: /* @__PURE__ */ __name((ctx) => ctx.environment, "environment"),
439
+ custom: /* @__PURE__ */ __name((ctx) => ctx.custom, "custom")
440
+ };
441
+ if (field in standardFields) {
442
+ return standardFields[field](context);
443
+ }
444
+ return context.custom?.[field];
445
+ },
446
+ /**
447
+ * Creates a context fingerprint for caching and consistency.
448
+ *
449
+ * @param context - Context to fingerprint
450
+ * @returns String fingerprint
451
+ */
452
+ createFingerprint(context) {
453
+ const relevant = {
454
+ userId: context.userId,
455
+ userRole: context.userRole,
456
+ environment: context.environment,
457
+ platform: context.platform,
458
+ country: context.country,
459
+ version: context.version,
460
+ custom: context.custom
461
+ };
462
+ const filtered = Object.entries(relevant).filter(([, value]) => value !== void 0).sort(([a], [b]) => a.localeCompare(b)).reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
463
+ return JSON.stringify(filtered);
464
+ },
465
+ /**
466
+ * Sanitizes a context by removing sensitive information.
467
+ *
468
+ * @param context - Context to sanitize
469
+ * @param sensitiveFields - Fields to remove (default: ['userEmail'])
470
+ * @returns Sanitized context
471
+ */
472
+ sanitizeContext(context, sensitiveFields = ["userEmail"]) {
473
+ const sanitized = { ...context };
474
+ if (context.custom) {
475
+ sanitized.custom = { ...context.custom };
476
+ }
477
+ for (const field of sensitiveFields) {
478
+ if (field in sanitized) {
479
+ delete sanitized[field];
480
+ }
481
+ if (sanitized.custom && field in sanitized.custom) {
482
+ delete sanitized.custom[field];
483
+ }
484
+ }
485
+ return sanitized;
486
+ }
487
+ };
488
+ function createBackendContext(params) {
489
+ return {
490
+ environment: params.environment,
491
+ userId: params.userId,
492
+ userEmail: params.userEmail,
493
+ userRole: params.userRole,
494
+ platform: params.platform ?? "api",
495
+ country: params.country,
496
+ version: params.version,
497
+ custom: params.custom
498
+ };
499
+ }
500
+ __name(createBackendContext, "createBackendContext");
501
+ function createFrontendContext(params) {
502
+ return {
503
+ environment: params.environment,
504
+ userId: params.userId,
505
+ userEmail: params.userEmail,
506
+ userRole: params.userRole,
507
+ platform: params.platform ?? "web",
508
+ country: params.country,
509
+ version: params.version,
510
+ custom: params.custom
511
+ };
512
+ }
513
+ __name(createFrontendContext, "createFrontendContext");
514
+
515
+ // src/utils/featureFlags/conditions.ts
516
+ function evaluateConditionOperator(condition, contextValue) {
517
+ const { operator } = condition;
518
+ if (isEqualityOperator(operator)) {
519
+ return evaluateEqualityOperator(operator, contextValue, condition.value);
520
+ }
521
+ if (isStringOperator(operator)) {
522
+ return evaluateStringOperator(operator, contextValue, condition.value);
523
+ }
524
+ if (isArrayOperator(operator)) {
525
+ return evaluateArrayOperator(operator, condition.value, contextValue);
526
+ }
527
+ if (isNumericOperator(operator)) {
528
+ return evaluateNumericOperator(operator, contextValue, condition.value);
529
+ }
530
+ return false;
531
+ }
532
+ __name(evaluateConditionOperator, "evaluateConditionOperator");
533
+ function isEqualityOperator(operator) {
534
+ return operator === "equals" || operator === "not_equals";
535
+ }
536
+ __name(isEqualityOperator, "isEqualityOperator");
537
+ function isStringOperator(operator) {
538
+ return operator === "contains" || operator === "not_contains";
539
+ }
540
+ __name(isStringOperator, "isStringOperator");
541
+ function isArrayOperator(operator) {
542
+ return operator === "in" || operator === "not_in";
543
+ }
544
+ __name(isArrayOperator, "isArrayOperator");
545
+ function isNumericOperator(operator) {
546
+ return operator === "greater_than" || operator === "less_than";
547
+ }
548
+ __name(isNumericOperator, "isNumericOperator");
549
+ function evaluateEqualityOperator(operator, contextValue, conditionValue) {
550
+ const isEqual = contextValue === conditionValue;
551
+ return operator === "equals" ? isEqual : !isEqual;
552
+ }
553
+ __name(evaluateEqualityOperator, "evaluateEqualityOperator");
554
+ function evaluateStringOperator(operator, contextValue, conditionValue) {
555
+ if (typeof contextValue === "string" && typeof conditionValue === "string") {
556
+ const contains = contextValue.includes(conditionValue);
557
+ return operator === "contains" ? contains : !contains;
558
+ }
559
+ if (Array.isArray(contextValue) && typeof conditionValue === "string") {
560
+ const contains = contextValue.includes(conditionValue);
561
+ return operator === "contains" ? contains : !contains;
562
+ }
563
+ return operator === "not_contains";
564
+ }
565
+ __name(evaluateStringOperator, "evaluateStringOperator");
566
+ function evaluateArrayOperator(operator, conditionValue, contextValue) {
567
+ if (!Array.isArray(conditionValue)) {
568
+ return operator === "not_in";
569
+ }
570
+ const isIncluded = conditionValue.includes(contextValue);
571
+ return operator === "in" ? isIncluded : !isIncluded;
572
+ }
573
+ __name(evaluateArrayOperator, "evaluateArrayOperator");
574
+ function compareValues(operator, left, right) {
575
+ switch (operator) {
576
+ case "greater_than":
577
+ return left > right;
578
+ case "less_than":
579
+ return left < right;
580
+ default:
581
+ return false;
582
+ }
583
+ }
584
+ __name(compareValues, "compareValues");
585
+ function evaluateNumericOperator(operator, contextValue, conditionValue) {
586
+ const contextNum = Number(contextValue);
587
+ const conditionNum = Number(conditionValue);
588
+ if (!isNaN(contextNum) && !isNaN(conditionNum)) {
589
+ return compareValues(operator, contextNum, conditionNum);
590
+ }
591
+ if (typeof contextValue === "string" && typeof conditionValue === "string") {
592
+ return compareValues(operator, contextValue, conditionValue);
593
+ }
594
+ return false;
595
+ }
596
+ __name(evaluateNumericOperator, "evaluateNumericOperator");
597
+ var ConditionUtils = {
598
+ /**
599
+ * Evaluates multiple conditions with AND logic.
600
+ *
601
+ * @param conditions - Array of conditions
602
+ * @param contextValue - Context value getter function
603
+ * @returns true if all conditions match
604
+ */
605
+ evaluateConditionsAnd: /* @__PURE__ */ __name((conditions, contextValue) => {
606
+ if (conditions.length === 0) return true;
607
+ return conditions.every((condition) => {
608
+ const value = contextValue(condition.field);
609
+ if (value === void 0) return false;
610
+ return evaluateConditionOperator(condition, value);
611
+ });
612
+ }, "evaluateConditionsAnd"),
613
+ /**
614
+ * Evaluates multiple conditions with OR logic.
615
+ *
616
+ * @param conditions - Array of conditions
617
+ * @param contextValue - Context value getter function
618
+ * @returns true if any condition matches
619
+ */
620
+ evaluateConditionsOr: /* @__PURE__ */ __name((conditions, contextValue) => {
621
+ if (conditions.length === 0) return true;
622
+ return conditions.some((condition) => {
623
+ const value = contextValue(condition.field);
624
+ if (value === void 0) return false;
625
+ return evaluateConditionOperator(condition, value);
626
+ });
627
+ }, "evaluateConditionsOr"),
628
+ /**
629
+ * Validates a condition structure.
630
+ *
631
+ * @param condition - Condition to validate
632
+ * @returns Validation result
633
+ */
634
+ validateCondition: /* @__PURE__ */ __name((condition) => {
635
+ const errors = [];
636
+ if (!condition.field) {
637
+ errors.push("Field is required");
638
+ }
639
+ if (!condition.operator) {
640
+ errors.push("Operator is required");
641
+ } else {
642
+ const validOperators = [
643
+ "equals",
644
+ "not_equals",
645
+ "contains",
646
+ "not_contains",
647
+ "in",
648
+ "not_in",
649
+ "greater_than",
650
+ "less_than"
651
+ ];
652
+ if (!validOperators.includes(condition.operator)) {
653
+ errors.push(`Invalid operator: ${condition.operator}`);
654
+ }
655
+ }
656
+ if (condition.value === void 0) {
657
+ errors.push("Value is required");
658
+ }
659
+ return {
660
+ isValid: errors.length === 0,
661
+ errors
662
+ };
663
+ }, "validateCondition"),
664
+ /**
665
+ * Creates a condition object with validation.
666
+ *
667
+ * @param field - Context field to evaluate
668
+ * @param operator - Comparison operator
669
+ * @param value - Value to compare against
670
+ * @returns Valid condition object
671
+ */
672
+ createCondition: /* @__PURE__ */ __name((field, operator, value) => {
673
+ const condition = { field, operator, value };
674
+ const validation = ConditionUtils.validateCondition(condition);
675
+ if (!validation.isValid) {
676
+ throw new Error(`Invalid condition: ${validation.errors.join(", ")}`);
677
+ }
678
+ return condition;
679
+ }, "createCondition")
680
+ };
681
+
682
+ // src/engine/featureFlags/engine.ts
683
+ var FeatureFlagEngine = class {
684
+ /**
685
+ * Creates a new feature flag evaluation engine.
686
+ *
687
+ * @param defaults - Default flag values to fall back to
688
+ * @param isLoggingEnabled - Whether to enable debug logging
689
+ */
690
+ constructor(defaults, isLoggingEnabled = false) {
691
+ this.defaults = defaults;
692
+ this.isLoggingEnabled = isLoggingEnabled;
693
+ }
694
+ static {
695
+ __name(this, "FeatureFlagEngine");
696
+ }
697
+ /** Storage for active feature flags */
698
+ flags = /* @__PURE__ */ new Map();
699
+ /** Storage for targeting rules, organized by flag key */
700
+ rules = /* @__PURE__ */ new Map();
701
+ /** Storage for manual overrides (useful for testing) */
702
+ overrides = /* @__PURE__ */ new Map();
703
+ /**
704
+ * Sets the active feature flags for evaluation.
705
+ * Clears existing flags and rules before setting new ones.
706
+ *
707
+ * @param flags - Array of feature flags to activate
708
+ */
709
+ setFlags(flags) {
710
+ this.flags.clear();
711
+ this.rules.clear();
712
+ for (const flag of flags) {
713
+ this.flags.set(flag.key, flag);
714
+ }
715
+ this.log("Loaded flags:", this.flags.size);
716
+ }
717
+ /**
718
+ * Sets the targeting rules for feature flags.
719
+ * Rules are automatically sorted by priority (higher numbers first).
720
+ *
721
+ * @param rules - Array of feature flag rules
722
+ */
723
+ setRules(rules) {
724
+ this.rules.clear();
725
+ for (const rule of rules) {
726
+ const existing = this.rules.get(rule.flagKey) ?? [];
727
+ existing.push(rule);
728
+ existing.sort((a, b) => b.priority - a.priority);
729
+ this.rules.set(rule.flagKey, existing);
730
+ }
731
+ this.log("Loaded rules:", rules.length);
732
+ }
733
+ /**
734
+ * Sets a manual override for a specific flag.
735
+ * Overrides take precedence over all other evaluation logic.
736
+ *
737
+ * @param key - The flag key to override
738
+ * @param value - The value to force for this flag
739
+ */
740
+ setOverride(key, value) {
741
+ this.overrides.set(key, value);
742
+ this.log("Override set:", key, value);
743
+ }
744
+ /**
745
+ * Removes a manual override for a specific flag.
746
+ *
747
+ * @param key - The flag key to remove override for
748
+ */
749
+ removeOverride(key) {
750
+ this.overrides.delete(key);
751
+ this.log("Override removed:", key);
752
+ }
753
+ /**
754
+ * Clears all manual overrides.
755
+ */
756
+ clearOverrides() {
757
+ this.overrides.clear();
758
+ this.log("All overrides cleared");
759
+ }
760
+ /**
761
+ * Gets all current flags.
762
+ *
763
+ * @returns Array of all feature flags
764
+ */
765
+ getFlags() {
766
+ return Array.from(this.flags.values());
767
+ }
768
+ /**
769
+ * Evaluates a feature flag and returns the complete evaluation result.
770
+ *
771
+ * @param key - The feature flag key to evaluate
772
+ * @param context - Optional context for targeting
773
+ * @returns Complete evaluation result with value and metadata
774
+ */
775
+ evaluate(key, context) {
776
+ const evaluatedAt = /* @__PURE__ */ new Date();
777
+ const overrideResult = this.checkOverride(key, evaluatedAt);
778
+ if (overrideResult) return overrideResult;
779
+ const flag = this.flags.get(key);
780
+ if (!flag) {
781
+ return this.createDefaultEvaluation(key, evaluatedAt);
782
+ }
783
+ if (!flag.isEnabled) {
784
+ return this.createDisabledEvaluation(key, evaluatedAt);
785
+ }
786
+ if (!this.isEnvironmentMatch(flag, context)) {
787
+ return this.createDefaultEvaluation(key, evaluatedAt);
788
+ }
789
+ const ruleResult = this.evaluateRules(key, context, evaluatedAt);
790
+ if (ruleResult) return ruleResult;
791
+ if (!this.isInFlagRollout(key, flag, context)) {
792
+ return this.createDefaultEvaluation(key, evaluatedAt);
793
+ }
794
+ return this.createFlagEvaluation(key, flag, evaluatedAt);
795
+ }
796
+ /**
797
+ * Checks for manual override and returns evaluation if found.
798
+ *
799
+ * @private
800
+ * @param key - The feature flag key
801
+ * @param evaluatedAt - Evaluation timestamp
802
+ * @returns Evaluation result or null if no override
803
+ */
804
+ checkOverride(key, evaluatedAt) {
805
+ if (!this.overrides.has(key)) return null;
806
+ const value = this.overrides.get(key);
807
+ return {
808
+ flagKey: key,
809
+ value,
810
+ isEnabled: isTruthy(value),
811
+ reason: "override",
812
+ evaluatedAt
813
+ };
814
+ }
815
+ /**
816
+ * Creates default evaluation result.
817
+ *
818
+ * @private
819
+ * @param key - The feature flag key
820
+ * @param evaluatedAt - Evaluation timestamp
821
+ * @returns Default evaluation result
822
+ */
823
+ createDefaultEvaluation(key, evaluatedAt) {
824
+ const defaultValue = this.defaults[key] ?? false;
825
+ return {
826
+ flagKey: key,
827
+ value: defaultValue,
828
+ isEnabled: isTruthy(defaultValue),
829
+ reason: "default",
830
+ evaluatedAt
831
+ };
832
+ }
833
+ /**
834
+ * Creates disabled evaluation result.
835
+ *
836
+ * @private
837
+ * @param key - The feature flag key
838
+ * @param evaluatedAt - Evaluation timestamp
839
+ * @returns Disabled evaluation result
840
+ */
841
+ createDisabledEvaluation(key, evaluatedAt) {
842
+ return {
843
+ flagKey: key,
844
+ value: false,
845
+ isEnabled: false,
846
+ reason: "disabled",
847
+ evaluatedAt
848
+ };
849
+ }
850
+ /**
851
+ * Creates flag evaluation result.
852
+ *
853
+ * @private
854
+ * @param key - The feature flag key
855
+ * @param flag - The feature flag
856
+ * @param evaluatedAt - Evaluation timestamp
857
+ * @returns Flag evaluation result
858
+ */
859
+ createFlagEvaluation(key, flag, evaluatedAt) {
860
+ return {
861
+ flagKey: key,
862
+ value: flag.value,
863
+ isEnabled: isTruthy(flag.value),
864
+ reason: "default",
865
+ evaluatedAt
866
+ };
867
+ }
868
+ /**
869
+ * Checks if environment matches for flag evaluation.
870
+ *
871
+ * @private
872
+ * @param flag - The feature flag
873
+ * @param context - Evaluation context
874
+ * @returns true if environment matches
875
+ */
876
+ isEnvironmentMatch(flag, context) {
877
+ return flag.environment === "all" || context?.environment === flag.environment;
878
+ }
879
+ /**
880
+ * Checks if user is in flag-level rollout.
881
+ *
882
+ * @private
883
+ * @param key - The feature flag key
884
+ * @param flag - The feature flag
885
+ * @param context - Evaluation context
886
+ * @returns true if user is in rollout
887
+ */
888
+ isInFlagRollout(key, flag, context) {
889
+ if (flag.rolloutPercentage === void 0) return true;
890
+ const identifier = createRolloutIdentifier(key, context?.userId);
891
+ return isInRollout(identifier, flag.rolloutPercentage);
892
+ }
893
+ /**
894
+ * Evaluates all rules for a flag.
895
+ *
896
+ * @private
897
+ * @param key - The feature flag key
898
+ * @param context - Evaluation context
899
+ * @param evaluatedAt - Evaluation timestamp
900
+ * @returns Rule evaluation result or null if no match
901
+ */
902
+ evaluateRules(key, context, evaluatedAt) {
903
+ const rules = this.rules.get(key) ?? [];
904
+ for (const rule of rules) {
905
+ if (!rule.isEnabled) continue;
906
+ const ruleResult = this.evaluateMatchingRule(key, rule, context, evaluatedAt);
907
+ if (ruleResult) return ruleResult;
908
+ }
909
+ return null;
910
+ }
911
+ /**
912
+ * Evaluates a single matching rule and returns result if it passes.
913
+ *
914
+ * @private
915
+ * @param key - The feature flag key
916
+ * @param rule - The rule to evaluate
917
+ * @param context - Evaluation context
918
+ * @param evaluatedAt - Evaluation timestamp
919
+ * @returns Rule evaluation result or null if no match
920
+ */
921
+ evaluateMatchingRule(key, rule, context, evaluatedAt) {
922
+ if (!this.evaluateRule(rule, context)) return null;
923
+ const isInRuleRollout = rule.rolloutPercentage === void 0 || isInRollout(createRolloutIdentifier(key, context?.userId), rule.rolloutPercentage);
924
+ if (!isInRuleRollout) return null;
925
+ return {
926
+ flagKey: key,
927
+ value: rule.value,
928
+ isEnabled: isTruthy(rule.value),
929
+ reason: "rule_match",
930
+ ruleId: rule.id,
931
+ evaluatedAt: evaluatedAt ?? /* @__PURE__ */ new Date()
932
+ };
933
+ }
934
+ /**
935
+ * Evaluates a single rule against the provided context.
936
+ * All conditions in the rule must match (AND logic).
937
+ *
938
+ * @private
939
+ * @param rule - The rule to evaluate
940
+ * @param context - Context to evaluate against
941
+ * @returns true if the rule matches the context
942
+ */
943
+ evaluateRule(rule, context) {
944
+ if (rule.conditions.length === 0) return true;
945
+ return rule.conditions.every((condition) => this.evaluateCondition(condition, context));
946
+ }
947
+ /**
948
+ * Evaluates a single condition against the provided context.
949
+ *
950
+ * @private
951
+ * @param condition - The condition to evaluate
952
+ * @param context - Context to evaluate against
953
+ * @returns true if the condition matches
954
+ */
955
+ evaluateCondition(condition, context) {
956
+ if (!context) return false;
957
+ const contextValue = ContextUtils.getContextValue(condition.field, context);
958
+ if (contextValue === void 0) return false;
959
+ return evaluateConditionOperator(condition, contextValue);
960
+ }
961
+ /**
962
+ * Logs debug information if logging is enabled.
963
+ *
964
+ * @private
965
+ * @param args - Arguments to log
966
+ */
967
+ log(...args) {
968
+ if (this.isLoggingEnabled) {
969
+ console.log("[FeatureFlagEngine]", ...args);
970
+ }
971
+ }
972
+ };
973
+
974
+ // src/cache/strategies/memory.ts
975
+ var MemoryCacheStrategy = class {
976
+ static {
977
+ __name(this, "MemoryCacheStrategy");
978
+ }
979
+ cache = /* @__PURE__ */ new Map();
980
+ accessOrder = /* @__PURE__ */ new Map();
981
+ stats = {
982
+ hits: 0,
983
+ misses: 0,
984
+ sets: 0,
985
+ deletes: 0
986
+ };
987
+ cleanupTimer;
988
+ maxSize;
989
+ cleanupInterval;
990
+ onEvict;
991
+ /**
992
+ * Creates a new memory cache strategy.
993
+ *
994
+ * @param config - Memory cache configuration
995
+ */
996
+ constructor(config = {}) {
997
+ const DEFAULT_MAX_ENTRIES = 1e3;
998
+ const DEFAULT_CLEANUP_INTERVAL = 6e4;
999
+ const defaultConfig = {
1000
+ maxEntries: DEFAULT_MAX_ENTRIES,
1001
+ cleanupInterval: DEFAULT_CLEANUP_INTERVAL
1002
+ };
1003
+ this.maxSize = config.maxSize ?? config.maxEntries ?? defaultConfig.maxEntries;
1004
+ this.cleanupInterval = config.cleanupInterval ?? defaultConfig.cleanupInterval;
1005
+ this.onEvict = config.onEvict;
1006
+ this.startCleanup();
1007
+ }
1008
+ /**
1009
+ * Stores a cache entry in memory.
1010
+ *
1011
+ * @param key - Cache key
1012
+ * @param entry - Cache entry to store
1013
+ * @returns Promise that resolves when entry is stored
1014
+ */
1015
+ async set(key, entry) {
1016
+ if (this.maxSize === 0) {
1017
+ this.stats.sets++;
1018
+ return;
1019
+ }
1020
+ if (!this.cache.has(key) && this.cache.size >= this.maxSize) {
1021
+ this.evictOldestEntries();
1022
+ }
1023
+ this.cache.set(key, entry);
1024
+ this.accessOrder.set(key, Date.now());
1025
+ this.stats.sets++;
1026
+ }
1027
+ /**
1028
+ * Retrieves a cache entry from memory.
1029
+ *
1030
+ * @param key - Cache key
1031
+ * @returns Promise that resolves to cache entry or null if not found
1032
+ */
1033
+ async get(key) {
1034
+ const entry = this.cache.get(key);
1035
+ if (!entry) {
1036
+ this.stats.misses++;
1037
+ return null;
1038
+ }
1039
+ this.accessOrder.set(key, Date.now());
1040
+ this.stats.hits++;
1041
+ return entry;
1042
+ }
1043
+ /**
1044
+ * Removes a cache entry from memory.
1045
+ *
1046
+ * @param key - Cache key to remove
1047
+ * @returns Promise that resolves when entry is removed
1048
+ */
1049
+ async delete(key) {
1050
+ this.cache.delete(key);
1051
+ this.accessOrder.delete(key);
1052
+ this.stats.deletes++;
1053
+ }
1054
+ /**
1055
+ * Clears all cache entries from memory.
1056
+ *
1057
+ * @returns Promise that resolves when cache is cleared
1058
+ */
1059
+ async clear() {
1060
+ this.cache.clear();
1061
+ this.accessOrder.clear();
1062
+ this.stats.hits = 0;
1063
+ this.stats.misses = 0;
1064
+ this.stats.sets = 0;
1065
+ this.stats.deletes = 0;
1066
+ }
1067
+ /**
1068
+ * Gets cache statistics.
1069
+ *
1070
+ * @returns Promise that resolves to cache statistics
1071
+ */
1072
+ async getStats() {
1073
+ const totalRequests = this.stats.hits + this.stats.misses;
1074
+ const hitRatio = totalRequests > 0 ? this.stats.hits / totalRequests : 0;
1075
+ return {
1076
+ hits: this.stats.hits,
1077
+ misses: this.stats.misses,
1078
+ sets: this.stats.sets,
1079
+ deletes: this.stats.deletes,
1080
+ size: this.cache.size,
1081
+ hitRatio
1082
+ };
1083
+ }
1084
+ /**
1085
+ * Disposes of the memory cache and cleans up resources.
1086
+ *
1087
+ * @returns Promise that resolves when cleanup is complete
1088
+ */
1089
+ async dispose() {
1090
+ if (this.cleanupTimer) {
1091
+ clearInterval(this.cleanupTimer);
1092
+ this.cleanupTimer = void 0;
1093
+ }
1094
+ await this.clear();
1095
+ }
1096
+ /**
1097
+ * Starts the periodic cleanup of expired entries.
1098
+ *
1099
+ * @private
1100
+ */
1101
+ startCleanup() {
1102
+ this.cleanupTimer = setInterval(() => {
1103
+ this.cleanupExpiredEntries();
1104
+ }, this.cleanupInterval);
1105
+ }
1106
+ /**
1107
+ * Removes expired entries from the cache.
1108
+ *
1109
+ * @private
1110
+ */
1111
+ cleanupExpiredEntries() {
1112
+ const now = Date.now();
1113
+ const expiredKeys = [];
1114
+ for (const [key, entry] of Array.from(this.cache.entries())) {
1115
+ if (now > entry.expiresAt) {
1116
+ expiredKeys.push(key);
1117
+ }
1118
+ }
1119
+ for (const key of expiredKeys) {
1120
+ const entry = this.cache.get(key);
1121
+ this.cache.delete(key);
1122
+ this.accessOrder.delete(key);
1123
+ if (entry && this.onEvict) {
1124
+ this.onEvict(key, entry);
1125
+ }
1126
+ }
1127
+ }
1128
+ /**
1129
+ * Evicts the oldest entries when cache is full.
1130
+ * Uses LRU-like eviction by removing the oldest entries by creation time.
1131
+ *
1132
+ * @private
1133
+ */
1134
+ evictOldestEntries() {
1135
+ if (this.maxSize === 0) {
1136
+ this.cache.clear();
1137
+ this.accessOrder.clear();
1138
+ return;
1139
+ }
1140
+ const EVICTION_PERCENTAGE = 0.1;
1141
+ const entriesToEvict = Math.max(1, Math.ceil(this.maxSize * EVICTION_PERCENTAGE));
1142
+ const sortedEntries = Array.from(this.accessOrder.entries()).sort(([, timeA], [, timeB]) => timeA - timeB).slice(0, entriesToEvict);
1143
+ for (const [key] of sortedEntries) {
1144
+ const entry = this.cache.get(key);
1145
+ this.cache.delete(key);
1146
+ this.accessOrder.delete(key);
1147
+ if (entry && this.onEvict) {
1148
+ this.onEvict(key, entry);
1149
+ }
1150
+ }
1151
+ }
1152
+ };
1153
+
1154
+ // src/cache/strategies/redis.ts
1155
+ var RedisCacheStrategy = class {
1156
+ /**
1157
+ * Creates a new Redis cache strategy.
1158
+ *
1159
+ * @param config - Redis cache configuration
1160
+ */
1161
+ constructor(config) {
1162
+ this.config = config;
1163
+ if (!config.url) {
1164
+ throw new Error("Redis URL is required");
1165
+ }
1166
+ this.keyPrefix = config.keyPrefix ?? "cache:";
1167
+ }
1168
+ static {
1169
+ __name(this, "RedisCacheStrategy");
1170
+ }
1171
+ client;
1172
+ stats = {
1173
+ hitCount: 0,
1174
+ missCount: 0,
1175
+ setCount: 0,
1176
+ deleteCount: 0
1177
+ };
1178
+ isConnected = false;
1179
+ keyPrefix;
1180
+ /**
1181
+ * Stores a cache entry in Redis.
1182
+ *
1183
+ * @param key - Cache key
1184
+ * @param entry - Cache entry to store
1185
+ * @returns Promise that resolves when entry is stored
1186
+ */
1187
+ async set(key, entry) {
1188
+ await this.ensureConnected();
1189
+ const redisKey = this.buildRedisKey(key);
1190
+ const serializedEntry = JSON.stringify(entry);
1191
+ const ttlMs = entry.expiresAt - Date.now();
1192
+ const ttlSeconds = Math.max(1, Math.ceil(ttlMs / 1e3));
1193
+ await this.client.set(redisKey, serializedEntry, "EX", ttlSeconds);
1194
+ this.stats.setCount++;
1195
+ }
1196
+ /**
1197
+ * Retrieves a cache entry from Redis.
1198
+ *
1199
+ * @param key - Cache key
1200
+ * @returns Promise that resolves to cache entry or null if not found
1201
+ */
1202
+ async get(key) {
1203
+ await this.ensureConnected();
1204
+ const redisKey = this.buildRedisKey(key);
1205
+ const serializedEntry = await this.client.get(redisKey);
1206
+ if (!serializedEntry || typeof serializedEntry !== "string") {
1207
+ this.stats.missCount++;
1208
+ return null;
1209
+ }
1210
+ try {
1211
+ const entry = JSON.parse(serializedEntry);
1212
+ this.stats.hitCount++;
1213
+ return entry;
1214
+ } catch {
1215
+ await this.client.del(redisKey);
1216
+ this.stats.missCount++;
1217
+ return null;
1218
+ }
1219
+ }
1220
+ /**
1221
+ * Removes a cache entry from Redis.
1222
+ *
1223
+ * @param key - Cache key to remove
1224
+ * @returns Promise that resolves when entry is removed
1225
+ */
1226
+ async delete(key) {
1227
+ await this.ensureConnected();
1228
+ const redisKey = this.buildRedisKey(key);
1229
+ await this.client.del(redisKey);
1230
+ this.stats.deleteCount++;
1231
+ }
1232
+ /**
1233
+ * Clears all cache entries from Redis.
1234
+ * This removes all keys with the configured prefix.
1235
+ *
1236
+ * @returns Promise that resolves when cache is cleared
1237
+ */
1238
+ async clear() {
1239
+ await this.ensureConnected();
1240
+ const pattern = `${this.keyPrefix}*`;
1241
+ const keys = await this.client.keys(pattern);
1242
+ if (keys.length > 0) {
1243
+ await this.client.del(...keys);
1244
+ }
1245
+ this.stats.hitCount = 0;
1246
+ this.stats.missCount = 0;
1247
+ }
1248
+ /**
1249
+ * Gets cache statistics.
1250
+ *
1251
+ * @returns Promise that resolves to cache statistics
1252
+ */
1253
+ async getStats() {
1254
+ await this.ensureConnected();
1255
+ const pattern = `${this.keyPrefix}*`;
1256
+ const keys = await this.client.keys(pattern);
1257
+ const entryCount = Array.isArray(keys) ? keys.length : 0;
1258
+ const totalRequests = this.stats.hitCount + this.stats.missCount;
1259
+ const hitRatio = totalRequests > 0 ? this.stats.hitCount / totalRequests : 0;
1260
+ return {
1261
+ hits: this.stats.hitCount,
1262
+ misses: this.stats.missCount,
1263
+ sets: this.stats.setCount,
1264
+ deletes: this.stats.deleteCount,
1265
+ size: entryCount,
1266
+ hitRatio
1267
+ };
1268
+ }
1269
+ /**
1270
+ * Disposes of the Redis cache and cleans up resources.
1271
+ *
1272
+ * @returns Promise that resolves when cleanup is complete
1273
+ */
1274
+ async dispose() {
1275
+ if (this.client && this.isConnected) {
1276
+ await this.client.quit();
1277
+ this.isConnected = false;
1278
+ }
1279
+ }
1280
+ /**
1281
+ * Ensures Redis connection is established.
1282
+ *
1283
+ * @private
1284
+ * @returns Promise that resolves when connected
1285
+ */
1286
+ async ensureConnected() {
1287
+ if (this.isConnected) return;
1288
+ try {
1289
+ this.client = await this.createIoRedisClient();
1290
+ this.isConnected = true;
1291
+ } catch (error) {
1292
+ throw new Error(
1293
+ `Failed to connect to Redis: ${error instanceof Error ? error.message : "Unknown error"}. Ensure Redis is running and accessible, and install ioredis package.`
1294
+ );
1295
+ }
1296
+ }
1297
+ /**
1298
+ * Creates an ioredis client.
1299
+ *
1300
+ * @private
1301
+ * @returns Promise that resolves to ioredis client
1302
+ */
1303
+ async createIoRedisClient() {
1304
+ const DEFAULT_TIMEOUT = 5e3;
1305
+ const defaultOptions = {
1306
+ connectTimeout: DEFAULT_TIMEOUT,
1307
+ commandTimeout: DEFAULT_TIMEOUT,
1308
+ enableOfflineQueue: false
1309
+ };
1310
+ const Redis = await import('ioredis');
1311
+ const client = new Redis.default(this.config.url, {
1312
+ connectTimeout: this.config.connectTimeout ?? defaultOptions.connectTimeout,
1313
+ commandTimeout: this.config.commandTimeout ?? defaultOptions.commandTimeout,
1314
+ enableOfflineQueue: defaultOptions.enableOfflineQueue
1315
+ });
1316
+ await new Promise((resolve, reject) => {
1317
+ client.on("ready", resolve);
1318
+ client.on("error", reject);
1319
+ });
1320
+ return client;
1321
+ }
1322
+ /**
1323
+ * Builds a Redis key with the configured prefix.
1324
+ *
1325
+ * @private
1326
+ * @param key - Base cache key
1327
+ * @returns Redis key with prefix
1328
+ */
1329
+ buildRedisKey(key) {
1330
+ return `${this.keyPrefix}${key}`;
1331
+ }
1332
+ };
1333
+
1334
+ // src/cache/index.ts
1335
+ var CacheManager = class {
1336
+ /**
1337
+ * Creates a new cache manager with the specified configuration.
1338
+ *
1339
+ * @param config - Cache configuration
1340
+ */
1341
+ constructor(config) {
1342
+ this.config = config;
1343
+ this.strategy = this.createStrategy(config);
1344
+ }
1345
+ static {
1346
+ __name(this, "CacheManager");
1347
+ }
1348
+ strategy;
1349
+ /**
1350
+ * Stores a value in the cache.
1351
+ *
1352
+ * @template T - Type of the value to cache
1353
+ * @param key - Cache key
1354
+ * @param value - Value to cache
1355
+ * @param ttl - Optional TTL override in seconds
1356
+ * @returns Promise that resolves when value is cached
1357
+ */
1358
+ async set(key, value, ttl) {
1359
+ if (!this.config.isEnabled) return;
1360
+ const finalTtl = ttl ?? this.config.ttl;
1361
+ const entry = {
1362
+ data: value,
1363
+ expiresAt: Date.now() + finalTtl * 1e3,
1364
+ createdAt: Date.now()
1365
+ };
1366
+ await this.strategy.set(key, entry);
1367
+ }
1368
+ /**
1369
+ * Retrieves a value from the cache.
1370
+ *
1371
+ * @template T - Expected type of the cached value
1372
+ * @param key - Cache key
1373
+ * @returns Promise that resolves to cached value or null if not found/expired
1374
+ */
1375
+ async get(key) {
1376
+ if (!this.config.isEnabled) return null;
1377
+ const entry = await this.strategy.get(key);
1378
+ if (!entry) return null;
1379
+ if (Date.now() > entry.expiresAt) {
1380
+ await this.strategy.delete(key);
1381
+ return null;
1382
+ }
1383
+ return entry.data;
1384
+ }
1385
+ /**
1386
+ * Removes a value from the cache.
1387
+ *
1388
+ * @param key - Cache key to remove
1389
+ * @returns Promise that resolves when value is removed
1390
+ */
1391
+ async delete(key) {
1392
+ if (!this.config.isEnabled) return;
1393
+ await this.strategy.delete(key);
1394
+ }
1395
+ /**
1396
+ * Clears all cached values.
1397
+ *
1398
+ * @returns Promise that resolves when cache is cleared
1399
+ */
1400
+ async clear() {
1401
+ if (!this.config.isEnabled) return;
1402
+ await this.strategy.clear();
1403
+ }
1404
+ /**
1405
+ * Checks if a key exists in the cache.
1406
+ *
1407
+ * @param key - Cache key to check
1408
+ * @returns Promise that resolves to true if key exists and is not expired
1409
+ */
1410
+ async has(key) {
1411
+ if (!this.config.isEnabled) return false;
1412
+ const entry = await this.strategy.get(key);
1413
+ if (!entry) return false;
1414
+ if (Date.now() > entry.expiresAt) {
1415
+ await this.strategy.delete(key);
1416
+ return false;
1417
+ }
1418
+ return true;
1419
+ }
1420
+ /**
1421
+ * Gets cache statistics.
1422
+ *
1423
+ * @returns Promise that resolves to cache statistics
1424
+ */
1425
+ async getStats() {
1426
+ return this.strategy.getStats();
1427
+ }
1428
+ /**
1429
+ * Creates the appropriate cache strategy based on configuration.
1430
+ *
1431
+ * @private
1432
+ * @param config - Cache configuration
1433
+ * @returns Cache strategy instance
1434
+ */
1435
+ createStrategy(config) {
1436
+ switch (config.strategy) {
1437
+ case "redis":
1438
+ if (!config.redisConfig) {
1439
+ throw new Error("Redis configuration is required for Redis cache strategy");
1440
+ }
1441
+ return new RedisCacheStrategy(config.redisConfig);
1442
+ case "memory":
1443
+ default:
1444
+ return new MemoryCacheStrategy(config.memoryConfig);
1445
+ }
1446
+ }
1447
+ /**
1448
+ * Disposes of the cache manager and cleans up resources.
1449
+ *
1450
+ * @returns Promise that resolves when cleanup is complete
1451
+ */
1452
+ async dispose() {
1453
+ await this.strategy.dispose?.();
1454
+ }
1455
+ };
1456
+
1457
+ // src/domain/featureFlags/provider.ts
1458
+ var FeatureFlagProvider = class {
1459
+ /**
1460
+ * Creates a new feature flag provider.
1461
+ *
1462
+ * @param config - Provider configuration
1463
+ * @param features - Record of feature flag keys to their default values
1464
+ */
1465
+ constructor(config, features) {
1466
+ this.config = config;
1467
+ this.features = features;
1468
+ this.engine = new FeatureFlagEngine(features, config.isLoggingEnabled ?? false);
1469
+ this.cacheManager = new CacheManager({
1470
+ isEnabled: config.isCacheEnabled,
1471
+ ttl: config.cacheTtl,
1472
+ strategy: "memory"
1473
+ // Default to memory, can be overridden
1474
+ });
1475
+ this.setupRefreshTimer();
1476
+ }
1477
+ static {
1478
+ __name(this, "FeatureFlagProvider");
1479
+ }
1480
+ engine;
1481
+ cacheManager;
1482
+ subscribers = /* @__PURE__ */ new Set();
1483
+ refreshTimer;
1484
+ isInitialized = false;
1485
+ features;
1486
+ initializePromise;
1487
+ /**
1488
+ * Initializes the provider by loading initial data.
1489
+ *
1490
+ * @returns Promise that resolves when initialization is complete
1491
+ */
1492
+ async initialize() {
1493
+ if (this.isInitialized) {
1494
+ return;
1495
+ }
1496
+ if (this.initializePromise) {
1497
+ return this.initializePromise;
1498
+ }
1499
+ this.initializePromise = this.doInitialize();
1500
+ return this.initializePromise;
1501
+ }
1502
+ /**
1503
+ * Performs the actual initialization work.
1504
+ *
1505
+ * @private
1506
+ * @returns Promise that resolves when initialization is complete
1507
+ */
1508
+ async doInitialize() {
1509
+ try {
1510
+ await this.refresh();
1511
+ this.isInitialized = true;
1512
+ this.log("Provider initialized successfully");
1513
+ } catch (error) {
1514
+ this.log("Failed to initialize provider:", error);
1515
+ this.initializePromise = void 0;
1516
+ throw error;
1517
+ }
1518
+ }
1519
+ /**
1520
+ * Gets a feature flag evaluation for the specified key and context.
1521
+ *
1522
+ * @param key - The feature flag key
1523
+ * @param context - Optional context for evaluation
1524
+ * @returns Promise resolving to the flag evaluation
1525
+ */
1526
+ async getFlag(key, context) {
1527
+ if (!this.isInitialized) {
1528
+ await this.initialize();
1529
+ }
1530
+ if (this.config.isCacheEnabled) {
1531
+ const cacheKey = this.generateCacheKey(key, context);
1532
+ const cached = await this.cacheManager.get(cacheKey);
1533
+ if (cached) {
1534
+ return cached;
1535
+ }
1536
+ const evaluation = this.engine.evaluate(key, context);
1537
+ await this.cacheManager.set(cacheKey, evaluation);
1538
+ return evaluation;
1539
+ }
1540
+ return this.engine.evaluate(key, context);
1541
+ }
1542
+ /**
1543
+ * Checks if a feature flag is enabled.
1544
+ *
1545
+ * @param key - The feature flag key
1546
+ * @param context - Optional context for evaluation
1547
+ * @returns Promise resolving to boolean indicating if flag is enabled
1548
+ */
1549
+ async isEnabled(key, context) {
1550
+ const evaluation = await this.getFlag(key, context);
1551
+ return evaluation.isEnabled;
1552
+ }
1553
+ /**
1554
+ * Gets the value of a feature flag.
1555
+ *
1556
+ * @template T - The expected type of the flag value
1557
+ * @param key - The feature flag key
1558
+ * @param context - Optional context for evaluation
1559
+ * @returns Promise resolving to the flag value
1560
+ */
1561
+ async getValue(key, context) {
1562
+ const evaluation = await this.getFlag(key, context);
1563
+ return evaluation.value;
1564
+ }
1565
+ /**
1566
+ * Gets all feature flag evaluations for the given context.
1567
+ *
1568
+ * @param context - Optional context for evaluation
1569
+ * @returns Promise resolving to record of flag evaluations
1570
+ */
1571
+ async getAllFlags(context) {
1572
+ if (!this.isInitialized) {
1573
+ await this.initialize();
1574
+ }
1575
+ const results = {};
1576
+ for (const key of Object.keys(this.features)) {
1577
+ results[key] = await this.getFlag(key, context);
1578
+ }
1579
+ const engineFlags = this.engine.getFlags();
1580
+ for (const flag of engineFlags) {
1581
+ if (!(flag.key in results)) {
1582
+ results[flag.key] = await this.getFlag(flag.key, context);
1583
+ }
1584
+ }
1585
+ return results;
1586
+ }
1587
+ /**
1588
+ * Refreshes the provider by fetching latest data from the source.
1589
+ *
1590
+ * @returns Promise that resolves when refresh is complete
1591
+ */
1592
+ async refresh() {
1593
+ try {
1594
+ const { flags, rules } = await this.fetchData();
1595
+ this.engine.setFlags(flags);
1596
+ this.engine.setRules(rules);
1597
+ await this.cacheManager.clear();
1598
+ this.notifySubscribers();
1599
+ this.log(`Refreshed with ${flags.length} flags and ${rules.length} rules`);
1600
+ } catch (error) {
1601
+ this.log("Failed to refresh provider:", error);
1602
+ throw error;
1603
+ }
1604
+ }
1605
+ /**
1606
+ * Subscribes to provider updates.
1607
+ *
1608
+ * @param callback - Function to call when provider updates
1609
+ * @returns Unsubscribe function
1610
+ */
1611
+ subscribe(callback) {
1612
+ this.subscribers.add(callback);
1613
+ return () => {
1614
+ this.subscribers.delete(callback);
1615
+ };
1616
+ }
1617
+ /**
1618
+ * Sets an override for a specific flag key.
1619
+ *
1620
+ * @param key - The flag key to override
1621
+ * @param value - The value to force for this flag
1622
+ */
1623
+ setOverride(key, value) {
1624
+ this.engine.setOverride(key, value);
1625
+ void this.cacheManager.clear();
1626
+ this.notifySubscribers();
1627
+ }
1628
+ /**
1629
+ * Removes an override for a specific flag key.
1630
+ *
1631
+ * @param key - The flag key to remove override for
1632
+ */
1633
+ removeOverride(key) {
1634
+ this.engine.removeOverride(key);
1635
+ void this.cacheManager.clear();
1636
+ this.notifySubscribers();
1637
+ }
1638
+ /**
1639
+ * Clears all overrides.
1640
+ */
1641
+ clearOverrides() {
1642
+ this.engine.clearOverrides();
1643
+ void this.cacheManager.clear();
1644
+ this.notifySubscribers();
1645
+ }
1646
+ /**
1647
+ * Disposes of the provider, cleaning up resources.
1648
+ */
1649
+ dispose() {
1650
+ if (this.refreshTimer) {
1651
+ clearInterval(this.refreshTimer);
1652
+ this.refreshTimer = void 0;
1653
+ }
1654
+ this.subscribers.clear();
1655
+ void this.cacheManager.clear();
1656
+ this.isInitialized = false;
1657
+ this.log("Provider disposed");
1658
+ }
1659
+ /**
1660
+ * Generates a cache key for flag evaluation.
1661
+ *
1662
+ * @protected
1663
+ * @param key - Feature flag key
1664
+ * @param context - Evaluation context
1665
+ * @returns Cache key string
1666
+ */
1667
+ generateCacheKey(key, context) {
1668
+ if (!context) {
1669
+ return key;
1670
+ }
1671
+ const contextKey = JSON.stringify({
1672
+ userId: context.userId,
1673
+ userRole: context.userRole,
1674
+ environment: context.environment,
1675
+ platform: context.platform,
1676
+ country: context.country,
1677
+ version: context.version
1678
+ });
1679
+ return `${key}:${contextKey}`;
1680
+ }
1681
+ /**
1682
+ * Sets up the automatic refresh timer if configured.
1683
+ *
1684
+ * @protected
1685
+ */
1686
+ setupRefreshTimer() {
1687
+ if (this.config.refreshInterval > 0) {
1688
+ this.refreshTimer = setInterval(() => {
1689
+ void this.refresh().catch((error) => {
1690
+ this.log("Auto-refresh failed:", error);
1691
+ });
1692
+ }, this.config.refreshInterval * 1e3);
1693
+ }
1694
+ }
1695
+ /**
1696
+ * Notifies all subscribers of provider updates.
1697
+ *
1698
+ * @protected
1699
+ */
1700
+ notifySubscribers() {
1701
+ for (const callback of Array.from(this.subscribers)) {
1702
+ try {
1703
+ callback();
1704
+ } catch (error) {
1705
+ this.log("Subscriber callback error:", error);
1706
+ }
1707
+ }
1708
+ }
1709
+ /**
1710
+ * Logs a message if logging is enabled.
1711
+ *
1712
+ * @protected
1713
+ * @param args - Arguments to log
1714
+ */
1715
+ log(...args) {
1716
+ if (this.config.isLoggingEnabled) {
1717
+ console.log("[FeatureFlagProvider]", ...args);
1718
+ }
1719
+ }
1720
+ };
1721
+
1722
+ // src/domain/featureFlags/providers/memory.ts
1723
+ var MemoryFeatureFlagProvider = class extends FeatureFlagProvider {
1724
+ static {
1725
+ __name(this, "MemoryFeatureFlagProvider");
1726
+ }
1727
+ flags = [];
1728
+ rules = [];
1729
+ /**
1730
+ * Creates a new memory feature flag provider.
1731
+ *
1732
+ * @param config - Provider configuration
1733
+ * @param features - Record of feature flag keys to their default values
1734
+ */
1735
+ constructor(config, features) {
1736
+ super(config, features);
1737
+ this.validateConfig();
1738
+ }
1739
+ /**
1740
+ * Fetches flags and rules from memory (FEATURES constant).
1741
+ *
1742
+ * @protected
1743
+ * @returns Promise resolving to flags and rules from memory
1744
+ */
1745
+ async fetchData() {
1746
+ this.log("Fetching feature flags from memory (FEATURES constant)");
1747
+ const currentTime = /* @__PURE__ */ new Date();
1748
+ this.flags = Object.entries(this.features).map(
1749
+ ([key, value]) => this.createFeatureFlagFromConstant(
1750
+ key,
1751
+ value,
1752
+ currentTime
1753
+ )
1754
+ );
1755
+ this.rules = [...this.getManualRules()];
1756
+ this.log(`Loaded ${this.flags.length} flags and ${this.rules.length} rules from memory`);
1757
+ return {
1758
+ flags: this.flags,
1759
+ rules: this.rules
1760
+ };
1761
+ }
1762
+ /**
1763
+ * Creates a FeatureFlag object from a FEATURES constant entry.
1764
+ *
1765
+ * @private
1766
+ * @param key - The feature flag key
1767
+ * @param value - The value from FEATURES constant
1768
+ * @param currentTime - Current timestamp
1769
+ * @returns FeatureFlag object
1770
+ */
1771
+ createFeatureFlagFromConstant(key, value, currentTime) {
1772
+ return {
1773
+ key,
1774
+ name: this.generateFlagName(key),
1775
+ description: `Memory-based flag for ${key}`,
1776
+ isEnabled: true,
1777
+ value,
1778
+ type: this.inferFlagType(value),
1779
+ environment: "all",
1780
+ rolloutPercentage: void 0,
1781
+ createdAt: currentTime,
1782
+ updatedAt: currentTime,
1783
+ createdBy: "memory-system",
1784
+ updatedBy: "memory-system"
1785
+ };
1786
+ }
1787
+ /**
1788
+ * Generates a human-readable name from a flag key.
1789
+ *
1790
+ * @private
1791
+ * @param key - The feature flag key
1792
+ * @returns Human-readable flag name
1793
+ */
1794
+ generateFlagName(key) {
1795
+ return key.split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(" ");
1796
+ }
1797
+ /**
1798
+ * Infers the flag type from its value.
1799
+ *
1800
+ * @private
1801
+ * @param value - The flag value
1802
+ * @returns The inferred type
1803
+ */
1804
+ inferFlagType(value) {
1805
+ if (typeof value === "boolean") return "boolean";
1806
+ if (typeof value === "string") return "string";
1807
+ if (typeof value === "number") return "number";
1808
+ return "json";
1809
+ }
1810
+ /**
1811
+ * Gets manually added rules for memory provider.
1812
+ *
1813
+ * @private
1814
+ * @returns Array of manually configured rules
1815
+ */
1816
+ getManualRules() {
1817
+ return this.config.memoryRules ?? [];
1818
+ }
1819
+ /**
1820
+ * Validates the memory provider configuration.
1821
+ *
1822
+ * @private
1823
+ * @throws Error if configuration is invalid
1824
+ */
1825
+ validateConfig() {
1826
+ if (this.config.provider !== "memory") {
1827
+ throw new Error('Memory provider requires provider to be set to "memory"');
1828
+ }
1829
+ if (this.config.memoryRules && !Array.isArray(this.config.memoryRules)) {
1830
+ throw new Error("memoryRules must be an array if provided");
1831
+ }
1832
+ }
1833
+ /**
1834
+ * Adds a rule to the memory provider at runtime.
1835
+ *
1836
+ * @param rule - The rule to add
1837
+ */
1838
+ addRule(rule) {
1839
+ this.rules.push(rule);
1840
+ this.engine.setRules(this.rules);
1841
+ void this.cacheManager.clear();
1842
+ this.notifySubscribers();
1843
+ this.log(`Added rule: ${rule.name} for flag: ${rule.flagKey}`);
1844
+ }
1845
+ /**
1846
+ * Removes a rule from the memory provider.
1847
+ *
1848
+ * @param ruleId - The ID of the rule to remove
1849
+ */
1850
+ removeRule(ruleId) {
1851
+ const initialCount = this.rules.length;
1852
+ this.rules = this.rules.filter((rule) => rule.id !== ruleId);
1853
+ if (this.rules.length < initialCount) {
1854
+ this.engine.setRules(this.rules);
1855
+ void this.cacheManager.clear();
1856
+ this.notifySubscribers();
1857
+ this.log(`Removed rule with ID: ${ruleId}`);
1858
+ } else {
1859
+ this.log(`Rule with ID ${ruleId} not found`);
1860
+ }
1861
+ }
1862
+ /**
1863
+ * Updates a flag value in memory.
1864
+ *
1865
+ * @param key - The flag key to update
1866
+ * @param value - The new value
1867
+ * @param updateProps - Optional properties to update
1868
+ */
1869
+ updateFlag(key, value, updateProps) {
1870
+ const flagIndex = this.flags.findIndex((flag) => flag.key === key);
1871
+ if (flagIndex === -1) {
1872
+ this.log(`Flag with key ${key} not found in memory`);
1873
+ return;
1874
+ }
1875
+ const updatedFlag = {
1876
+ ...this.flags[flagIndex],
1877
+ value,
1878
+ type: this.inferFlagType(value),
1879
+ updatedAt: /* @__PURE__ */ new Date(),
1880
+ updatedBy: "memory-runtime",
1881
+ ...updateProps
1882
+ };
1883
+ this.flags[flagIndex] = updatedFlag;
1884
+ this.engine.setFlags(this.flags);
1885
+ void this.cacheManager.clear();
1886
+ this.notifySubscribers();
1887
+ this.log(`Updated flag: ${key} with new value:`, value);
1888
+ }
1889
+ /**
1890
+ * Adds a new flag to memory at runtime.
1891
+ *
1892
+ * @param key - The flag key
1893
+ * @param value - The flag value
1894
+ * @param props - Optional flag properties
1895
+ */
1896
+ addFlag(key, value, props) {
1897
+ if (this.flagExists(key)) {
1898
+ return;
1899
+ }
1900
+ const newFlag = this.createNewFlag(key, value, props);
1901
+ this.addFlagToStore(key, newFlag, value);
1902
+ }
1903
+ /**
1904
+ * Checks if a flag with the given key exists.
1905
+ */
1906
+ flagExists(key) {
1907
+ const exists = this.flags.some((flag) => flag.key === key);
1908
+ if (exists) {
1909
+ this.log(`Flag with key ${key} already exists in memory`);
1910
+ }
1911
+ return exists;
1912
+ }
1913
+ /**
1914
+ * Creates a new flag object.
1915
+ */
1916
+ createNewFlag(key, value, props) {
1917
+ const currentTime = /* @__PURE__ */ new Date();
1918
+ const flagDefaults = this.getDefaultFlagProperties(key, value);
1919
+ const flagProps = this.mergeWithUserProps(flagDefaults, props);
1920
+ return {
1921
+ key,
1922
+ ...flagProps,
1923
+ value,
1924
+ type: flagDefaults.type,
1925
+ createdAt: currentTime,
1926
+ updatedAt: currentTime,
1927
+ createdBy: "memory-runtime",
1928
+ updatedBy: "memory-runtime"
1929
+ };
1930
+ }
1931
+ /**
1932
+ * Gets default properties for a new flag.
1933
+ */
1934
+ getDefaultFlagProperties(key, value) {
1935
+ return {
1936
+ name: this.generateFlagName(key),
1937
+ description: `Runtime-added flag for ${key}`,
1938
+ isEnabled: true,
1939
+ type: this.inferFlagType(value),
1940
+ environment: "all",
1941
+ rolloutPercentage: void 0
1942
+ };
1943
+ }
1944
+ /**
1945
+ * Merges default properties with user-provided properties.
1946
+ */
1947
+ mergeWithUserProps(defaults, props) {
1948
+ const merged = { ...defaults, ...props };
1949
+ if (props?.type) {
1950
+ merged.type = props.type;
1951
+ }
1952
+ return merged;
1953
+ }
1954
+ /**
1955
+ * Adds the flag to the store and notifies systems.
1956
+ */
1957
+ addFlagToStore(key, newFlag, value) {
1958
+ this.flags.push(newFlag);
1959
+ this.engine.setFlags(this.flags);
1960
+ void this.cacheManager.clear();
1961
+ this.notifySubscribers();
1962
+ this.log(`Added new flag: ${key} with value:`, value);
1963
+ }
1964
+ /**
1965
+ * Removes a flag from memory.
1966
+ *
1967
+ * @param key - The flag key to remove
1968
+ */
1969
+ removeFlag(key) {
1970
+ const initialCount = this.flags.length;
1971
+ this.flags = this.flags.filter((flag) => flag.key !== key);
1972
+ if (this.flags.length < initialCount) {
1973
+ this.engine.setFlags(this.flags);
1974
+ void this.cacheManager.clear();
1975
+ this.notifySubscribers();
1976
+ this.log(`Removed flag: ${key}`);
1977
+ } else {
1978
+ this.log(`Flag with key ${key} not found in memory`);
1979
+ }
1980
+ }
1981
+ /**
1982
+ * Gets all current flags in memory.
1983
+ *
1984
+ * @returns Array of current flags
1985
+ */
1986
+ getCurrentFlags() {
1987
+ return [...this.flags];
1988
+ }
1989
+ /**
1990
+ * Gets all current rules in memory.
1991
+ *
1992
+ * @returns Array of current rules
1993
+ */
1994
+ getCurrentRules() {
1995
+ return [...this.rules];
1996
+ }
1997
+ /**
1998
+ * Resets the memory provider to its initial state.
1999
+ */
2000
+ async reset() {
2001
+ this.log("Resetting memory provider to initial state");
2002
+ await this.refresh();
2003
+ }
2004
+ /**
2005
+ * Gets statistics about the memory provider.
2006
+ *
2007
+ * @returns Provider statistics
2008
+ */
2009
+ getStats() {
2010
+ return {
2011
+ flagCount: this.flags.length,
2012
+ ruleCount: this.rules.length,
2013
+ cacheSize: 0,
2014
+ // Memory cache size would need to be tracked
2015
+ subscriberCount: this.subscribers.size,
2016
+ isInitialized: this.isInitialized
2017
+ };
2018
+ }
2019
+ /**
2020
+ * Logs messages with MemoryFeatureFlagProvider prefix.
2021
+ *
2022
+ * @param args - Arguments to log
2023
+ */
2024
+ log(...args) {
2025
+ if (this.config.isLoggingEnabled) {
2026
+ console.log("[MemoryFeatureFlagProvider]", ...args);
2027
+ }
2028
+ }
2029
+ };
2030
+
2031
+ // src/domain/featureFlags/providers/file.ts
2032
+ var FileFeatureFlagProvider = class extends FeatureFlagProvider {
2033
+ static {
2034
+ __name(this, "FileFeatureFlagProvider");
2035
+ }
2036
+ fileWatcher;
2037
+ /**
2038
+ * Creates a new file feature flag provider.
2039
+ *
2040
+ * @param config - Provider configuration with file settings
2041
+ * @param features - Record of feature flag keys to their default values
2042
+ */
2043
+ constructor(config, features) {
2044
+ super(config, features);
2045
+ this.validateConfig();
2046
+ this.setupFileWatcher();
2047
+ throw new Error("File provider implementation coming soon");
2048
+ }
2049
+ /**
2050
+ * Fetches flags and rules from the configuration file.
2051
+ *
2052
+ * @protected
2053
+ * @returns Promise resolving to flags and rules from file
2054
+ */
2055
+ async fetchData() {
2056
+ throw new Error(
2057
+ 'File Provider is not yet fully implemented. This requires dedicated file paths to be configured in @plyaz/core.\n\nRequired Implementation:\n1. Define standard file paths for feature flags\n2. Implement file reading and parsing logic\n3. Add YAML and JSON format support\n4. Set up file watching for hot reload\n5. Add file validation and error handling\n\nRecommended File Structure:\n@plyaz/core/config/\n├── feature-flags.json\n├── feature-flags.yaml\n├── rules/\n│ ├── targeting-rules.json\n│ └── rollout-rules.json\n└── environments/\n ├── development.json\n ├── staging.json\n └── production.json\n\nExample Configuration:\n{\n provider: "file",\n fileConfig: {\n filePath: "@plyaz/core/config/feature-flags.json",\n format: "json",\n shouldWatchForChanges: true\n },\n isCacheEnabled: true,\n cacheTtl: 60\n}\n\nNote: Default provider should be memory for testing environments.\nFile provider is intended for configuration-driven deployments.\n\nMigration Note: This provider will be moved to @plyaz/core/src/domain/featureFlags/providers/'
2058
+ );
2059
+ }
2060
+ /**
2061
+ * Validates the file provider configuration.
2062
+ *
2063
+ * @private
2064
+ * @throws Error if configuration is invalid
2065
+ */
2066
+ validateConfig() {
2067
+ if (this.config.provider !== "file") {
2068
+ throw new Error('File provider requires provider to be set to "file"');
2069
+ }
2070
+ if (!this.config.fileConfig) {
2071
+ throw new Error("File configuration is required for file provider");
2072
+ }
2073
+ const { filePath, format } = this.config.fileConfig;
2074
+ if (!filePath) {
2075
+ throw new Error("File path is required");
2076
+ }
2077
+ if (!format || !["json", "yaml"].includes(format)) {
2078
+ throw new Error('File format must be either "json" or "yaml"');
2079
+ }
2080
+ }
2081
+ /**
2082
+ * Sets up file watching for hot reload if enabled.
2083
+ *
2084
+ * @private
2085
+ */
2086
+ setupFileWatcher() {
2087
+ if (!this.config.fileConfig?.shouldWatchForChanges) {
2088
+ return;
2089
+ }
2090
+ this.log("File watching would be enabled for hot reload");
2091
+ this.log("File path:", this.config.fileConfig.filePath);
2092
+ }
2093
+ /**
2094
+ * Disposes of the file provider and stops file watching.
2095
+ */
2096
+ dispose() {
2097
+ super.dispose();
2098
+ if (this.fileWatcher) {
2099
+ clearInterval(this.fileWatcher);
2100
+ this.fileWatcher = void 0;
2101
+ this.log("File watching stopped");
2102
+ }
2103
+ }
2104
+ /**
2105
+ * Gets information about the file provider.
2106
+ *
2107
+ * @returns File provider information
2108
+ */
2109
+ getFileInfo() {
2110
+ return {
2111
+ filePath: this.config.fileConfig?.filePath,
2112
+ format: this.config.fileConfig?.format,
2113
+ isWatchEnabled: Boolean(this.config.fileConfig?.shouldWatchForChanges),
2114
+ isImplemented: false,
2115
+ requiredImplementation: [
2116
+ "File reading and parsing logic",
2117
+ "YAML and JSON format support",
2118
+ "File watching for hot reload",
2119
+ "Standard file path configuration",
2120
+ "File validation and error handling"
2121
+ ]
2122
+ };
2123
+ }
2124
+ };
2125
+
2126
+ // src/domain/featureFlags/providers/redis.ts
2127
+ var RedisFeatureFlagProvider = class extends FeatureFlagProvider {
2128
+ static {
2129
+ __name(this, "RedisFeatureFlagProvider");
2130
+ }
2131
+ /**
2132
+ * Creates a new Redis feature flag provider.
2133
+ *
2134
+ * @param config - Provider configuration with Redis settings
2135
+ * @param features - Record of feature flag keys to their default values
2136
+ */
2137
+ constructor(config, features) {
2138
+ super(config, features);
2139
+ this.validateConfig();
2140
+ throw new Error("Redis provider requires @plyaz/core on Cache implementation");
2141
+ }
2142
+ /**
2143
+ * Fetches flags and rules from Redis storage.
2144
+ *
2145
+ * @protected
2146
+ * @returns Promise resolving to flags and rules from Redis
2147
+ */
2148
+ async fetchData() {
2149
+ throw new Error(
2150
+ 'Redis Provider is not yet fully implemented. This requires integration with the cache layer.\n\nRequired Implementation:\n1. Integrate with cache/strategies/redis.ts\n2. Implement Redis data storage patterns\n3. Add Redis client management\n4. Set up data serialization/deserialization\n5. Add connection health monitoring\n6. Implement Redis key management strategies\n\nRedis Storage Patterns:\n- Hash-based storage (recommended)\n- List-based storage\n- String/JSON storage\n\nKey Structure:\n- {prefix}:flags - Feature flags hash\n- {prefix}:rules - Targeting rules list\n- {prefix}:overrides - Manual overrides hash\n- {prefix}:meta - Metadata and versioning\n\nExample Configuration:\n{\n provider: "redis",\n redisConfig: {\n url: "redis://localhost:6379",\n keyPrefix: "plyaz:feature_flags"\n },\n isCacheEnabled: true,\n cacheTtl: 300\n}\n\nRedis Provider Benefits:\n- Distributed caching across instances\n- Real-time flag updates\n- Persistent storage option\n- High performance evaluation\n\nMigration Note: This provider will be moved to @plyaz/core/src/domain/featureFlags/providers/\nIt should integrate with the cache layer at @plyaz/core/src/cache/strategies/redis.ts'
2151
+ );
2152
+ }
2153
+ /**
2154
+ * Validates the Redis provider configuration.
2155
+ *
2156
+ * @private
2157
+ * @throws Error if configuration is invalid
2158
+ */
2159
+ validateConfig() {
2160
+ if (this.config.provider !== "redis") {
2161
+ throw new Error('Redis provider requires provider to be set to "redis"');
2162
+ }
2163
+ if (!this.config.redisConfig) {
2164
+ throw new Error("Redis configuration is required for Redis provider");
2165
+ }
2166
+ if (!this.config.redisConfig.url) {
2167
+ throw new Error("Redis URL is required");
2168
+ }
2169
+ if (!this.isValidRedisUrl(this.config.redisConfig.url)) {
2170
+ throw new Error("Redis URL must be a valid Redis connection string (redis:// or rediss://)");
2171
+ }
2172
+ this.log("Redis provider configuration is valid, but implementation is not ready");
2173
+ this.log("Redis URL:", this.config.redisConfig.url);
2174
+ this.log("Key Prefix:", this.config.redisConfig.keyPrefix ?? "feature_flags");
2175
+ }
2176
+ /**
2177
+ * Validates Redis URL format.
2178
+ *
2179
+ * @private
2180
+ * @param url - URL to validate
2181
+ * @returns True if valid Redis URL
2182
+ */
2183
+ isValidRedisUrl(url) {
2184
+ return url.startsWith("redis://") || url.startsWith("rediss://");
2185
+ }
2186
+ /**
2187
+ * Gets Redis provider information.
2188
+ *
2189
+ * @returns Redis provider status information
2190
+ */
2191
+ getRedisInfo() {
2192
+ return {
2193
+ url: this.config.redisConfig?.url,
2194
+ keyPrefix: this.config.redisConfig?.keyPrefix ?? "feature_flags",
2195
+ isImplemented: false,
2196
+ requiredImplementation: [
2197
+ "Integration with cache/strategies/redis.ts",
2198
+ "Redis data storage patterns",
2199
+ "Client management and health monitoring",
2200
+ "Data serialization/deserialization",
2201
+ "Key management strategies"
2202
+ ],
2203
+ recommendedPatterns: [
2204
+ "Hash-based storage for flags",
2205
+ "List-based storage for rules",
2206
+ "Pub/Sub for real-time updates",
2207
+ "TTL for automatic cleanup"
2208
+ ]
2209
+ };
2210
+ }
2211
+ };
2212
+
2213
+ // src/domain/featureFlags/providers/api.ts
2214
+ var ApiFeatureFlagProvider = class extends FeatureFlagProvider {
2215
+ static {
2216
+ __name(this, "ApiFeatureFlagProvider");
2217
+ }
2218
+ /**
2219
+ * Creates a new API feature flag provider.
2220
+ *
2221
+ * @param config - Provider configuration with API settings
2222
+ * @throws Error indicating that @plyaz/api implementation is required
2223
+ */
2224
+ constructor(config, features) {
2225
+ super(config, features);
2226
+ this.validateConfig();
2227
+ throw new Error("API provider requires @plyaz/api package implementation");
2228
+ }
2229
+ /**
2230
+ * Fetches flags and rules from the API endpoint.
2231
+ * Currently throws an error as the API implementation is not ready.
2232
+ *
2233
+ * @protected
2234
+ * @returns Promise that rejects with implementation error
2235
+ * @throws Error indicating missing API implementation
2236
+ */
2237
+ async fetchData() {
2238
+ throw new Error(
2239
+ 'API Provider is not yet implemented. This requires @plyaz/api package with the following endpoints:\n\nRequired API Endpoints (to be implemented in @plyaz/api):\n- GET /api/v1/feature-flags (get all flags)\n- GET /api/v1/feature-flag-rules (get all rules)\n- POST /api/v1/feature-flags/evaluate (evaluate flags)\n- POST /api/v1/feature-flags/evaluate/bulk (bulk evaluation)\n\nRequired Backend Implementation:\n1. Install and configure @plyaz/api package\n2. Implement the API endpoints in NestJS modules\n3. Set up authentication (API key or JWT)\n4. Configure CORS and rate limiting\n5. Add request/response validation\n6. Set up database integration with @plyaz/db\n\nExample Configuration:\n{\n provider: "api",\n apiEndpoint: "https://api.plyaz.co.uk",\n apiKey: "your-api-key",\n isCacheEnabled: true,\n cacheTtl: 300\n}\n\nSee /docs/feature-flag-to-implement/api-requirements.md for complete implementation details.\n\nMigration Note: This provider will be moved to @plyaz/core/src/domain/featureFlags/providers/'
2240
+ );
2241
+ }
2242
+ /**
2243
+ * Validates the API provider configuration.
2244
+ *
2245
+ * @private
2246
+ * @throws Error if configuration is invalid or incomplete
2247
+ */
2248
+ validateConfig() {
2249
+ if (this.config.provider !== "api") {
2250
+ throw new Error('API provider requires provider to be set to "api"');
2251
+ }
2252
+ if (!this.config.apiEndpoint) {
2253
+ throw new Error(
2254
+ 'API endpoint is required for API provider. Set apiEndpoint in your configuration.\nExample: apiEndpoint: "https://api.plyaz.co.uk"'
2255
+ );
2256
+ }
2257
+ if (!this.config.apiKey) {
2258
+ throw new Error(
2259
+ "API key is required for API provider. Set apiKey in your configuration.\nExample: apiKey: process.env.FEATURE_FLAG_API_KEY"
2260
+ );
2261
+ }
2262
+ if (!this.isValidUrl(this.config.apiEndpoint)) {
2263
+ throw new Error(
2264
+ `API endpoint must be a valid URL. Received: ${this.config.apiEndpoint}
2265
+ Example: "https://api.plyaz.co.uk"`
2266
+ );
2267
+ }
2268
+ this.log("API provider configuration is valid, but implementation is not ready");
2269
+ this.log("API Endpoint:", this.config.apiEndpoint);
2270
+ this.log("API Key:", this.config.apiKey ? "[SET]" : "[MISSING]");
2271
+ }
2272
+ /**
2273
+ * Validates URL format.
2274
+ *
2275
+ * @private
2276
+ * @param url - URL to validate
2277
+ * @returns True if valid URL
2278
+ */
2279
+ isValidUrl(url) {
2280
+ try {
2281
+ new URL(url);
2282
+ return true;
2283
+ } catch {
2284
+ return false;
2285
+ }
2286
+ }
2287
+ /**
2288
+ * Gets API provider status and configuration info.
2289
+ *
2290
+ * @returns API provider status information
2291
+ */
2292
+ getApiInfo() {
2293
+ return {
2294
+ endpoint: this.config.apiEndpoint,
2295
+ hasApiKey: Boolean(this.config.apiKey),
2296
+ isImplemented: false,
2297
+ requiredPackages: ["@plyaz/api"],
2298
+ documentationPath: "/docs/feature-flag-to-implement/api-requirements.md"
2299
+ };
2300
+ }
2301
+ };
2302
+
2303
+ // src/domain/featureFlags/providers/database.ts
2304
+ var DatabaseFeatureFlagProvider = class extends FeatureFlagProvider {
2305
+ static {
2306
+ __name(this, "DatabaseFeatureFlagProvider");
2307
+ }
2308
+ /**
2309
+ * Creates a new database feature flag provider.
2310
+ *
2311
+ * @param config - Provider configuration with database settings
2312
+ * @throws Error indicating that @plyaz/db implementation is required
2313
+ */
2314
+ constructor(config, features) {
2315
+ super(config, features);
2316
+ this.validateConfig();
2317
+ throw new Error("Database provider requires @plyaz/db package implementation");
2318
+ }
2319
+ /**
2320
+ * Fetches flags and rules from the database.
2321
+ * Currently throws an error as the database implementation is not ready.
2322
+ *
2323
+ * @protected
2324
+ * @returns Promise that rejects with implementation error
2325
+ * @throws Error indicating missing database implementation
2326
+ */
2327
+ async fetchData() {
2328
+ throw new Error(
2329
+ 'Database Provider is not yet implemented. This requires @plyaz/db package with the following components:\n\nRequired Database Setup:\n1. PostgreSQL or MySQL database\n2. Tables created using provided schema\n3. ORM implementation (Drizzle or Prisma)\n4. Repository pattern for data access\n5. NestJS modules and services\n\nRequired Tables:\n- feature_flags (main flags table)\n- feature_flag_rules (targeting rules table)\n- feature_flag_evaluations (audit log table)\n- feature_flag_overrides (temporary overrides table)\n\nDatabase Schema:\nThe complete schema is provided in:\n/docs/feature-flag-to-implement/database-requirements.md\n\nRequired Implementation Steps:\n1. Install and configure @plyaz/db package\n2. Set up database connection and ORM\n3. Create tables using the provided SQL schema\n4. Implement FeatureFlagsRepository interface\n5. Add NestJS modules, services, and controllers\n6. Set up database migrations\n7. Add comprehensive test coverage\n\nExample Configuration:\n{\n provider: "database",\n databaseConfig: {\n connectionString: "postgresql://user:pass@localhost:5432/plyaz",\n tableName: "feature_flags"\n },\n isCacheEnabled: true,\n cacheTtl: 300\n}\n\nSee /docs/feature-flag-to-implement/database-requirements.md for complete implementation details.\n\nMigration Note: This provider will be moved to @plyaz/core/src/domain/featureFlags/providers/'
2330
+ );
2331
+ }
2332
+ /**
2333
+ * Validates the database provider configuration.
2334
+ *
2335
+ * @private
2336
+ * @throws Error if configuration is invalid or incomplete
2337
+ */
2338
+ validateConfig() {
2339
+ this.validateProviderType();
2340
+ this.validateDatabaseConfig();
2341
+ this.validateConnectionString();
2342
+ this.logConfigurationStatus();
2343
+ }
2344
+ validateProviderType() {
2345
+ if (this.config.provider !== "database") {
2346
+ throw new Error('Database provider requires provider to be set to "database"');
2347
+ }
2348
+ }
2349
+ validateDatabaseConfig() {
2350
+ if (!this.config.databaseConfig) {
2351
+ throw new Error(
2352
+ 'Database configuration is required for database provider. Set databaseConfig in your configuration.\nExample: databaseConfig: { connectionString: "postgresql://...", tableName: "feature_flags" }'
2353
+ );
2354
+ }
2355
+ }
2356
+ validateConnectionString() {
2357
+ const { connectionString, tableName } = this.config.databaseConfig;
2358
+ if (!connectionString) {
2359
+ throw new Error(
2360
+ 'Database connection string is required. Set connectionString in your databaseConfig.\nExample: connectionString: "postgresql://user:pass@localhost:5432/plyaz"'
2361
+ );
2362
+ }
2363
+ if (!this.isValidPostgresUrl(connectionString) && !this.isValidMysqlUrl(connectionString)) {
2364
+ throw new Error(
2365
+ `Database connection string must be a valid PostgreSQL or MySQL URL. Received: ${connectionString}
2366
+ Examples:
2367
+ - PostgreSQL: "postgresql://user:pass@localhost:5432/plyaz"
2368
+ - MySQL: "mysql://user:pass@localhost:3306/plyaz"`
2369
+ );
2370
+ }
2371
+ if (!tableName || typeof tableName !== "string") {
2372
+ throw new Error("Database provider requires databaseConfig.tableName");
2373
+ }
2374
+ }
2375
+ logConfigurationStatus() {
2376
+ const { connectionString, tableName } = this.config.databaseConfig;
2377
+ this.log("Database provider configuration is valid, but implementation is not ready");
2378
+ this.log("Connection String:", this.maskConnectionString(connectionString));
2379
+ this.log("Table Name:", tableName ?? "feature_flags (default)");
2380
+ }
2381
+ /**
2382
+ * Validates PostgreSQL URL format.
2383
+ *
2384
+ * @private
2385
+ * @param url - URL to validate
2386
+ * @returns True if valid PostgreSQL URL
2387
+ */
2388
+ isValidPostgresUrl(url) {
2389
+ return url.startsWith("postgresql://") || url.startsWith("postgres://");
2390
+ }
2391
+ /**
2392
+ * Validates MySQL URL format.
2393
+ *
2394
+ * @private
2395
+ * @param url - URL to validate
2396
+ * @returns True if valid MySQL URL
2397
+ */
2398
+ isValidMysqlUrl(url) {
2399
+ return url.startsWith("mysql://");
2400
+ }
2401
+ /**
2402
+ * Masks sensitive parts of connection string for logging.
2403
+ *
2404
+ * @private
2405
+ * @param connectionString - Original connection string
2406
+ * @returns Masked connection string
2407
+ */
2408
+ maskConnectionString(connectionString) {
2409
+ try {
2410
+ const url = new URL(connectionString);
2411
+ const masked = `${url.protocol}//${url.username}:****@${url.host}${url.pathname}`;
2412
+ return masked;
2413
+ } catch {
2414
+ return "[INVALID_URL]";
2415
+ }
2416
+ }
2417
+ /**
2418
+ * Gets database provider status and configuration info.
2419
+ *
2420
+ * @returns Database provider status information
2421
+ */
2422
+ getDatabaseInfo() {
2423
+ return {
2424
+ connectionString: this.config.databaseConfig?.connectionString ? this.maskConnectionString(this.config.databaseConfig.connectionString) : void 0,
2425
+ tableName: this.config.databaseConfig?.tableName ?? "feature_flags",
2426
+ isImplemented: false,
2427
+ requiredPackages: ["@plyaz/db"],
2428
+ recommendedORM: ["drizzle-orm", "prisma"],
2429
+ documentationPath: "/docs/feature-flag-to-implement/database-requirements.md",
2430
+ schemaPath: "/docs/feature-flag-to-implement/database-requirements.md#database-schema"
2431
+ };
2432
+ }
2433
+ };
2434
+
2435
+ // src/domain/featureFlags/providers/factory.ts
2436
+ var PROVIDER_REGISTRY = {
2437
+ memory: MemoryFeatureFlagProvider,
2438
+ file: FileFeatureFlagProvider,
2439
+ redis: RedisFeatureFlagProvider,
2440
+ api: ApiFeatureFlagProvider,
2441
+ database: DatabaseFeatureFlagProvider
2442
+ };
2443
+ var FeatureFlagProviderFactory = class {
2444
+ static {
2445
+ __name(this, "FeatureFlagProviderFactory");
2446
+ }
2447
+ /**
2448
+ * Creates a new feature flag provider instance based on configuration.
2449
+ *
2450
+ * @param config - Provider configuration
2451
+ * @param features - Record of feature flag keys to their default values
2452
+ * @returns Configured provider instance
2453
+ * @throws Error if provider type is unsupported or configuration is invalid
2454
+ */
2455
+ static create(config, features) {
2456
+ this.validateConfig(config);
2457
+ const ProviderClass = PROVIDER_REGISTRY[config.provider];
2458
+ if (!ProviderClass) {
2459
+ throw new Error(
2460
+ `Unsupported provider type: ${config.provider}. Supported types: ${this.getSupportedProviders().join(", ")}`
2461
+ );
2462
+ }
2463
+ try {
2464
+ return new ProviderClass(config, features);
2465
+ } catch (error) {
2466
+ throw new Error(
2467
+ `Failed to create ${config.provider} provider: ${error instanceof Error ? error.message : "Unknown error"}`
2468
+ );
2469
+ }
2470
+ }
2471
+ /**
2472
+ * Creates a provider with automatic initialization.
2473
+ *
2474
+ * @param config - Provider configuration
2475
+ * @param features - Record of feature flag keys to their default values
2476
+ * @returns Promise resolving to initialized provider instance
2477
+ */
2478
+ static async createAndInitialize(config, features) {
2479
+ const provider = this.create(config, features);
2480
+ await provider.initialize();
2481
+ return provider;
2482
+ }
2483
+ /**
2484
+ * Gets a list of all supported provider types.
2485
+ *
2486
+ * @returns Array of supported provider names
2487
+ */
2488
+ static getSupportedProviders() {
2489
+ return Object.keys(PROVIDER_REGISTRY);
2490
+ }
2491
+ /**
2492
+ * Checks if a provider type is supported.
2493
+ *
2494
+ * @param providerType - Provider type to check
2495
+ * @returns True if provider type is supported
2496
+ */
2497
+ static isProviderSupported(providerType) {
2498
+ return providerType in PROVIDER_REGISTRY;
2499
+ }
2500
+ /**
2501
+ * Gets provider information including implementation status.
2502
+ *
2503
+ * @returns Record of provider information
2504
+ */
2505
+ static getProvidersInfo() {
2506
+ return {
2507
+ memory: {
2508
+ name: "Memory Provider",
2509
+ isImplemented: true,
2510
+ description: "In-memory provider using FEATURES constant"
2511
+ },
2512
+ file: {
2513
+ name: "File Provider",
2514
+ isImplemented: true,
2515
+ description: "File-based provider supporting JSON and YAML formats"
2516
+ },
2517
+ redis: {
2518
+ name: "Redis Provider",
2519
+ isImplemented: true,
2520
+ description: "Redis-based provider for distributed caching",
2521
+ requirements: ["ioredis or redis package"]
2522
+ },
2523
+ api: {
2524
+ name: "API Provider",
2525
+ isImplemented: false,
2526
+ description: "Remote API provider for centralized flag management",
2527
+ requirements: ["@plyaz/api package", "API endpoints implementation"]
2528
+ },
2529
+ database: {
2530
+ name: "Database Provider",
2531
+ isImplemented: false,
2532
+ description: "Database provider for persistent flag storage",
2533
+ requirements: ["@plyaz/db package", "Database schema setup"]
2534
+ }
2535
+ };
2536
+ }
2537
+ /**
2538
+ * Creates a default memory provider for testing environments.
2539
+ *
2540
+ * @param features - Record of feature flag keys to their default values
2541
+ * @param overrides - Optional configuration overrides
2542
+ * @returns Memory provider instance
2543
+ */
2544
+ static createDefault(features, overrides) {
2545
+ const defaultConfig = {
2546
+ provider: "memory",
2547
+ isCacheEnabled: true,
2548
+ cacheTtl: 300,
2549
+ // 5 minutes
2550
+ refreshInterval: 0,
2551
+ // No auto-refresh
2552
+ isLoggingEnabled: false,
2553
+ shouldFallbackToDefaults: true,
2554
+ ...overrides
2555
+ };
2556
+ return this.create(defaultConfig, features);
2557
+ }
2558
+ /**
2559
+ * Validates provider configuration before instantiation.
2560
+ *
2561
+ * @private
2562
+ * @param config - Configuration to validate
2563
+ * @throws Error if configuration is invalid
2564
+ */
2565
+ static validateConfig(config) {
2566
+ if (!config) {
2567
+ throw new Error("Provider configuration is required");
2568
+ }
2569
+ if (!config.provider) {
2570
+ throw new Error("Provider type is required in configuration");
2571
+ }
2572
+ if (!this.isProviderSupported(config.provider)) {
2573
+ throw new Error(
2574
+ `Unsupported provider type: ${config.provider}. Supported providers: ${this.getSupportedProviders().join(", ")}`
2575
+ );
2576
+ }
2577
+ if (config.isCacheEnabled && config.cacheTtl <= 0) {
2578
+ throw new Error("Cache TTL must be greater than 0 when caching is enabled");
2579
+ }
2580
+ if (config.refreshInterval < 0) {
2581
+ throw new Error("Refresh interval cannot be negative");
2582
+ }
2583
+ this.validateProviderSpecificConfig(config);
2584
+ }
2585
+ /**
2586
+ * Validates provider-specific configuration requirements.
2587
+ *
2588
+ * @private
2589
+ * @param config - Configuration to validate
2590
+ * @throws Error if provider-specific configuration is invalid
2591
+ */
2592
+ static validateProviderSpecificConfig(config) {
2593
+ const validationMap = {
2594
+ file: /* @__PURE__ */ __name(() => this.validateFileConfig(config), "file"),
2595
+ redis: /* @__PURE__ */ __name(() => this.validateRedisConfig(config), "redis"),
2596
+ api: /* @__PURE__ */ __name(() => this.validateApiConfig(config), "api"),
2597
+ database: /* @__PURE__ */ __name(() => this.validateDatabaseConfig(config), "database"),
2598
+ memory: /* @__PURE__ */ __name(() => {
2599
+ }, "memory")
2600
+ // Memory provider has no additional requirements
2601
+ };
2602
+ const validator = validationMap[config.provider];
2603
+ if (!validator) {
2604
+ throw new Error(`Unknown provider type: ${config.provider}`);
2605
+ }
2606
+ validator();
2607
+ }
2608
+ static validateFileConfig(config) {
2609
+ if (!config.fileConfig) {
2610
+ throw new Error("File configuration is required for file provider");
2611
+ }
2612
+ }
2613
+ static validateRedisConfig(config) {
2614
+ if (!config.redisConfig) {
2615
+ throw new Error("Redis configuration is required for Redis provider");
2616
+ }
2617
+ }
2618
+ static validateApiConfig(config) {
2619
+ if (!config.apiEndpoint) {
2620
+ throw new Error("API endpoint is required for API provider");
2621
+ }
2622
+ }
2623
+ static validateDatabaseConfig(config) {
2624
+ if (!config.databaseConfig) {
2625
+ throw new Error("Database configuration is required for database provider");
2626
+ }
2627
+ }
2628
+ };
2629
+ var DEFAULT_FEATURE_FLAG_CONFIG = {
2630
+ provider: "memory",
2631
+ isCacheEnabled: true,
2632
+ cacheTtl: 300,
2633
+ // 5 minutes
2634
+ refreshInterval: 0,
2635
+ // No auto-refresh
2636
+ shouldFallbackToDefaults: true,
2637
+ isLoggingEnabled: false
2638
+ };
2639
+ var FeatureFlagSystem = {
2640
+ /**
2641
+ * Initialize for frontend/client applications.
2642
+ */
2643
+ initializeForFrontend: /* @__PURE__ */ __name(async (config = {}) => {
2644
+ const finalConfig = {
2645
+ ...DEFAULT_FEATURE_FLAG_CONFIG,
2646
+ ...config
2647
+ };
2648
+ const provider = FeatureFlagProviderFactory.create(finalConfig, FEATURES);
2649
+ await provider.initialize();
2650
+ return provider;
2651
+ }, "initializeForFrontend"),
2652
+ /**
2653
+ * Initialize for backend/server applications.
2654
+ */
2655
+ initializeForBackend: /* @__PURE__ */ __name(async (config = {}) => {
2656
+ const finalConfig = {
2657
+ ...DEFAULT_FEATURE_FLAG_CONFIG,
2658
+ ...config
2659
+ };
2660
+ const provider = FeatureFlagProviderFactory.create(finalConfig, FEATURES);
2661
+ await provider.initialize();
2662
+ return provider;
2663
+ }, "initializeForBackend"),
2664
+ /**
2665
+ * Initialize for testing environments.
2666
+ */
2667
+ initializeForTesting: /* @__PURE__ */ __name(async (overrides = {}) => {
2668
+ const provider = FeatureFlagProviderFactory.createDefault(FEATURES, {
2669
+ provider: "memory",
2670
+ isCacheEnabled: false,
2671
+ isLoggingEnabled: false
2672
+ });
2673
+ await provider.initialize();
2674
+ Object.entries(overrides).forEach(([key, value]) => {
2675
+ provider.setOverride(key, value);
2676
+ });
2677
+ return provider;
2678
+ }, "initializeForTesting")
2679
+ };
2680
+ var FeatureFlagController = class {
2681
+ constructor(featureFlagService) {
2682
+ this.featureFlagService = featureFlagService;
2683
+ }
2684
+ async evaluateFlag(key, body = {}) {
2685
+ try {
2686
+ return await this.featureFlagService.evaluateFlag(key, body.context);
2687
+ } catch (error) {
2688
+ throw new HttpException(
2689
+ `Failed to evaluate flag: ${error instanceof Error ? error.message : "Unknown error"}`,
2690
+ HttpStatus.BAD_REQUEST
2691
+ );
2692
+ }
2693
+ }
2694
+ async isEnabled(key, body = {}) {
2695
+ try {
2696
+ const isEnabled = await this.featureFlagService.isEnabled(key, body.context);
2697
+ return { isEnabled };
2698
+ } catch (error) {
2699
+ throw new HttpException(
2700
+ `Failed to check flag status: ${error instanceof Error ? error.message : "Unknown error"}`,
2701
+ HttpStatus.BAD_REQUEST
2702
+ );
2703
+ }
2704
+ }
2705
+ async evaluateAllFlags(body = {}) {
2706
+ try {
2707
+ return await this.featureFlagService.getAllFlags(body.context);
2708
+ } catch (error) {
2709
+ throw new HttpException(
2710
+ `Failed to evaluate all flags: ${error instanceof Error ? error.message : "Unknown error"}`,
2711
+ HttpStatus.INTERNAL_SERVER_ERROR
2712
+ );
2713
+ }
2714
+ }
2715
+ async createFlag(createData) {
2716
+ try {
2717
+ return await this.featureFlagService.createFlag(createData);
2718
+ } catch (error) {
2719
+ throw new HttpException(
2720
+ `Failed to create flag: ${error instanceof Error ? error.message : "Unknown error"}`,
2721
+ HttpStatus.BAD_REQUEST
2722
+ );
2723
+ }
2724
+ }
2725
+ async updateFlag(key, updateData) {
2726
+ try {
2727
+ return await this.featureFlagService.updateFlag(key, updateData);
2728
+ } catch (error) {
2729
+ throw new HttpException(
2730
+ `Failed to update flag: ${error instanceof Error ? error.message : "Unknown error"}`,
2731
+ HttpStatus.BAD_REQUEST
2732
+ );
2733
+ }
2734
+ }
2735
+ async deleteFlag(key) {
2736
+ try {
2737
+ await this.featureFlagService.deleteFlag(key);
2738
+ return { isSuccessful: true };
2739
+ } catch (error) {
2740
+ throw new HttpException(
2741
+ `Failed to delete flag: ${error instanceof Error ? error.message : "Unknown error"}`,
2742
+ HttpStatus.BAD_REQUEST
2743
+ );
2744
+ }
2745
+ }
2746
+ async setOverride(key, value) {
2747
+ try {
2748
+ await this.featureFlagService.setOverride(key, value);
2749
+ return { isSuccessful: true };
2750
+ } catch (error) {
2751
+ throw new HttpException(
2752
+ `Failed to set override: ${error instanceof Error ? error.message : "Unknown error"}`,
2753
+ HttpStatus.BAD_REQUEST
2754
+ );
2755
+ }
2756
+ }
2757
+ async removeOverride(key) {
2758
+ try {
2759
+ await this.featureFlagService.removeOverride(key);
2760
+ return { isSuccessful: true };
2761
+ } catch (error) {
2762
+ throw new HttpException(
2763
+ `Failed to remove override: ${error instanceof Error ? error.message : "Unknown error"}`,
2764
+ HttpStatus.BAD_REQUEST
2765
+ );
2766
+ }
2767
+ }
2768
+ async getAllFeatureFlags(environment) {
2769
+ try {
2770
+ return await this.featureFlagService.getAllFeatureFlags(environment);
2771
+ } catch (error) {
2772
+ throw new HttpException(
2773
+ `Failed to get flags: ${error instanceof Error ? error.message : "Unknown error"}`,
2774
+ HttpStatus.INTERNAL_SERVER_ERROR
2775
+ );
2776
+ }
2777
+ }
2778
+ async getFlagRules(key) {
2779
+ try {
2780
+ return await this.featureFlagService.getFlagRules(key);
2781
+ } catch (error) {
2782
+ throw new HttpException(
2783
+ `Failed to get flag rules: ${error instanceof Error ? error.message : "Unknown error"}`,
2784
+ HttpStatus.BAD_REQUEST
2785
+ );
2786
+ }
2787
+ }
2788
+ async refreshCache() {
2789
+ try {
2790
+ await this.featureFlagService.refreshCache();
2791
+ return { isSuccessful: true };
2792
+ } catch (error) {
2793
+ throw new HttpException(
2794
+ `Failed to refresh cache: ${error instanceof Error ? error.message : "Unknown error"}`,
2795
+ HttpStatus.INTERNAL_SERVER_ERROR
2796
+ );
2797
+ }
2798
+ }
2799
+ };
2800
+ __name(FeatureFlagController, "FeatureFlagController");
2801
+ __decorateClass([
2802
+ Post(":key/evaluate"),
2803
+ __decorateParam(0, Param("key")),
2804
+ __decorateParam(1, Body())
2805
+ ], FeatureFlagController.prototype, "evaluateFlag", 1);
2806
+ __decorateClass([
2807
+ Post(":key/enabled"),
2808
+ __decorateParam(0, Param("key")),
2809
+ __decorateParam(1, Body())
2810
+ ], FeatureFlagController.prototype, "isEnabled", 1);
2811
+ __decorateClass([
2812
+ Post("evaluate-all"),
2813
+ __decorateParam(0, Body())
2814
+ ], FeatureFlagController.prototype, "evaluateAllFlags", 1);
2815
+ __decorateClass([
2816
+ Post(),
2817
+ __decorateParam(0, Body())
2818
+ ], FeatureFlagController.prototype, "createFlag", 1);
2819
+ __decorateClass([
2820
+ Put(":key"),
2821
+ __decorateParam(0, Param("key")),
2822
+ __decorateParam(1, Body())
2823
+ ], FeatureFlagController.prototype, "updateFlag", 1);
2824
+ __decorateClass([
2825
+ Delete(":key"),
2826
+ __decorateParam(0, Param("key"))
2827
+ ], FeatureFlagController.prototype, "deleteFlag", 1);
2828
+ __decorateClass([
2829
+ Post(":key/override"),
2830
+ __decorateParam(0, Param("key")),
2831
+ __decorateParam(1, Body("value"))
2832
+ ], FeatureFlagController.prototype, "setOverride", 1);
2833
+ __decorateClass([
2834
+ Delete(":key/override"),
2835
+ __decorateParam(0, Param("key"))
2836
+ ], FeatureFlagController.prototype, "removeOverride", 1);
2837
+ __decorateClass([
2838
+ Get(),
2839
+ __decorateParam(0, Query("environment"))
2840
+ ], FeatureFlagController.prototype, "getAllFeatureFlags", 1);
2841
+ __decorateClass([
2842
+ Get(":key/rules"),
2843
+ __decorateParam(0, Param("key"))
2844
+ ], FeatureFlagController.prototype, "getFlagRules", 1);
2845
+ __decorateClass([
2846
+ Post("refresh")
2847
+ ], FeatureFlagController.prototype, "refreshCache", 1);
2848
+ FeatureFlagController = __decorateClass([
2849
+ Controller("feature-flags")
2850
+ ], FeatureFlagController);
2851
+ var FeatureFlagService = class {
2852
+ constructor(featureFlagRepository) {
2853
+ this.featureFlagRepository = featureFlagRepository;
2854
+ }
2855
+ logger = new Logger(FeatureFlagService.name);
2856
+ provider;
2857
+ /**
2858
+ * Initializes the service on module startup.
2859
+ */
2860
+ async onModuleInit() {
2861
+ try {
2862
+ await this.initializeProvider();
2863
+ this.logger.log("Feature flag service initialized successfully");
2864
+ } catch (error) {
2865
+ this.logger.error("Failed to initialize feature flag service", error);
2866
+ throw error;
2867
+ }
2868
+ }
2869
+ /**
2870
+ * Cleans up resources on module shutdown.
2871
+ */
2872
+ async onModuleDestroy() {
2873
+ this.provider.dispose();
2874
+ this.logger.log("Feature flag service disposed");
2875
+ }
2876
+ /**
2877
+ * Initializes the feature flag provider.
2878
+ */
2879
+ async initializeProvider() {
2880
+ const DEFAULT_CACHE_TTL = 300;
2881
+ const config = {
2882
+ provider: process.env.FEATURE_FLAG_PROVIDER ?? "database",
2883
+ isCacheEnabled: process.env.FEATURE_FLAG_CACHE_ENABLED === "true",
2884
+ cacheTtl: Number(process.env.FEATURE_FLAG_CACHE_TTL) || DEFAULT_CACHE_TTL,
2885
+ refreshInterval: Number(process.env.FEATURE_FLAG_REFRESH_INTERVAL) || 0,
2886
+ shouldFallbackToDefaults: true,
2887
+ isLoggingEnabled: process.env.NODE_ENV === "development"
2888
+ };
2889
+ this.provider = FeatureFlagProviderFactory.create(config, FEATURES);
2890
+ await this.provider.initialize();
2891
+ }
2892
+ /**
2893
+ * Gets the current provider instance.
2894
+ */
2895
+ getProvider() {
2896
+ if (!this.provider) {
2897
+ throw new Error("Feature flag provider not initialized");
2898
+ }
2899
+ return this.provider;
2900
+ }
2901
+ /**
2902
+ * Evaluates a feature flag for the given context.
2903
+ *
2904
+ * @param key - Feature flag key
2905
+ * @param context - Evaluation context
2906
+ * @returns Feature flag evaluation result
2907
+ */
2908
+ async evaluateFlag(key, context) {
2909
+ try {
2910
+ const provider = this.getProvider();
2911
+ const evaluation = await provider.getFlag(key, context);
2912
+ this.logger.debug(`Evaluated flag ${key}: ${evaluation.isEnabled}`, {
2913
+ key,
2914
+ value: evaluation.value,
2915
+ reason: evaluation.reason,
2916
+ context
2917
+ });
2918
+ return evaluation;
2919
+ } catch (error) {
2920
+ this.logger.error(`Failed to evaluate flag ${key}`, error);
2921
+ throw error;
2922
+ }
2923
+ }
2924
+ /**
2925
+ * Checks if a feature flag is enabled.
2926
+ *
2927
+ * @param key - Feature flag key
2928
+ * @param context - Evaluation context
2929
+ * @returns Boolean indicating if flag is enabled
2930
+ */
2931
+ async isEnabled(key, context) {
2932
+ const evaluation = await this.evaluateFlag(key, context);
2933
+ return evaluation.isEnabled;
2934
+ }
2935
+ /**
2936
+ * Gets all feature flags with their evaluations.
2937
+ *
2938
+ * @param context - Evaluation context
2939
+ * @returns All feature flag evaluations
2940
+ */
2941
+ async getAllFlags(context) {
2942
+ try {
2943
+ const provider = this.getProvider();
2944
+ const flags = await provider.getAllFlags();
2945
+ this.logger.debug(`Evaluated all flags for context`, {
2946
+ flagCount: Object.keys(flags).length,
2947
+ context
2948
+ });
2949
+ return flags;
2950
+ } catch (error) {
2951
+ this.logger.error("Failed to evaluate all flags", error);
2952
+ throw error;
2953
+ }
2954
+ }
2955
+ /**
2956
+ * Creates a new feature flag.
2957
+ *
2958
+ * @param createData - Flag creation data
2959
+ * @returns Created feature flag
2960
+ */
2961
+ async createFlag(createData) {
2962
+ try {
2963
+ const flag = await this.featureFlagRepository.createFlag(createData);
2964
+ await this.refreshCache();
2965
+ this.logger.log(`Created feature flag: ${createData.key}`);
2966
+ return flag;
2967
+ } catch (error) {
2968
+ this.logger.error(`Failed to create flag ${createData.key}`, error);
2969
+ throw error;
2970
+ }
2971
+ }
2972
+ /**
2973
+ * Updates an existing feature flag.
2974
+ *
2975
+ * @param key - Feature flag key
2976
+ * @param updateData - Flag update data
2977
+ * @returns Updated feature flag
2978
+ */
2979
+ async updateFlag(key, updateData) {
2980
+ try {
2981
+ const flag = await this.featureFlagRepository.updateFlag(key, updateData);
2982
+ await this.refreshCache();
2983
+ this.logger.log(`Updated feature flag: ${key}`);
2984
+ return flag;
2985
+ } catch (error) {
2986
+ this.logger.error(`Failed to update flag ${key}`, error);
2987
+ throw error;
2988
+ }
2989
+ }
2990
+ /**
2991
+ * Deletes a feature flag.
2992
+ *
2993
+ * @param key - Feature flag key
2994
+ */
2995
+ async deleteFlag(key) {
2996
+ try {
2997
+ await this.featureFlagRepository.deleteFlag(key);
2998
+ await this.refreshCache();
2999
+ this.logger.log(`Deleted feature flag: ${key}`);
3000
+ } catch (error) {
3001
+ this.logger.error(`Failed to delete flag ${key}`, error);
3002
+ throw error;
3003
+ }
3004
+ }
3005
+ /**
3006
+ * Sets a manual override for a flag.
3007
+ *
3008
+ * @param key - Feature flag key
3009
+ * @param value - Override value
3010
+ */
3011
+ async setOverride(key, value) {
3012
+ try {
3013
+ const provider = this.getProvider();
3014
+ provider.setOverride(key, value);
3015
+ this.logger.log(`Set override for flag ${key}: ${value}`);
3016
+ } catch (error) {
3017
+ this.logger.error(`Failed to set override for flag ${key}`, error);
3018
+ throw error;
3019
+ }
3020
+ }
3021
+ /**
3022
+ * Removes a manual override for a flag.
3023
+ *
3024
+ * @param key - Feature flag key
3025
+ */
3026
+ async removeOverride(key) {
3027
+ try {
3028
+ const provider = this.getProvider();
3029
+ provider.removeOverride(key);
3030
+ this.logger.log(`Removed override for flag ${key}`);
3031
+ } catch (error) {
3032
+ this.logger.error(`Failed to remove override for flag ${key}`, error);
3033
+ throw error;
3034
+ }
3035
+ }
3036
+ /**
3037
+ * Gets all available feature flags.
3038
+ *
3039
+ * @param environment - Filter by environment
3040
+ * @returns List of feature flags
3041
+ */
3042
+ async getAllFeatureFlags(environment) {
3043
+ try {
3044
+ return await this.featureFlagRepository.getAllFlags(environment);
3045
+ } catch (error) {
3046
+ this.logger.error("Failed to get all feature flags", error);
3047
+ throw error;
3048
+ }
3049
+ }
3050
+ /**
3051
+ * Gets all rules for a specific flag.
3052
+ *
3053
+ * @param key - Feature flag key
3054
+ * @returns List of rules for the flag
3055
+ */
3056
+ async getFlagRules(key) {
3057
+ try {
3058
+ return await this.featureFlagRepository.getFlagRules(key);
3059
+ } catch (error) {
3060
+ this.logger.error(`Failed to get rules for flag ${key}`, error);
3061
+ throw error;
3062
+ }
3063
+ }
3064
+ /**
3065
+ * Forces a refresh of the feature flag cache.
3066
+ */
3067
+ async refreshCache() {
3068
+ try {
3069
+ const provider = this.getProvider();
3070
+ await provider.refresh();
3071
+ this.logger.log("Feature flag cache refreshed");
3072
+ } catch (error) {
3073
+ this.logger.error("Failed to refresh feature flag cache", error);
3074
+ throw error;
3075
+ }
3076
+ }
3077
+ /**
3078
+ * Gets provider health status.
3079
+ *
3080
+ * @returns Provider health information
3081
+ */
3082
+ async getHealthStatus() {
3083
+ return {
3084
+ isInitialized: !!this.provider,
3085
+ provider: "database",
3086
+ // or get from config
3087
+ isCacheEnabled: true
3088
+ // or get from config
3089
+ };
3090
+ }
3091
+ };
3092
+ __name(FeatureFlagService, "FeatureFlagService");
3093
+ FeatureFlagService = __decorateClass([
3094
+ Injectable()
3095
+ ], FeatureFlagService);
3096
+ var FeatureFlagRepository = class {
3097
+ logger = new Logger(FeatureFlagRepository.name);
3098
+ // 24 * 60 * 60 * 1000
3099
+ // TODO: Inject database service when @plyaz/db is available
3100
+ // constructor(private readonly databaseService: DatabaseService) {}
3101
+ /**
3102
+ * Creates a new feature flag in the database.
3103
+ *
3104
+ * @param createData - Flag creation data
3105
+ * @returns Created feature flag
3106
+ */
3107
+ async createFlag(createData) {
3108
+ this.logger.log(`Creating flag: ${createData.key}`);
3109
+ const currentTime = /* @__PURE__ */ new Date();
3110
+ const newFlag = {
3111
+ key: createData.key,
3112
+ name: createData.name,
3113
+ description: createData.description ?? `Feature flag for ${createData.key}`,
3114
+ isEnabled: createData.isEnabled ?? true,
3115
+ value: createData.value,
3116
+ type: this.inferFlagType(createData.value),
3117
+ environment: createData.environment ?? "all",
3118
+ rolloutPercentage: createData.rolloutPercentage,
3119
+ createdAt: currentTime,
3120
+ updatedAt: currentTime,
3121
+ createdBy: "api",
3122
+ updatedBy: "api"
3123
+ };
3124
+ this.logger.debug(`Flag created: ${createData.key}`, newFlag);
3125
+ return newFlag;
3126
+ }
3127
+ /**
3128
+ * Updates an existing feature flag.
3129
+ *
3130
+ * @param key - Feature flag key
3131
+ * @param updateData - Flag update data
3132
+ * @returns Updated feature flag
3133
+ */
3134
+ async updateFlag(key, updateData) {
3135
+ this.logger.log(`Updating flag: ${key}`);
3136
+ const updatedFlag = {
3137
+ key,
3138
+ name: updateData.name ?? `Updated ${key}`,
3139
+ description: updateData.description ?? `Updated feature flag for ${key}`,
3140
+ isEnabled: updateData.isEnabled ?? true,
3141
+ value: updateData.value ?? true,
3142
+ type: updateData.value ? this.inferFlagType(updateData.value) : "boolean",
3143
+ environment: updateData.environment ?? "all",
3144
+ rolloutPercentage: updateData.rolloutPercentage,
3145
+ createdAt: new Date(Date.now() - FeatureFlagRepository.ONE_DAY_MS),
3146
+ // 1 day ago
3147
+ updatedAt: /* @__PURE__ */ new Date(),
3148
+ createdBy: "api",
3149
+ updatedBy: "api"
3150
+ };
3151
+ this.logger.debug(`Flag updated: ${key}`, updatedFlag);
3152
+ return updatedFlag;
3153
+ }
3154
+ /**
3155
+ * Deletes a feature flag from the database.
3156
+ *
3157
+ * @param key - Feature flag key
3158
+ */
3159
+ async deleteFlag(key) {
3160
+ this.logger.log(`Deleting flag: ${key}`);
3161
+ this.logger.debug(`Flag deleted: ${key}`);
3162
+ }
3163
+ /**
3164
+ * Gets all feature flags from the database.
3165
+ *
3166
+ * @param environment - Filter by environment
3167
+ * @returns List of feature flags
3168
+ */
3169
+ async getAllFlags(environment) {
3170
+ this.logger.log(`Getting all flags${environment ? ` for environment: ${environment}` : ""}`);
3171
+ const sampleFlags = this.createSampleFlags();
3172
+ const filteredFlags = this.filterFlagsByEnvironment(sampleFlags, environment);
3173
+ this.logger.debug(`Retrieved ${filteredFlags.length} flags`);
3174
+ return filteredFlags;
3175
+ }
3176
+ /**
3177
+ * Creates sample flags for stub implementation.
3178
+ *
3179
+ * @returns Array of sample feature flags
3180
+ */
3181
+ createSampleFlags() {
3182
+ return [
3183
+ {
3184
+ key: "AUTH_GOOGLE",
3185
+ name: "Google Authentication",
3186
+ description: "Enable Google OAuth authentication",
3187
+ isEnabled: true,
3188
+ value: "true",
3189
+ type: "boolean",
3190
+ environment: "all",
3191
+ rolloutPercentage: void 0,
3192
+ createdAt: new Date(Date.now() - FeatureFlagRepository.ONE_DAY_MS),
3193
+ updatedAt: /* @__PURE__ */ new Date(),
3194
+ createdBy: "system",
3195
+ updatedBy: "system"
3196
+ },
3197
+ {
3198
+ key: "AUTH_DISCORD",
3199
+ name: "Discord Authentication",
3200
+ description: "Enable Discord OAuth authentication",
3201
+ isEnabled: true,
3202
+ value: "true",
3203
+ type: "boolean",
3204
+ environment: "all",
3205
+ rolloutPercentage: void 0,
3206
+ createdAt: new Date(Date.now() - FeatureFlagRepository.ONE_DAY_MS),
3207
+ updatedAt: /* @__PURE__ */ new Date(),
3208
+ createdBy: "system",
3209
+ updatedBy: "system"
3210
+ }
3211
+ ];
3212
+ }
3213
+ /**
3214
+ * Filters flags by environment.
3215
+ *
3216
+ * @param flags - Array of flags to filter
3217
+ * @param environment - Environment to filter by
3218
+ * @returns Filtered flags
3219
+ */
3220
+ filterFlagsByEnvironment(flags, environment) {
3221
+ return environment ? flags.filter((flag) => flag.environment === environment || flag.environment === "all") : flags;
3222
+ }
3223
+ /**
3224
+ * Gets all rules for a specific flag.
3225
+ *
3226
+ * @param key - Feature flag key
3227
+ * @returns List of rules for the flag
3228
+ */
3229
+ async getFlagRules(key) {
3230
+ this.logger.log(`Getting rules for flag: ${key}`);
3231
+ const rules = [];
3232
+ this.logger.debug(`Retrieved ${rules.length} rules for flag: ${key}`);
3233
+ return rules;
3234
+ }
3235
+ /**
3236
+ * Gets a single feature flag by key.
3237
+ *
3238
+ * @param key - Feature flag key
3239
+ * @returns Feature flag or null if not found
3240
+ */
3241
+ async getFlagByKey(key) {
3242
+ this.logger.log(`Getting flag by key: ${key}`);
3243
+ return null;
3244
+ }
3245
+ /**
3246
+ * Infers the type of a feature flag value.
3247
+ *
3248
+ * @param value - The flag value
3249
+ * @returns Inferred type string
3250
+ */
3251
+ inferFlagType(value) {
3252
+ if (typeof value === "boolean") return "boolean";
3253
+ if (typeof value === "string") return "string";
3254
+ if (typeof value === "number") return "number";
3255
+ if (typeof value === "object" && value !== null) return "json";
3256
+ return "boolean";
3257
+ }
3258
+ /**
3259
+ * Maps a database row to a FeatureFlag object.
3260
+ * This will be used when @plyaz/db is available.
3261
+ *
3262
+ * @param row - Database row
3263
+ * @returns Mapped feature flag
3264
+ */
3265
+ // private mapRowToFlag(row: any): FeatureFlag {
3266
+ // return {
3267
+ // key: row.key,
3268
+ // name: row.name,
3269
+ // description: row.description,
3270
+ // isEnabled: row.is_enabled,
3271
+ // value: JSON.parse(row.value),
3272
+ // type: row.type,
3273
+ // environment: row.environment,
3274
+ // rolloutPercentage: row.rollout_percentage,
3275
+ // createdAt: row.created_at,
3276
+ // updatedAt: row.updated_at,
3277
+ // createdBy: row.created_by,
3278
+ // updatedBy: row.updated_by,
3279
+ // } satisfies FeatureFlag;
3280
+ // }
3281
+ /**
3282
+ * Maps a database row to a FeatureFlagRule object.
3283
+ * This will be used when @plyaz/db is available.
3284
+ *
3285
+ * @param row - Database row
3286
+ * @returns Mapped feature flag rule
3287
+ */
3288
+ // private mapRowToRule(row: any): FeatureFlagRule {
3289
+ // return {
3290
+ // id: row.id,
3291
+ // flagKey: row.flag_key,
3292
+ // name: row.name,
3293
+ // description: row.description,
3294
+ // isEnabled: row.is_enabled,
3295
+ // priority: row.priority,
3296
+ // conditions: JSON.parse(row.conditions),
3297
+ // value: JSON.parse(row.value),
3298
+ // rolloutPercentage: row.rollout_percentage,
3299
+ // createdAt: row.created_at,
3300
+ // updatedAt: row.updated_at,
3301
+ // createdBy: row.created_by,
3302
+ // updatedBy: row.updated_by,
3303
+ // } satisfies FeatureFlagRule;
3304
+ // }
3305
+ };
3306
+ __name(FeatureFlagRepository, "FeatureFlagRepository");
3307
+ // Constants for time calculations
3308
+ __publicField(FeatureFlagRepository, "ONE_DAY_MS", 864e5);
3309
+ FeatureFlagRepository = __decorateClass([
3310
+ Injectable()
3311
+ ], FeatureFlagRepository);
3312
+
3313
+ // src/backend/featureFlags/feature-flag.module.ts
3314
+ var FeatureFlagModule = class {
3315
+ /**
3316
+ * Static method for importing the module with configuration.
3317
+ * This allows customization of the feature flag system.
3318
+ *
3319
+ * @param options - Module configuration options
3320
+ * @returns Configured module
3321
+ *
3322
+ * @example
3323
+ * ```typescript
3324
+ * @Module({
3325
+ * imports: [
3326
+ * FeatureFlagModule.forRoot({
3327
+ * provider: 'redis',
3328
+ * cacheEnabled: true,
3329
+ * cacheTtl: 600,
3330
+ * })
3331
+ * ],
3332
+ * })
3333
+ * export class AppModule {}
3334
+ * ```
3335
+ */
3336
+ static forRoot(options) {
3337
+ return {
3338
+ module: FeatureFlagModule,
3339
+ providers: [
3340
+ {
3341
+ provide: "FEATURE_FLAG_CONFIG",
3342
+ useValue: options ?? {}
3343
+ },
3344
+ FeatureFlagService,
3345
+ FeatureFlagRepository
3346
+ ],
3347
+ exports: [FeatureFlagService, FeatureFlagRepository]
3348
+ };
3349
+ }
3350
+ /**
3351
+ * Static method for importing the module asynchronously.
3352
+ * Useful when configuration depends on other services.
3353
+ *
3354
+ * @param options - Async module configuration options
3355
+ * @returns Configured async module
3356
+ *
3357
+ * @example
3358
+ * ```typescript
3359
+ * @Module({
3360
+ * imports: [
3361
+ * FeatureFlagModule.forRootAsync({
3362
+ * imports: [ConfigModule],
3363
+ * inject: [ConfigService],
3364
+ * useFactory: (configService: ConfigService) => ({
3365
+ * provider: configService.get('FEATURE_FLAG_PROVIDER'),
3366
+ * cacheEnabled: configService.get('FEATURE_FLAG_CACHE_ENABLED'),
3367
+ * }),
3368
+ * })
3369
+ * ],
3370
+ * })
3371
+ * export class AppModule {}
3372
+ * ```
3373
+ */
3374
+ static forRootAsync(options) {
3375
+ return {
3376
+ module: FeatureFlagModule,
3377
+ imports: options.imports ?? [],
3378
+ providers: [
3379
+ {
3380
+ provide: "FEATURE_FLAG_CONFIG",
3381
+ inject: options.inject ?? [],
3382
+ useFactory: options.useFactory ?? (() => ({}))
3383
+ },
3384
+ FeatureFlagService,
3385
+ FeatureFlagRepository
3386
+ ],
3387
+ exports: [FeatureFlagService, FeatureFlagRepository]
3388
+ };
3389
+ }
3390
+ };
3391
+ __name(FeatureFlagModule, "FeatureFlagModule");
3392
+ FeatureFlagModule = __decorateClass([
3393
+ Global(),
3394
+ Module({
3395
+ controllers: [FeatureFlagController],
3396
+ providers: [FeatureFlagService, FeatureFlagRepository],
3397
+ exports: [FeatureFlagService, FeatureFlagRepository]
3398
+ })
3399
+ ], FeatureFlagModule);
3400
+
3401
+ // src/backend/featureFlags/index.ts
3402
+ function FeatureFlagGuard() {
3403
+ return function(_target, _propertyName, descriptor) {
3404
+ return descriptor;
3405
+ };
3406
+ }
3407
+ __name(FeatureFlagGuard, "FeatureFlagGuard");
3408
+ function FeatureFlag() {
3409
+ return function(_target, _propertyName, descriptor) {
3410
+ return descriptor;
3411
+ };
3412
+ }
3413
+ __name(FeatureFlag, "FeatureFlag");
3414
+ var FeatureFlagContext = createContext(
3415
+ null
3416
+ );
3417
+ function FeatureFlagAppProvider({
3418
+ config,
3419
+ // defaultContext: _defaultContext,
3420
+ children,
3421
+ features,
3422
+ onReady,
3423
+ onError,
3424
+ isShowLoading = false,
3425
+ loadingComponent,
3426
+ errorComponent
3427
+ }) {
3428
+ const [state, setState] = useState({
3429
+ provider: null,
3430
+ isInitialized: false,
3431
+ isLoading: true,
3432
+ error: null,
3433
+ lastUpdated: null
3434
+ });
3435
+ const isMountedRef = useRef(true);
3436
+ const initializeProvider = useCallback(async () => {
3437
+ try {
3438
+ setState((prev) => ({ ...prev, isLoading: true, error: null }));
3439
+ const provider = FeatureFlagProviderFactory.create(config, features);
3440
+ await provider.initialize();
3441
+ if (isMountedRef.current) {
3442
+ setState({
3443
+ provider,
3444
+ isInitialized: true,
3445
+ isLoading: false,
3446
+ error: null,
3447
+ lastUpdated: /* @__PURE__ */ new Date()
3448
+ });
3449
+ onReady?.(provider);
3450
+ }
3451
+ } catch (error) {
3452
+ const errorObj = error instanceof Error ? error : new Error("Failed to initialize feature flags");
3453
+ if (isMountedRef.current) {
3454
+ setState((prev) => ({
3455
+ ...prev,
3456
+ isLoading: false,
3457
+ error: errorObj
3458
+ }));
3459
+ onError?.(errorObj);
3460
+ }
3461
+ }
3462
+ }, [config, onReady, onError]);
3463
+ const refresh = useCallback(async () => {
3464
+ if (!state.provider) {
3465
+ await initializeProvider();
3466
+ return;
3467
+ }
3468
+ try {
3469
+ setState((prev) => ({ ...prev, isLoading: true, error: null }));
3470
+ await state.provider.refresh();
3471
+ if (isMountedRef.current) {
3472
+ setState((prev) => ({
3473
+ ...prev,
3474
+ isLoading: false,
3475
+ lastUpdated: /* @__PURE__ */ new Date()
3476
+ }));
3477
+ }
3478
+ } catch (error) {
3479
+ const errorObj = error instanceof Error ? error : new Error("Failed to refresh feature flags");
3480
+ if (isMountedRef.current) {
3481
+ setState((prev) => ({
3482
+ ...prev,
3483
+ isLoading: false,
3484
+ error: errorObj
3485
+ }));
3486
+ onError?.(errorObj);
3487
+ }
3488
+ }
3489
+ }, [state.provider, initializeProvider, onError]);
3490
+ useEffect(() => {
3491
+ void initializeProvider();
3492
+ }, [initializeProvider]);
3493
+ useEffect(() => {
3494
+ if (!state.provider) return;
3495
+ const unsubscribe = state.provider.subscribe(() => {
3496
+ if (isMountedRef.current) {
3497
+ setState((prev) => ({
3498
+ ...prev,
3499
+ lastUpdated: /* @__PURE__ */ new Date()
3500
+ }));
3501
+ }
3502
+ });
3503
+ return unsubscribe;
3504
+ }, [state.provider]);
3505
+ useEffect(() => {
3506
+ return () => {
3507
+ isMountedRef.current = false;
3508
+ if (state.provider) {
3509
+ state.provider.dispose();
3510
+ }
3511
+ };
3512
+ }, [state.provider]);
3513
+ if (state.isLoading && isShowLoading) {
3514
+ if (loadingComponent) {
3515
+ const LoadingComponent = loadingComponent;
3516
+ return /* @__PURE__ */ jsx(LoadingComponent, {});
3517
+ }
3518
+ return /* @__PURE__ */ jsx("div", { children: "Loading feature flags..." });
3519
+ }
3520
+ if (state.error && errorComponent) {
3521
+ const ErrorComponent = errorComponent;
3522
+ return /* @__PURE__ */ jsx(ErrorComponent, { error: state.error, retry: initializeProvider });
3523
+ }
3524
+ if (!state.provider) {
3525
+ return /* @__PURE__ */ jsx("div", { children: "Feature flag provider not available" });
3526
+ }
3527
+ const contextValue = {
3528
+ provider: state.provider,
3529
+ isInitialized: state.isInitialized,
3530
+ isLoading: state.isLoading,
3531
+ error: state.error,
3532
+ lastUpdated: state.lastUpdated,
3533
+ refresh
3534
+ };
3535
+ return /* @__PURE__ */ jsx(FeatureFlagContext.Provider, { value: contextValue, children });
3536
+ }
3537
+ __name(FeatureFlagAppProvider, "FeatureFlagAppProvider");
3538
+ FeatureFlagAppProvider.displayName = "FeatureFlagAppProvider";
3539
+ function createFeatureFlagProvider(config, options) {
3540
+ return /* @__PURE__ */ __name(function withFeatureFlags(component) {
3541
+ const WrappedComponent = /* @__PURE__ */ __name((props) => /* @__PURE__ */ jsx(
3542
+ FeatureFlagAppProvider,
3543
+ {
3544
+ config,
3545
+ features: options?.features ?? {},
3546
+ ...options,
3547
+ children: React.createElement(component, props)
3548
+ }
3549
+ ), "WrappedComponent");
3550
+ WrappedComponent.displayName = `withFeatureFlags(${component.displayName ?? component.name})`;
3551
+ return WrappedComponent;
3552
+ }, "withFeatureFlags");
3553
+ }
3554
+ __name(createFeatureFlagProvider, "createFeatureFlagProvider");
3555
+ function useFeatureFlagProvider() {
3556
+ const context = useContext(FeatureFlagContext);
3557
+ if (!context) {
3558
+ throw new Error(
3559
+ "useFeatureFlagProvider must be used within a FeatureFlagAppProvider. Make sure to wrap your component tree with <FeatureFlagAppProvider>."
3560
+ );
3561
+ }
3562
+ return context.provider;
3563
+ }
3564
+ __name(useFeatureFlagProvider, "useFeatureFlagProvider");
3565
+ function useFeatureFlagProviderStatus() {
3566
+ const context = useContext(FeatureFlagContext);
3567
+ if (!context) {
3568
+ throw new Error(
3569
+ "useFeatureFlagProviderStatus must be used within a FeatureFlagProvider. Make sure to wrap your component tree with <FeatureFlagAppProvider>."
3570
+ );
3571
+ }
3572
+ return {
3573
+ isInitialized: context.isInitialized,
3574
+ isLoading: context.isLoading,
3575
+ error: context.error,
3576
+ lastUpdated: context.lastUpdated
3577
+ };
3578
+ }
3579
+ __name(useFeatureFlagProviderStatus, "useFeatureFlagProviderStatus");
3580
+ function createEvaluationFunction(params, setState) {
3581
+ const { provider, key, context, defaultValue } = params;
3582
+ return async () => {
3583
+ if (!provider) return;
3584
+ setState((prev) => ({ ...prev, isLoading: true, error: null }));
3585
+ try {
3586
+ const evaluation = await provider.getFlag(key, context);
3587
+ setState((prev) => ({
3588
+ ...prev,
3589
+ value: evaluation?.value ?? defaultValue ?? false,
3590
+ isLoading: false,
3591
+ error: null,
3592
+ evaluation
3593
+ }));
3594
+ } catch (error) {
3595
+ setState((prev) => ({
3596
+ ...prev,
3597
+ value: defaultValue ?? false,
3598
+ isLoading: false,
3599
+ error: error instanceof Error ? error : new Error("Evaluation failed"),
3600
+ evaluation: null
3601
+ }));
3602
+ }
3603
+ };
3604
+ }
3605
+ __name(createEvaluationFunction, "createEvaluationFunction");
3606
+ function useFeatureFlagEvaluation(key, context, defaultValue, provider) {
3607
+ const [state, setState] = useState({
3608
+ value: defaultValue ?? false,
3609
+ isLoading: true,
3610
+ error: null,
3611
+ evaluation: null
3612
+ });
3613
+ const evaluateFlag = useCallback(
3614
+ createEvaluationFunction({ provider, key, context, defaultValue }, setState),
3615
+ [provider, key, context, defaultValue]
3616
+ );
3617
+ useEffect(() => {
3618
+ const timer = setTimeout(() => {
3619
+ void evaluateFlag();
3620
+ }, 0);
3621
+ return () => clearTimeout(timer);
3622
+ }, [evaluateFlag]);
3623
+ return {
3624
+ value: state.value,
3625
+ isLoading: state.isLoading,
3626
+ error: state.error,
3627
+ refresh: evaluateFlag
3628
+ };
3629
+ }
3630
+ __name(useFeatureFlagEvaluation, "useFeatureFlagEvaluation");
3631
+ function useMultipleFeatureFlagsEvaluation(keys, context, defaultValue, provider) {
3632
+ const [flagStates, setFlagStates] = useState(() => {
3633
+ const initialStates = {};
3634
+ keys.forEach((key) => {
3635
+ initialStates[key] = {
3636
+ value: defaultValue,
3637
+ isLoading: true,
3638
+ error: null,
3639
+ evaluation: null
3640
+ };
3641
+ });
3642
+ return initialStates;
3643
+ });
3644
+ const evaluateFlags = useCallback(async () => {
3645
+ if (!provider) return;
3646
+ setFlagStates((prevStates) => {
3647
+ const newStates = { ...prevStates };
3648
+ keys.forEach((key) => {
3649
+ newStates[key] = {
3650
+ ...newStates[key],
3651
+ isLoading: true,
3652
+ error: null
3653
+ };
3654
+ });
3655
+ return newStates;
3656
+ });
3657
+ const evaluations = await Promise.allSettled(
3658
+ keys.map(async (key) => {
3659
+ try {
3660
+ const evaluation = await provider.getFlag(key, context);
3661
+ return { key, evaluation, error: null };
3662
+ } catch (error) {
3663
+ return {
3664
+ key,
3665
+ evaluation: null,
3666
+ error: error instanceof Error ? error : new Error("Evaluation failed")
3667
+ };
3668
+ }
3669
+ })
3670
+ );
3671
+ setFlagStates((prevStates) => {
3672
+ const newStates = { ...prevStates };
3673
+ evaluations.forEach((result, index) => {
3674
+ const key = keys[index];
3675
+ if (result.status === "fulfilled") {
3676
+ const { evaluation, error } = result.value;
3677
+ newStates[key] = {
3678
+ value: evaluation?.value ?? defaultValue,
3679
+ isLoading: false,
3680
+ error,
3681
+ evaluation
3682
+ };
3683
+ }
3684
+ });
3685
+ return newStates;
3686
+ });
3687
+ }, [keys, provider, context, defaultValue]);
3688
+ useEffect(() => {
3689
+ const timer = setTimeout(() => {
3690
+ void evaluateFlags();
3691
+ }, 0);
3692
+ return () => clearTimeout(timer);
3693
+ }, [evaluateFlags]);
3694
+ const refreshFunctions = useMemo(() => {
3695
+ const refreshFns = {};
3696
+ keys.forEach((key) => {
3697
+ refreshFns[key] = async () => {
3698
+ if (!provider) return;
3699
+ setFlagStates((prev) => ({
3700
+ ...prev,
3701
+ [key]: { ...prev[key], isLoading: true, error: null }
3702
+ }));
3703
+ try {
3704
+ const evaluation = await provider.getFlag(key, context);
3705
+ setFlagStates((prev) => ({
3706
+ ...prev,
3707
+ [key]: {
3708
+ value: evaluation?.value ?? defaultValue,
3709
+ isLoading: false,
3710
+ error: null,
3711
+ evaluation
3712
+ }
3713
+ }));
3714
+ } catch (error) {
3715
+ setFlagStates((prev) => ({
3716
+ ...prev,
3717
+ [key]: {
3718
+ value: defaultValue,
3719
+ isLoading: false,
3720
+ error: error instanceof Error ? error : new Error("Evaluation failed"),
3721
+ evaluation: null
3722
+ }
3723
+ }));
3724
+ }
3725
+ };
3726
+ });
3727
+ return refreshFns;
3728
+ }, [keys, provider, context, defaultValue]);
3729
+ return useMemo(() => {
3730
+ const result = {};
3731
+ keys.forEach((key) => {
3732
+ const state = flagStates[key] ?? {
3733
+ value: defaultValue,
3734
+ isLoading: true,
3735
+ error: null};
3736
+ result[key] = {
3737
+ value: state.value,
3738
+ isLoading: state.isLoading,
3739
+ error: state.error,
3740
+ refresh: refreshFunctions[key] ?? (async () => {
3741
+ })
3742
+ };
3743
+ });
3744
+ return result;
3745
+ }, [keys, flagStates, refreshFunctions, defaultValue]);
3746
+ }
3747
+ __name(useMultipleFeatureFlagsEvaluation, "useMultipleFeatureFlagsEvaluation");
3748
+
3749
+ // src/frontend/featureFlags/hooks/useFeatureFlag.ts
3750
+ function useFeatureFlag(key, options = {}) {
3751
+ const { context, isAutoRefresh = true, defaultValue, isSuspense = false } = options;
3752
+ const provider = useFeatureFlagProvider();
3753
+ const {
3754
+ value,
3755
+ isLoading,
3756
+ error,
3757
+ refresh: evaluateFlag
3758
+ } = useFeatureFlagEvaluation(
3759
+ key,
3760
+ context,
3761
+ defaultValue,
3762
+ provider
3763
+ );
3764
+ const refresh = useCallback(async () => {
3765
+ await evaluateFlag();
3766
+ }, [evaluateFlag]);
3767
+ useEffect(() => {
3768
+ if (!provider || !isAutoRefresh) return;
3769
+ const unsubscribe = provider.subscribe(() => {
3770
+ void evaluateFlag();
3771
+ });
3772
+ return unsubscribe;
3773
+ }, [provider, isAutoRefresh, evaluateFlag]);
3774
+ if (isSuspense && isLoading) {
3775
+ throw evaluateFlag();
3776
+ }
3777
+ return {
3778
+ value,
3779
+ isLoading,
3780
+ error,
3781
+ refresh
3782
+ };
3783
+ }
3784
+ __name(useFeatureFlag, "useFeatureFlag");
3785
+ function useFeatureFlagEnabled(key, options = {}) {
3786
+ const { value } = useFeatureFlag(key, {
3787
+ ...options,
3788
+ defaultValue: options.defaultValue ?? false
3789
+ });
3790
+ return value;
3791
+ }
3792
+ __name(useFeatureFlagEnabled, "useFeatureFlagEnabled");
3793
+ function useFeatureFlagValue(key, options = {}) {
3794
+ const { value } = useFeatureFlag(key, options);
3795
+ return value;
3796
+ }
3797
+ __name(useFeatureFlagValue, "useFeatureFlagValue");
3798
+ function useMultipleFeatureFlags(keys, options = {}) {
3799
+ const provider = useFeatureFlagProvider();
3800
+ const { context, isAutoRefresh = true, defaultValue = false } = options;
3801
+ const flagStates = useMultipleFeatureFlagsEvaluation(keys, context, defaultValue, provider);
3802
+ useEffect(() => {
3803
+ if (!provider || !isAutoRefresh) return;
3804
+ const unsubscribe = provider.subscribe(() => {
3805
+ keys.forEach((key) => {
3806
+ if (typeof flagStates[key]?.refresh === "function") {
3807
+ void flagStates[key].refresh();
3808
+ }
3809
+ });
3810
+ });
3811
+ return unsubscribe;
3812
+ }, [provider, isAutoRefresh, flagStates, keys]);
3813
+ return flagStates;
3814
+ }
3815
+ __name(useMultipleFeatureFlags, "useMultipleFeatureFlags");
3816
+ function useFeatureFlagHelpers() {
3817
+ const provider = useFeatureFlagProvider();
3818
+ const setOverride = useCallback(
3819
+ (key, value) => {
3820
+ provider.setOverride(key, value);
3821
+ },
3822
+ [provider]
3823
+ );
3824
+ const removeOverride = useCallback(
3825
+ (key) => {
3826
+ provider.removeOverride(key);
3827
+ },
3828
+ [provider]
3829
+ );
3830
+ const clearOverrides = useCallback(() => {
3831
+ provider.clearOverrides();
3832
+ }, [provider]);
3833
+ const refresh = useCallback(async () => {
3834
+ await provider.refresh();
3835
+ }, [provider]);
3836
+ const getMultipleFlags = useCallback(
3837
+ async (keys, context) => {
3838
+ const results = {};
3839
+ await Promise.all(
3840
+ keys.map(async (key) => {
3841
+ const evaluation = await provider.getFlag(key, context);
3842
+ results[key] = evaluation.value;
3843
+ })
3844
+ );
3845
+ return results;
3846
+ },
3847
+ [provider]
3848
+ );
3849
+ const isAnyEnabled = useCallback(
3850
+ async (keys, context) => {
3851
+ const evaluations = await Promise.all(keys.map((key) => provider.isEnabled(key, context)));
3852
+ return evaluations.some((enabled) => enabled);
3853
+ },
3854
+ [provider]
3855
+ );
3856
+ const isAllEnabled = useCallback(
3857
+ async (keys, context) => {
3858
+ const evaluations = await Promise.all(keys.map((key) => provider.isEnabled(key, context)));
3859
+ return evaluations.every((enabled) => enabled);
3860
+ },
3861
+ [provider]
3862
+ );
3863
+ const whenEnabled = useCallback(
3864
+ async (key, callback, fallback, context) => {
3865
+ const isEnabled = await provider.isEnabled(key, context);
3866
+ if (isEnabled) {
3867
+ return callback();
3868
+ } else if (fallback) {
3869
+ return fallback();
3870
+ }
3871
+ return void 0;
3872
+ },
3873
+ [provider]
3874
+ );
3875
+ return useMemo(
3876
+ () => ({
3877
+ setOverride,
3878
+ removeOverride,
3879
+ clearOverrides,
3880
+ refresh,
3881
+ getMultipleFlags,
3882
+ isAnyEnabled,
3883
+ isAllEnabled,
3884
+ whenEnabled
3885
+ }),
3886
+ [
3887
+ setOverride,
3888
+ removeOverride,
3889
+ clearOverrides,
3890
+ refresh,
3891
+ getMultipleFlags,
3892
+ isAnyEnabled,
3893
+ isAllEnabled,
3894
+ whenEnabled
3895
+ ]
3896
+ );
3897
+ }
3898
+ __name(useFeatureFlagHelpers, "useFeatureFlagHelpers");
3899
+
3900
+ export { ApiFeatureFlagProvider, CacheManager, ConditionUtils, ContextUtils, DEFAULT_FEATURE_FLAG_CONFIG, DatabaseFeatureFlagProvider, FeatureFlag, FeatureFlagAppProvider, FeatureFlagContext, FeatureFlagContextBuilder, FeatureFlagController, FeatureFlagEngine, FeatureFlagGuard, FeatureFlagModule, FeatureFlagProvider, FeatureFlagProviderFactory, FeatureFlagRepository, FeatureFlagService, FeatureFlagSystem, FileFeatureFlagProvider, HashUtils, MemoryFeatureFlagProvider, RedisFeatureFlagProvider, ValueUtils, createBackendContext, createFeatureFlagProvider, createFrontendContext, createRolloutIdentifier, evaluateArrayOperator, evaluateConditionOperator, evaluateEqualityOperator, evaluateNumericOperator, evaluateStringOperator, hashString, isArrayOperator, isEqualityOperator, isInRollout, isNumericOperator, isStringOperator, isTruthy, toBoolean, useFeatureFlag, useFeatureFlagEnabled, useFeatureFlagHelpers, useFeatureFlagProvider, useFeatureFlagProviderStatus, useFeatureFlagValue, useMultipleFeatureFlags };
3901
+ //# sourceMappingURL=index.mjs.map
3902
+ //# sourceMappingURL=index.mjs.map