Skip to content

Commit d04eb63

Browse files
Simplify StorageWrapper interface
1 parent 4e4df83 commit d04eb63

File tree

4 files changed

+12
-24
lines changed

4 files changed

+12
-24
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.AsyncStorageWrapper {
7+
export function createMemoryStorage(): SplitIO.StorageWrapper {
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.SyncStorageWrapper | SplitIO.AsyncStorageWrapper): StorageAdapter | undefined {
22+
function validateStorage(log: ILogger, prefix: string, wrapper?: SplitIO.StorageWrapper): 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
@@ -4,7 +4,7 @@ import { LOG_PREFIX } from './constants';
44
import { StorageAdapter } from '../types';
55

66

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

types/splitio.d.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -463,34 +463,22 @@ interface IClientSideSyncSharedSettings extends IClientSideSharedSettings, ISync
463463
*/
464464
declare namespace SplitIO {
465465

466-
interface SyncStorageWrapper {
466+
interface StorageWrapper {
467467
/**
468468
* Returns the value associated with the given key, or null if the key does not exist.
469+
* If the operation is asynchronous, returns a Promise.
469470
*/
470-
getItem(key: string): string | null;
471+
getItem(key: string): string | null | Promise<string | null>;
471472
/**
472473
* Sets the value for the given key, creating a new key/value pair if key does not exist.
474+
* If the operation is asynchronous, returns a Promise.
473475
*/
474-
setItem(key: string, value: string): void;
476+
setItem(key: string, value: string): void | Promise<void>;
475477
/**
476478
* Removes the key/value pair for the given key, if the key exists.
479+
* If the operation is asynchronous, returns a Promise.
477480
*/
478-
removeItem(key: string): void;
479-
}
480-
481-
interface AsyncStorageWrapper {
482-
/**
483-
* Returns a promise that resolves to the value associated with the given key, or null if the key does not exist.
484-
*/
485-
getItem(key: string): Promise<string | null>;
486-
/**
487-
* 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.
488-
*/
489-
setItem(key: string, value: string): Promise<void>;
490-
/**
491-
* Returns a promise that resolves when the key/value pair for the given key is removed, if the key exists.
492-
*/
493-
removeItem(key: string): Promise<void>;
481+
removeItem(key: string): void | Promise<void>;
494482
}
495483

496484
/**
@@ -1013,7 +1001,7 @@ declare namespace SplitIO {
10131001
*
10141002
* @defaultValue `window.localStorage`
10151003
*/
1016-
wrapper?: SyncStorageWrapper | AsyncStorageWrapper;
1004+
wrapper?: StorageWrapper;
10171005
}
10181006
/**
10191007
* Storage for asynchronous (consumer) SDK.
@@ -1380,7 +1368,7 @@ declare namespace SplitIO {
13801368
*
13811369
* @defaultValue `window.localStorage`
13821370
*/
1383-
wrapper?: SyncStorageWrapper | AsyncStorageWrapper;
1371+
wrapper?: StorageWrapper;
13841372
};
13851373
}
13861374
/**

0 commit comments

Comments
 (0)