Skip to content

Commit 041afa8

Browse files
authored
fix: allow empty string for precomputedConfiguration (#149)
* chore: allow empty string for precomputedConfiguration * Update docs * chore: add test for handling empty string
1 parent fce5191 commit 041afa8

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

docs/js-client-sdk.iprecomputedclientconfigsync.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Configuration parameters for initializing the Eppo precomputed client.
88

99
This interface is used for cases where precomputed assignments are available from an external process that can bootstrap the SDK client.
1010

11+
precomputedConfiguration - The configuration as a string to bootstrap the client. assignmentLogger - Optional logger for assignment events. throwOnFailedInitialization - Optional flag to throw an error if initialization fails.
12+
1113
**Signature:**
1214

1315
```typescript

docs/js-client-sdk.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ Configuration parameters for initializing the Eppo precomputed client.
224224

225225
This interface is used for cases where precomputed assignments are available from an external process that can bootstrap the SDK client.
226226

227+
precomputedConfiguration - The configuration as a string to bootstrap the client. assignmentLogger - Optional logger for assignment events. throwOnFailedInitialization - Optional flag to throw an error if initialization fails.
228+
227229

228230
</td></tr>
229231
</tbody></table>

src/index.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,4 +1342,25 @@ describe('EppoClient config', () => {
13421342
expect(retryManager['config']['maxRetryDelayMs']).toEqual(3);
13431343
expect(retryManager['config']['maxRetries']).toEqual(4);
13441344
});
1345+
1346+
it('handles empty precomputed configuration string', () => {
1347+
// Test with throwOnFailedInitialization = true (default)
1348+
expect(() =>
1349+
offlinePrecomputedInit({
1350+
precomputedConfiguration: '',
1351+
}),
1352+
).toThrow('Invalid precomputed configuration wire');
1353+
1354+
// Test with throwOnFailedInitialization = false
1355+
td.replace(applicationLogger, 'error');
1356+
const client = offlinePrecomputedInit({
1357+
precomputedConfiguration: '',
1358+
throwOnFailedInitialization: false,
1359+
});
1360+
1361+
expect(client).toBe(EppoPrecomputedJSClient.instance);
1362+
td.verify(applicationLogger.error('[Eppo SDK] Invalid precomputed configuration wire'), {
1363+
times: 1,
1364+
});
1365+
});
13451366
});

src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -667,15 +667,17 @@ export function offlinePrecomputedInit(
667667
): EppoPrecomputedClient {
668668
const throwOnFailedInitialization = config.throwOnFailedInitialization ?? true;
669669

670-
const configurationWire: IConfigurationWire = JSON.parse(config.precomputedConfiguration);
671-
if (!configurationWire.precomputed) {
670+
let configurationWire: IConfigurationWire;
671+
try {
672+
configurationWire = JSON.parse(config.precomputedConfiguration);
673+
if (!configurationWire.precomputed) throw new Error();
674+
} catch (error) {
672675
const errorMessage = 'Invalid precomputed configuration wire';
673676
if (throwOnFailedInitialization) {
674677
throw new Error(errorMessage);
675-
} else {
676-
applicationLogger.error('[Eppo SDK] ${errorMessage}');
677-
return EppoPrecomputedJSClient.instance;
678678
}
679+
applicationLogger.error(`[Eppo SDK] ${errorMessage}`);
680+
return EppoPrecomputedJSClient.instance;
679681
}
680682
const { subjectKey, subjectAttributes, response } = configurationWire.precomputed;
681683
const parsedResponse: IObfuscatedPrecomputedConfigurationResponse = JSON.parse(response);

0 commit comments

Comments
 (0)