All files storage.ts

90.9% Statements 20/22
100% Branches 14/14
100% Functions 3/3
90.47% Lines 19/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 603x 3x               3x 10x                       3x 5x 5x 4x 4x 3x 3x   1x                                 3x 3x   3x 1x   2x   3x   1x    
import { StorageType } from "./enums";
import { encryptAndSign, verifyAndDecrypt } from "./crypto";
import { StoreOptions } from "./types";
 
/**
 * Resolves the storage object based on type.
 * @param {StorageType} [type=StorageType.Local] - The storage type.
 * @returns {Storage} The storage object.
 */
export function resolveStorage(type: StorageType = StorageType.Local): Storage {
  return type === StorageType.Session ? window.sessionStorage : window.localStorage;
}
 
/**
 * Retrieves persisted state from storage.
 * @template T
 * @param {string} name - Store name.
 * @param {T} initialState - Initial state.
 * @param {Storage} storage - Storage object.
 * @param {StoreOptions} [options] - Store options.
 * @returns {Promise<T>} The persisted state or initial state.
 */
export async function getPersistedState<T>(name: string, initialState: T, storage: Storage, options?: StoreOptions): Promise<T> {
  const raw = storage.getItem(`store:${name}`);
  if (!raw) return initialState;
  try {
    if (options?.enableEncryption && options.encryptionKey) {
      const decrypted = await verifyAndDecrypt(raw, options.encryptionKey);
      return decrypted ? { ...initialState, ...decrypted as T } : initialState;
    } else {
      return { ...initialState, ...JSON.parse(raw) };
    }
  } catch {
    console.warn(`[stadojs] Failed to parse persisted state for store "${name}".`);
    return initialState;
  }
}
 
/**
 * Persists the current state if enabled.
 * @template T
 * @param {string} name - Store name.
 * @param {T} state - State to persist.
 * @param {Storage} storage - Storage object.
 * @param {StoreOptions} [options] - Store options.
 * @returns {Promise<void>}
 */
export async function persistState<T>(name: string, state: T, storage: Storage, options?: StoreOptions) {
  try {
    let toStore: string;
    if (options?.enableEncryption && options.encryptionKey) {
      toStore = await encryptAndSign(state, options.encryptionKey);
    } else {
      toStore = JSON.stringify(state);
    }
    storage.setItem(`store:${name}`, toStore);
  } catch {
    console.warn(`[stadojs] Could not persist state for "${name}".`);
  }
}