Skip to content

Commit

Permalink
feat: add options to configure variant function call
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori committed Dec 20, 2024
1 parent e0e9525 commit 5e254cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
16 changes: 13 additions & 3 deletions packages/experiment-browser/src/experimentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
import { LocalStorage } from './storage/local-storage';
import { FetchHttpClient, WrapperClient } from './transport/http';
import { exposureEvent } from './types/analytics';
import { Client, FetchOptions } from './types/client';
import { Client, FetchOptions, VariantOptions } from './types/client';
import { Exposure, ExposureTrackingProvider } from './types/exposure';
import { ExperimentPlugin, IntegrationPlugin } from './types/plugin';
import { ExperimentUserProvider } from './types/provider';
Expand Down Expand Up @@ -286,15 +286,25 @@ export class ExperimentClient implements Client {
*
* @param key The key to get the variant for.
* @param fallback The highest priority fallback.
* @param options Additional options to configure the function behavior.
* @see ExperimentConfig
* @see ExposureTrackingProvider
* @see VariantOptions
*/
public variant(key: string, fallback?: string | Variant): Variant {
public variant(
key: string,
fallback?: string | Variant | undefined,
options?: VariantOptions | undefined,
): Variant {
if (!this.apiKey) {
return { value: undefined };
}
const shouldTrackExposure =
(this.config.automaticExposureTracking &&
options.trackExposure !== false) ||
options.trackExposure === true;
const sourceVariant = this.variantAndSource(key, fallback);
if (this.config.automaticExposureTracking) {
if (shouldTrackExposure) {
this.exposureInternal(key, sourceVariant);
}
this.debug(
Expand Down
12 changes: 11 additions & 1 deletion packages/experiment-browser/src/types/client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { ExperimentUserProvider } from './provider';
import { Source } from './source';
import { ExperimentUser } from './user';
import { Variant, Variants } from './variant';

export type FetchOptions = {
flagKeys?: string[];
};

export type VariantOptions = {
trackExposure?: boolean;
source: Source;
};

/**
* Interface for the main client.
* @category Core Usage
Expand All @@ -14,7 +20,11 @@ export interface Client {
start(user?: ExperimentUser): Promise<void>;
stop(): void;
fetch(user?: ExperimentUser, options?: FetchOptions): Promise<Client>;
variant(key: string, fallback?: string | Variant): Variant;
variant(
key: string,
fallback?: string | Variant | undefined,
options?: VariantOptions | undefined,
): Variant;
all(): Variants;
clear(): void;
exposure(key: string): void;
Expand Down

0 comments on commit 5e254cc

Please sign in to comment.