From 8246fab0ce8ea5cae96c295963ae4a7c680fb245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20HANSS?= Date: Fri, 5 May 2023 17:05:53 +0200 Subject: [PATCH] Add typescript types --- package.json | 4 +- types.d.ts | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 types.d.ts diff --git a/package.json b/package.json index a2bb494..b551800 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { "name": "rn-update-apk", - "version": "5.0.0", + "version": "5.0.1", "description": "Check for new APK versions and update app from React Native", "main": "index.js", + "types": "types.d.ts", "repository": { "type": "git", "url": "git+https://github.com/mikehardy/react-native-update-apk.git" @@ -23,6 +24,7 @@ ], "author": "Mike Hardy (https://github.com/mikehardy)", "contributors": [ + "Andréas 'ScreamZ' HANSS (https://github.com/ScreamZ)", "parryworld (https://github.com/parryworld)" ], "license": "MIT", diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 0000000..55f3952 --- /dev/null +++ b/types.d.ts @@ -0,0 +1,153 @@ +interface AppSpecification{firstInstallTime: string, lastUpdateTime: string, name: string} + +export function getApps(): Promise>; +export function getNonSystemApps(): Promise>; +/** + * Displayed version name (e.g. 1.0.0) + */ +export function getInstalledVersionName(): string; +/** + * Build number/Version code (e.g. 1) + */ +export function getInstalledVersionCode(): string; +/** + * Package name (e.g. io.codingspark.app) + */ +export function getInstalledPackageName(): string; +/** + * First install timestamp (e.g. 1511326247) + */ +export function getInstalledFirstInstallTime(): number; +/** + * Last update timestamp (e.g. 1511326247) + */ +export function getInstalledLastUpdateTime(): number; +export function getInstalledPackageInstaller(): string; +export function getInstalledSigningInfo(): Array<{ + issuer: string; + serialNumber: string; + signature: number; + subject: string; + thumbprint: string; + toString: string; +}>; + +export type UpdateAPKConstructorArgs = { + /** + * iOS App Store ID + * + * @see https://support.google.com/admob/answer/10038409 + */ + iosAppId?: string; + + /** + * The URL to the remote JSON file that contains the latest version information. + * + * Sample file : + * ```ts + * { + * "versionName": "2.0.0", + * "apkUrl": "http://192.168.0.157:3000/build.apk", + * "forceUpdate": false, + * "whatsNew": "<< what changes the app update will bring >>" + * } + * ``` + */ + apkVersionUrl: string; + + /** + * You should use it if you need to pass options to fetch request. + */ + apkVersionOptions?: { + method: string; + headers: Record; + }; + + /** + * Complements or replaces the DownloadFileOptions (from react-native-fs) to download the new APK. + * + * By default the following options are already set: fromUrl, toFile, begin, progress, background and progressDivider + * You should use it if you need to pass additional information (for example: headers) to download the new APK + */ + apkOptions?: Record; + + /** + * The name of this `fileProviderAuthority` is defined in AndroidManifest.xml. **THEY MUST MATCH**. + * By default other modules like rn-fetch-blob define one (conveniently named the same as below) + * but if you don't match the names you will get an odd-looking XML exception: + * `Attempt to invoke virtual method 'android.content.res.XmlResourceParser ....' on a null object reference` + * + * @example + * + * { + * fileProviderAuthority ${UpdateAPK.getInstalledPackageName()}.provider` + * } + * + * @see https://github.com/mikehardy/react-native-update-apk/blob/main/example/android/app/src/main/AndroidManifest.xml + */ + fileProviderAuthority: string; + + /** + * This callback is called if there is a new version but it is not a forceUpdate. + * Force update can be defined in the remote JSON with a boolean value. + * + * @example + * + * { + * "versionName": "2.0.0", + * "apkUrl": "http://192.168.0.157:3000/build.apk", + * "forceUpdate": false, + * "whatsNew": "<< what changes the app update will bring >>" + * } + * + * @see {@link UpdateAPKConstructorArgs.forceUpdateApp} to hook on forced updates. + */ + needUpdateApp( + performUpdate: (force: boolean) => Promise, + /** The changelog defined in the remote file */ + changelog: string + ): void; + + /** + * This will be called before the download/update where you defined forceUpdate: true in the version JSON + * + * @see {@link UpdateAPKConstructorArgs.needUpdateApp} to hook on non-forced updates. + */ + forceUpdateApp(): void; + + /** + * This hooks trigger when the application is up to date and doesn't need to be updated. + */ + notNeedUpdateApp(): void; + + /** + * This is passed to react-native-fs as a callback, will trigger when the download starts. + */ + downloadApkStart(): void; + + /** + * Called with 0-99 for progress during the download. + */ + downloadApkProgress(percent: number): void; + + /** + * This is called prior to the update. + * If you throw inside this it will abort the update + * + * Note: Async code is not supported here. + */ + downloadApkEnd(): void; + + /** + * This is called if the fetch of the version or the APK fails, so should be generic + */ + onError(error: Error): void; +}; + +export class UpdateAPK { + constructor(args: UpdateAPKConstructorArgs); + /** + * Check if there is a new version available + */ + checkUpdate(): Promise; +} \ No newline at end of file