Skip to content

Commit 3d11260

Browse files
committed
expose hook assignment method in eppo client interface
1 parent 530eea6 commit 3d11260

File tree

3 files changed

+40
-32
lines changed

3 files changed

+40
-32
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@eppo/js-client-sdk-common",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Eppo SDK for client-side JavaScript applications (base for both web and react native)",
55
"main": "dist/index.js",
66
"files": [

src/client/eppo-client.spec.ts

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -309,49 +309,39 @@ describe('EppoClient E2E test', () => {
309309
describe('onPreAssignment', () => {
310310
it('called with subject ID', () => {
311311
const mockHooks = td.object<IAssignmentHooks>();
312-
client.getAssignmentWithHooks('subject-identifer', experimentName, {}, mockHooks);
312+
client.getAssignmentWithHooks('subject-identifer', experimentName, mockHooks);
313313
expect(td.explain(mockHooks.onPreAssignment).callCount).toEqual(1);
314314
expect(td.explain(mockHooks.onPreAssignment).calls[0].args[0]).toEqual('subject-identifer');
315315
});
316316

317317
it('overrides returned assignment', async () => {
318-
const variation = await client.getAssignmentWithHooks(
319-
'subject-identifer',
320-
experimentName,
321-
{},
322-
{
323-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
324-
onPreAssignment(subject: string): Promise<string> {
325-
return Promise.resolve('my-overridden-variation');
326-
},
318+
const variation = await client.getAssignmentWithHooks('subject-identifer', experimentName, {
319+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
320+
onPreAssignment(subject: string): Promise<string> {
321+
return Promise.resolve('my-overridden-variation');
322+
},
327323

328-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
329-
onPostAssignment(variation: string): Promise<void> {
330-
return Promise.resolve();
331-
},
324+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
325+
onPostAssignment(variation: string): Promise<void> {
326+
return Promise.resolve();
332327
},
333-
);
328+
});
334329

335330
expect(variation).toEqual('my-overridden-variation');
336331
});
337332

338333
it('uses regular assignment logic if onPreAssignment returns null', async () => {
339-
const variation = await client.getAssignmentWithHooks(
340-
'subject-identifer',
341-
experimentName,
342-
{},
343-
{
344-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
345-
onPreAssignment(subject: string): Promise<string | null> {
346-
return Promise.resolve(null);
347-
},
334+
const variation = await client.getAssignmentWithHooks('subject-identifer', experimentName, {
335+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
336+
onPreAssignment(subject: string): Promise<string | null> {
337+
return Promise.resolve(null);
338+
},
348339

349-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
350-
onPostAssignment(variation: string): Promise<void> {
351-
return Promise.resolve();
352-
},
340+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
341+
onPostAssignment(variation: string): Promise<void> {
342+
return Promise.resolve();
353343
},
354-
);
344+
});
355345

356346
expect(variation).not.toEqual(null);
357347
});
@@ -363,7 +353,6 @@ describe('EppoClient E2E test', () => {
363353
const variation = await client.getAssignmentWithHooks(
364354
'subject-identifer',
365355
experimentName,
366-
{},
367356
mockHooks,
368357
);
369358
expect(td.explain(mockHooks.onPostAssignment).callCount).toEqual(1);

src/client/eppo-client.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ export interface IEppoClient {
3131
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3232
subjectAttributes?: Record<string, any>,
3333
): string;
34+
35+
/**
36+
* Asynchronously maps a subject to a variation for a given experiment, with pre and post assignment hooks
37+
*
38+
* @param subjectKey an identifier of the experiment subject, for example a user ID.
39+
* @param experimentKey experiment identifier
40+
* @param assignmentHooks interface for pre and post assignment hooks
41+
* @param subjectAttributes optional attributes associated with the subject, for example name and email.
42+
* The subject attributes are used for evaluating any targeting rules tied to the experiment.
43+
* @returns a variation value if the subject is part of the experiment sample, otherwise null
44+
* @public
45+
*/
46+
getAssignmentWithHooks(
47+
subjectKey: string,
48+
experimentKey: string,
49+
assignmentHooks: IAssignmentHooks,
50+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
51+
subjectAttributes?: Record<string, any>,
52+
): Promise<string>;
3453
}
3554

3655
export default class EppoClient implements IEppoClient {
@@ -77,8 +96,8 @@ export default class EppoClient implements IEppoClient {
7796
async getAssignmentWithHooks(
7897
subjectKey: string,
7998
experimentKey: string,
80-
subjectAttributes = {},
8199
assignmentHooks: IAssignmentHooks,
100+
subjectAttributes = {},
82101
): Promise<string> {
83102
let assignment = await assignmentHooks?.onPreAssignment(subjectKey);
84103
if (assignment == null) {

0 commit comments

Comments
 (0)