Skip to content

Commit

Permalink
fix!: remove context from client interfaces (open-feature#373)
Browse files Browse the repository at this point in the history
This PR removes the context from the client's API (as per draft spec).

---------

Signed-off-by: Todd Baert <toddbaert@gmail.com>
  • Loading branch information
toddbaert authored Mar 13, 2023
1 parent 6478a8e commit a692a32
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 57 deletions.
39 changes: 10 additions & 29 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SafeLogger } from '@openfeature/shared';
import {
ApiEvents,
ClientMetadata,
ErrorCode, EvaluationContext,
ErrorCode,
EvaluationDetails,
FlagValue,
FlagValueType,
Expand All @@ -30,7 +30,6 @@ type HandlerWrapper = {

export class OpenFeatureClient implements Client {
readonly metadata: ClientMetadata;
private _context: EvaluationContext;
private _hooks: Hook[] = [];
private _clientLogger?: Logger;
private _handlerWrappers: HandlerWrapper[] = [];
Expand All @@ -41,13 +40,11 @@ export class OpenFeatureClient implements Client {
private readonly providerAccessor: () => Provider & Partial<EventProvider>,
private readonly globalLogger: () => Logger,
options: OpenFeatureClientOptions,
context: EvaluationContext = {}
) {
this.metadata = {
name: options.name,
version: options.version,
} as const;
this._context = context;

this.attachListeners();
window.dispatchEvent(new CustomEvent(ApiEvents.ProviderChanged));
Expand Down Expand Up @@ -83,41 +80,36 @@ export class OpenFeatureClient implements Client {
getBooleanValue(
flagKey: string,
defaultValue: boolean,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): boolean {
return this.getBooleanDetails(flagKey, defaultValue, context, options).value;
return this.getBooleanDetails(flagKey, defaultValue, options).value;
}

getBooleanDetails(
flagKey: string,
defaultValue: boolean,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): EvaluationDetails<boolean> {
return this.evaluate<boolean>(
flagKey,
this._provider.resolveBooleanEvaluation,
defaultValue,
'boolean',
context,
options
);
}

getStringValue<T extends string = string>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): T {
return this.getStringDetails<T>(flagKey, defaultValue, context, options).value;
return this.getStringDetails<T>(flagKey, defaultValue, options).value;
}

getStringDetails<T extends string = string>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): EvaluationDetails<T> {
return this.evaluate<T>(
Expand All @@ -126,24 +118,21 @@ export class OpenFeatureClient implements Client {
this._provider.resolveStringEvaluation as () => EvaluationDetails<T>,
defaultValue,
'string',
context,
options
);
}

getNumberValue<T extends number = number>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): T {
return this.getNumberDetails(flagKey, defaultValue, context, options).value;
return this.getNumberDetails(flagKey, defaultValue, options).value;
}

getNumberDetails<T extends number = number>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): EvaluationDetails<T> {
return this.evaluate<T>(
Expand All @@ -152,40 +141,35 @@ export class OpenFeatureClient implements Client {
this._provider.resolveNumberEvaluation as () => EvaluationDetails<T>,
defaultValue,
'number',
context,
options
);
}

getObjectValue<T extends JsonValue = JsonValue>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): T {
return this.getObjectDetails(flagKey, defaultValue, context, options).value;
return this.getObjectDetails(flagKey, defaultValue, options).value;
}

getObjectDetails<T extends JsonValue = JsonValue>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): EvaluationDetails<T> {
return this.evaluate<T>(flagKey, this._provider.resolveObjectEvaluation, defaultValue, 'object', context, options);
return this.evaluate<T>(flagKey, this._provider.resolveObjectEvaluation, defaultValue, 'object', options);
}

private evaluate<T extends FlagValue>(
flagKey: string,
resolver: (
flagKey: string,
defaultValue: T,
context: EvaluationContext,
logger: Logger
) => ResolutionDetails<T>,
defaultValue: T,
flagType: FlagValueType,
invocationContext: EvaluationContext = {},
options: FlagEvaluationOptions = {}
): EvaluationDetails<T> {
// merge global, client, and evaluation context
Expand All @@ -199,11 +183,8 @@ export class OpenFeatureClient implements Client {
const allHooksReversed = [...allHooks].reverse();

// merge global and client contexts
const mergedContext = {
const context = {
...OpenFeature.getContext(),
...OpenFeature.getTransactionContext(),
...this._context,
...invocationContext,
};

// this reference cannot change during the course of evaluation
Expand All @@ -214,15 +195,15 @@ export class OpenFeatureClient implements Client {
flagValueType: flagType,
clientMetadata: this.metadata,
providerMetadata: OpenFeature.providerMetadata,
context: mergedContext,
context,
logger: this._logger,
};

try {
const frozenContext = this.beforeHooks(allHooks, hookContext, options);
this.beforeHooks(allHooks, hookContext, options);

// run the referenced resolver, binding the provider.
const resolution = resolver.call(this._provider, flagKey, defaultValue, frozenContext, this._logger);
const resolution = resolver.call(this._provider, flagKey, defaultValue, this._logger);

const evaluationDetails = {
...resolution,
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/open-feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ export class OpenFeatureAPI extends OpenFeatureCommonAPI {
await this?._provider?.onClose?.();
}

getClient(name?: string, version?: string, context?: EvaluationContext): Client {
getClient(name?: string, version?: string): Client {
return new OpenFeatureClient(
() => this._provider,
() => this._logger,
{ name, version },
context
);
}
}
Expand Down
26 changes: 0 additions & 26 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export interface Provider extends CommonProvider {
resolveBooleanEvaluation(
flagKey: string,
defaultValue: boolean,
context: EvaluationContext,
logger: Logger
): ResolutionDetails<boolean>;

Expand All @@ -55,7 +54,6 @@ export interface Provider extends CommonProvider {
resolveStringEvaluation(
flagKey: string,
defaultValue: string,
context: EvaluationContext,
logger: Logger
): ResolutionDetails<string>;

Expand All @@ -65,7 +63,6 @@ export interface Provider extends CommonProvider {
resolveNumberEvaluation(
flagKey: string,
defaultValue: number,
context: EvaluationContext,
logger: Logger
): ResolutionDetails<number>;

Expand All @@ -75,7 +72,6 @@ export interface Provider extends CommonProvider {
resolveObjectEvaluation<T extends JsonValue>(
flagKey: string,
defaultValue: T,
context: EvaluationContext,
logger: Logger
): ResolutionDetails<T>;
}
Expand Down Expand Up @@ -166,14 +162,12 @@ export interface Features {
*
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @param {boolean} defaultValue The value returned if an error occurs
* @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
* @param {FlagEvaluationOptions} options Additional flag evaluation options
* @returns {boolean} Flag evaluation response
*/
getBooleanValue(
flagKey: string,
defaultValue: boolean,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): boolean;

Expand All @@ -182,14 +176,12 @@ export interface Features {
*
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @param {boolean} defaultValue The value returned if an error occurs
* @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
* @param {FlagEvaluationOptions} options Additional flag evaluation options
* @returns {EvaluationDetails<boolean>} Flag evaluation details response
*/
getBooleanDetails(
flagKey: string,
defaultValue: boolean,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): EvaluationDetails<boolean>;

Expand All @@ -199,20 +191,17 @@ export interface Features {
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @template {string} T A optional generic argument constraining the string
* @param {T} defaultValue The value returned if an error occurs
* @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
* @param {FlagEvaluationOptions} options Additional flag evaluation options
* @returns {T} Flag evaluation response
*/
getStringValue(
flagKey: string,
defaultValue: string,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): string;
getStringValue<T extends string = string>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): T;

Expand All @@ -222,20 +211,17 @@ export interface Features {
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @template {string} T A optional generic argument constraining the string
* @param {T} defaultValue The value returned if an error occurs
* @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
* @param {FlagEvaluationOptions} options Additional flag evaluation options
* @returns {EvaluationDetails<T>} Flag evaluation details response
*/
getStringDetails(
flagKey: string,
defaultValue: string,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): EvaluationDetails<string>;
getStringDetails<T extends string = string>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): EvaluationDetails<T>;

Expand All @@ -245,20 +231,17 @@ export interface Features {
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @template {number} T A optional generic argument constraining the number
* @param {T} defaultValue The value returned if an error occurs
* @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
* @param {FlagEvaluationOptions} options Additional flag evaluation options
* @returns {T} Flag evaluation response
*/
getNumberValue(
flagKey: string,
defaultValue: number,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): number
getNumberValue<T extends number = number>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): T;

Expand All @@ -268,20 +251,17 @@ export interface Features {
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @template {number} T A optional generic argument constraining the number
* @param {T} defaultValue The value returned if an error occurs
* @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
* @param {FlagEvaluationOptions} options Additional flag evaluation options
* @returns {Promise<EvaluationDetails<T>>} Flag evaluation details response
*/
getNumberDetails(
flagKey: string,
defaultValue: number,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): EvaluationDetails<number>;
getNumberDetails<T extends number = number>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): EvaluationDetails<T>;

Expand All @@ -291,20 +271,17 @@ export interface Features {
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @template {JsonValue} T A optional generic argument describing the structure
* @param {T} defaultValue The value returned if an error occurs
* @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
* @param {FlagEvaluationOptions} options Additional flag evaluation options
* @returns {Promise<T>} Flag evaluation response
*/
getObjectValue(
flagKey: string,
defaultValue: JsonValue,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): JsonValue;
getObjectValue<T extends JsonValue = JsonValue>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): T;

Expand All @@ -314,20 +291,17 @@ export interface Features {
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @template {JsonValue} T A optional generic argument describing the structure
* @param {T} defaultValue The value returned if an error occurs
* @param {EvaluationContext} context The evaluation context used on an individual flag evaluation
* @param {FlagEvaluationOptions} options Additional flag evaluation options
* @returns {Promise<EvaluationDetails<T>>} Flag evaluation details response
*/
getObjectDetails(
flagKey: string,
defaultValue: JsonValue,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): EvaluationDetails<JsonValue>;
getObjectDetails<T extends JsonValue = JsonValue>(
flagKey: string,
defaultValue: T,
context?: EvaluationContext,
options?: FlagEvaluationOptions
): EvaluationDetails<T>;
}
Expand Down

0 comments on commit a692a32

Please sign in to comment.