Skip to content

Commit

Permalink
feat: add client context mutation (open-feature#207)
Browse files Browse the repository at this point in the history
Adding this for consistency with other SDKs.

Signed-off-by: Todd Baert <[email protected]>
  • Loading branch information
toddbaert authored Sep 8, 2022
1 parent 2144138 commit 5b0442a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type OpenFeatureClientOptions = {

export class OpenFeatureClient implements Client {
readonly metadata: ClientMetadata;
readonly context: EvaluationContext;
private _context: EvaluationContext;
private _hooks: Hook[] = [];

constructor(
Expand All @@ -35,7 +35,15 @@ export class OpenFeatureClient implements Client {
name: options.name,
version: options.version,
} as const;
this.context = context;
this._context = context;
}

set context (context: EvaluationContext) {
this._context = context;
}

get context (): EvaluationContext {
return this._context;
}

addHooks(...hooks: Hook<FlagValue>[]): void {
Expand Down Expand Up @@ -159,7 +167,7 @@ export class OpenFeatureClient implements Client {
// merge global and client contexts
const mergedContext = {
...OpenFeature.context,
...this.context,
...this._context,
...invocationContext,
};

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export type EvaluationDetails<T extends FlagValue> = {

export interface Client extends EvaluationLifeCycle, Features {
readonly metadata: ClientMetadata;
context: EvaluationContext;
}

export type HookHints = Readonly<Record<string, unknown>>;
Expand Down
10 changes: 10 additions & 0 deletions test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,5 +407,15 @@ describe(OpenFeatureClient.name, () => {
);
});
});

describe('client evaluation context', () => {
it('can be mutated', async () => {
const KEY = 'key';
const VAL = 'val';
const client = OpenFeature.getClient();
client.context = { [KEY]: VAL };
expect(client.context[KEY]).toEqual(VAL);
});
});
});
});

0 comments on commit 5b0442a

Please sign in to comment.