diff --git a/package-lock.json b/package-lock.json index 86f4e55..3f8e1f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "flagsmith-nodejs", - "version": "4.0.0", + "version": "5.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "flagsmith-nodejs", - "version": "4.0.0", + "version": "5.0.0", "license": "MIT", "dependencies": { "pino": "^8.8.0", diff --git a/package.json b/package.json index dfa08b9..bc69b54 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "flagsmith-nodejs", - "version": "4.0.0", + "version": "5.0.0", "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.", "main": "./build/cjs/index.js", "type": "module", diff --git a/sdk/index.ts b/sdk/index.ts index e67b189..c73ed81 100644 --- a/sdk/index.ts +++ b/sdk/index.ts @@ -128,17 +128,6 @@ export class Flagsmith { } if (!!data.cache) { - const missingMethods: string[] = ['has', 'get', 'set'].filter( - method => data.cache && !data.cache[method] - ); - - if (missingMethods.length > 0) { - throw new Error( - `Please implement the following methods in your cache: ${missingMethods.join( - ', ' - )}` - ); - } this.cache = data.cache; } diff --git a/sdk/types.ts b/sdk/types.ts index db9fb2e..1e99c4e 100644 --- a/sdk/types.ts +++ b/sdk/types.ts @@ -5,11 +5,24 @@ import { Logger } from 'pino'; import { BaseOfflineHandler } from './offline_handlers.js'; export type IFlagsmithValue = T; + + +/** + * Stores and retrieves {@link Flags} from a cache. + */ export interface FlagsmithCache { - get(key: string): Promise | undefined; - set(key: string, value: Flags, ttl?: string | number): boolean | Promise; - has(key: string): boolean | Promise; - [key: string]: any; + /** + * Retrieve the cached {@link Flags} for the given environment or identity, or `undefined` if no cached value exists. + * @param key An environment ID or identity identifier, which is used as the cache key. + */ + get(key: string): Promise; + + /** + * Persist an environment or identity's {@link Flags} in the cache. + * @param key An environment ID or identity identifier, which is used as the cache key. + * @param value The {@link Flags} to be stored in the cache. + */ + set(key: string, value: Flags): Promise; } export type Fetch = typeof fetch diff --git a/tests/sdk/flagsmith-cache.test.ts b/tests/sdk/flagsmith-cache.test.ts index abf0f62..f18df25 100644 --- a/tests/sdk/flagsmith-cache.test.ts +++ b/tests/sdk/flagsmith-cache.test.ts @@ -4,15 +4,6 @@ beforeEach(() => { vi.clearAllMocks(); }); -test('test_wrong_cache_interface_throws_an_error', async () => { - const cache = { - set: () => { }, - get: () => { }, - }; - - expect(() => { const flg = flagsmith({ cache }); }).toThrow(); -}); - test('test_empty_cache_not_read_but_populated', async () => { fetch.mockResolvedValue(new Response(flagsJSON)); @@ -23,7 +14,7 @@ test('test_empty_cache_not_read_but_populated', async () => { const allFlags = (await flg.getEnvironmentFlags()).allFlags(); expect(set).toBeCalled(); - expect(await cache.has('flags')).toBe(true); + expect(await cache.get('flags')).toBeTruthy(); expect(fetch).toBeCalledTimes(1); expect(allFlags[0].enabled).toBe(true); @@ -42,7 +33,7 @@ test('test_api_not_called_when_cache_present', async () => { const allFlags = await (await flg.getEnvironmentFlags()).allFlags(); expect(set).toBeCalled(); - expect(await cache.has('flags')).toBe(true); + expect(await cache.get('flags')).toBeTruthy(); expect(fetch).toBeCalledTimes(1); expect(allFlags[0].enabled).toBe(true); @@ -97,7 +88,7 @@ test('test_cache_used_for_identity_flags', async () => { const identityFlags = (await flg.getIdentityFlags(identifier, traits)).allFlags(); expect(set).toBeCalled(); - expect(await cache.has('flags-identifier')).toBe(true); + expect(await cache.get('flags-identifier')).toBeTruthy(); expect(fetch).toBeCalledTimes(1); @@ -124,7 +115,7 @@ test('test_cache_used_for_identity_flags_local_evaluation', async () => { const identityFlags = (await flg.getIdentityFlags(identifier, traits)).allFlags(); expect(set).toBeCalled(); - expect(await cache.has('flags-identifier')).toBe(true); + expect(await cache.get('flags-identifier')).toBeTruthy(); expect(fetch).toBeCalledTimes(1); diff --git a/tests/sdk/utils.ts b/tests/sdk/utils.ts index 27fcc2e..da62508 100644 --- a/tests/sdk/utils.ts +++ b/tests/sdk/utils.ts @@ -10,17 +10,12 @@ const DATA_DIR = __dirname + '/data/'; export class TestCache implements FlagsmithCache { cache: Record = {}; - async get(name: string): Promise { + async get(name: string): Promise { return this.cache[name]; } - async has(name: string): Promise { - return !!this.cache[name]; - } - - async set(name: string, value: Flags, ttl: number|string) { + async set(name: string, value: Flags) { this.cache[name] = value; - return true } }