-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d20a275
commit 8cc379b
Showing
10 changed files
with
258 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,45 @@ | ||
import { Platform, NativeModules } from 'react-native'; | ||
import { Platform, NativeModules } from "react-native"; | ||
|
||
import { lookupVersion } from './src/utils'; | ||
import { versionCompare } from './src/versions'; | ||
import { lookupVersion } from "./src/utils"; | ||
import { versionCompare } from "./src/versions"; | ||
|
||
const DEFAULT_COUNTRY = 'us'; | ||
const DEFAULT_COUNTRY = "us"; | ||
|
||
export const checkVersion = async(options = {}) => { | ||
// Get options object | ||
const platform = options.platform || Platform.OS; | ||
const country = options.country || DEFAULT_COUNTRY; | ||
const bundleId = options.bundleId || (NativeModules.RNDeviceInfo | ||
? NativeModules.RNDeviceInfo.bundleId | ||
: null); | ||
const currentVersion = options.currentVersion || (NativeModules.RNDeviceInfo | ||
? NativeModules.RNDeviceInfo.appVersion | ||
: ''); | ||
// Get options object | ||
const platform = options.platform || Platform.OS; | ||
const country = options.country || DEFAULT_COUNTRY; | ||
const bundleId = options.bundleId || (NativeModules.RNDeviceInfo | ||
? NativeModules.RNDeviceInfo.bundleId | ||
: null); | ||
const currentVersion = options.currentVersion || (NativeModules.RNDeviceInfo | ||
? NativeModules.RNDeviceInfo.appVersion | ||
: ""); | ||
|
||
// Check if we have retrieved a bundle ID | ||
if (!bundleId && !('RNDeviceInfo' in NativeModules)) { | ||
throw Error( | ||
'[react-native-check-version] Missing react-native-device-info dependency, ' + | ||
'please manually specify a bundleId in the options object.' | ||
); | ||
} | ||
// Check if we have retrieved a bundle ID | ||
if (!bundleId && !("RNDeviceInfo" in NativeModules)) { | ||
throw Error( | ||
"[react-native-check-version] Missing react-native-device-info dependency, " + | ||
"please manually specify a bundleId in the options object." | ||
); | ||
} | ||
|
||
try { | ||
const data = await lookupVersion(platform, bundleId, country); | ||
const version = versionCompare(currentVersion, data.version); | ||
return { platform, bundleId, ...data, ...version }; | ||
} catch (e) { | ||
// On error - return default object | ||
return { | ||
platform, | ||
bundleId, | ||
version: null, | ||
needsUpdate: false, | ||
notes: '', | ||
url: null, | ||
lastChecked: (new Date()).toISOString(), | ||
country, | ||
error: e | ||
}; | ||
} | ||
try { | ||
const data = await lookupVersion(platform, bundleId, country); | ||
const version = versionCompare(currentVersion, data.version); | ||
return { platform, bundleId, ...data, ...version }; | ||
} catch (e) { | ||
// On error - return default object | ||
return { | ||
platform, | ||
bundleId, | ||
version: null, | ||
needsUpdate: false, | ||
notes: "", | ||
url: null, | ||
lastChecked: (new Date()).toISOString(), | ||
country, | ||
error: e | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import axios from "axios"; | ||
|
||
export const getAndroidVersion = async(bundleId, country) => { | ||
const url = `https://play.google.com/store/apps/details?id=${bundleId}&hl=${country}`; | ||
let res; | ||
try { | ||
res = await axios.get(url, { | ||
headers: { | ||
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36" | ||
} | ||
}); | ||
} catch (e) { | ||
console.log(e.response); | ||
if (e.response && e.response.status && e.response.status === 404) { | ||
throw new Error( | ||
`App with bundle ID "${bundleId}" not found in Google Play.` | ||
); | ||
} | ||
throw e; | ||
} | ||
|
||
const version = res.data.match(/\[\[\[['"]((\d+\.)+\d+)['"]\]\],/)[1]; | ||
|
||
return { | ||
version: version || null, | ||
releasedAt: (new Date()).toISOString(), | ||
notes: "", | ||
url: `https://play.google.com/store/apps/details?id=${bundleId}&hl=${country}`, | ||
lastChecked: (new Date()).toISOString() | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import axios from "axios"; | ||
|
||
export const getIosVersion = async(bundleId, country) => { | ||
// Adds a random number to the end of the URL to prevent caching | ||
const url = `https://itunes.apple.com/lookup?lang=en&bundleId=${bundleId}&country=${country}&_=${new Date().valueOf()}`; | ||
|
||
let res = await axios.get(url); | ||
if (!res.data || !("results" in res.data)) { | ||
throw new Error("Unknown error connecting to iTunes."); | ||
} | ||
if (!res.data.results.length) { | ||
throw new Error("App for this bundle ID not found."); | ||
} | ||
|
||
res = res.data.results[0]; | ||
|
||
return { | ||
version: res.version || null, | ||
released: res.currentVersionReleaseDate || res.releaseDate || null, | ||
notes: res.releaseNotes || "", | ||
url: res.trackViewUrl || res.artistViewUrl || res.sellerUrl || null, | ||
country, | ||
lastChecked: (new Date()).toISOString() | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,26 @@ | ||
import axios from 'axios'; | ||
import dateFns from 'date-fns'; | ||
import { getIosVersion } from "./providers/ios"; | ||
import { getAndroidVersion } from "./providers/android"; | ||
|
||
const cache = {}; | ||
|
||
const getTokenValue = (startToken, endToken, tokenSearchData) => { | ||
const indexStart = tokenSearchData.indexOf(startToken); | ||
let value = tokenSearchData.substring(indexStart + startToken.length); | ||
const indexEnd = value.indexOf(endToken); | ||
value = value | ||
.substring(0, indexEnd) | ||
.replace(/<[^>]+>/g, '') | ||
.trim(); | ||
|
||
return value; | ||
}; | ||
|
||
export const lookupVersion = async(platform, bundleId, country = 'us') => { | ||
const key = `${platform}.${bundleId}`; | ||
let res = cache[key]; | ||
if (res) { | ||
return res; | ||
} | ||
|
||
let url; | ||
switch (platform) { | ||
case 'ios': | ||
// Adds a random number to the end of the URL to prevent caching | ||
url = `https://itunes.apple.com/lookup?lang=en&bundleId=${bundleId}&country=${country}&_=${new Date().valueOf()}`; | ||
res = await axios.get(url); | ||
if (!res.data || !('results' in res.data)) { | ||
throw new Error('Unknown error connecting to iTunes.'); | ||
} | ||
if (!res.data.results.length) { | ||
throw new Error('App for this bundle ID not found.'); | ||
} | ||
res = res.data.results[0]; | ||
|
||
res = { | ||
version: res.version || null, | ||
released: res.currentVersionReleaseDate || res.releaseDate || null, | ||
notes: res.releaseNotes || '', | ||
url: res.trackViewUrl || res.artistViewUrl || res.sellerUrl || null, | ||
country, | ||
lastChecked: (new Date()).toISOString() | ||
}; | ||
|
||
cache[key] = res; | ||
return res; | ||
case 'android': | ||
url = `https://apkcombo.com/x/${bundleId}/`; | ||
try { | ||
res = await axios.get(url, { | ||
headers: { | ||
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36' | ||
} | ||
}); | ||
} catch (e) { | ||
console.log(e.response); | ||
if (e.response && e.response.status && e.response.status === 404) { | ||
throw new Error( | ||
`App with bundle ID "${bundleId}" not found in Google Play.` | ||
); | ||
} | ||
throw e; | ||
} | ||
|
||
let resData = res.data; | ||
resData = resData.split('class="information-table">', 2)[1] || ''; | ||
|
||
// Version | ||
const version = (getTokenValue('Version', 'Update', resData) || '').split(' (')[0]; | ||
|
||
// Release Date | ||
let released = null; | ||
try { | ||
released = dateFns.parse(getTokenValue('Update', 'Developer', resData), 'LLLL d, yyyy', new Date()); | ||
} catch (e) { | ||
} | ||
|
||
res = { | ||
version: version || null, | ||
releasedAt: (released && (released).toISOString()) || (new Date()).toISOString(), | ||
notes: '', | ||
url: `https://play.google.com/store/apps/details?id=${bundleId}`, | ||
lastChecked: (new Date()).toISOString() | ||
}; | ||
|
||
cache[key] = res; | ||
return res; | ||
default: | ||
throw new Error('Unsupported platform defined.'); | ||
} | ||
export const lookupVersion = async(platform, bundleId, country = "us") => { | ||
const key = `${platform}.${bundleId}`; | ||
let res = cache[key]; | ||
if (res) { | ||
return res; | ||
} | ||
|
||
switch (platform) { | ||
case "ios": | ||
res = await getIosVersion(bundleId, country); | ||
break; | ||
case "android": | ||
res = await getAndroidVersion(bundleId, country); | ||
break; | ||
default: | ||
throw new Error("Unsupported platform defined."); | ||
} | ||
|
||
cache[key] = res; | ||
return res; | ||
}; |
Oops, something went wrong.