@turnkey/core
Version:
A core JavaScript web and React Native package for interfacing with Turnkey's infrastructure.
77 lines (73 loc) • 3.52 kB
JavaScript
'use strict';
var enums = require('../../__types__/enums.js');
var utils = require('../../utils.js');
let AsyncStorage;
try {
const mod = require("@react-native-async-storage/async-storage");
AsyncStorage = mod.default ?? mod;
}
catch {
throw new Error("Please install @react-native-async-storage/async-storage in your app to use MobileStorageManager");
}
class MobileStorageManager {
constructor() {
this.getStorageValue = async (sessionKey) => {
const item = await AsyncStorage.getItem(sessionKey);
return item ? JSON.parse(item) : undefined;
};
this.setStorageValue = async (sessionKey, storageValue) => {
await AsyncStorage.setItem(sessionKey, JSON.stringify(storageValue));
};
this.setActiveSessionKey = async (sessionKey) => {
await this.setStorageValue(MobileStorageManager.ACTIVE_SESSION_KEY, sessionKey);
};
this.removeStorageValue = async (sessionKey) => {
await AsyncStorage.removeItem(sessionKey);
};
this.storeSession = async (session, sessionKey = enums.SessionKey.DefaultSessionkey) => {
const sessionWithMetadata = utils.parseSession(session);
await this.setStorageValue(sessionKey, sessionWithMetadata);
const raw = await this.getStorageValue(MobileStorageManager.ALL_SESSION_KEYS);
const keys = Array.isArray(raw) ? raw : [];
if (!keys.includes(sessionKey)) {
keys.push(sessionKey);
await this.setStorageValue(MobileStorageManager.ALL_SESSION_KEYS, keys);
}
await this.setStorageValue(MobileStorageManager.ACTIVE_SESSION_KEY, sessionKey);
};
this.getSession = async (sessionKey = enums.SessionKey.DefaultSessionkey) => {
return this.getStorageValue(sessionKey);
};
this.getActiveSessionKey = async () => {
return this.getStorageValue(MobileStorageManager.ACTIVE_SESSION_KEY);
};
this.getActiveSession = async () => {
const key = await this.getActiveSessionKey();
return key ? this.getSession(key) : undefined;
};
this.listSessionKeys = async () => {
const raw = await this.getStorageValue(MobileStorageManager.ALL_SESSION_KEYS);
return Array.isArray(raw) ? raw : [];
};
this.clearSession = async (sessionKey) => {
await this.removeStorageValue(sessionKey);
const keys = await this.listSessionKeys();
const updated = keys.filter((k) => k !== sessionKey);
await this.setStorageValue(MobileStorageManager.ALL_SESSION_KEYS, updated);
const active = await this.getActiveSessionKey();
if (active === sessionKey) {
await this.removeStorageValue(MobileStorageManager.ACTIVE_SESSION_KEY);
}
};
this.clearAllSessions = async () => {
const keys = await this.listSessionKeys();
await Promise.all(keys.map((k) => AsyncStorage.removeItem(k)));
await this.removeStorageValue(MobileStorageManager.ALL_SESSION_KEYS);
await this.removeStorageValue(MobileStorageManager.ACTIVE_SESSION_KEY);
};
}
}
MobileStorageManager.ALL_SESSION_KEYS = "@turnkey/all-session-keys";
MobileStorageManager.ACTIVE_SESSION_KEY = "@turnkey/active-session-key";
exports.MobileStorageManager = MobileStorageManager;
//# sourceMappingURL=storage.js.map