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