├── .gitignore ├── .eslintignore ├── .github ├── FUNDING.yml └── workflows │ ├── pr.yml │ └── release.yml ├── babel.config.js ├── .prettierrc.js ├── .npmignore ├── .vscode └── settings.json ├── __tests__ ├── ios.test.ts ├── index.test.ts └── android.test.ts ├── tsconfig.json ├── src ├── android.ts ├── ios.ts └── index.ts ├── LICENSE.md ├── .eslintrc.js ├── README.md └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/**/* 2 | .expo/* 3 | npm-debug.* 4 | dist/ -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | dist/ 3 | .eslintrc.js 4 | babel.config.js -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: watanabeyu -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: ['module:metro-react-native-babel-preset'], 3 | }; 4 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | tabWidth: 2, 3 | trailingComma: 'es5', 4 | singleQuote: true, 5 | semi: true, 6 | } 7 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | example 2 | node_modules 3 | extras 4 | src 5 | tsconfig.json 6 | __tests__ 7 | .vscode 8 | .eslintignore 9 | .eslintrc 10 | .gitignore 11 | .circleci -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.formatOnSave": true, 3 | "editor.codeActionsOnSave": { 4 | "source.fixAll.eslint": true 5 | }, 6 | "editor.defaultFormatter": "esbenp.prettier-vscode" 7 | } 8 | -------------------------------------------------------------------------------- /__tests__/ios.test.ts: -------------------------------------------------------------------------------- 1 | import { getIOSVersion } from '../src/ios'; 2 | 3 | jest.mock('node-fetch'); 4 | 5 | describe('ios', () => { 6 | beforeEach(() => { 7 | jest.mock('react-native', () => ({ Platform: { os: 'ios' } })); 8 | }); 9 | 10 | it('get version', async () => { 11 | try { 12 | await getIOSVersion('https://apps.apple.com/jp/app/github/id1477376905'); 13 | } catch (e) { 14 | expect(e).toHaveProperty('message'); 15 | } 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /.github/workflows/pr.yml: -------------------------------------------------------------------------------- 1 | name: PR for master 2 | on: 3 | pull_request: 4 | types: ['opened', 'synchronize'] 5 | branches: 6 | - 'master' 7 | 8 | jobs: 9 | build_and_deploy: 10 | name: build test 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - name: Checkout Repo 15 | uses: actions/checkout@master 16 | 17 | - name: Install Dependencies 18 | run: npm install 19 | 20 | - name: Test 21 | run: npm run test 22 | 23 | - name: Build 24 | run: npm run build 25 | -------------------------------------------------------------------------------- /__tests__/index.test.ts: -------------------------------------------------------------------------------- 1 | import { compareVersion } from '../src'; 2 | 3 | describe('compare', () => { 4 | beforeEach(() => { 5 | jest.mock('react-native', () => ({ Platform: { os: 'ios' } })); 6 | }); 7 | 8 | it('compare version', () => { 9 | const remoteIsOld = compareVersion('1.1.0', '1.0.3'); 10 | expect(remoteIsOld).toBe('old'); 11 | 12 | const remoteIsNew = compareVersion('1.0.0', '1.1.0'); 13 | expect(remoteIsNew).toBe('new'); 14 | 15 | const equal = compareVersion('1.1.0', '1.1.0'); 16 | expect(equal).toBe('equal'); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ESNEXT", 4 | "module": "ESNext", 5 | "lib": ["esnext"], 6 | "skipLibCheck": true, 7 | "declaration": true, 8 | "moduleResolution": "node", 9 | "outDir": "dist", 10 | "removeComments": true, 11 | "strict": true, 12 | "noUnusedLocals": true, 13 | "noUnusedParameters": true, 14 | "esModuleInterop": true, 15 | "inlineSourceMap": true, 16 | "noEmitOnError": false, 17 | "inlineSources": true, 18 | "baseUrl": "." 19 | }, 20 | "include": ["src/**/*"], 21 | "exclude": ["node_modules", "dist"] 22 | } 23 | -------------------------------------------------------------------------------- /__tests__/android.test.ts: -------------------------------------------------------------------------------- 1 | import { getAndroidVersion } from '../src/android'; 2 | 3 | jest.mock('node-fetch'); 4 | 5 | describe('android', () => { 6 | beforeEach(() => { 7 | jest.mock('react-native', () => ({ Platform: { os: 'android' } })); 8 | jest.mock('react-native', () => ({ Platform: { os: 'android' } })); 9 | }); 10 | 11 | it('get version', async () => { 12 | try { 13 | await getAndroidVersion( 14 | 'https://play.google.com/store/apps/details?id=com.github.android' 15 | ); 16 | } catch (e) { 17 | expect(e).toHaveProperty('message'); 18 | } 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /src/android.ts: -------------------------------------------------------------------------------- 1 | export const getAndroidVersion = async (storeURL = '') => { 2 | if ( 3 | !storeURL.match( 4 | /^https?:\/\/play\.google\.com\/store\/apps\/details\?id=[0-9a-zA-Z.]+/ 5 | ) 6 | ) { 7 | throw new Error('androidStoreURL is invalid.'); 8 | } 9 | 10 | const response = await fetch(storeURL).then((r) => { 11 | if (r.status === 200) { 12 | return r.text(); 13 | } 14 | 15 | throw new Error('androidStoreURL is invalid.'); 16 | }); 17 | 18 | const matches = response.match(/\[\[\[['"]((\d+\.)+\d+)['"]\]\],/); 19 | 20 | if (!matches) { 21 | throw new Error("can't get android app version."); 22 | } 23 | 24 | return matches[1]; 25 | }; 26 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: publish package 2 | on: 3 | release: 4 | types: [published] 5 | 6 | jobs: 7 | build_and_deploy: 8 | name: build test 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout Repo 13 | uses: actions/checkout@master 14 | 15 | - uses: actions/setup-node@v3 16 | with: 17 | registry-url: 'https://registry.npmjs.org' 18 | 19 | - name: Install Dependencies 20 | run: npm install 21 | 22 | - name: Test 23 | run: npm run test 24 | 25 | - name: Build 26 | run: npm run build 27 | 28 | - name: Pubish 29 | run: npm publish 30 | env: 31 | NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} 32 | -------------------------------------------------------------------------------- /src/ios.ts: -------------------------------------------------------------------------------- 1 | export const getIOSVersion = async (storeURL = '', country = 'jp') => { 2 | const appID = storeURL.match(/.+id([0-9]+)\??/); 3 | 4 | if (!appID) { 5 | throw new Error('iosStoreURL is invalid.'); 6 | } 7 | 8 | const response = await fetch( 9 | `https://itunes.apple.com/lookup?id=${ 10 | appID[1] 11 | }&country=${country}&${new Date().getTime()}`, 12 | { 13 | headers: { 14 | 'cache-control': 'no-cache', 15 | }, 16 | } 17 | ) 18 | .then((r) => r.text()) 19 | .then((r) => JSON.parse(r)); 20 | 21 | if (!response || !response.results || response.results.length === 0) { 22 | throw new Error(`appID(${appID[1]}) is not released.`); 23 | } 24 | 25 | return response.results[0].version as string; 26 | }; 27 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 Yu Watanabe - http://www.bad-company.jp - fight.ippatu5648@gmail.com 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parser: '@typescript-eslint/parser', 4 | plugins: ['import', 'unused-imports'], 5 | extends: [ 6 | 'eslint:recommended', 7 | 'plugin:@typescript-eslint/recommended', 8 | 'prettier', 9 | ], 10 | settings: { 11 | 'import/extensions': ['.js', '.jsx', '.ts', '.tsx'], 12 | 'import/core-modules': ['app'], 13 | 'import/resolver': { 14 | node: { 15 | extensions: ['.js', '.jsx', '.ts', '.tsx'], 16 | }, 17 | }, 18 | 'import/no-unresolved': [ 19 | 2, 20 | { 21 | ignore: ['^app/.+$'], 22 | }, 23 | ], 24 | }, 25 | rules: { 26 | '@typescript-eslint/explicit-module-boundary-types': 'off', 27 | '@typescript-eslint/no-unused-vars': 'off', 28 | 'unused-imports/no-unused-imports': 'warn', 29 | 'import/order': [ 30 | 'warn', 31 | { 32 | groups: [ 33 | 'builtin', 34 | 'external', 35 | 'internal', 36 | 'parent', 37 | 'sibling', 38 | 'index', 39 | 'object', 40 | 'type', 41 | ], 42 | 'newlines-between': 'always', 43 | pathGroupsExcludedImportTypes: ['internal'], 44 | alphabetize: { order: 'asc', caseInsensitive: true }, 45 | pathGroups: [ 46 | { pattern: 'app/**', group: 'external', position: 'after' }, 47 | ], 48 | }, 49 | ], 50 | }, 51 | } 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-store-version 2 | 3 | This module check an app's version on google playstore or ios app store. 4 | By writing code successfully, you can make a forced update. 5 | 6 | I've only been updating occasionally, but I'd be happy to sponsor you to keep me motivated. 7 | https://github.com/sponsors/watanabeyu 8 | 9 | ## Installation 10 | 11 | ```bash 12 | $ npm install --save react-native-store-version 13 | ``` 14 | 15 | ## CHANGELOG 16 | 17 | ### v1.4.0 18 | 19 | - Sorry for the long wait for an update. 20 | - fix android.ts by [hussainimdad004](https://github.com/hussainimdad004) 21 | - https://github.com/watanabeyu/react-native-store-version/issues/39 22 | - use prettier 23 | - move example to [snack](https://snack.expo.dev/@watanabe_yu/react-native-store-version-example) 24 | 25 | ### v1.3.0 26 | 27 | - if failed, throw an error. 28 | - add result detail. 29 | 30 | ## Usage 31 | 32 | ```tsx 33 | import checkVersion from 'react-native-store-version'; 34 | 35 | export default function App() { 36 | useEffect(() => { 37 | const init = async () => { 38 | try { 39 | const check = await checkVersion({ 40 | version: '1.0.0', // app local version 41 | iosStoreURL: 'ios app store url', 42 | androidStoreURL: 'android app store url', 43 | country: 'jp', // default value is 'jp' 44 | }); 45 | 46 | if (check.result === 'new') { 47 | // if app store version is new 48 | } 49 | } catch (e) { 50 | console.log(e); 51 | } 52 | }; 53 | 54 | init(); 55 | }, []); 56 | } 57 | ``` 58 | 59 | ## Return value 60 | 61 | ```jsx 62 | // correct 63 | { 64 | local: "1.0.0", 65 | remote: "1.1.0", 66 | result: "new", // "new" | "old" | "equal" 67 | detail: "remote > local", // "remote > local" | "remote < local" | "remote === local" 68 | } 69 | 70 | // catch error 71 | { 72 | message: "string", 73 | } 74 | ``` 75 | 76 | result compare from a `local` to `remote`. 77 | If `local(1.0.0)` and `remote(1.1.0)`, result is new. 78 | 79 | ## Example 80 | 81 | [Check out example on snack](https://snack.expo.dev/@watanabe_yu/react-native-store-version-example) 82 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-store-version", 3 | "version": "1.4.1", 4 | "main": "dist/index.js", 5 | "types": "dist/index.d.ts", 6 | "license": "MIT", 7 | "scripts": { 8 | "build": "tsc --project tsconfig.json", 9 | "test": "jest" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git@github.com:watanabeyu/react-native-store-version.git" 14 | }, 15 | "keywords": [ 16 | "react-native", 17 | "expo", 18 | "appstore", 19 | "playstore", 20 | "version" 21 | ], 22 | "author": "Yu Watanabe (https://github.com/watanabeyu)", 23 | "bugs": { 24 | "url": "https://github.com/watanabeyu/react-native-store-version/issues" 25 | }, 26 | "homepage": "https://github.com/watanabeyu/react-native-store-version", 27 | "dependencies": { 28 | "compare-versions": "^4.1.3" 29 | }, 30 | "devDependencies": { 31 | "@types/jest": "^28.1.6", 32 | "@types/react-native": "^0.69.5", 33 | "@typescript-eslint/eslint-plugin": "^5.32.0", 34 | "@typescript-eslint/parser": "^5.32.0", 35 | "babel-jest": "^28.1.3", 36 | "eslint": "^8.21.0", 37 | "eslint-config-prettier": "^8.5.0", 38 | "eslint-plugin-import": "^2.26.0", 39 | "eslint-plugin-unused-imports": "^2.0.0", 40 | "jest": "^28.1.3", 41 | "metro-react-native-babel-preset": "^0.72.0", 42 | "node-fetch": "^3.2.10", 43 | "prettier": "^2.7.1", 44 | "react-native": "^0.69.3", 45 | "ts-jest": "^28.0.7", 46 | "typescript": "^4.7.4" 47 | }, 48 | "jest": { 49 | "preset": "react-native", 50 | "moduleNameMapper": { 51 | "^app(.*)$": "/$1" 52 | }, 53 | "moduleFileExtensions": [ 54 | "ts", 55 | "tsx", 56 | "js" 57 | ], 58 | "transform": { 59 | "^.+\\.(ts|tsx)$": "ts-jest" 60 | }, 61 | "globals": { 62 | "ts-jest": { 63 | "tsconfig": "tsconfig.json", 64 | "diagnostics": false 65 | } 66 | }, 67 | "testMatch": [ 68 | "**/__tests__/**/*.test.(ts|tsx|js)", 69 | "**/src/**/*.test.(ts|tsx|js)" 70 | ], 71 | "testPathIgnorePatterns": [ 72 | "node_modules", 73 | "dist" 74 | ] 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import compareVersions from 'compare-versions'; 2 | import { Platform } from 'react-native'; 3 | 4 | import { getAndroidVersion } from './android'; 5 | import { getIOSVersion } from './ios'; 6 | 7 | type CheckVersionParams = { 8 | country?: string; 9 | version: string; 10 | iosStoreURL?: string; 11 | androidStoreURL?: string; 12 | }; 13 | 14 | type CheckVersionResponse = { 15 | local: string; 16 | remote: string; 17 | result: 'new' | 'old' | 'equal'; 18 | detail: 'remote > local' | 'remote < local' | 'remote === local'; 19 | }; 20 | 21 | export const compareVersion = ( 22 | local: string, 23 | remote: string 24 | ): CheckVersionResponse['result'] => { 25 | switch (compareVersions(local, remote)) { 26 | case -1: 27 | return 'new'; 28 | case 1: 29 | return 'old'; 30 | default: 31 | return 'equal'; 32 | } 33 | }; 34 | 35 | const checkVersion = async ( 36 | params: CheckVersionParams 37 | ): Promise => { 38 | if (!params.version) { 39 | throw new Error('local version is not set.'); 40 | } 41 | 42 | /* check store url */ 43 | if (Platform.OS === 'ios' && !params.iosStoreURL) { 44 | throw new Error('iosStoreURL is not set.'); 45 | } 46 | 47 | if (Platform.OS === 'android' && !params.androidStoreURL) { 48 | throw new Error('androidStoreURL is not set.'); 49 | } 50 | 51 | /* get version */ 52 | let remoteVersion: string; 53 | 54 | try { 55 | remoteVersion = 56 | Platform.OS === 'ios' 57 | ? await getIOSVersion(params.iosStoreURL, params.country || 'jp') 58 | : await getAndroidVersion(params.androidStoreURL); 59 | } catch (e) { 60 | if (e instanceof Error) { 61 | throw new Error(e.message); 62 | } 63 | 64 | throw new Error(`can't get ${Platform.OS} version`); 65 | } 66 | 67 | const result = compareVersion(params.version, remoteVersion); 68 | let detail: CheckVersionResponse['detail']; 69 | switch (result) { 70 | case 'new': 71 | detail = 'remote > local'; 72 | break; 73 | case 'old': 74 | detail = 'remote < local'; 75 | break; 76 | default: 77 | detail = 'remote === local'; 78 | break; 79 | } 80 | 81 | /* compare version */ 82 | return { 83 | local: params.version, 84 | remote: remoteVersion, 85 | result, 86 | detail, 87 | }; 88 | }; 89 | 90 | export default checkVersion; 91 | --------------------------------------------------------------------------------