Skip to content

feat: Simplify FlagsmithCache interface #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
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