UNPKG

magically-sdk

Version:

Official SDK for Magically - Build mobile apps with AI

163 lines (162 loc) 5.13 kB
"use strict"; /** * Platform detection and abstraction layer * Provides environment-specific implementations */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Linking = exports.SecureStore = exports.Platform = exports.WebBrowser = exports.AsyncStorage = void 0; class EdgeStorage { async getItem(key) { // In edge environment, return null (no persistent storage) return null; } async setItem(key, value) { // No-op in edge environment } async removeItem(key) { // No-op in edge environment } } class EdgeWebBrowser { async openBrowserAsync(url) { // Can't open browser in edge environment return { type: 'cancel' }; } maybeCompleteAuthSession() { return { type: 'success' }; } } class EdgePlatform { constructor() { this.OS = 'web'; } select(obj) { return obj.web || obj.default; } } class EdgeLinking { createURL(path, options) { // In edge environment, return a simple URL return path ? `exp://auth/${path}` : 'exp://auth/callback'; } } class EdgeSecureStore { async getItemAsync(key) { // No secure storage in edge environment return null; } async setItemAsync(key, value) { // No-op in edge environment } async deleteItemAsync(key) { // No-op in edge environment } } class WebSecureStore { async getItemAsync(key) { try { // Safe localStorage access for web if (typeof window !== 'undefined' && window.localStorage) { return window.localStorage.getItem(key); } return null; } catch (error) { console.warn('WebSecureStore: Failed to get item', error); return null; } } async setItemAsync(key, value) { try { // Safe localStorage access for web if (typeof window !== 'undefined' && window.localStorage) { window.localStorage.setItem(key, value); } } catch (error) { console.warn('WebSecureStore: Failed to set item', error); } } async deleteItemAsync(key) { try { // Safe localStorage access for web if (typeof window !== 'undefined' && window.localStorage) { window.localStorage.removeItem(key); } } catch (error) { console.warn('WebSecureStore: Failed to delete item', error); } } } // Detect environment function isEdgeEnvironment() { // Most reliable check: caches.default is UNIQUE to Cloudflare Workers if (typeof caches !== 'undefined' && typeof caches.default !== 'undefined') { return true; } // Secondary check: navigator.userAgent (requires global_navigator compatibility flag) if (typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') { return true; } // Default to false - assume React Native/Expo if not detected as Cloudflare Workers return false; } // Initialize based on environment if (isEdgeEnvironment()) { // Edge environment - use stubs exports.AsyncStorage = new EdgeStorage(); exports.WebBrowser = new EdgeWebBrowser(); exports.Platform = new EdgePlatform(); exports.SecureStore = new EdgeSecureStore(); exports.Linking = new EdgeLinking(); } else { // React Native environment - use actual implementations // These will be replaced by the bundler in React Native apps // Import core dependencies first (these should always be available) try { exports.AsyncStorage = require('@react-native-async-storage/async-storage').default; } catch (e) { exports.AsyncStorage = new EdgeStorage(); } try { exports.WebBrowser = require('expo-web-browser'); } catch (e) { exports.WebBrowser = new EdgeWebBrowser(); } try { exports.Platform = require('react-native').Platform; } catch (e) { exports.Platform = new EdgePlatform(); } // Import optional dependencies (may not be installed) // Check if we're on web platform first if (exports.Platform.OS === 'web') { // Web platform uses localStorage through WebSecureStore exports.SecureStore = new WebSecureStore(); } else { // Native platforms use expo-secure-store try { exports.SecureStore = require('expo-secure-store'); } catch (e) { // Fallback to AsyncStorage wrapped in SecureStore interface if expo-secure-store not available exports.SecureStore = { getItemAsync: async (key) => exports.AsyncStorage.getItem(key), setItemAsync: async (key, value) => exports.AsyncStorage.setItem(key, value), deleteItemAsync: async (key) => exports.AsyncStorage.removeItem(key), }; } } try { exports.Linking = require('expo-linking'); } catch (e) { exports.Linking = new EdgeLinking(); } }