API Reference
This document provides a comprehensive reference of the Source Push SDK APIs.
Core APIs
codePush
The main module that provides all the functionality for Source Push.
import codePush from "@srcpush/react-native-code-push";
checkForUpdate
Queries the Source Push service for updates.
codePush.checkForUpdate(deploymentKey?: string): Promise<RemotePackage | null>
Parameters
deploymentKey
(optional): The deployment key to use for checking updates
Returns
- Promise that resolves to a
RemotePackage
if an update is available, ornull
if the app is up to date
sync
Synchronizes your app with the Source Push server.
codePush.sync(options?: SyncOptions, syncStatusCallback?: (status: SyncStatus) => void): Promise<SyncStatus>
Options
interface SyncOptions {
deploymentKey?: string;
installMode?: InstallMode;
mandatoryInstallMode?: InstallMode;
minimumBackgroundDuration?: number;
updateDialog?: UpdateDialog;
}
RestartApp
Restarts your app to apply an update.
codePush.restartApp(onlyIfUpdateIsPending?: boolean): void
Types and Enums
InstallMode
enum InstallMode {
IMMEDIATE,
ON_NEXT_RESTART,
ON_NEXT_RESUME,
}
SyncStatus
enum SyncStatus {
UP_TO_DATE,
UPDATE_INSTALLED,
UPDATE_IGNORED,
UNKNOWN_ERROR,
SYNC_IN_PROGRESS,
CHECKING_FOR_UPDATE,
AWAITING_USER_ACTION,
DOWNLOADING_PACKAGE,
INSTALLING_UPDATE,
}
UpdateDialog
interface UpdateDialog {
appendReleaseDescription?: boolean;
descriptionPrefix?: string;
mandatoryContinueButtonLabel?: string;
mandatoryUpdateMessage?: string;
optionalIgnoreButtonLabel?: string;
optionalInstallButtonLabel?: string;
optionalUpdateMessage?: string;
title?: string;
}
HOC Configuration
codePush(options?)
interface CodePushOptions {
checkFrequency?: CheckFrequency;
installMode?: InstallMode;
mandatoryInstallMode?: InstallMode;
minimumBackgroundDuration?: number;
updateDialog?: UpdateDialog;
}
Example usage:
const codePushOptions = {
checkFrequency: codePush.CheckFrequency.ON_APP_RESUME,
installMode: codePush.InstallMode.ON_NEXT_RESTART,
};
export default codePush(codePushOptions)(App);
Event Handling
Sync Status Callback
codePush.sync({}, (status) => {
switch (status) {
case codePush.SyncStatus.CHECKING_FOR_UPDATE:
console.log("Checking for updates");
break;
case codePush.SyncStatus.DOWNLOADING_PACKAGE:
console.log("Downloading package");
break;
case codePush.SyncStatus.INSTALLING_UPDATE:
console.log("Installing update");
break;
case codePush.SyncStatus.UP_TO_DATE:
console.log("App up to date");
break;
case codePush.SyncStatus.UPDATE_INSTALLED:
console.log("Update installed");
break;
}
});
Error Handling
Error Types
enum CodePushError {
NETWORK_ERROR,
INVALID_UPDATE,
SIGNATURE_VERIFICATION_ERROR,
DEPLOYMENT_KEY_MISMATCH,
}
Error Handling Example
try {
const update = await codePush.checkForUpdate();
if (update) {
await codePush.sync();
}
} catch (error) {
if (error.code === CodePushError.NETWORK_ERROR) {
console.error("Network error occurred");
} else if (error.code === CodePushError.INVALID_UPDATE) {
console.error("Invalid update package");
}
}
Advanced Usage
Download Progress
codePush.sync({}, null, ({ receivedBytes, totalBytes }) => {
const progress = (receivedBytes / totalBytes) * 100;
console.log(`Download progress: ${progress}%`);
});
Binary Version Matching
codePush.sync({
deploymentKey: "YOUR-KEY",
targetBinaryVersion: "1.2.3",
});
Package Info
const getCurrentPackage = async () => {
try {
const update = await codePush.getCurrentPackage();
console.log("Label:", update.label);
console.log("Description:", update.description);
console.log("IsMandatory:", update.isMandatory);
} catch (error) {
console.error("Error getting current package:", error);
}
};