Skip to content

Commit d96ada4

Browse files
committed
Add optional enrichContext for SyncContext
Signed-off-by: marcozabel <[email protected]>
1 parent f365db9 commit d96ada4

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import {Hook} from "@openfeature/web-sdk";
2+
import { EvaluationContext } from "@openfeature/server-sdk";
3+
import { BeforeHookContext, HookHints } from "@openfeature/core";
24

35
export class SyncMetadataHook implements Hook<any> {
4-
contextSupplier: () => { [key: string]: any } | null;
6+
enrichedContext: () => EvaluationContext;
57

6-
constructor(getContext: () => { [key: string]: any } | null) {
7-
this.contextSupplier = getContext;
8+
constructor(enrichedContext: () => EvaluationContext) {
9+
this.enrichedContext = enrichedContext
810
}
911

10-
public before(): any {
11-
return this.contextSupplier();
12+
public before(hookContext: BeforeHookContext, hookHints?: HookHints): EvaluationContext {
13+
return this.enrichedContext.apply(this);
1214
}
1315
}

libs/providers/flagd/src/lib/configuration.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DEFAULT_MAX_CACHE_SIZE } from './constants';
2+
import { EvaluationContext } from "@openfeature/core";
23

34
export type CacheOption = 'lru' | 'disabled';
45
export type ResolverType = 'rpc' | 'in-process';
@@ -83,20 +84,36 @@ export interface Config {
8384
defaultAuthority?: string;
8485
}
8586

86-
export type FlagdProviderOptions = Partial<Config>;
87+
interface FlagdConfig extends Config {
8788

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'> = {
89105
deadlineMs: 500,
90106
host: 'localhost',
91107
tls: false,
92108
selector: '',
93109
cache: 'lru',
94110
maxCacheSize: DEFAULT_MAX_CACHE_SIZE,
111+
contextEnricher: (syncContext: {[key: string]: string} | null) => syncContext as EvaluationContext
95112
};
96113

97-
const DEFAULT_RPC_CONFIG: Config = { ...DEFAULT_CONFIG, resolverType: 'rpc', port: 8013 };
114+
const DEFAULT_RPC_CONFIG: FlagdConfig = { ...DEFAULT_CONFIG, resolverType: 'rpc', port: 8013 };
98115

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 };
100117

101118
enum ENV_VAR {
102119
FLAGD_HOST = 'FLAGD_HOST',
@@ -167,7 +184,7 @@ const getEnvVarConfig = (): Partial<Config> => {
167184
};
168185
};
169186

170-
export function getConfig(options: FlagdProviderOptions = {}) {
187+
export function getConfig(options: FlagdProviderOptions = {}): FlagdConfig {
171188
const envVarConfig = getEnvVarConfig();
172189
const defaultConfig =
173190
options.resolverType == 'in-process' || envVarConfig.resolverType == 'in-process'

libs/providers/flagd/src/lib/flagd-provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class FlagdProvider implements Provider {
1313
name: 'flagd',
1414
};
1515

16-
readonly hooks: Hook[] = [];
16+
readonly hooks?: Hook[];
1717
readonly runsOn = 'server';
1818
readonly events = new OpenFeatureEventEmitter();
1919
private syncContext: { [key: string]: string } | null = null;
@@ -39,7 +39,7 @@ export class FlagdProvider implements Provider {
3939
this._service = new InProcessService(config, this.setSyncContext, undefined, logger);
4040

4141
if (config?.offlineFlagSourcePath === undefined) {
42-
this.hooks.push(new SyncMetadataHook(this.getSyncContext));
42+
this.hooks = [new SyncMetadataHook(() => config.contextEnricher(this.getSyncContext()))];
4343
}
4444
} else {
4545
this._service = new GRPCService(config, undefined, logger);

0 commit comments

Comments
 (0)