Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
21 changes: 17 additions & 4 deletions sdk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ import { Logger } from 'pino';
import { BaseOfflineHandler } from './offline_handlers.js';

export type IFlagsmithValue<T = string | number | boolean | null> = T;


/**
* Stores and retrieves {@link Flags} from a cache.
*/
export interface FlagsmithCache {
get(key: string): Promise<Flags | undefined> | undefined;
set(key: string, value: Flags, ttl?: string | number): boolean | Promise<boolean>;
has(key: string): boolean | Promise<boolean>;
[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<Flags | undefined>;

/**
* 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<void>;
}

export type Fetch = typeof fetch
Expand Down
17 changes: 4 additions & 13 deletions tests/sdk/flagsmith-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
9 changes: 2 additions & 7 deletions tests/sdk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@ const DATA_DIR = __dirname + '/data/';
export class TestCache implements FlagsmithCache {
cache: Record<string, Flags> = {};

async get(name: string): Promise<Flags> {
async get(name: string): Promise<Flags | undefined> {
return this.cache[name];
}

async has(name: string): Promise<boolean> {
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
}
}

Expand Down