diff --git a/package.json b/package.json index cef6571..322527c 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,6 @@ "test": "jest", "lint": "prettier --write ." }, - "dependencies": { - "axios": "^0.27.2" - }, "devDependencies": { "@babel/core": "^7.11.1", "@babel/plugin-proposal-class-properties": "^7.10.4", diff --git a/src/providers/android.js b/src/providers/android.js index cb9d619..b304c0d 100644 --- a/src/providers/android.js +++ b/src/providers/android.js @@ -1,25 +1,29 @@ -import axios from "axios"; - -export const getAndroidVersion = async(bundleId, country) => { +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, { + res = await fetch(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", 'sec-fetch-site': 'same-origin' } }); } catch (e) { - if (e.response && e.response.status && e.response.status === 404) { + throw e; + } + + if (!res.ok) { + + if (res.status === 404) { throw new Error( `App with bundle ID "${bundleId}" not found in Google Play.` ); } - throw e; + throw res.statusText } - const version = res.data.match(/\[\[\[['"]((\d+\.)+\d+)['"]\]\],/)[1]; + const text = await res.text(); + const version = text.match(/\[\[\[['"]((\d+\.)+\d+)['"]\]\],/)[1]; return { version: version || null, diff --git a/src/providers/ios.js b/src/providers/ios.js index bf286c3..58d5453 100644 --- a/src/providers/ios.js +++ b/src/providers/ios.js @@ -1,18 +1,19 @@ -import axios from "axios"; - -export const getIosVersion = async(bundleId, country) => { +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)) { + let res = await fetch(url); + + const data = await res.json(); + + if (!data || !("results" in data)) { throw new Error("Unknown error connecting to iTunes."); } - if (!res.data.results.length) { + if (!data.results.length) { throw new Error("App for this bundle ID not found."); } - res = res.data.results[0]; + res = data.results[0]; return { version: res.version || null, diff --git a/tests/index.test.js b/tests/index.test.js index d442d57..eed01d0 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -1,7 +1,9 @@ import { checkVersion } from "../index"; +global.fetch = jest.requireActual('node-fetch') + describe("checkVersion", () => { - test("can get version for valid bundle ID for Android", async() => { + test("can get version for valid bundle ID for Android", async () => { const data = await checkVersion({ platform: "android", bundleId: "com.streetartcities.map", @@ -13,7 +15,20 @@ describe("checkVersion", () => { expect(data.needsUpdate).toEqual(true); }); - test("can get version for valid bundle ID for iOS", async() => { + test("can get version for unknown bundle ID for Android with error property", async () => { + const data = await checkVersion({ + platform: "android", + bundleId: "com.notarealapp.unknown", + currentVersion: "1.0.0" + }); + + expect(data.platform).toEqual("android"); + expect(data.bundleId).toEqual("com.notarealapp.unknown"); + expect(data.error.toString()).toEqual('Error: App with bundle ID "com.notarealapp.unknown" not found in Google Play.'); + }); + + + test("can get version for valid bundle ID for iOS", async () => { const data = await checkVersion({ platform: "ios", bundleId: "nl.hoyapp.mobile", @@ -24,4 +39,16 @@ describe("checkVersion", () => { expect(data.bundleId).toEqual("nl.hoyapp.mobile"); expect(data.needsUpdate).toEqual(true); }); + + test("can get version for unknown bundle ID for iOS with error property", async () => { + const data = await checkVersion({ + platform: "ios", + bundleId: "com.notarealapp.unknown", + currentVersion: "1.0.0" + }); + + expect(data.platform).toEqual("ios"); + expect(data.bundleId).toEqual("com.notarealapp.unknown"); + expect(data.error.toString()).toEqual("Error: App for this bundle ID not found."); + }); });