Skip to content

Commit 3d04ec7

Browse files
authored
Merge pull request #165 from Flagsmith/feat/simplify-cache-interfac
feat: Simplify FlagsmithCache interface
2 parents 3dcc3f5 + 227233a commit 3d04ec7

File tree

6 files changed

+26
-38
lines changed

6 files changed

+26
-38
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith-nodejs",
3-
"version": "4.0.0",
3+
"version": "5.0.0",
44
"description": "Flagsmith lets you manage features flags and remote config across web, mobile and server side applications. Deliver true Continuous Integration. Get builds out faster. Control who has access to new features.",
55
"main": "./build/cjs/index.js",
66
"type": "module",

sdk/index.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,6 @@ export class Flagsmith {
128128
}
129129

130130
if (!!data.cache) {
131-
const missingMethods: string[] = ['has', 'get', 'set'].filter(
132-
method => data.cache && !data.cache[method]
133-
);
134-
135-
if (missingMethods.length > 0) {
136-
throw new Error(
137-
`Please implement the following methods in your cache: ${missingMethods.join(
138-
', '
139-
)}`
140-
);
141-
}
142131
this.cache = data.cache;
143132
}
144133

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 & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ beforeEach(() => {
44
vi.clearAllMocks();
55
});
66

7-
test('test_wrong_cache_interface_throws_an_error', async () => {
8-
const cache = {
9-
set: () => { },
10-
get: () => { },
11-
};
12-
13-
expect(() => { const flg = flagsmith({ cache }); }).toThrow();
14-
});
15-
167
test('test_empty_cache_not_read_but_populated', async () => {
178
fetch.mockResolvedValue(new Response(flagsJSON));
189

@@ -23,7 +14,7 @@ test('test_empty_cache_not_read_but_populated', async () => {
2314
const allFlags = (await flg.getEnvironmentFlags()).allFlags();
2415

2516
expect(set).toBeCalled();
26-
expect(await cache.has('flags')).toBe(true);
17+
expect(await cache.get('flags')).toBeTruthy();
2718

2819
expect(fetch).toBeCalledTimes(1);
2920
expect(allFlags[0].enabled).toBe(true);
@@ -42,7 +33,7 @@ test('test_api_not_called_when_cache_present', async () => {
4233
const allFlags = await (await flg.getEnvironmentFlags()).allFlags();
4334

4435
expect(set).toBeCalled();
45-
expect(await cache.has('flags')).toBe(true);
36+
expect(await cache.get('flags')).toBeTruthy();
4637

4738
expect(fetch).toBeCalledTimes(1);
4839
expect(allFlags[0].enabled).toBe(true);
@@ -97,7 +88,7 @@ test('test_cache_used_for_identity_flags', async () => {
9788
const identityFlags = (await flg.getIdentityFlags(identifier, traits)).allFlags();
9889

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

10293
expect(fetch).toBeCalledTimes(1);
10394

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

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

129120
expect(fetch).toBeCalledTimes(1);
130121

tests/sdk/utils.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,12 @@ const DATA_DIR = __dirname + '/data/';
1010
export class TestCache implements FlagsmithCache {
1111
cache: Record<string, Flags> = {};
1212

13-
async get(name: string): Promise<Flags> {
13+
async get(name: string): Promise<Flags | undefined> {
1414
return this.cache[name];
1515
}
1616

17-
async has(name: string): Promise<boolean> {
18-
return !!this.cache[name];
19-
}
20-
21-
async set(name: string, value: Flags, ttl: number|string) {
17+
async set(name: string, value: Flags) {
2218
this.cache[name] = value;
23-
return true
2419
}
2520
}
2621

0 commit comments

Comments
 (0)