diff --git a/package.json b/package.json index 65404865..36673650 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@eppo/js-client-sdk-common", - "version": "3.0.9", + "version": "3.1.0", "description": "Eppo SDK for client-side JavaScript applications (base for both web and react native)", "main": "dist/index.js", "files": [ diff --git a/src/client/eppo-client.spec.ts b/src/client/eppo-client.spec.ts index aaf1f10f..17cca50d 100644 --- a/src/client/eppo-client.spec.ts +++ b/src/client/eppo-client.spec.ts @@ -104,6 +104,8 @@ describe('EppoClient E2E test', () => { expect(client.getBoolAssignment(flagKey, 'subject-identifer', {}, true)).toBe(true); expect(client.getBoolAssignment(flagKey, 'subject-identifer', {}, false)).toBe(false); + expect(client.getBooleanAssignment(flagKey, 'subject-identifer', {}, true)).toBe(true); + expect(client.getBooleanAssignment(flagKey, 'subject-identifer', {}, false)).toBe(false); expect(client.getNumericAssignment(flagKey, 'subject-identifer', {}, 1)).toBe(1); expect(client.getNumericAssignment(flagKey, 'subject-identifer', {}, 0)).toBe(0); expect(client.getJSONAssignment(flagKey, 'subject-identifer', {}, {})).toEqual({}); @@ -122,6 +124,7 @@ describe('EppoClient E2E test', () => { expect(() => { client.getBoolAssignment(flagKey, 'subject-identifer', {}, true); + client.getBooleanAssignment(flagKey, 'subject-identifer', {}, true); }).toThrow(); expect(() => { @@ -217,7 +220,7 @@ describe('EppoClient E2E test', () => { }[] = []; const typeAssignmentFunctions = { - [VariationType.BOOLEAN]: client.getBoolAssignment.bind(client), + [VariationType.BOOLEAN]: client.getBooleanAssignment.bind(client), [VariationType.NUMERIC]: client.getNumericAssignment.bind(client), [VariationType.INTEGER]: client.getIntegerAssignment.bind(client), [VariationType.STRING]: client.getStringAssignment.bind(client), @@ -264,7 +267,7 @@ describe('EppoClient E2E test', () => { client.setIsGracefulFailureMode(false); const typeAssignmentFunctions = { - [VariationType.BOOLEAN]: client.getBoolAssignment.bind(client), + [VariationType.BOOLEAN]: client.getBooleanAssignment.bind(client), [VariationType.NUMERIC]: client.getNumericAssignment.bind(client), [VariationType.INTEGER]: client.getIntegerAssignment.bind(client), [VariationType.STRING]: client.getStringAssignment.bind(client), @@ -301,6 +304,7 @@ describe('EppoClient E2E test', () => { const nonExistentFlag = 'non-existent-flag'; expect(client.getBoolAssignment(nonExistentFlag, 'subject-identifer', {}, true)).toBe(true); + expect(client.getBooleanAssignment(nonExistentFlag, 'subject-identifer', {}, true)).toBe(true); expect(client.getNumericAssignment(nonExistentFlag, 'subject-identifer', {}, 1)).toBe(1); expect(client.getJSONAssignment(nonExistentFlag, 'subject-identifer', {}, {})).toEqual({}); expect(client.getStringAssignment(nonExistentFlag, 'subject-identifer', {}, 'default')).toBe( diff --git a/src/client/eppo-client.ts b/src/client/eppo-client.ts index 4e940f09..c8b3389f 100644 --- a/src/client/eppo-client.ts +++ b/src/client/eppo-client.ts @@ -35,11 +35,12 @@ export interface IEppoClient { /** * Maps a subject to a variation for a given experiment. * - * @param subjectKey an identifier of the experiment subject, for example a user ID. * @param flagKey feature flag identifier + * @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. + * @param defaultValue default value to return if the subject is not part of the experiment sample * The subject attributes are used for evaluating any targeting rules tied to the experiment. - * @returns a variation value if the subject is part of the experiment sample, otherwise null + * @returns a variation value if the subject is part of the experiment sample, otherwise the default value * @public */ getStringAssignment( @@ -49,6 +50,9 @@ export interface IEppoClient { defaultValue: string, ): string; + /** + * @deprecated use getBooleanAssignment instead. + */ getBoolAssignment( flagKey: string, subjectKey: string, @@ -56,6 +60,31 @@ export interface IEppoClient { defaultValue: boolean, ): boolean; + /** + * Maps a subject to a boolean variation for a given experiment. + * + * @param flagKey feature flag identifier + * @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. + * @param defaultValue default value to return if the subject is not part of the experiment sample + * @returns a boolean variation value if the subject is part of the experiment sample, otherwise the default value + */ + getBooleanAssignment( + flagKey: string, + subjectKey: string, + subjectAttributes: Record, + defaultValue: boolean, + ): boolean; + + /** + * Maps a subject to an Integer variation for a given experiment. + * + * @param flagKey feature flag identifier + * @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. + * @param defaultValue default value to return if the subject is not part of the experiment sample + * @returns a number variation value if the subject is part of the experiment sample, otherwise the default value + */ getIntegerAssignment( flagKey: string, subjectKey: string, @@ -63,6 +92,15 @@ export interface IEppoClient { defaultValue: number, ): number; + /** + * Maps a subject to a Numeric variation for a given experiment. + * + * @param flagKey feature flag identifier + * @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. + * @param defaultValue default value to return if the subject is not part of the experiment sample + * @returns a number variation value if the subject is part of the experiment sample, otherwise the default value + */ getNumericAssignment( flagKey: string, subjectKey: string, @@ -70,6 +108,15 @@ export interface IEppoClient { defaultValue: number, ): number; + /** + * Maps a subject to a JSON variation for a given experiment. + * + * @param flagKey feature flag identifier + * @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. + * @param defaultValue default value to return if the subject is not part of the experiment sample + * @returns a JSON object variation value if the subject is part of the experiment sample, otherwise the default value + */ getJSONAssignment( flagKey: string, subjectKey: string, @@ -232,6 +279,15 @@ export default class EppoClient implements IEppoClient { subjectKey: string, subjectAttributes: Record, defaultValue: boolean, + ): boolean { + return this.getBooleanAssignment(flagKey, subjectKey, subjectAttributes, defaultValue); + } + + public getBooleanAssignment( + flagKey: string, + subjectKey: string, + subjectAttributes: Record, + defaultValue: boolean, ): boolean { return ( this.getAssignmentVariation(