Skip to content

Commit

Permalink
Merge pull request #48 from jensdev/master
Browse files Browse the repository at this point in the history
Replace Axios with built-in fetch
  • Loading branch information
tschoffelen committed Mar 24, 2023
2 parents 065a1df + fc2f402 commit 8d9730c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
3 changes: 0 additions & 3 deletions package.json
Expand Up @@ -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",
Expand Down
18 changes: 11 additions & 7 deletions 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,
Expand Down
15 changes: 8 additions & 7 deletions 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,
Expand Down
31 changes: 29 additions & 2 deletions 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",
Expand All @@ -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",
Expand All @@ -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.");
});
});

0 comments on commit 8d9730c

Please sign in to comment.