1- import { z } from 'zod' ;
2- import { AwsRegion } from '../utils/Region' ;
3- import { AndFeatureFlag , LocalHostTargetedFeatureFlag } from './CombinedFeatureFlags' ;
4- import { FleetTargetedFeatureFlag , StaticFeatureFlag } from './FeatureFlag' ;
1+ import { CompoundFeatureFlag } from './CombinedFeatureFlags' ;
2+ import { StaticFeatureFlag } from './FeatureFlag' ;
3+ import { buildLocalHost , buildRegional , FeatureFlagConfigSchema , FeatureFlagConfigType } from './FeatureFlagBuilder' ;
54import { Describable , FeatureFlag , TargetedFeatureFlag } from './FeatureFlagI' ;
65
76export class FeatureFlagConfig implements Describable {
8- private readonly EnhancedDryRun : FeatureFlag ;
9- private readonly AnotherFeature : FeatureFlag ;
7+ private readonly StaticFlag = new StaticFeatureFlag ( 'TestFlag' , false ) ; // Here to generate types
8+ private readonly EnhancedDryRun : TargetedFeatureFlag < string > ;
109
1110 private readonly describables : Describable [ ] ;
1211
1312 constructor ( config ?: unknown ) {
14- let features : Record < string , FeatureFlagType > ;
13+ let features : Record < string , FeatureFlagConfigType > ;
1514
1615 if ( config ) {
1716 const parsed = FeatureFlagConfigSchema . parse ( config ) ;
@@ -20,17 +19,19 @@ export class FeatureFlagConfig implements Describable {
2019 features = { } ;
2120 }
2221
23- this . EnhancedDryRun = buildStatic ( 'EnhancedDryRun' , features ) ;
24- this . AnotherFeature = buildLocalHost ( 'AnotherFeature' , features ) ;
22+ this . EnhancedDryRun = new CompoundFeatureFlag (
23+ buildLocalHost ( 'EnhancedDryRun' , features [ 'EnhancedDryRun' ] ) ,
24+ buildRegional ( 'EnhancedDryRun' , features [ 'EnhancedDryRun' ] ) ,
25+ ) ;
2526
26- this . describables = [ this . EnhancedDryRun , this . AnotherFeature ] ;
27+ this . describables = [ this . EnhancedDryRun ] ;
2728 }
2829
2930 get ( key : FeatureFlagConfigKey ) : FeatureFlag {
3031 return this [ key ] ;
3132 }
3233
33- getTargeted < T > ( key : FeatureFlagConfigKey ) : TargetedFeatureFlag < T > {
34+ getTargeted ( key : TargetedFeatureFlagConfigKey ) : TargetedFeatureFlag < unknown > {
3435 return this [ key ] ;
3536 }
3637
@@ -51,40 +52,5 @@ export class FeatureFlagConfig implements Describable {
5152 }
5253}
5354
54- export type FeatureFlagConfigKey = 'EnhancedDryRun' | 'AnotherFeature' ;
55-
56- const FeatureFlagSchema = z . object ( {
57- enabled : z . boolean ( ) ,
58- fleetPercentage : z . number ( ) . optional ( ) ,
59- allowlistedRegions : z . array ( z . enum ( Object . values ( AwsRegion ) ) ) . optional ( ) ,
60- } ) ;
61-
62- const FeatureFlagConfigSchema = z . object ( {
63- version : z . number ( ) ,
64- description : z . string ( ) ,
65- features : z . record ( z . string ( ) , FeatureFlagSchema ) ,
66- } ) ;
67- type FeatureFlagType = z . infer < typeof FeatureFlagSchema > ;
68-
69- function buildStatic ( name : string , features : Record < string , FeatureFlagType > ) {
70- let enabled = false ;
71-
72- if ( features [ name ] !== undefined ) {
73- enabled = features [ name ] . enabled ;
74- }
75-
76- return new StaticFeatureFlag ( name , enabled ) ;
77- }
78-
79- function buildLocalHost ( name : string , features : Record < string , FeatureFlagType > ) {
80- let pct = 0 ;
81-
82- if ( features [ name ] ?. fleetPercentage !== undefined ) {
83- pct = features [ name ] . fleetPercentage ;
84- }
85-
86- return new AndFeatureFlag (
87- buildStatic ( name , features ) ,
88- new LocalHostTargetedFeatureFlag ( new FleetTargetedFeatureFlag ( name , pct ) ) ,
89- ) ;
90- }
55+ export type TargetedFeatureFlagConfigKey = 'EnhancedDryRun' ;
56+ export type FeatureFlagConfigKey = 'StaticFlag' ;
0 commit comments