Skip to content

Commit ec73898

Browse files
initialize the configuration store (#69)
* v3.0.2-rc.2 * initialize the configuration store * add logging * v3.0.2-rc.3 * remove console.log * rc4 * return empty object * enable strict null checks * v3.0.2-rc.5 * fix getTestAssignments * fix test * ??
1 parent c961b14 commit ec73898

10 files changed

+32
-27
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@eppo/js-client-sdk",
3-
"version": "3.0.2-rc.1",
3+
"version": "3.0.2-rc.5",
44
"description": "Eppo SDK for client-side JavaScript applications",
55
"main": "dist/index.js",
66
"files": [
@@ -61,4 +61,4 @@
6161
"@eppo/js-client-sdk-common": "3.0.8",
6262
"md5": "^2.3.0"
6363
}
64-
}
64+
}

src/chrome.configuration-store.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ describe('ChromeStore', () => {
3737
expect(await chromeStore.isExpired()).toBe(true);
3838
});
3939

40-
it('should return null when no entries are found', async () => {
40+
it('should return empty object when no entries are found', async () => {
4141
(extendedStorageLocal.get as jest.Mock).mockImplementation(() => {
4242
return Promise.resolve({});
4343
});
4444

4545
const entries = await chromeStore.getEntries();
46-
expect(entries).toBeNull();
46+
expect(entries).toEqual({});
4747
});
4848

4949
it('should be initialized after setting entries', async () => {

src/chrome.configuration-store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ export class ChromeStorageAsyncStore<T> implements IAsyncStore<T> {
1414
return Promise.resolve(true);
1515
}
1616

17-
public async getEntries(): Promise<Record<string, T> | null> {
17+
public async getEntries(): Promise<Record<string, T>> {
1818
const configuration = await this.storageArea.get(this.chromeStorageKey);
1919
if (configuration?.[this.chromeStorageKey]) {
2020
return Promise.resolve(JSON.parse(configuration[this.chromeStorageKey]));
2121
}
22-
return Promise.resolve(null);
22+
return Promise.resolve({});
2323
}
2424

2525
public async setEntries(entries: Record<string, T>): Promise<void> {

src/configuration-factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ export function configurationStorageFactory(
3131
return new MemoryOnlyConfigurationStore();
3232
} else if (persistentStore) {
3333
return new HybridConfigurationStore(new MemoryStore<Flag>(), persistentStore);
34-
} else if (hasChromeStorage) {
34+
} else if (hasChromeStorage && chromeStorage) {
3535
// Chrome storage is available, use it as a fallback
3636
return new HybridConfigurationStore(
3737
new MemoryStore<Flag>(),
3838
new ChromeStorageAsyncStore<Flag>(chromeStorage),
3939
);
40-
} else if (hasWindowLocalStorage) {
40+
} else if (hasWindowLocalStorage && windowLocalStorage) {
4141
// window.localStorage is available, use it as a fallback
4242
return new HybridConfigurationStore(
4343
new MemoryStore<Flag>(),

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,11 @@ export async function init(config: IClientConfig): Promise<IEppoClient> {
180180
hasWindowLocalStorage: hasWindowLocalStorage(),
181181
},
182182
{
183-
chromeStorage: hasChromeStorage() && chrome.storage.local,
184-
windowLocalStorage: hasWindowLocalStorage() && window.localStorage,
183+
chromeStorage: hasChromeStorage() ? chrome.storage.local : undefined,
184+
windowLocalStorage: hasWindowLocalStorage() ? window.localStorage : undefined,
185185
},
186186
);
187+
await configurationStore.init();
187188
EppoJSClient.instance.setConfigurationStore(configurationStore);
188189

189190
const requestConfiguration: FlagConfigurationRequestParameters = {

src/local-storage-assignment-cache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ class LocalStorageAssignmentShim {
1313
return this.getCache().has(key);
1414
}
1515

16-
public get(key: string): string {
16+
public get(key: string): string | undefined {
1717
if (!hasWindowLocalStorage()) {
18-
return null;
18+
return undefined;
1919
}
2020

21-
return this.getCache().get(key);
21+
return this.getCache().get(key) ?? undefined;
2222
}
2323

2424
public set(key: string, value: string) {

src/local-storage.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ describe('EppoLocalStorage', () => {
2222
});
2323

2424
describe('get and set', () => {
25-
it('returns null if entry is not present', async () => {
26-
expect(await storage.getEntries()).toEqual(null);
25+
it('returns empty object if entry is not present', async () => {
26+
expect(await storage.getEntries()).toEqual({});
2727
});
2828

2929
it('returns stored entries', async () => {

src/local-storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ export class LocalStorageBackedAsyncStore<T> implements IAsyncStore<T> {
2222
return Promise.resolve(true);
2323
}
2424

25-
getEntries(): Promise<Record<string, T> | null> {
25+
getEntries(): Promise<Record<string, T>> {
2626
const configuration = this.localStorage.getItem(this.localStorageKey);
2727
if (!configuration) {
28-
return Promise.resolve(null);
28+
return Promise.resolve({});
2929
}
3030
return Promise.resolve(JSON.parse(configuration));
3131
}

test/testHelpers.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,17 @@ export function getTestAssignments(
5252
): { subject: SubjectTestCase; assignment: string | boolean | number | object }[] {
5353
const assignments: {
5454
subject: SubjectTestCase;
55-
assignment: string | boolean | number | null | object;
55+
assignment: string | boolean | number | object;
5656
}[] = [];
5757
for (const subject of testCase.subjects) {
58-
const assignment = assignmentFn(
59-
testCase.flag,
60-
subject.subjectKey,
61-
subject.subjectAttributes,
62-
testCase.defaultValue,
63-
obfuscated,
64-
);
58+
const assignment =
59+
assignmentFn(
60+
testCase.flag,
61+
subject.subjectKey,
62+
subject.subjectAttributes,
63+
testCase.defaultValue,
64+
obfuscated,
65+
) || testCase.defaultValue; // Fallback to defaultValue if null
6566
assignments.push({ subject: subject, assignment: assignment });
6667
}
6768
return assignments;

tsconfig.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
"target": "es2017",
99
"outDir": "dist",
1010
"noImplicitAny": true,
11+
"strictNullChecks": true
1112
},
12-
"include": ["src/**/*.ts"],
13+
"include": [
14+
"src/**/*.ts"
15+
],
1316
"exclude": [
1417
"node_modules",
1518
"dist",
1619
"src/**/*.spec.ts",
1720
"test/**/*"
1821
]
19-
}
22+
}

0 commit comments

Comments
 (0)