Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only try to JSON.parse from iTunes API when it's a successful response #46

Open
jfcorsini opened this issue Sep 2, 2022 · 1 comment

Comments

@jfcorsini
Copy link

jfcorsini commented Sep 2, 2022

Hi! 馃憢

Firstly, thanks for your work on this project! 馃檪

Sometimes when using the iTunes API, I was getting a 400 error, which would get thrown only when trying to parse the JSON, which meant that the content returned by the API was not a proper JSON. In order to get a more clear error message, would be great to return a different message.

I was not able to find the proper API documentation for the iTunes API (specially what is returned in a 400 / 404/, but I have decided to add a header 'Accept': 'application/json', to make sure we always get JSON returned.

Ideally, we could also extend the errors from this library to contain an enum that identify what went wrong, e.g. AppNotFound, ApiRequestFailed, NoConnection, etc. If @watanabeyu thinks this would be useful, I can create a Pull Request 馃槃

Here is the diff from patch-package that solved my problem:

diff --git a/node_modules/react-native-store-version/dist/ios.js b/node_modules/react-native-store-version/dist/ios.js
index 59098da..1fec82e 100644
--- a/node_modules/react-native-store-version/dist/ios.js
+++ b/node_modules/react-native-store-version/dist/ios.js
@@ -3,16 +3,20 @@ export const getIOSVersion = async (storeURL = '', country = 'jp') => {
     if (!appID) {
         throw new Error('iosStoreURL is invalid.');
     }
-    const response = await fetch(`https://itunes.apple.com/lookup?id=${appID[1]}&country=${country}&${new Date().getTime()}`, {
+    const response = await fetch(`https://itunes.apple.com/lookup?id=${appID[1]}&country=${country}`, {
         headers: {
+            'Accept': 'application/json',
             'cache-control': 'no-cache',
         },
     })
-        .then((r) => r.text())
-        .then((r) => JSON.parse(r));
-    if (response.results.length === 0) {
+    if (response.status !== 200) {
+        throw new Error(`iTunes API returned an error with code: ${response.status}`);
+    }
+    const results = (await response.json()).results
+
+    if (results.length === 0) {
         throw new Error(`appID(${appID[1]}) is not released.`);
     }
-    return response.results[0].version;
+    return results[0].version;
 };

This issue body was partially generated by patch-package.

@watanabeyu
Copy link
Owner

Hi @jfcorsini

Thanks your submission, it is very excited.
If you don't mind, can you submit a PR?

thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants