UNPKG

@silicon.js/app-updates

Version:

lightweight react package for checking app updates from ios app store and android play store using react context

37 lines (31 loc) 1.05 kB
import { ANDROID_PLAY_STORE_URL, IOS_APP_STORE_URL } from '../constant'; const fetchIOSVersion = async (appId: string, countryCode: string): Promise<string | null> => { try { const response = await fetch(`${IOS_APP_STORE_URL}?bundleId=${appId}&country=${countryCode}`); const data = await response.json(); if (data.resultCount > 0) { return data.results[0].version; } return null; } catch (error) { throw new Error('Failed to fetch iOS version'); } }; const fetchAndroidVersion = async (appId: string): Promise<string | null> => { try { const response = await fetch(`${ANDROID_PLAY_STORE_URL}?id=${appId}&hl=en`, { method: 'GET' }); const html = await response.text(); // Extract version from Play Store HTML const versionMatch = html.match(/\[\[\["([\d.]+)"\]\]/); if (versionMatch && versionMatch[1]) { return versionMatch[1]; } return null; } catch (error) { throw new Error('Failed to fetch Android version'); } }; export const fetchAppUpdates = { ios: fetchIOSVersion, android: fetchAndroidVersion, };