Skip to content

Commit a1c091b

Browse files
refactor: split StorageWrapper into sync and async interfaces for better type safety
1 parent c594a6a commit a1c091b

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

src/storages/inLocalStorage/__tests__/wrapper.mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock';
44

55
export const PREFIX = 'SPLITIO';
66

7-
export function createMemoryStorage(): SplitIO.StorageWrapper {
7+
export function createMemoryStorage(): SplitIO.AsyncStorageWrapper {
88
let cache: Record<string, string> = {};
99
return {
1010
getItem(key: string) {

src/storages/inLocalStorage/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { ILogger } from '../../logger/types';
1919
import SplitIO from '../../../types/splitio';
2020
import { storageAdapter } from './storageAdapter';
2121

22-
function validateStorage(log: ILogger, prefix: string, wrapper?: SplitIO.StorageWrapper): StorageAdapter | undefined {
22+
function validateStorage(log: ILogger, prefix: string, wrapper?: SplitIO.SyncStorageWrapper | SplitIO.AsyncStorageWrapper): StorageAdapter | undefined {
2323
if (wrapper) {
2424
if (isValidStorageWrapper(wrapper)) return storageAdapter(log, prefix, wrapper);
2525
log.warn(LOG_PREFIX + 'Invalid storage provided. Falling back to LocalStorage API');

src/storages/inLocalStorage/storageAdapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function isTillKey(key: string) {
77
return key.endsWith('.till');
88
}
99

10-
export function storageAdapter(log: ILogger, prefix: string, wrapper: SplitIO.StorageWrapper): Required<StorageAdapter> {
10+
export function storageAdapter(log: ILogger, prefix: string, wrapper: SplitIO.SyncStorageWrapper | SplitIO.AsyncStorageWrapper): Required<StorageAdapter> {
1111
let keys: string[] = [];
1212
let cache: Record<string, string> = {};
1313

types/splitio.d.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,19 +458,34 @@ interface IClientSideSyncSharedSettings extends IClientSideSharedSettings, ISync
458458
*/
459459
declare namespace SplitIO {
460460

461-
interface StorageWrapper {
461+
interface SyncStorageWrapper {
462462
/**
463-
* Returns a promise that resolves to the current value associated with the given key, or null if the given key does not exist.
463+
* Returns the value associated with the given key, or null if the key does not exist.
464464
*/
465-
getItem(key: string): Promise<string | null> | string | null;
465+
getItem(key: string): string | null;
466466
/**
467-
* Returns a promise that resolves when the value of the pair identified by key is set to value, creating a new key/value pair if none existed for key previously.
467+
* Sets the value for the given key, creating a new key/value pair if key does not exist.
468468
*/
469-
setItem(key: string, value: string): Promise<void> | void;
469+
setItem(key: string, value: string): void;
470470
/**
471-
* Returns a promise that resolves when the key/value pair with the given key is removed, if a key/value pair with the given key exists.
471+
* Removes the key/value pair for the given key, if the key exists.
472472
*/
473-
removeItem(key: string): Promise<void> | void;
473+
removeItem(key: string): void;
474+
}
475+
476+
interface AsyncStorageWrapper {
477+
/**
478+
* Returns a promise that resolves to the value associated with the given key, or null if the key does not exist.
479+
*/
480+
getItem(key: string): Promise<string | null>;
481+
/**
482+
* Returns a promise that resolves when the value of the pair identified by key is set to value, creating a new key/value pair if key does not exist.
483+
*/
484+
setItem(key: string, value: string): Promise<void>;
485+
/**
486+
* Returns a promise that resolves when the key/value pair for the given key is removed, if the key exists.
487+
*/
488+
removeItem(key: string): Promise<void>;
474489
}
475490

476491
/**
@@ -992,7 +1007,7 @@ declare namespace SplitIO {
9921007
*
9931008
* @defaultValue `window.localStorage`
9941009
*/
995-
wrapper?: StorageWrapper;
1010+
wrapper?: SyncStorageWrapper | AsyncStorageWrapper;
9961011
}
9971012
/**
9981013
* Storage for asynchronous (consumer) SDK.
@@ -1338,7 +1353,7 @@ declare namespace SplitIO {
13381353
*
13391354
* @defaultValue `window.localStorage`
13401355
*/
1341-
wrapper?: StorageWrapper;
1356+
wrapper?: SyncStorageWrapper | AsyncStorageWrapper;
13421357
};
13431358
}
13441359
/**

0 commit comments

Comments
 (0)