Skip to content

Commit 8d2c380

Browse files
committed
Simplify FlagsmithCache interface
1 parent 604f610 commit 8d2c380

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

sdk/types.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@ import { Logger } from 'pino';
55
import { BaseOfflineHandler } from './offline_handlers.js';
66

77
export type IFlagsmithValue<T = string | number | boolean | null> = T;
8+
9+
10+
/**
11+
* Stores and retrieves {@link Flags} from a cache.
12+
*/
813
export interface FlagsmithCache {
9-
get(key: string): Promise<Flags | undefined> | undefined;
10-
set(key: string, value: Flags, ttl?: string | number): boolean | Promise<boolean>;
11-
has(key: string): boolean | Promise<boolean>;
12-
[key: string]: any;
14+
/**
15+
* Retrieve the cached {@link Flags} for the given environment or identity, or `undefined` if no cached value exists.
16+
* @param key An environment ID or identity identifier, which is used as the cache key.
17+
*/
18+
get(key: string): Promise<Flags | undefined>;
19+
20+
/**
21+
* Persist an environment or identity's {@link Flags} in the cache.
22+
* @param key An environment ID or identity identifier, which is used as the cache key.
23+
* @param value The {@link Flags} to be stored in the cache.
24+
*/
25+
set(key: string, value: Flags): Promise<void>;
1326
}
1427

1528
export type Fetch = typeof fetch

tests/sdk/flagsmith-cache.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test('test_empty_cache_not_read_but_populated', async () => {
1414
const allFlags = (await flg.getEnvironmentFlags()).allFlags();
1515

1616
expect(set).toBeCalled();
17-
expect(await cache.has('flags')).toBe(true);
17+
expect(await cache.get('flags')).toBeTruthy();
1818

1919
expect(fetch).toBeCalledTimes(1);
2020
expect(allFlags[0].enabled).toBe(true);
@@ -33,7 +33,7 @@ test('test_api_not_called_when_cache_present', async () => {
3333
const allFlags = await (await flg.getEnvironmentFlags()).allFlags();
3434

3535
expect(set).toBeCalled();
36-
expect(await cache.has('flags')).toBe(true);
36+
expect(await cache.get('flags')).toBeTruthy();
3737

3838
expect(fetch).toBeCalledTimes(1);
3939
expect(allFlags[0].enabled).toBe(true);
@@ -88,7 +88,7 @@ test('test_cache_used_for_identity_flags', async () => {
8888
const identityFlags = (await flg.getIdentityFlags(identifier, traits)).allFlags();
8989

9090
expect(set).toBeCalled();
91-
expect(await cache.has('flags-identifier')).toBe(true);
91+
expect(await cache.get('flags-identifier')).toBeTruthy();
9292

9393
expect(fetch).toBeCalledTimes(1);
9494

@@ -115,7 +115,7 @@ test('test_cache_used_for_identity_flags_local_evaluation', async () => {
115115
const identityFlags = (await flg.getIdentityFlags(identifier, traits)).allFlags();
116116

117117
expect(set).toBeCalled();
118-
expect(await cache.has('flags-identifier')).toBe(true);
118+
expect(await cache.get('flags-identifier')).toBeTruthy();
119119

120120
expect(fetch).toBeCalledTimes(1);
121121

tests/sdk/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ export class TestCache implements FlagsmithCache {
1818
return !!this.cache[name];
1919
}
2020

21-
async set(name: string, value: Flags, ttl: number|string) {
21+
async set(name: string, value: Flags) {
2222
this.cache[name] = value;
23-
return true
2423
}
2524
}
2625

0 commit comments

Comments
 (0)