Skip to content

Commit 5b0442a

Browse files
authored
feat: add client context mutation (#207)
Adding this for consistency with other SDKs. Signed-off-by: Todd Baert <[email protected]>
1 parent 2144138 commit 5b0442a

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/client.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type OpenFeatureClientOptions = {
2121

2222
export class OpenFeatureClient implements Client {
2323
readonly metadata: ClientMetadata;
24-
readonly context: EvaluationContext;
24+
private _context: EvaluationContext;
2525
private _hooks: Hook[] = [];
2626

2727
constructor(
@@ -35,7 +35,15 @@ export class OpenFeatureClient implements Client {
3535
name: options.name,
3636
version: options.version,
3737
} as const;
38-
this.context = context;
38+
this._context = context;
39+
}
40+
41+
set context (context: EvaluationContext) {
42+
this._context = context;
43+
}
44+
45+
get context (): EvaluationContext {
46+
return this._context;
3947
}
4048

4149
addHooks(...hooks: Hook<FlagValue>[]): void {
@@ -159,7 +167,7 @@ export class OpenFeatureClient implements Client {
159167
// merge global and client contexts
160168
const mergedContext = {
161169
...OpenFeature.context,
162-
...this.context,
170+
...this._context,
163171
...invocationContext,
164172
};
165173

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ export type EvaluationDetails<T extends FlagValue> = {
211211

212212
export interface Client extends EvaluationLifeCycle, Features {
213213
readonly metadata: ClientMetadata;
214+
context: EvaluationContext;
214215
}
215216

216217
export type HookHints = Readonly<Record<string, unknown>>;

test/client.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,5 +407,15 @@ describe(OpenFeatureClient.name, () => {
407407
);
408408
});
409409
});
410+
411+
describe('client evaluation context', () => {
412+
it('can be mutated', async () => {
413+
const KEY = 'key';
414+
const VAL = 'val';
415+
const client = OpenFeature.getClient();
416+
client.context = { [KEY]: VAL };
417+
expect(client.context[KEY]).toEqual(VAL);
418+
});
419+
});
410420
});
411421
});

0 commit comments

Comments
 (0)