Skip to content

Commit e830ec3

Browse files
committed
CR changes
1 parent 0a9f095 commit e830ec3

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

src/client/eppo-client-experiment-container.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as applicationLogger from '../application-logger';
33
import { MemoryOnlyConfigurationStore } from '../configuration-store/memory.store';
44
import { Flag, ObfuscatedFlag } from '../interfaces';
55

6-
import EppoClient, { IFlagExperiment } from './eppo-client';
6+
import EppoClient, { IContainerExperiment } from './eppo-client';
77
import { initConfiguration } from './test-utils';
88

99
type Container = { name: string };

src/client/eppo-client.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ export type FlagConfigurationRequestParameters = {
6868
skipInitialPoll?: boolean;
6969
};
7070

71-
export interface IFlagExperiment<T> {
71+
export interface IContainerExperiment<T> {
7272
flagKey: string;
73-
controlVariation: T;
74-
treatmentVariations: Array<T>;
73+
controlVariationEntry: T;
74+
treatmentVariationEntries: Array<T>;
7575
}
7676

7777
export default class EppoClient {
@@ -531,32 +531,49 @@ export default class EppoClient {
531531
}
532532

533533
/**
534-
* For use with 3rd party CMS tooling, such as the Contentful Eppo plugin
534+
* For use with 3rd party CMS tooling, such as the Contentful Eppo plugin.
535+
*
536+
* CMS plugins that integrate with Eppo will follow a common format for
537+
* creating a feature flag. The flag created by the CMS plugin will have
538+
* variations with values 'control', 'treatment-1', 'treatment-2', etc.
539+
* This function allows users to easily return the CMS container entry
540+
* for the assigned variation.
541+
*
542+
* @param flagExperiment the flag key, control container entry and treatment container entries.
543+
* @param subjectKey an identifier of the experiment subject, for example a user ID.
544+
* @param subjectAttributes optional attributes associated with the subject, for example name and email.
545+
* @returns The container entry associated with the experiment.
535546
*/
536-
public getExperimentContainer<T>(
537-
flagExperiment: IFlagExperiment<T>,
547+
public getExperimentContainerEntry<T>(
548+
flagExperiment: IContainerExperiment<T>,
538549
subjectKey: string,
539550
subjectAttributes: Attributes,
540551
): T {
541-
const { flagKey, controlVariation, treatmentVariations } = flagExperiment;
552+
const { flagKey, controlVariationEntry, treatmentVariationEntries } = flagExperiment;
542553
const assignment = this.getStringAssignment(flagKey, subjectKey, subjectAttributes, 'control');
543554
if (assignment === 'control') {
544-
return controlVariation;
555+
return controlVariationEntry;
545556
}
546557
if (!assignment.startsWith('treatment-')) {
547558
logger.warn(
548559
`Variation ${assignment} cannot be mapped to a container. Defaulting to control variation.`,
549560
);
550-
return controlVariation;
561+
return controlVariationEntry;
562+
}
563+
const treatmentVariationIndex = Number.parseInt(assignment.split('-')[1]) - 1;
564+
if (isNaN(treatmentVariationIndex)) {
565+
logger.warn(
566+
`Variation ${assignment} cannot be mapped to a container. Defaulting to control variation.`,
567+
);
568+
return controlVariationEntry;
551569
}
552-
const treatmentVariationIndex = Number.parseInt(assignment.split('-')[1]);
553-
if (treatmentVariationIndex > treatmentVariations.length) {
570+
if (treatmentVariationIndex >= treatmentVariationEntries.length) {
554571
logger.warn(
555572
`Selected treatment variation (${treatmentVariationIndex}) index is out of bounds. Defaulting to control variation.`,
556573
);
557-
return controlVariation;
574+
return controlVariationEntry;
558575
}
559-
return treatmentVariations[treatmentVariationIndex - 1];
576+
return treatmentVariationEntries[treatmentVariationIndex];
560577
}
561578

562579
private evaluateBanditAction(

src/client/test-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export async function initConfiguration(
1212
queryParams: {
1313
apiKey: 'dummy',
1414
sdkName: 'js-client-sdk-common',
15-
sdkVersion: '1.0.0',
15+
sdkVersion: '3.0.0',
1616
},
1717
});
1818
const httpClient = new FetchHttpClient(apiEndpoints, 1000);

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import EppoClient, {
1919
FlagConfigurationRequestParameters,
2020
IAssignmentDetails,
21+
IContainerExperiment,
2122
} from './client/eppo-client';
2223
import FlagConfigRequestor from './configuration-requestor';
2324
import {
@@ -48,6 +49,7 @@ export {
4849
IAssignmentEvent,
4950
IBanditLogger,
5051
IBanditEvent,
52+
IContainerExperiment,
5153
EppoClient,
5254
constants,
5355
ApiEndpoints,

0 commit comments

Comments
 (0)