UNPKG

nexus-react-core

Version:

A comprehensive React toolkit with services, hooks, and Redux store management

194 lines 5.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.detectPlatformDataSync = exports.isMobilePlatform = exports.isTelegramPlatform = exports.detectPlatformData = void 0; /** * Get user's IP address using a free IP detection service */ const getUserIP = async () => { try { // Using ipify.org - a free, simple IP detection service const response = await fetch('https://api.ipify.org?format=json'); const data = await response.json(); return data.ip || 'Unknown'; } catch (error) { console.warn('Failed to detect IP address:', error); return 'Unknown'; } }; /** * Detects the current platform and device information * Returns platform data to send with auth requests */ const detectPlatformData = async () => { if (typeof window === 'undefined') { // Server-side fallback return { platform: 'web', userAgent: '', deviceInfo: { type: 'desktop', os: 'Unknown', browser: 'Unknown', version: 'Unknown' }, ipAddress: 'Unknown' }; } const userAgent = navigator.userAgent; const deviceInfo = getDeviceInfo(userAgent); // Get IP address const ipAddress = await getUserIP(); let platformData; // 1. Check if running in Telegram WebApp if (window.Telegram?.WebApp || userAgent.includes('Telegram') || window.location.pathname.includes('/telegram')) { platformData = { platform: 'telegram', userAgent, deviceInfo, ipAddress }; } // 2. Check if mobile device else if (deviceInfo.type === 'mobile' || deviceInfo.type === 'tablet') { platformData = { platform: 'mobile', userAgent, deviceInfo, ipAddress }; } // 3. Default to web else { platformData = { platform: 'web', userAgent, deviceInfo, ipAddress }; } return platformData; }; exports.detectPlatformData = detectPlatformData; /** * Extract device information from user agent */ const getDeviceInfo = (userAgent) => { const ua = userAgent.toLowerCase(); // Detect device type let type = 'desktop'; if (ua.includes('mobile') && !ua.includes('tablet')) { type = 'mobile'; } else if (ua.includes('tablet') || ua.includes('ipad')) { type = 'tablet'; } // Detect OS let os = 'Unknown'; if (ua.includes('windows')) os = 'Windows'; else if (ua.includes('mac os')) os = 'macOS'; else if (ua.includes('linux')) os = 'Linux'; else if (ua.includes('android')) os = 'Android'; else if (ua.includes('ios') || ua.includes('iphone') || ua.includes('ipad')) os = 'iOS'; // Detect Browser let browser = 'Unknown'; let version = 'Unknown'; if (ua.includes('chrome') && !ua.includes('edg')) { browser = 'Chrome'; const match = ua.match(/chrome\/(\d+)/); version = match ? match[1] : 'Unknown'; } else if (ua.includes('firefox')) { browser = 'Firefox'; const match = ua.match(/firefox\/(\d+)/); version = match ? match[1] : 'Unknown'; } else if (ua.includes('safari') && !ua.includes('chrome')) { browser = 'Safari'; const match = ua.match(/version\/(\d+)/); version = match ? match[1] : 'Unknown'; } else if (ua.includes('edg')) { browser = 'Edge'; const match = ua.match(/edg\/(\d+)/); version = match ? match[1] : 'Unknown'; } return { type, os, browser, version }; }; /** * Simple function to check if user is on Telegram */ const isTelegramPlatform = () => { if (typeof window === 'undefined') return false; return !!(window.Telegram?.WebApp || navigator.userAgent.includes('Telegram') || window.location.pathname.includes('/telegram')); }; exports.isTelegramPlatform = isTelegramPlatform; /** * Simple function to check if user is on mobile */ const isMobilePlatform = () => { if (typeof window === 'undefined') return false; const ua = navigator.userAgent.toLowerCase(); return ua.includes('mobile') || ua.includes('tablet') || ua.includes('ipad'); }; exports.isMobilePlatform = isMobilePlatform; /** * Synchronous version for immediate checks (without IP address) */ const detectPlatformDataSync = () => { if (typeof window === 'undefined') { return { platform: 'web', userAgent: '', deviceInfo: { type: 'desktop', os: 'Unknown', browser: 'Unknown', version: 'Unknown' } }; } const userAgent = navigator.userAgent; const deviceInfo = getDeviceInfo(userAgent); let platformData; // 1. Check if running in Telegram WebApp if (window.Telegram?.WebApp || userAgent.includes('Telegram') || window.location.pathname.includes('/telegram')) { platformData = { platform: 'telegram', userAgent, deviceInfo }; } // 2. Check if mobile device else if (deviceInfo.type === 'mobile' || deviceInfo.type === 'tablet') { platformData = { platform: 'mobile', userAgent, deviceInfo }; } // 3. Default to web else { platformData = { platform: 'web', userAgent, deviceInfo }; } return platformData; }; exports.detectPlatformDataSync = detectPlatformDataSync; //# sourceMappingURL=platformDetection.js.map