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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript Typings support #80

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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",
Expand Down
157 changes: 157 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
interface AppSpecification{firstInstallTime: string, lastUpdateTime: string, name: string}

export function getApps(): Promise<Array<AppSpecification>>;
export function getNonSystemApps(): Promise<Array<AppSpecification>>;
/**
* 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<string, string>;
};

/**
* 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<string, string>;

/**
* 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(
/**
* This is a function that you should call when you want to start the update.
* **You need to pass true, for some reasons otherwise it won't do anything**.
*/
performUpdate: (shouldUpdate: boolean) => Promise<void>,
/** 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<void>;
}