Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add options to configure variant function call #149

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading