Skip to content

Commit

Permalink
CR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
greghuels committed Sep 30, 2024
1 parent 0a9f095 commit e650c4d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
43 changes: 30 additions & 13 deletions src/client/eppo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ export type FlagConfigurationRequestParameters = {
skipInitialPoll?: boolean;
};

export interface IFlagExperiment<T> {
export interface IContainerExperiment<T> {
flagKey: string;
controlVariation: T;
treatmentVariations: Array<T>;
controlVariationEntry: T;
treatmentVariationEntries: Array<T>;
}

export default class EppoClient {
Expand Down Expand Up @@ -531,32 +531,49 @@ export default class EppoClient {
}

/**
* For use with 3rd party CMS tooling, such as the Contentful Eppo plugin
* For use with 3rd party CMS tooling, such as the Contentful Eppo plugin.
*
* CMS plugins that integrate with Eppo will follow a common format for
* creating a feature flag. The flag created by the CMS plugin will have
* variations with values 'control', 'treatment-1', 'treatment-2', etc.
* This function allows users to easily return the CMS container entry
* for the assigned variation.
*
* @param flagExperiment the flag key, control container entry and treatment container entries.
* @param subjectKey an identifier of the experiment subject, for example a user ID.
* @param subjectAttributes optional attributes associated with the subject, for example name and email.
* @returns The container entry associated with the experiment.
*/
public getExperimentContainer<T>(
flagExperiment: IFlagExperiment<T>,
public getExperimentContainerEntry<T>(
flagExperiment: IContainerExperiment<T>,
subjectKey: string,
subjectAttributes: Attributes,
): T {
const { flagKey, controlVariation, treatmentVariations } = flagExperiment;
const { flagKey, controlVariationEntry, treatmentVariationEntries } = flagExperiment;
const assignment = this.getStringAssignment(flagKey, subjectKey, subjectAttributes, 'control');
if (assignment === 'control') {
return controlVariation;
return controlVariationEntry;
}
if (!assignment.startsWith('treatment-')) {
logger.warn(
`Variation ${assignment} cannot be mapped to a container. Defaulting to control variation.`,
);
return controlVariation;
return controlVariationEntry;
}
const treatmentVariationIndex = Number.parseInt(assignment.split('-')[1]) - 1;
if (isNaN(treatmentVariationIndex)) {
logger.warn(
`Variation ${assignment} cannot be mapped to a container. Defaulting to control variation.`,
);
return controlVariationEntry;
}
const treatmentVariationIndex = Number.parseInt(assignment.split('-')[1]);
if (treatmentVariationIndex > treatmentVariations.length) {
if (treatmentVariationIndex >= treatmentVariationEntries.length) {
logger.warn(
`Selected treatment variation (${treatmentVariationIndex}) index is out of bounds. Defaulting to control variation.`,
);
return controlVariation;
return controlVariationEntry;
}
return treatmentVariations[treatmentVariationIndex - 1];
return treatmentVariationEntries[treatmentVariationIndex];
}

private evaluateBanditAction(
Expand Down
2 changes: 1 addition & 1 deletion src/client/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function initConfiguration(
queryParams: {
apiKey: 'dummy',
sdkName: 'js-client-sdk-common',
sdkVersion: '1.0.0',
sdkVersion: '3.0.0',
},
});
const httpClient = new FetchHttpClient(apiEndpoints, 1000);
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import EppoClient, {
FlagConfigurationRequestParameters,
IAssignmentDetails,
IContainerExperiment,
} from './client/eppo-client';
import FlagConfigRequestor from './configuration-requestor';
import {
Expand Down Expand Up @@ -48,6 +49,7 @@ export {
IAssignmentEvent,
IBanditLogger,
IBanditEvent,
IContainerExperiment,
EppoClient,
constants,
ApiEndpoints,
Expand Down

0 comments on commit e650c4d

Please sign in to comment.