Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generics for InMemoryProvider Flag config #1046

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 === '[email protected]') {
return 'bye';
}
return 'hi';
},
};
Comment on lines +36 to 53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was there for testing?


export type FlagConfiguration = Record<string, Flag>;