|
1 | 1 | import { DEFAULT_MAX_CACHE_SIZE } from './constants'; |
| 2 | +import { EvaluationContext } from "@openfeature/core"; |
2 | 3 |
|
3 | 4 | export type CacheOption = 'lru' | 'disabled'; |
4 | 5 | export type ResolverType = 'rpc' | 'in-process'; |
@@ -83,20 +84,36 @@ export interface Config { |
83 | 84 | defaultAuthority?: string; |
84 | 85 | } |
85 | 86 |
|
86 | | -export type FlagdProviderOptions = Partial<Config>; |
| 87 | +interface FlagdConfig extends Config { |
87 | 88 |
|
88 | | -const DEFAULT_CONFIG: Omit<Config, 'port' | 'resolverType'> = { |
| 89 | + /** |
| 90 | + * Function providing an EvaluationContext to mix into every evaluation. |
| 91 | + * The syncContext from the SyncFlagsResponse |
| 92 | + * (https://buf.build/open-feature/flagd/docs/main:flagd.sync.v1#flagd.sync.v1.SyncFlagsResponse), |
| 93 | + * represented as a {@link dev.openfeature.sdk.Structure}, is passed as an argument. |
| 94 | + * |
| 95 | + * This function runs every time the provider (re)connects, and its result is cached and used in every evaluation. |
| 96 | + * By default, the entire sync response (converted to a Structure) is used. |
| 97 | + */ |
| 98 | + contextEnricher: (syncContext: {[key: string]: string} | null) => EvaluationContext |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +export type FlagdProviderOptions = Partial<FlagdConfig>; |
| 103 | + |
| 104 | +const DEFAULT_CONFIG: Omit<FlagdConfig, 'port' | 'resolverType'> = { |
89 | 105 | deadlineMs: 500, |
90 | 106 | host: 'localhost', |
91 | 107 | tls: false, |
92 | 108 | selector: '', |
93 | 109 | cache: 'lru', |
94 | 110 | maxCacheSize: DEFAULT_MAX_CACHE_SIZE, |
| 111 | + contextEnricher: (syncContext: {[key: string]: string} | null) => syncContext as EvaluationContext |
95 | 112 | }; |
96 | 113 |
|
97 | | -const DEFAULT_RPC_CONFIG: Config = { ...DEFAULT_CONFIG, resolverType: 'rpc', port: 8013 }; |
| 114 | +const DEFAULT_RPC_CONFIG: FlagdConfig = { ...DEFAULT_CONFIG, resolverType: 'rpc', port: 8013 }; |
98 | 115 |
|
99 | | -const DEFAULT_IN_PROCESS_CONFIG: Config = { ...DEFAULT_CONFIG, resolverType: 'in-process', port: 8015 }; |
| 116 | +const DEFAULT_IN_PROCESS_CONFIG: FlagdConfig = { ...DEFAULT_CONFIG, resolverType: 'in-process', port: 8015 }; |
100 | 117 |
|
101 | 118 | enum ENV_VAR { |
102 | 119 | FLAGD_HOST = 'FLAGD_HOST', |
@@ -167,7 +184,7 @@ const getEnvVarConfig = (): Partial<Config> => { |
167 | 184 | }; |
168 | 185 | }; |
169 | 186 |
|
170 | | -export function getConfig(options: FlagdProviderOptions = {}) { |
| 187 | +export function getConfig(options: FlagdProviderOptions = {}): FlagdConfig { |
171 | 188 | const envVarConfig = getEnvVarConfig(); |
172 | 189 | const defaultConfig = |
173 | 190 | options.resolverType == 'in-process' || envVarConfig.resolverType == 'in-process' |
|
0 commit comments