expo-storage-universal-native
Version:
Native implementation of expo-storage-universal
98 lines (97 loc) • 3.03 kB
JavaScript
import o from "@react-native-async-storage/async-storage";
import * as r from "expo-secure-store";
const a = o.default ?? o;
class n {
/**
* Creates a new instance of NativeRegularStorage.
*/
constructor() {
}
/**
* Retrieves a value from AsyncStorage.
* @param {string} key - The key of the item to retrieve.
* @returns {Promise<string | undefined>} - A promise that resolves to the retrieved value or undefined if not found.
* @example
* const value = await storage.find('userId');
*/
async find(e) {
try {
return await a.getItem(e) ?? void 0;
} catch (t) {
throw console.error("Error finding item in AsyncStorage:", t), t;
}
}
/**
* Saves a value to AsyncStorage.
* @param {string} key - The key under which the value should be stored.
* @param {string} value - The value to store.
* @returns {Promise<void>} - A promise that resolves when the value has been saved.
* @example
* await storage.save('userId', '12345');
*/
async save(e, t) {
await a.setItem(e, t);
}
/**
* Removes a value from AsyncStorage.
* @param {string} key - The key of the item to remove.
* @returns {Promise<void>} - A promise that resolves when the value has been removed.
* @example
* await storage.remove('userId');
*/
async remove(e) {
await a.removeItem(e);
}
}
class i {
/**
* Creates a new instance of NativeSecureStorage.
* No initialization is required as SecureStore is stateless.
*/
constructor() {
}
/**
* Finds a value from secure storage.
* @param {string} key - The key of the item to retrieve.
* @returns {Promise<string | undefined>} - A promise that resolves to the retrieved value or undefined if not found.
* @throws {Error} If there's an error accessing the secure storage.
*
* @remarks
* - Returns undefined if the key doesn't exist
* - Automatically decrypts the stored value
*/
async find(e) {
return await r.getItemAsync(e) ?? void 0;
}
/**
* Saves a value to secure storage.
* @param {string} key - The key under which the value should be stored.
* @param {string} value - The value to store.
* @returns {Promise<void>} - A promise that resolves when the value has been saved.
* @throws {Error} If there's an error saving to secure storage.
*
* @remarks
* - Automatically encrypts the value before storage
* - If the key already exists, the value will be overwritten
*/
async save(e, t) {
await r.setItemAsync(e, t);
}
/**
* Removes a value from secure storage.
* @param {string} key - The key of the item to remove.
* @returns {Promise<void>} - A promise that resolves when the value has been removed.
* @throws {Error} If there's an error removing from secure storage.
*
* @remarks
* - No-op if the key doesn't exist
* - Permanently deletes the encrypted data
*/
async remove(e) {
await r.deleteItemAsync(e);
}
}
export {
n as NativeRegularStorage,
i as NativeSecureStorage
};