@turnkey/core
Version:
A core JavaScript web and React Native package for interfacing with Turnkey's infrastructure.
78 lines (75 loc) • 3.75 kB
JavaScript
import WindowWrapper from '../../__polyfills__/window.mjs';
import { SessionKey } from '../../__types__/base.mjs';
import { parseSession } from '../../utils.mjs';
import { TurnkeyError, TurnkeyErrorCodes } from '@turnkey/sdk-types';
const browserStorage = WindowWrapper.localStorage;
class WebStorageManager {
constructor() {
this.getStorageValue = async (sessionKey) => {
const item = browserStorage.getItem(sessionKey);
return item ? JSON.parse(item) : undefined;
};
this.setStorageValue = async (sessionKey, storageValue) => {
if (storageValue === undefined) {
throw new TurnkeyError("Session value cannot be undefined", TurnkeyErrorCodes.STORE_SESSION_ERROR);
}
browserStorage.setItem(sessionKey, JSON.stringify(storageValue));
};
this.setActiveSessionKey = async (sessionKey) => {
await this.setStorageValue(WebStorageManager.ACTIVE_SESSION_KEY, sessionKey);
};
this.removeStorageValue = async (sessionKey) => {
browserStorage.removeItem(sessionKey);
};
this.storeSession = async (session, sessionKey = SessionKey.DefaultSessionkey) => {
const sessionWithMetadata = parseSession(session);
await this.setStorageValue(sessionKey, sessionWithMetadata);
// Ensure the session key is stored in the session keys list
const keys = (await this.getStorageValue(WebStorageManager.ALL_SESSION_KEYS)) ?? [];
if (!keys.includes(sessionKey)) {
keys.push(sessionKey);
await this.setStorageValue(WebStorageManager.ALL_SESSION_KEYS, keys);
}
// Set the active session key
await this.setStorageValue(WebStorageManager.ACTIVE_SESSION_KEY, sessionKey);
};
this.getSession = async (sessionKey = SessionKey.DefaultSessionkey) => {
return this.getStorageValue(sessionKey);
};
this.getActiveSessionKey = async () => {
return this.getStorageValue(WebStorageManager.ACTIVE_SESSION_KEY);
};
this.getActiveSession = async () => {
const key = await this.getActiveSessionKey();
return key ? this.getSession(key) : undefined;
};
this.listSessionKeys = async () => {
return ((await this.getStorageValue(WebStorageManager.ALL_SESSION_KEYS)) ?? []);
};
this.clearSession = async (sessionKey) => {
await this.removeStorageValue(sessionKey);
const keys = await this.listSessionKeys();
const updated = keys.filter((k) => k !== sessionKey);
await this.setStorageValue(WebStorageManager.ALL_SESSION_KEYS, updated);
const active = await this.getActiveSessionKey();
if (active === sessionKey) {
await this.removeStorageValue(WebStorageManager.ACTIVE_SESSION_KEY);
}
};
this.clearAllSessions = async () => {
const keys = await this.listSessionKeys();
await Promise.all(keys.map((k) => this.removeStorageValue(k)));
await this.removeStorageValue(WebStorageManager.ALL_SESSION_KEYS);
await this.removeStorageValue(WebStorageManager.ACTIVE_SESSION_KEY);
};
this.storeWallets = async (wallets) => {
for (const wallet of wallets) {
browserStorage.setItem(wallet.walletId, JSON.stringify(wallet));
}
};
}
}
WebStorageManager.ALL_SESSION_KEYS = "@turnkey/all-session-keys";
WebStorageManager.ACTIVE_SESSION_KEY = "@turnkey/active-session-key";
export { WebStorageManager };
//# sourceMappingURL=storage.mjs.map