From e97a23edd0dfc255e7d4aa96f6223671410c6453 Mon Sep 17 00:00:00 2001
From: Will Chou <w.chou06@gmail.com>
Date: Sat, 19 Oct 2024 09:13:30 -0400
Subject: [PATCH] generic for Flag config

---
 .../in-memory-provider/flag-configuration.ts  | 27 ++++++++++++++++---
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/packages/web/src/provider/in-memory-provider/flag-configuration.ts b/packages/web/src/provider/in-memory-provider/flag-configuration.ts
index 05f507d44..6ab05b9ba 100644
--- a/packages/web/src/provider/in-memory-provider/flag-configuration.ts
+++ b/packages/web/src/provider/in-memory-provider/flag-configuration.ts
@@ -10,15 +10,15 @@ type Variants<T> = Record<string, T>;
 /**
  * A Feature Flag definition, containing it's specification
  */
-export type Flag = {
+export type Flag<V = Variants<boolean> | Variants<string> | Variants<number> | Variants<JsonValue>> = {
   /**
    * An object containing all possible flags mappings (variant -> flag value)
    */
-  variants: Variants<boolean> | Variants<string> | Variants<number> | Variants<JsonValue>;
+  variants: V;
   /**
    * The variant it will resolve to in STATIC evaluation
    */
-  defaultVariant: string;
+  defaultVariant: keyof V;
   /**
    * Determines if flag evaluation is enabled or not for this flag.
    * If false, falls back to the default value provided to the client
@@ -30,7 +30,26 @@ export type Flag = {
    * If it does not return a valid variant it falls back to the default value provided to the client
    * @param EvaluationContext
    */
-  contextEvaluator?: (ctx: EvaluationContext) => string;
+  contextEvaluator?: (ctx: EvaluationContext) => keyof V;
+};
+
+// sample
+const flag: Flag<{
+  hi: boolean,
+  bye: boolean,
+}> = {
+  variants: {
+    'hi': true,
+    bye: false,
+  },
+  disabled: false,
+  defaultVariant: 'hi',
+  contextEvaluator: (ctx: EvaluationContext) => {
+    if (ctx.user === 'bob@flags.com') {
+      return 'bye';
+    }
+    return 'hi';
+  },
 };
 
 export type FlagConfiguration = Record<string, Flag>;