UNPKG

@oxyhq/services

Version:

OxyHQ Expo/React Native SDK — UI components, screens, and native features

111 lines (105 loc) 2.76 kB
"use strict"; /** * Create an in-memory storage implementation used as a safe fallback. */ const MEMORY_STORAGE = () => { const store = new Map(); return { async getItem(key) { return store.has(key) ? store.get(key) : null; }, async setItem(key, value) { store.set(key, value); }, async removeItem(key) { store.delete(key); }, async clear() { store.clear(); } }; }; /** * Create a web storage implementation backed by `localStorage`. * Falls back to in-memory storage when unavailable. */ const createWebStorage = () => { if (typeof window === 'undefined' || typeof window.localStorage === 'undefined') { return MEMORY_STORAGE(); } return { async getItem(key) { try { return window.localStorage.getItem(key); } catch { return null; } }, async setItem(key, value) { try { window.localStorage.setItem(key, value); } catch { // Ignore quota or access issues for now. } }, async removeItem(key) { try { window.localStorage.removeItem(key); } catch { // Ignore failures. } }, async clear() { try { window.localStorage.clear(); } catch { // Ignore failures. } } }; }; let asyncStorageInstance = null; /** * Lazily import React Native AsyncStorage implementation. */ const createNativeStorage = async () => { if (asyncStorageInstance) { return asyncStorageInstance; } try { const asyncStorageModule = await import('@react-native-async-storage/async-storage'); asyncStorageInstance = asyncStorageModule.default; return asyncStorageInstance; } catch (error) { if (__DEV__) { console.error('Failed to import AsyncStorage:', error); } throw new Error('AsyncStorage is required in React Native environment'); } }; /** * Detect whether the current runtime is React Native. */ export const isReactNative = () => typeof navigator !== 'undefined' && navigator.product === 'ReactNative'; /** * Create a platform-appropriate storage implementation. * Defaults to in-memory storage when no platform storage is available. */ export const createPlatformStorage = async () => { if (isReactNative()) { return createNativeStorage(); } return createWebStorage(); }; export const STORAGE_KEY_PREFIX = 'oxy_session'; /** * Produce strongly typed storage key names for the supplied prefix. * * @param prefix - Storage key prefix */ export const getStorageKeys = (prefix = STORAGE_KEY_PREFIX) => ({ activeSessionId: `${prefix}_active_session_id`, sessionIds: `${prefix}_session_ids`, language: `${prefix}_language` }); //# sourceMappingURL=storageHelpers.js.map