Skip to content

Commit 67e9446

Browse files
committed
Switch to initializing the client with an options object
1 parent aee17ce commit 67e9446

File tree

2 files changed

+84
-23
lines changed

2 files changed

+84
-23
lines changed

src/client/eppo-precomputed-client.spec.ts

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('EppoPrecomputedClient E2E test', () => {
6969

7070
beforeAll(() => {
7171
storage.setEntries({ [precomputedFlagKey]: mockPrecomputedFlag });
72-
client = new EppoPrecomputedClient(storage, false);
72+
client = new EppoPrecomputedClient({ precomputedFlagStore: storage, isObfuscated: false });
7373
});
7474

7575
afterAll(() => {
@@ -89,7 +89,10 @@ describe('EppoPrecomputedClient E2E test', () => {
8989
it('Invokes logger for queued events', () => {
9090
const mockLogger = td.object<IAssignmentLogger>();
9191

92-
const client = new EppoPrecomputedClient(storage, false);
92+
const client = new EppoPrecomputedClient({
93+
precomputedFlagStore: storage,
94+
isObfuscated: false,
95+
});
9396
client.getStringAssignment(precomputedFlagKey, 'default-value');
9497
client.setAssignmentLogger(mockLogger);
9598

@@ -101,7 +104,10 @@ describe('EppoPrecomputedClient E2E test', () => {
101104
it('Does not log same queued event twice', () => {
102105
const mockLogger = td.object<IAssignmentLogger>();
103106

104-
const client = new EppoPrecomputedClient(storage, false);
107+
const client = new EppoPrecomputedClient({
108+
precomputedFlagStore: storage,
109+
isObfuscated: false,
110+
});
105111

106112
client.getStringAssignment(precomputedFlagKey, 'default-value');
107113
client.setAssignmentLogger(mockLogger);
@@ -112,7 +118,10 @@ describe('EppoPrecomputedClient E2E test', () => {
112118

113119
it('Does not invoke logger for events that exceed queue size', () => {
114120
const mockLogger = td.object<IAssignmentLogger>();
115-
const client = new EppoPrecomputedClient(storage, false);
121+
const client = new EppoPrecomputedClient({
122+
precomputedFlagStore: storage,
123+
isObfuscated: false,
124+
});
116125

117126
for (let i = 0; i < MAX_EVENT_QUEUE_SIZE + 100; i++) {
118127
client.getStringAssignment(precomputedFlagKey, 'default-value');
@@ -123,23 +132,32 @@ describe('EppoPrecomputedClient E2E test', () => {
123132
});
124133

125134
it('returns null if getStringAssignment was called for the subject before any precomputed flags were loaded', () => {
126-
const localClient = new EppoPrecomputedClient(new MemoryOnlyConfigurationStore(), false);
135+
const localClient = new EppoPrecomputedClient({
136+
precomputedFlagStore: new MemoryOnlyConfigurationStore(),
137+
isObfuscated: false,
138+
});
127139
expect(localClient.getStringAssignment(precomputedFlagKey, 'hello world')).toEqual(
128140
'hello world',
129141
);
130142
expect(localClient.isInitialized()).toBe(false);
131143
});
132144

133145
it('returns default value when key does not exist', async () => {
134-
const client = new EppoPrecomputedClient(storage, false);
146+
const client = new EppoPrecomputedClient({
147+
precomputedFlagStore: storage,
148+
isObfuscated: false,
149+
});
135150
const nonExistentFlag = 'non-existent-flag';
136151
expect(client.getStringAssignment(nonExistentFlag, 'default')).toBe('default');
137152
});
138153

139154
it('logs variation assignment with correct metadata', () => {
140155
const mockLogger = td.object<IAssignmentLogger>();
141156
storage.setEntries({ [precomputedFlagKey]: mockPrecomputedFlag });
142-
const client = new EppoPrecomputedClient(storage, false);
157+
const client = new EppoPrecomputedClient({
158+
precomputedFlagStore: storage,
159+
isObfuscated: false,
160+
});
143161
client.setAssignmentLogger(mockLogger);
144162

145163
client.getStringAssignment(precomputedFlagKey, 'default');
@@ -160,7 +178,10 @@ describe('EppoPrecomputedClient E2E test', () => {
160178
td.when(mockLogger.logAssignment(td.matchers.anything())).thenThrow(new Error('logging error'));
161179

162180
storage.setEntries({ [precomputedFlagKey]: mockPrecomputedFlag });
163-
const client = new EppoPrecomputedClient(storage, false);
181+
const client = new EppoPrecomputedClient({
182+
precomputedFlagStore: storage,
183+
isObfuscated: false,
184+
});
164185
client.setAssignmentLogger(mockLogger);
165186

166187
const assignment = client.getStringAssignment(precomputedFlagKey, 'default');
@@ -175,7 +196,10 @@ describe('EppoPrecomputedClient E2E test', () => {
175196
beforeEach(() => {
176197
mockLogger = td.object<IAssignmentLogger>();
177198
storage.setEntries({ [precomputedFlagKey]: mockPrecomputedFlag });
178-
client = new EppoPrecomputedClient(storage, false);
199+
client = new EppoPrecomputedClient({
200+
precomputedFlagStore: storage,
201+
isObfuscated: false,
202+
});
179203
client.setAssignmentLogger(mockLogger);
180204
});
181205

@@ -394,7 +418,10 @@ describe('EppoPrecomputedClient E2E test', () => {
394418
});
395419

396420
it('Fetches initial configuration with parameters in constructor', async () => {
397-
client = new EppoPrecomputedClient(precomputedFlagStore, true);
421+
client = new EppoPrecomputedClient({
422+
precomputedFlagStore: precomputedFlagStore,
423+
isObfuscated: true,
424+
});
398425
client.setSubjectAndPrecomputedFlagsRequestParameters(requestParameters);
399426
// no configuration loaded
400427
let variation = client.getStringAssignment(precomputedFlagKey, 'default');
@@ -406,7 +433,10 @@ describe('EppoPrecomputedClient E2E test', () => {
406433
});
407434

408435
it('Fetches initial configuration with parameters provided later', async () => {
409-
client = new EppoPrecomputedClient(precomputedFlagStore, true);
436+
client = new EppoPrecomputedClient({
437+
precomputedFlagStore: precomputedFlagStore,
438+
isObfuscated: true,
439+
});
410440
client.setSubjectAndPrecomputedFlagsRequestParameters(requestParameters);
411441
// no configuration loaded
412442
let variation = client.getStringAssignment(precomputedFlagKey, 'default');
@@ -427,7 +457,10 @@ describe('EppoPrecomputedClient E2E test', () => {
427457
}
428458
}
429459

430-
client = new EppoPrecomputedClient(new MockStore(), true);
460+
client = new EppoPrecomputedClient({
461+
precomputedFlagStore: new MockStore(),
462+
isObfuscated: true,
463+
});
431464
client.setSubjectAndPrecomputedFlagsRequestParameters({
432465
...requestParameters,
433466
pollAfterSuccessfulInitialization: true,
@@ -457,7 +490,10 @@ describe('EppoPrecomputedClient E2E test', () => {
457490
}
458491
}
459492

460-
client = new EppoPrecomputedClient(new MockStore(), false);
493+
client = new EppoPrecomputedClient({
494+
precomputedFlagStore: new MockStore(),
495+
isObfuscated: false,
496+
});
461497
client.setSubjectAndPrecomputedFlagsRequestParameters(requestParameters);
462498
// no configuration loaded
463499
let variation = client.getStringAssignment(precomputedFlagKey, 'default');
@@ -472,7 +508,10 @@ describe('EppoPrecomputedClient E2E test', () => {
472508
let client: EppoPrecomputedClient;
473509

474510
beforeEach(async () => {
475-
client = new EppoPrecomputedClient(storage, true);
511+
client = new EppoPrecomputedClient({
512+
precomputedFlagStore: storage,
513+
isObfuscated: true,
514+
});
476515
client.setSubjectAndPrecomputedFlagsRequestParameters(requestParameters);
477516
await client.fetchPrecomputedFlags();
478517
});
@@ -544,7 +583,10 @@ describe('EppoPrecomputedClient E2E test', () => {
544583
...requestParameters,
545584
pollAfterSuccessfulInitialization,
546585
};
547-
client = new EppoPrecomputedClient(precomputedFlagStore, true);
586+
client = new EppoPrecomputedClient({
587+
precomputedFlagStore: precomputedFlagStore,
588+
isObfuscated: true,
589+
});
548590
client.setSubjectAndPrecomputedFlagsRequestParameters(requestParameters);
549591
// no configuration loaded
550592
let variation = client.getStringAssignment(precomputedFlagKey, 'default');
@@ -609,7 +651,10 @@ describe('EppoPrecomputedClient E2E test', () => {
609651
throwOnFailedInitialization,
610652
pollAfterFailedInitialization,
611653
};
612-
client = new EppoPrecomputedClient(precomputedFlagStore, true);
654+
client = new EppoPrecomputedClient({
655+
precomputedFlagStore: precomputedFlagStore,
656+
isObfuscated: true,
657+
});
613658
client.setSubjectAndPrecomputedFlagsRequestParameters(requestParameters);
614659
// no configuration loaded
615660
expect(client.getStringAssignment(precomputedFlagKey, 'default')).toBe('default');
@@ -650,7 +695,10 @@ describe('EppoPrecomputedClient E2E test', () => {
650695
},
651696
});
652697

653-
const client = new EppoPrecomputedClient(storage, true);
698+
const client = new EppoPrecomputedClient({
699+
precomputedFlagStore: storage,
700+
isObfuscated: true,
701+
});
654702
client.setSubjectSaltAndPrecomputedFlagStore(
655703
'test-subject',
656704
{ attr1: 'value1' },
@@ -669,7 +717,10 @@ describe('EppoPrecomputedClient E2E test', () => {
669717
it('logs variation assignment with format from precomputed flags response', () => {
670718
const mockLogger = td.object<IAssignmentLogger>();
671719
storage.setEntries({ [precomputedFlagKey]: mockPrecomputedFlag });
672-
const client = new EppoPrecomputedClient(storage, false);
720+
const client = new EppoPrecomputedClient({
721+
precomputedFlagStore: storage,
722+
isObfuscated: false,
723+
});
673724
client.setAssignmentLogger(mockLogger);
674725

675726
client.getStringAssignment(precomputedFlagKey, 'default');
@@ -688,7 +739,10 @@ describe('EppoPrecomputedClient E2E test', () => {
688739
beforeEach(() => {
689740
store = new MemoryOnlyConfigurationStore<PrecomputedFlag>();
690741
mockLogger = td.object<IAssignmentLogger>();
691-
client = new EppoPrecomputedClient(store, false);
742+
client = new EppoPrecomputedClient({
743+
precomputedFlagStore: store,
744+
isObfuscated: false,
745+
});
692746
client.setAssignmentLogger(mockLogger);
693747
});
694748

src/client/eppo-precomputed-client.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ export function convertContextAttributesToSubjectAttributes(
5454
};
5555
}
5656

57+
interface EppoPrecomputedClientOptions {
58+
precomputedFlagStore: IConfigurationStore<PrecomputedFlag>;
59+
isObfuscated?: boolean;
60+
}
61+
5762
export default class EppoPrecomputedClient {
5863
private readonly queuedAssignmentEvents: IAssignmentEvent[] = [];
5964
private assignmentLogger?: IAssignmentLogger;
@@ -63,11 +68,13 @@ export default class EppoPrecomputedClient {
6368
private subjectKey?: string;
6469
private subjectAttributes?: Attributes;
6570
private decodedFlagKeySalt = '';
71+
private precomputedFlagStore: IConfigurationStore<PrecomputedFlag>;
72+
private isObfuscated: boolean;
6673

67-
constructor(
68-
private precomputedFlagStore: IConfigurationStore<PrecomputedFlag>,
69-
private isObfuscated = true,
70-
) {}
74+
constructor(options: EppoPrecomputedClientOptions) {
75+
this.precomputedFlagStore = options.precomputedFlagStore;
76+
this.isObfuscated = options.isObfuscated ?? true; // Default to true if not provided
77+
}
7178

7279
public setIsObfuscated(isObfuscated: boolean) {
7380
this.isObfuscated = isObfuscated;

0 commit comments

Comments
 (0)