|
| 1 | +import { ERROR_REASON, GENERAL_ERROR } from './constants'; |
| 2 | +import { OpenFeature } from './open-feature'; |
| 3 | +import { |
| 4 | + Client, |
| 5 | + EvaluationContext, |
| 6 | + EvaluationDetails, |
| 7 | + FlagEvaluationOptions, |
| 8 | + FlagValue, |
| 9 | + Hook, |
| 10 | + ResolutionDetails, |
| 11 | + TransformingProvider, |
| 12 | +} from './types'; |
| 13 | + |
| 14 | +type OpenFeatureClientOptions = { |
| 15 | + name?: string; |
| 16 | + version?: string; |
| 17 | +}; |
| 18 | + |
| 19 | +export class OpenFeatureClient implements Client { |
| 20 | + name?: string | undefined; |
| 21 | + version?: string | undefined; |
| 22 | + readonly context: EvaluationContext; |
| 23 | + |
| 24 | + constructor(private readonly api: OpenFeature, options: OpenFeatureClientOptions, context: EvaluationContext = {}) { |
| 25 | + this.name = options.name; |
| 26 | + this.version = options.version; |
| 27 | + this.context = context; |
| 28 | + } |
| 29 | + |
| 30 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 31 | + addHooks(...hooks: Hook<FlagValue>[]): void { |
| 32 | + throw new Error('Method not implemented.'); |
| 33 | + } |
| 34 | + |
| 35 | + get hooks(): Hook<FlagValue>[] { |
| 36 | + throw new Error('Method not implemented.'); |
| 37 | + } |
| 38 | + |
| 39 | + async getBooleanValue( |
| 40 | + flagKey: string, |
| 41 | + defaultValue: boolean, |
| 42 | + context?: EvaluationContext, |
| 43 | + options?: FlagEvaluationOptions |
| 44 | + ): Promise<boolean> { |
| 45 | + return (await this.getBooleanDetails(flagKey, defaultValue, context, options)).value; |
| 46 | + } |
| 47 | + |
| 48 | + getBooleanDetails( |
| 49 | + flagKey: string, |
| 50 | + defaultValue: boolean, |
| 51 | + context?: EvaluationContext, |
| 52 | + options?: FlagEvaluationOptions |
| 53 | + ): Promise<EvaluationDetails<boolean>> { |
| 54 | + return this.evaluate<boolean>(flagKey, this.provider.resolveBooleanEvaluation, defaultValue, context, options); |
| 55 | + } |
| 56 | + |
| 57 | + async getStringValue( |
| 58 | + flagKey: string, |
| 59 | + defaultValue: string, |
| 60 | + context?: EvaluationContext, |
| 61 | + options?: FlagEvaluationOptions |
| 62 | + ): Promise<string> { |
| 63 | + return (await this.getStringDetails(flagKey, defaultValue, context, options)).value; |
| 64 | + } |
| 65 | + |
| 66 | + getStringDetails( |
| 67 | + flagKey: string, |
| 68 | + defaultValue: string, |
| 69 | + context?: EvaluationContext, |
| 70 | + options?: FlagEvaluationOptions |
| 71 | + ): Promise<EvaluationDetails<string>> { |
| 72 | + return this.evaluate<string>(flagKey, this.provider.resolveStringEvaluation, defaultValue, context, options); |
| 73 | + } |
| 74 | + |
| 75 | + async getNumberValue( |
| 76 | + flagKey: string, |
| 77 | + defaultValue: number, |
| 78 | + context?: EvaluationContext, |
| 79 | + options?: FlagEvaluationOptions |
| 80 | + ): Promise<number> { |
| 81 | + return (await this.getNumberDetails(flagKey, defaultValue, context, options)).value; |
| 82 | + } |
| 83 | + |
| 84 | + getNumberDetails( |
| 85 | + flagKey: string, |
| 86 | + defaultValue: number, |
| 87 | + context?: EvaluationContext, |
| 88 | + options?: FlagEvaluationOptions |
| 89 | + ): Promise<EvaluationDetails<number>> { |
| 90 | + return this.evaluate<number>(flagKey, this.provider.resolveNumberEvaluation, defaultValue, context, options); |
| 91 | + } |
| 92 | + |
| 93 | + async getObjectValue<T extends object>( |
| 94 | + flagKey: string, |
| 95 | + defaultValue: T, |
| 96 | + context?: EvaluationContext, |
| 97 | + options?: FlagEvaluationOptions |
| 98 | + ): Promise<T> { |
| 99 | + return (await this.getObjectDetails(flagKey, defaultValue, context, options)).value; |
| 100 | + } |
| 101 | + |
| 102 | + getObjectDetails<T extends object>( |
| 103 | + flagKey: string, |
| 104 | + defaultValue: T, |
| 105 | + context?: EvaluationContext, |
| 106 | + options?: FlagEvaluationOptions |
| 107 | + ): Promise<EvaluationDetails<T>> { |
| 108 | + return this.evaluate<T>(flagKey, this.provider.resolveObjectEvaluation, defaultValue, context, options); |
| 109 | + } |
| 110 | + |
| 111 | + private async evaluate<T extends FlagValue>( |
| 112 | + flagKey: string, |
| 113 | + resolver: ( |
| 114 | + flagKey: string, |
| 115 | + defaultValue: T, |
| 116 | + transformedContext: unknown, |
| 117 | + options: FlagEvaluationOptions | undefined |
| 118 | + ) => Promise<ResolutionDetails<T>>, |
| 119 | + defaultValue: T, |
| 120 | + context: EvaluationContext = {}, |
| 121 | + options: FlagEvaluationOptions = {} |
| 122 | + ): Promise<EvaluationDetails<T>> { |
| 123 | + // merge global, client, and evaluation context |
| 124 | + const mergedContext = { |
| 125 | + ...this.api.context, |
| 126 | + ...this.context, |
| 127 | + ...context, |
| 128 | + }; |
| 129 | + |
| 130 | + try { |
| 131 | + // if a transformer is defined, run it to prepare the context. |
| 132 | + const transformedContext = |
| 133 | + typeof this.provider.contextTransformer === 'function' |
| 134 | + ? await this.provider.contextTransformer(mergedContext) |
| 135 | + : mergedContext; |
| 136 | + |
| 137 | + // run the referenced resolver, binding the provider. |
| 138 | + const resolution = await resolver.call(this.provider, flagKey, defaultValue, transformedContext, options); |
| 139 | + return { |
| 140 | + ...resolution, |
| 141 | + flagKey, |
| 142 | + }; |
| 143 | + } catch (err: unknown) { |
| 144 | + const errorCode = (!!err && (err as { code: string }).code) || GENERAL_ERROR; |
| 145 | + return { |
| 146 | + errorCode, |
| 147 | + value: defaultValue, |
| 148 | + reason: ERROR_REASON, |
| 149 | + flagKey, |
| 150 | + }; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private get provider() { |
| 155 | + return OpenFeature.instance.provider as TransformingProvider<unknown>; |
| 156 | + } |
| 157 | +} |
0 commit comments