diff --git a/CodePush.js b/CodePush.js index 183ff65..384cc59 100644 --- a/CodePush.js +++ b/CodePush.js @@ -1,15 +1,14 @@ -'use strict'; +"use strict"; -var { Alert } = require("./AlertAdapter"); -var NativeCodePush = require("react-native").NativeModules.CodePush; -var PackageMixins = require("./package-mixins")(NativeCodePush); -var requestFetchAdapter = require("./request-fetch-adapter.js"); -var Sdk = require("code-push/script/acquisition-sdk").AcquisitionManager; -var semver = require("semver"); +import { AcquisitionManager as Sdk } from "code-push/script/acquisition-sdk"; +import { Alert } from "./AlertAdapter"; +import requestFetchAdapter from "./request-fetch-adapter"; +import semver from "semver"; -function checkForUpdate(deploymentKey = null) { - var config, sdk; - +let NativeCodePush = require("react-native").NativeModules.CodePush; +const PackageMixins = require("./package-mixins")(NativeCodePush); + +async function checkForUpdate(deploymentKey = null) { /* * Before we ask the server if an update exists, we * need to retrieve three pieces of information from the @@ -19,114 +18,90 @@ function checkForUpdate(deploymentKey = null) { * for their specific deployment and version and which are actually * different from the CodePush update they have already installed. */ - return getConfiguration() - .then((configResult) => { - /* - * If a deployment key was explicitly provided, - * then let's override the one we retrieved - * from the native-side of the app. This allows - * dynamically "redirecting" end-users at different - * deployments (e.g. an early access deployment for insiders). - */ - if (deploymentKey) { - config = Object.assign({}, configResult, { deploymentKey }); - } else { - config = configResult; - } - - sdk = new module.exports.AcquisitionSdk(requestFetchAdapter, config); - - // Allow dynamic overwrite of function. This is only to be used for tests. - return module.exports.getCurrentPackage(); - }) - .then((localPackage) => { - var queryPackage = { appVersion: config.appVersion }; - - /* - * If the app has a previously installed update, and that update - * was targetted at the same app version that is currently running, - * then we want to use its package hash to determine whether a new - * release has been made on the server. Otherwise, we only need - * to send the app version to the server, since we are interested - * in any updates for current app store version, regardless of hash. - */ - if (localPackage && localPackage.appVersion && semver.compare(localPackage.appVersion, config.appVersion) === 0) { - queryPackage = localPackage; - } - - return new Promise((resolve, reject) => { - sdk.queryUpdateWithCurrentPackage(queryPackage, (err, update) => { - if (err) { - return reject(err); - } - - /* - * There are three cases where checkForUpdate will resolve to null: - * ---------------------------------------------------------------- - * 1) The server said there isn't an update. This is the most common case. - * 2) The server said there is an update but it requires a newer binary version. - * This would occur when end-users are running an older app store version than - * is available, and CodePush is making sure they don't get an update that - * potentially wouldn't be compatible with what they are running. - * 3) The server said there is an update, but the update's hash is the same as - * the currently running update. This should _never_ happen, unless there is a - * bug in the server, but we're adding this check just to double-check that the - * client app is resilient to a potential issue with the update check. - */ - if (!update || update.updateAppVersion || (update.packageHash === localPackage.packageHash)) { - return resolve(null); - } - - update = Object.assign(update, PackageMixins.remote); - - NativeCodePush.isFailedUpdate(update.packageHash) - .then((isFailedHash) => { - update.failedInstall = isFailedHash; - resolve(update); - }) - .catch(reject) - .done(); - }) - }); - }); + const nativeConfig = await getConfiguration(); + /* + * If a deployment key was explicitly provided, + * then let's override the one we retrieved + * from the native-side of the app. This allows + * dynamically "redirecting" end-users at different + * deployments (e.g. an early access deployment for insiders). + */ + const config = deploymentKey ? { ...nativeConfig, ...{ deploymentKey } } + : nativeConfig; + const sdk = getPromisifiedSdk(requestFetchAdapter, config); + // Use dynamically overridden getCurrentPackage() during tests. + const localPackage = await module.exports.getCurrentPackage(); + /* + * If the app has a previously installed update, and that update + * was targetted at the same app version that is currently running, + * then we want to use its package hash to determine whether a new + * release has been made on the server. Otherwise, we only need + * to send the app version to the server, since we are interested + * in any updates for current app store version, regardless of hash. + */ + const queryPackage = localPackage && localPackage.appVersion && semver.compare(localPackage.appVersion, config.appVersion) === 0 + ? localPackage + : { appVersion: config.appVersion }; + const update = await sdk.queryUpdateWithCurrentPackage(queryPackage); + /* + * There are three cases where checkForUpdate will resolve to null: + * ---------------------------------------------------------------- + * 1) The server said there isn't an update. This is the most common case. + * 2) The server said there is an update but it requires a newer binary version. + * This would occur when end-users are running an older app store version than + * is available, and CodePush is making sure they don't get an update that + * potentially wouldn't be compatible with what they are running. + * 3) The server said there is an update, but the update's hash is the same as + * the currently running update. This should _never_ happen, unless there is a + * bug in the server, but we're adding this check just to double-check that the + * client app is resilient to a potential issue with the update check. + */ + if (!update || update.updateAppVersion || (update.packageHash === localPackage.packageHash)) { + return null; + } else { + const remotePackage = { ...update, ...PackageMixins.remote }; + remotePackage.failedInstall = await NativeCodePush.isFailedUpdate(remotePackage.packageHash); + return remotePackage; + } } -var getConfiguration = (() => { - var config; - return function getConfiguration() { +const getConfiguration = (() => { + let config; + return async function getConfiguration() { if (config) { - return Promise.resolve(config); + return config; } else if (testConfig) { - return Promise.resolve(testConfig); + return testConfig; } else { - return NativeCodePush.getConfiguration() - .then((configuration) => { - if (!config) config = configuration; - return config; - }); + config = await NativeCodePush.getConfiguration(); + return config; } } })(); -function getCurrentPackage() { - return new Promise((resolve, reject) => { - var localPackage; - NativeCodePush.getCurrentPackage() - .then((currentPackage) => { - localPackage = currentPackage; - return NativeCodePush.isFailedUpdate(currentPackage.packageHash); - }) - .then((failedUpdate) => { - localPackage.failedInstall = failedUpdate; - return NativeCodePush.isFirstRun(localPackage.packageHash); - }) - .then((isFirstRun) => { - localPackage.isFirstRun = isFirstRun; - resolve(localPackage); - }) - .catch(reject) - .done(); - }); +async function getCurrentPackage() { + const localPackage = await NativeCodePush.getCurrentPackage(); + localPackage.failedInstall = await NativeCodePush.isFailedUpdate(localPackage.packageHash); + localPackage.isFirstRun = await NativeCodePush.isFirstRun(localPackage.packageHash); + return localPackage; +} + +function getPromisifiedSdk(requestFetchAdapter, config) { + // Use dynamically overridden AcquisitionSdk during tests. + const sdk = new module.exports.AcquisitionSdk(requestFetchAdapter, config); + sdk.queryUpdateWithCurrentPackage = (queryPackage) => { + return new Promise((resolve, reject) => { + module.exports.AcquisitionSdk.prototype.queryUpdateWithCurrentPackage.call(sdk, queryPackage, (err, update) => { + if (err) { + reject(err); + } else { + resolve(update); + } + }); + }); + }; + + return sdk; } /* Logs messages to console with the [CodePush] prefix */ @@ -152,8 +127,8 @@ function setUpTestDependencies(testSdk, providedTestConfig, testNativeBridge) { * releases, and displaying a standard confirmation UI to the end-user * when an update is available. */ -function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) { - var syncOptions = { +async function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) { + const syncOptions = { deploymentKey: null, ignoreFailedUpdates: true, @@ -165,7 +140,7 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) syncStatusChangeCallback = typeof syncStatusChangeCallback == "function" ? syncStatusChangeCallback - : function(syncStatus) { + : (syncStatus) => { switch(syncStatus) { case CodePush.SyncStatus.CHECKING_FOR_UPDATE: log("Checking for update."); @@ -204,105 +179,95 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) downloadProgressCallback = typeof downloadProgressCallback == "function" ? downloadProgressCallback - : function(downloadProgress) { + : (downloadProgress) => { log(`Expecting ${downloadProgress.totalBytes} bytes, received ${downloadProgress.receivedBytes} bytes.`); }; - return new Promise((resolve, reject) => { - var rejectPromise = (error) => { - syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR); - log(error.message); - reject(error); + try { + await CodePush.notifyApplicationReady(); + + syncStatusChangeCallback(CodePush.SyncStatus.CHECKING_FOR_UPDATE); + const remotePackage = await checkForUpdate(syncOptions.deploymentKey); + + const doDownloadAndInstall = async () => { + syncStatusChangeCallback(CodePush.SyncStatus.DOWNLOADING_PACKAGE); + const localPackage = await remotePackage.download(downloadProgressCallback); + + syncStatusChangeCallback(CodePush.SyncStatus.INSTALLING_UPDATE); + await localPackage.install(syncOptions.installMode, () => { + syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_INSTALLED); + }); + + return CodePush.SyncStatus.UPDATE_INSTALLED; }; - CodePush.notifyApplicationReady() - .then(() => { - syncStatusChangeCallback(CodePush.SyncStatus.CHECKING_FOR_UPDATE); - return checkForUpdate(syncOptions.deploymentKey); - }) - .then((remotePackage) => { - var doDownloadAndInstall = () => { - syncStatusChangeCallback(CodePush.SyncStatus.DOWNLOADING_PACKAGE); - remotePackage.download(downloadProgressCallback) - .then((localPackage) => { - syncStatusChangeCallback(CodePush.SyncStatus.INSTALLING_UPDATE); - return localPackage.install(syncOptions.installMode, () => { - syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_INSTALLED); - resolve(CodePush.SyncStatus.UPDATE_INSTALLED); - }); - }) - .catch(rejectPromise) - .done(); + if (!remotePackage || (remotePackage.failedInstall && syncOptions.ignoreFailedUpdates)) { + syncStatusChangeCallback(CodePush.SyncStatus.UP_TO_DATE); + return CodePush.SyncStatus.UP_TO_DATE; + } else if (syncOptions.updateDialog) { + // updateDialog supports any truthy value (e.g. true, "goo", 12), + // but we should treat a non-object value as just the default dialog + if (typeof syncOptions.updateDialog !== "object") { + syncOptions.updateDialog = CodePush.DEFAULT_UPDATE_DIALOG; + } else { + syncOptions.updateDialog = { ...CodePush.DEFAULT_UPDATE_DIALOG, ...syncOptions.updateDialog }; + } + + return await new Promise((resolve, reject) => { + let message = null; + const dialogButtons = [{ + text: null, + onPress: async () => { + resolve(await doDownloadAndInstall()); + } + }]; + + if (remotePackage.isMandatory) { + message = syncOptions.updateDialog.mandatoryUpdateMessage; + dialogButtons[0].text = syncOptions.updateDialog.mandatoryContinueButtonLabel; + } else { + message = syncOptions.updateDialog.optionalUpdateMessage; + dialogButtons[0].text = syncOptions.updateDialog.optionalInstallButtonLabel; + // Since this is an optional update, add another button + // to allow the end-user to ignore it + dialogButtons.push({ + text: syncOptions.updateDialog.optionalIgnoreButtonLabel, + onPress: () => { + syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_IGNORED); + resolve(CodePush.SyncStatus.UPDATE_IGNORED); + } + }); } - if (!remotePackage || (remotePackage.failedInstall && syncOptions.ignoreFailedUpdates)) { - syncStatusChangeCallback(CodePush.SyncStatus.UP_TO_DATE); - resolve(CodePush.SyncStatus.UP_TO_DATE); + // If the update has a description, and the developer + // explicitly chose to display it, then set that as the message + if (syncOptions.updateDialog.appendReleaseDescription && remotePackage.description) { + message += `${syncOptions.updateDialog.descriptionPrefix} ${remotePackage.description}`; } - else if (syncOptions.updateDialog) { - // updateDialog supports any truthy value (e.g. true, "goo", 12), - // but we should treat a non-object value as just the default dialog - if (typeof syncOptions.updateDialog !== "object") { - syncOptions.updateDialog = CodePush.DEFAULT_UPDATE_DIALOG; - } else { - syncOptions.updateDialog = Object.assign({}, CodePush.DEFAULT_UPDATE_DIALOG, syncOptions.updateDialog); - } - - var message = null; - var dialogButtons = [ - { - text: null, - onPress: () => { - doDownloadAndInstall(); - } - } - ]; - - if (remotePackage.isMandatory) { - message = syncOptions.updateDialog.mandatoryUpdateMessage; - dialogButtons[0].text = syncOptions.updateDialog.mandatoryContinueButtonLabel; - } else { - message = syncOptions.updateDialog.optionalUpdateMessage; - dialogButtons[0].text = syncOptions.updateDialog.optionalInstallButtonLabel; - - // Since this is an optional update, add another button - // to allow the end-user to ignore it - dialogButtons.push({ - text: syncOptions.updateDialog.optionalIgnoreButtonLabel, - onPress: () => { - syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_IGNORED); - resolve(CodePush.SyncStatus.UPDATE_IGNORED); - } - }); - } - - // If the update has a description, and the developer - // explicitly chose to display it, then set that as the message - if (syncOptions.updateDialog.appendReleaseDescription && remotePackage.description) { - message += `${syncOptions.updateDialog.descriptionPrefix} ${remotePackage.description}`; - } - - syncStatusChangeCallback(CodePush.SyncStatus.AWAITING_USER_ACTION); - Alert.alert(syncOptions.updateDialog.title, message, dialogButtons); - } else { - doDownloadAndInstall(); - } - }) - .catch(rejectPromise) - .done(); - }); + + syncStatusChangeCallback(CodePush.SyncStatus.AWAITING_USER_ACTION); + Alert.alert(syncOptions.updateDialog.title, message, dialogButtons); + }); + } else { + return await doDownloadAndInstall(); + } + } catch (error) { + syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR); + log(error.message); + throw error; + } }; -var CodePush = { +const CodePush = { AcquisitionSdk: Sdk, - checkForUpdate: checkForUpdate, - getConfiguration: getConfiguration, - getCurrentPackage: getCurrentPackage, - log: log, + checkForUpdate, + getConfiguration, + getCurrentPackage, + log, notifyApplicationReady: NativeCodePush.notifyApplicationReady, restartApp: NativeCodePush.restartApp, - setUpTestDependencies: setUpTestDependencies, - sync: sync, + setUpTestDependencies, + sync, InstallMode: { IMMEDIATE: NativeCodePush.codePushInstallModeImmediate, // Restart the app immediately ON_NEXT_RESTART: NativeCodePush.codePushInstallModeOnNextRestart, // Don't artificially restart the app. Allow the update to be "picked up" on the next app restart @@ -330,4 +295,4 @@ var CodePush = { } }; -module.exports = CodePush; +export default CodePush; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/CheckForUpdateTestApp.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/CheckForUpdateTestApp.js index 62393c2..3161a34 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/CheckForUpdateTestApp.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/CheckForUpdateTestApp.js @@ -1,17 +1,15 @@ "use strict"; -import React from "react-native"; - -let { +import React, { AppRegistry, ScrollView, StyleSheet, Text, TouchableOpacity, View, -} = React; +} from "react-native"; -let TESTS = [ +const TESTS = [ require("./testcases/FirstUpdateTest"), require("./testcases/NewUpdateTest"), require("./testcases/NoRemotePackageTest"), @@ -38,6 +36,7 @@ let CheckForUpdateTestApp = React.createClass({ ); } + return ( @@ -64,7 +63,7 @@ let CheckForUpdateTestApp = React.createClass({ } }); -let styles = StyleSheet.create({ +const styles = StyleSheet.create({ container: { backgroundColor: "white", marginTop: 40, diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/FirstUpdateTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/FirstUpdateTest.js index 33b286b..21fbd0e 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/FirstUpdateTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/FirstUpdateTest.js @@ -2,13 +2,13 @@ import React from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; import createMockAcquisitionSdk from "../../utils/mockAcquisitionSdk"; - import { serverPackage } from "../resources/testPackages"; + +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); const localPackage = {}; let FirstUpdateTest = createTestCaseComponent( @@ -18,20 +18,13 @@ let FirstUpdateTest = createTestCaseComponent( let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage); let mockConfiguration = { appVersion : "1.5.0" }; CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.getCurrentPackage = () => { - return Promise.resolve(localPackage); - } - return Promise.resolve(); + CodePush.getCurrentPackage = async () => { + return localPackage; + }; }, - () => { - return CodePush.checkForUpdate() - .then((update) => { - if (update) { - assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote)); - } else { - throw new Error("checkForUpdate did not return the update from the server"); - } - }); + async () => { + let update = await CodePush.checkForUpdate(); + assert.equal(JSON.stringify(update), JSON.stringify({ ...serverPackage, ...PackageMixins.remote, failedInstall: false }), "checkForUpdate did not return the update from the server"); } ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NewUpdateTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NewUpdateTest.js index f020fce..c097c16 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NewUpdateTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NewUpdateTest.js @@ -2,14 +2,14 @@ import React from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; import createMockAcquisitionSdk from "../../utils/mockAcquisitionSdk"; - import { serverPackage, localPackage } from "../resources/testPackages"; +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); + let NewUpdateTest = createTestCaseComponent( "NewUpdateTest", "should return an update when server has a package that is newer than the current one", @@ -17,21 +17,14 @@ let NewUpdateTest = createTestCaseComponent( let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage); let mockConfiguration = { appVersion : "1.5.0" }; CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.getCurrentPackage = () => { - return Promise.resolve(localPackage); - } - return Promise.resolve(); + CodePush.getCurrentPackage = async () => { + return localPackage; + }; }, - () => { - return CodePush.checkForUpdate() - .then((update) => { - if (update) { - assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote)); - } else { - throw new Error("checkForUpdate did not return the update from the server"); - } - }); + async () => { + let update = await CodePush.checkForUpdate(); + assert.equal(JSON.stringify(update), JSON.stringify({ ...serverPackage, ...PackageMixins.remote, failedInstall: false }), "checkForUpdate did not return the update from the server"); } ); -module.exports = NewUpdateTest; \ No newline at end of file +export default NewUpdateTest; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NoRemotePackageTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NoRemotePackageTest.js index d3fb582..2264832 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NoRemotePackageTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NoRemotePackageTest.js @@ -2,14 +2,14 @@ import React from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; import createMockAcquisitionSdk from "../../utils/mockAcquisitionSdk"; -let serverPackage = null; -let localPackage = {}; +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); +const serverPackage = null; +const localPackage = {}; let NoRemotePackageTest = createTestCaseComponent( "NoRemotePackageTest", @@ -18,19 +18,14 @@ let NoRemotePackageTest = createTestCaseComponent( let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage); let mockConfiguration = { appVersion : "1.5.0" }; CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.getCurrentPackage = () => { - return Promise.resolve(localPackage); - } - return Promise.resolve(); + CodePush.getCurrentPackage = async () => { + return localPackage; + }; }, - () => { - return CodePush.checkForUpdate() - .then((update) => { - if (update) { - throw new Error("checkForUpdate should not return an update if there is none on the server"); - } - }); + async () => { + let update = await CodePush.checkForUpdate(); + assert(!update, "checkForUpdate should not return an update if there is none on the server"); } ); -module.exports = NoRemotePackageTest; \ No newline at end of file +export default NoRemotePackageTest; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/RemotePackageAppVersionNewerTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/RemotePackageAppVersionNewerTest.js index 4b4af51..db22e67 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/RemotePackageAppVersionNewerTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/RemotePackageAppVersionNewerTest.js @@ -2,37 +2,30 @@ import React from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; import createMockAcquisitionSdk from "../../utils/mockAcquisitionSdk"; - import { updateAppVersionPackage as serverPackage } from "../resources/testPackages"; -let localPackage = {}; + +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); +const localPackage = {}; let RemotePackageAppVersionNewerTest = createTestCaseComponent( "RemotePackageAppVersionNewerTest", "should drop the update when the server reports one with a newer binary version", () => { - return new Promise((resolve, reject) => { - let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage); - let mockConfiguration = { appVersion : "1.0.0" }; - CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.getCurrentPackage = () => { - return Promise.resolve(localPackage); - } - resolve(); - }); + let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage); + let mockConfiguration = { appVersion : "1.0.0" }; + CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); + CodePush.getCurrentPackage = async () => { + return localPackage; + }; }, - () => { - return CodePush.checkForUpdate() - .then((update) => { - if (update) { - throw new Error("checkForUpdate should not return an update if remote package is of a different binary version"); - } - }); + async () => { + let update = await CodePush.checkForUpdate(); + assert(!update, "checkForUpdate should not return an update if remote package is of a different binary version"); } ); -module.exports = RemotePackageAppVersionNewerTest; \ No newline at end of file +export default RemotePackageAppVersionNewerTest; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SamePackageTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SamePackageTest.js index 88288c8..76aac9a 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SamePackageTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SamePackageTest.js @@ -2,13 +2,13 @@ import React from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; import createMockAcquisitionSdk from "../../utils/mockAcquisitionSdk"; - import { serverPackage } from "../resources/testPackages"; + +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); const localPackage = serverPackage; let SamePackageTest = createTestCaseComponent( @@ -18,19 +18,14 @@ let SamePackageTest = createTestCaseComponent( let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage); let mockConfiguration = { appVersion : "1.5.0" }; CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.getCurrentPackage = () => { - return Promise.resolve(localPackage); - } - return Promise.resolve(); + CodePush.getCurrentPackage = async () => { + return localPackage; + }; }, - () => { - return CodePush.checkForUpdate() - .then((update) => { - if (update) { - throw new Error("checkForUpdate should not return a package when local package is identical"); - } - }); + async () => { + let update = await CodePush.checkForUpdate(); + assert(!update, "checkForUpdate should not return a package when local package is identical"); } ); -module.exports = SamePackageTest; \ No newline at end of file +export default SamePackageTest; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SwitchDeploymentKeyTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SwitchDeploymentKeyTest.js index 4205559..25e9568 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SwitchDeploymentKeyTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SwitchDeploymentKeyTest.js @@ -2,16 +2,15 @@ import React from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; import createMockAcquisitionSdk from "../../utils/mockAcquisitionSdk"; - import { serverPackage } from "../resources/testPackages"; -const localPackage = {}; -let deploymentKey = "myKey123"; +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); +const localPackage = {}; +const deploymentKey = "myKey123"; let SwitchDeploymentKeyTest = createTestCaseComponent( "SwitchDeploymentKeyTest", @@ -20,21 +19,14 @@ let SwitchDeploymentKeyTest = createTestCaseComponent( let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage, deploymentKey); let mockConfiguration = { appVersion : "1.5.0" }; CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.getCurrentPackage = () => { - return Promise.resolve(localPackage); - } - return Promise.resolve(); + CodePush.getCurrentPackage = async () => { + return localPackage; + }; }, - () => { - return CodePush.checkForUpdate(deploymentKey) - .then((update) => { - if (update) { - assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote)); - } else { - throw new Error("checkForUpdate did not return the update from the server"); - } - }); + async () => { + let update = await CodePush.checkForUpdate(deploymentKey); + assert.equal(JSON.stringify(update), JSON.stringify({ ...serverPackage, ...PackageMixins.remote, failedInstall: false }), "checkForUpdate did not return the update from the server"); } ); -module.exports = SwitchDeploymentKeyTest; \ No newline at end of file +export default SwitchDeploymentKeyTest; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/DownloadProgressTestApp.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/DownloadProgressTestApp.js index 65cf112..0b0b1bc 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/DownloadProgressTestApp.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/DownloadProgressTestApp.js @@ -1,15 +1,13 @@ "use strict"; -import React from "react-native"; - -let { +import React, { AppRegistry, ScrollView, StyleSheet, Text, TouchableOpacity, View, -} = React; +} from "react-native"; let TESTS = [ require("./testcases/DownloadProgressTest") diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/resources/TestPackages.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/resources/TestPackages.js index ae3254c..d13201c 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/resources/TestPackages.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/resources/TestPackages.js @@ -1,6 +1,6 @@ -let { Platform } = require("react-native"); +import { Platform } from "react-native"; -let packages = [ +const packages = [ { downloadUrl: "smallFile", description: "Angry flappy birds", diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/testcases/DownloadProgressTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/testcases/DownloadProgressTest.js index b54f3e5..ee7bd98 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/testcases/DownloadProgressTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/testcases/DownloadProgressTest.js @@ -2,13 +2,14 @@ import React from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; - import testPackages from "../resources/TestPackages"; -let localPackage = {}; + +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); +const localPackage = {}; + let saveProgress; function checkReceivedAndExpectedBytesEqual() { @@ -49,4 +50,4 @@ let DownloadProgressTest = createTestCaseComponent( } ); -module.exports = DownloadProgressTest; \ No newline at end of file +export default DownloadProgressTest; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/CheckIsFirstRunAndPassTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/CheckIsFirstRunAndPassTest.js index 52ddf35..fe05015 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/CheckIsFirstRunAndPassTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/CheckIsFirstRunAndPassTest.js @@ -1,27 +1,23 @@ "use strict"; -import React from "react-native"; -import CodePush from "react-native-code-push"; - -let { +import React, { AppRegistry, Text, View, -} = React; +} from "react-native"; +import CodePush from "react-native-code-push"; let IsFirstRunTest = React.createClass({ getInitialState() { return {}; }, - componentDidMount() { - CodePush.getCurrentPackage() - .then((localPackage) => { - if (localPackage.isFirstRun) { - this.setState({ passed: true }); - } else { - this.setState({ passed: false }); - } - }); + async componentDidMount() { + let localPackage = await CodePush.getCurrentPackage(); + if (localPackage.isFirstRun) { + this.setState({ passed: true }); + } else { + this.setState({ passed: false }); + } }, render() { let text = "Testing..."; diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV1.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV1.js index d7f2eb2..a0528ee 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV1.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV1.js @@ -1,23 +1,22 @@ "use strict"; -import React from "react-native"; -import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); -import createMockAcquisitionSdk from "../../utils/mockAcquisitionSdk"; - -let { +import React, { AppRegistry, Platform, Text, View, -} = React; +} from "react-native"; +import CodePush from "react-native-code-push"; +import createMockAcquisitionSdk from "../../utils/mockAcquisitionSdk"; + +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); let IsFailedUpdateTest = React.createClass({ getInitialState() { return {}; }, - componentDidMount() { + async componentDidMount() { let serverPackage = { description: "Angry flappy birds", appVersion: "1.5.0", @@ -39,20 +38,14 @@ let IsFailedUpdateTest = React.createClass({ let mockConfiguration = { appVersion : "1.5.0" }; CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.notifyApplicationReady() - .then(() => { - return CodePush.checkForUpdate(); - }) - .then((remotePackage) => { - if (remotePackage.failedInstall) { - this.setState({ passed: true }); - } else { - return remotePackage.download(); - } - }) - .then((localPackage) => { - return localPackage && localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + await CodePush.notifyApplicationReady() + let remotePackage = await CodePush.checkForUpdate(); + if (remotePackage.failedInstall) { + this.setState({ passed: true }); + } else { + let localPackage = await remotePackage.download(); + return localPackage && await localPackage.install(NativeCodePush.codePushInstallModeImmediate); + } }, render() { let text = "Testing..."; diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV2.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV2.js index bd8fc8a..08fda7a 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV2.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV2.js @@ -1,16 +1,15 @@ "use strict"; -import React from "react-native"; -import CodePush from "react-native-code-push"; - -let { +import React, { AppRegistry, Text, View, -} = React; +} from "react-native"; +import CodePush from "react-native-code-push"; let IsFailedUpdateTest = React.createClass({ componentDidMount() { + // Should trigger a rollback. CodePush.restartApp(); }, render() { diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/NotifyApplicationReadyAndRestart.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/NotifyApplicationReadyAndRestart.js index fcd9f2d..59c496c 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/NotifyApplicationReadyAndRestart.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/NotifyApplicationReadyAndRestart.js @@ -3,25 +3,23 @@ import React from "react-native"; import { Platform, AppRegistry, Text, View } from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; -let RCTTestModule = React.NativeModules.TestModule; + +const NativeCodePush = React.NativeModules.CodePush; +const RCTTestModule = React.NativeModules.TestModule; let NotifyApplicationReadyTest = React.createClass({ getInitialState() { return {}; }, - componentDidMount() { - CodePush.notifyApplicationReady() - .then(() => { - if (Platform.OS === "android") { - return NativeCodePush.downloadAndReplaceCurrentBundle("http://10.0.3.2:8081/CodePushDemoAppTests/InstallUpdateTests/resources/PassNotifyApplicationReadyTest.includeRequire.runModule.bundle?platform=android&dev=true"); - } else if (Platform.OS === "ios") { - return NativeCodePush.downloadAndReplaceCurrentBundle("http://localhost:8081/CodePushDemoAppTests/InstallUpdateTests/resources/PassNotifyApplicationReadyTest.includeRequire.runModule.bundle?platform=ios&dev=true"); - } - }) - .then(() => { - CodePush.restartApp(); - }); + async componentDidMount() { + await CodePush.notifyApplicationReady(); + if (Platform.OS === "android") { + await NativeCodePush.downloadAndReplaceCurrentBundle("http://10.0.3.2:8081/CodePushDemoAppTests/InstallUpdateTests/resources/PassNotifyApplicationReadyTest.includeRequire.runModule.bundle?platform=android&dev=true"); + } else if (Platform.OS === "ios") { + await NativeCodePush.downloadAndReplaceCurrentBundle("http://localhost:8081/CodePushDemoAppTests/InstallUpdateTests/resources/PassNotifyApplicationReadyTest.includeRequire.runModule.bundle?platform=ios&dev=true"); + } + + CodePush.restartApp(); }, render() { return ( diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassInstallModeImmediateTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassInstallModeImmediateTest.js index d93d8dc..ef8e17d 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassInstallModeImmediateTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassInstallModeImmediateTest.js @@ -1,12 +1,10 @@ "use strict"; -import React from "react-native"; - -let { +import React, { AppRegistry, Text, View, -} = React; +} from "react-native"; let InstallModeImmediateTest = React.createClass({ render() { diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassInstallModeOnNextRestartTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassInstallModeOnNextRestartTest.js index 7743f49..e525da3 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassInstallModeOnNextRestartTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassInstallModeOnNextRestartTest.js @@ -1,12 +1,10 @@ "use strict"; -import React from "react-native"; - -let { +import React, { AppRegistry, Text, View, -} = React; +} from "react-native"; let InstallModeOnNextRestartTest = React.createClass({ render() { diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassInstallModeOnNextResumeTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassInstallModeOnNextResumeTest.js index cfd0a98..491a391 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassInstallModeOnNextResumeTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassInstallModeOnNextResumeTest.js @@ -1,12 +1,10 @@ "use strict"; -import React from "react-native"; - -let { +import React, { AppRegistry, Text, View, -} = React; +} from "react-native"; let InstallModeOnNextResumeTest = React.createClass({ render() { diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassNotifyApplicationReadyTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassNotifyApplicationReadyTest.js index 5f426c7..df457b6 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassNotifyApplicationReadyTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/PassNotifyApplicationReadyTest.js @@ -1,12 +1,10 @@ "use strict"; -import React from "react-native"; - -let { +import React, { AppRegistry, Text, View, -} = React; +} from "react-native"; let NotifyApplicationReadyTest = React.createClass({ render() { diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1.js index 12eb9c5..565ab06 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1.js @@ -1,51 +1,36 @@ "use strict"; -import React from "react-native"; -import CodePush from "react-native-code-push"; -let RCTTestModule = React.NativeModules.TestModule; -let NativeCodePush = React.NativeModules.CodePush; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); - -let { +import React, { AppRegistry, Platform, Text, View, -} = React; +} from "react-native"; +import CodePush from "react-native-code-push"; + +const RCTTestModule = React.NativeModules.TestModule; +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); let RollbackTest = React.createClass({ - componentDidMount() { - let remotePackage = { - description: "Angry flappy birds", - appVersion: "1.5.0", - label: "2.4.0", - isMandatory: false, - isAvailable: true, - updateAppVersion: false, - packageHash: "hash241", - packageSize: 1024 - }; + async componentDidMount() { + let remotePackage = require("./remotePackage"); + remotePackage.packageHash = "hash241"; if (Platform.OS === "android") { remotePackage.downloadUrl = "http://10.0.3.2:8081/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV2.includeRequire.runModule.bundle?platform=android&dev=true" - CodePush.notifyApplicationReady() - .then(() => { - NativeCodePush.downloadAndReplaceCurrentBundle("http://10.0.3.2:8081/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1Pass.includeRequire.runModule.bundle?platform=android&dev=true"); - }); + await CodePush.notifyApplicationReady(); + await NativeCodePush.downloadAndReplaceCurrentBundle("http://10.0.3.2:8081/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1Pass.includeRequire.runModule.bundle?platform=android&dev=true"); } else if (Platform.OS === "ios") { remotePackage.downloadUrl = "http://localhost:8081/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV2.includeRequire.runModule.bundle?platform=ios&dev=true" - CodePush.notifyApplicationReady() - .then(() => { - NativeCodePush.downloadAndReplaceCurrentBundle("http://localhost:8081/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1Pass.includeRequire.runModule.bundle?platform=ios&dev=true"); - }); + await CodePush.notifyApplicationReady() + await NativeCodePush.downloadAndReplaceCurrentBundle("http://localhost:8081/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1Pass.includeRequire.runModule.bundle?platform=ios&dev=true"); } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + let localPackage = await remotePackage.download(); + return await localPackage.install(NativeCodePush.codePushInstallModeImmediate); }, render() { return ( diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1Pass.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1Pass.js index 2f8b87f..b6d2ed7 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1Pass.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1Pass.js @@ -1,12 +1,10 @@ "use strict"; -import React from "react-native"; - -let { +import React, { AppRegistry, Text, View, -} = React; +} from "react-native"; let RollbackTest = React.createClass({ render() { diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV2.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV2.js index 12c9ce7..8b348a6 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV2.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV2.js @@ -1,13 +1,11 @@ "use strict"; -import React from "react-native"; -import CodePush from "react-native-code-push"; - -let { +import React, { AppRegistry, Text, View, -} = React; +} from "react-native"; +import CodePush from "react-native-code-push"; let RollbackTest = React.createClass({ componentDidMount() { diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeImmediateTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeImmediateTest.js index aa83023..7c74f3d 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeImmediateTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeImmediateTest.js @@ -3,11 +3,12 @@ import React from "react-native"; import { DeviceEventEmitter, Platform, AppRegistry } from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); + let remotePackage = require("../resources/remotePackage"); let InstallModeImmediateTest = createTestCaseComponent( @@ -21,13 +22,10 @@ let InstallModeImmediateTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + async () => { + let localPackage = await remotePackage.download(); + return await localPackage.install(NativeCodePush.codePushInstallModeImmediate); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextRestartTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextRestartTest.js index 6429de8..9381951 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextRestartTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextRestartTest.js @@ -3,11 +3,12 @@ import React from "react-native"; import { DeviceEventEmitter, Platform, AppRegistry } from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); + let remotePackage = require("../resources/remotePackage"); let InstallModeOnNextRestartTest = createTestCaseComponent( @@ -22,16 +23,11 @@ let InstallModeOnNextRestartTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeOnNextRestart); - }) - .then(() => { - CodePush.restartApp(); - }); + async () => { + let localPackage = await remotePackage.download(); + await localPackage.install(NativeCodePush.codePushInstallModeOnNextRestart); + CodePush.restartApp(); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextResumeTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextResumeTest.js index 942d17e..ed695e7 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextResumeTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextResumeTest.js @@ -3,11 +3,12 @@ import React from "react-native"; import { DeviceEventEmitter, Platform, AppRegistry } from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); + let remotePackage = require("../resources/remotePackage"); let InstallModeOnNextResumeTest = createTestCaseComponent( @@ -21,16 +22,11 @@ let InstallModeOnNextResumeTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeOnNextResume); - }) - .then(() => { - CodePush.restartApp(); - }); + async () => { + let localPackage = await remotePackage.download() + await localPackage.install(NativeCodePush.codePushInstallModeOnNextResume); + CodePush.restartApp(); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFailedUpdateTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFailedUpdateTest.js index 78c4c6a..5669de1 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFailedUpdateTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFailedUpdateTest.js @@ -3,11 +3,12 @@ import React from "react-native"; import { DeviceEventEmitter, Platform, AppRegistry } from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); + let remotePackage = require("../resources/remotePackage"); let IsFailedUpdateTest = createTestCaseComponent( @@ -21,13 +22,10 @@ let IsFailedUpdateTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + async () => { + let localPackage = await remotePackage.download(); + return await localPackage.install(NativeCodePush.codePushInstallModeImmediate); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFirstRunTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFirstRunTest.js index 0c0866b..f8c28cd 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFirstRunTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFirstRunTest.js @@ -3,11 +3,11 @@ import React from "react-native"; import { DeviceEventEmitter, Platform, AppRegistry } from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); let remotePackage = require("../resources/remotePackage"); let IsFirstRunTest = createTestCaseComponent( @@ -21,13 +21,10 @@ let IsFirstRunTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + async () => { + let localPackage = await remotePackage.download(); + return await localPackage.install(NativeCodePush.codePushInstallModeImmediate); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsPendingTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsPendingTest.js index 901ef12..3e61f71 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsPendingTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsPendingTest.js @@ -3,11 +3,12 @@ import React from "react-native"; import { DeviceEventEmitter, Platform, AppRegistry } from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); + let remotePackage = require("../resources/remotePackage"); let IsPendingTest = createTestCaseComponent( @@ -21,20 +22,13 @@ let IsPendingTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeOnNextRestart); - }) - .then((localPackage) => { - assert(localPackage.isPending, "isPending should be set to \"true\" after an install"); - return CodePush.getCurrentPackage(); - }) - .then((localPackage) => { - assert(localPackage.isPending, "isPending should be set to \"true\" after an install"); - }); + async () => { + let localPackage = await remotePackage.download(); + await localPackage.install(NativeCodePush.codePushInstallModeOnNextRestart); + assert(localPackage.isPending, "isPending should be set to \"true\" after an install"); + localPackage = await CodePush.getCurrentPackage(); + assert(localPackage.isPending, "isPending should be set to \"true\" after an install"); } ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/NotifyApplicationReadyTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/NotifyApplicationReadyTest.js index a38fcf2..d77e125 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/NotifyApplicationReadyTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/NotifyApplicationReadyTest.js @@ -3,11 +3,12 @@ import React from "react-native"; import { DeviceEventEmitter, Platform, AppRegistry } from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); + let remotePackage = require("../resources/remotePackage"); let NotifyApplicationReadyTest = createTestCaseComponent( @@ -21,13 +22,10 @@ let NotifyApplicationReadyTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + async () => { + let localPackage = await remotePackage.download() + return await localPackage.install(NativeCodePush.codePushInstallModeImmediate); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/RollbackTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/RollbackTest.js index 3c098ba..d002965 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/RollbackTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/RollbackTest.js @@ -3,11 +3,12 @@ import React from "react-native"; import { DeviceEventEmitter, Platform, AppRegistry } from "react-native"; import CodePush from "react-native-code-push"; -let NativeCodePush = React.NativeModules.CodePush; import createTestCaseComponent from "../../utils/createTestCaseComponent"; -let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import assert from "assert"; +const NativeCodePush = React.NativeModules.CodePush; +const PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); + let remotePackage = require("../resources/remotePackage"); let RollbackTest = createTestCaseComponent( @@ -21,13 +22,10 @@ let RollbackTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + async () => { + let localPackage = await remotePackage.download() + return await localPackage.install(NativeCodePush.codePushInstallModeImmediate); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/utils/createTestCaseComponent.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/utils/createTestCaseComponent.js index 22d300c..719fbd5 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/utils/createTestCaseComponent.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/utils/createTestCaseComponent.js @@ -2,10 +2,10 @@ import React from "react-native"; import { DeviceEventEmitter, Text, View } from "react-native"; -let NativeCodePush = React.NativeModules.CodePush; +const NativeCodePush = React.NativeModules.CodePush; // RCTTestModule is not implemented yet for RN Android. -let RCTTestModule = React.NativeModules.TestModule || {}; +const RCTTestModule = React.NativeModules.TestModule || {}; function createTestCaseComponent(displayName, description, setUp, runTest, passAfterRun = true) { let TestCaseComponent = React.createClass({ @@ -18,18 +18,17 @@ function createTestCaseComponent(displayName, description, setUp, runTest, passA done: false, }; }, - componentDidMount() { - setUp() - .then(runTest) - .then(() => { - if (passAfterRun) { - this.setState({done: true}, RCTTestModule.markTestCompleted); - } - }) - .catch((err) => { - console.error(err); - throw err; - }); + async componentDidMount() { + try { + await setUp(); + await runTest(); + if (passAfterRun) { + this.setState({done: true}, RCTTestModule.markTestCompleted); + } + } catch (err) { + console.error(err); + throw err; + } }, render() { return ( diff --git a/Examples/CodePushDemoApp/crossplatformdemo.js b/Examples/CodePushDemoApp/crossplatformdemo.js index b05a128..b340b3f 100644 --- a/Examples/CodePushDemoApp/crossplatformdemo.js +++ b/Examples/CodePushDemoApp/crossplatformdemo.js @@ -1,7 +1,6 @@ 'use strict'; -var React = require('react-native'); -var { +import React, { AppRegistry, Dimensions, Image, @@ -9,84 +8,85 @@ var { Text, TouchableOpacity, View, -} = React; +} from "react-native"; -var Button = require("react-native-button"); +import Button from "react-native-button"; +import CodePush from "react-native-code-push"; -var CodePush = require('react-native-code-push'); - -var CodePushDemoApp = React.createClass({ - sync() { - var self = this; - CodePush.sync( - { - updateDialog: true, - installMode: CodePush.InstallMode.ON_NEXT_RESUME - }, - function(syncStatus) { - switch(syncStatus) { - case CodePush.SyncStatus.CHECKING_FOR_UPDATE: - self.setState({ - syncMessage: "Checking for update." - }); - break; - case CodePush.SyncStatus.DOWNLOADING_PACKAGE: - self.setState({ - syncMessage: "Downloading package." - }); - break; - case CodePush.SyncStatus.AWAITING_USER_ACTION: - self.setState({ - syncMessage: "Awaiting user action." - }); - break; - case CodePush.SyncStatus.INSTALLING_UPDATE: - self.setState({ - syncMessage: "Installing update." - }); - break; - case CodePush.SyncStatus.UP_TO_DATE: - self.setState({ - syncMessage: "App up to date.", - progress: false - }); - break; - case CodePush.SyncStatus.UPDATE_IGNORED: - self.setState({ - syncMessage: "Update cancelled by user.", - progress: false - }); - break; - case CodePush.SyncStatus.UPDATE_INSTALLED: - self.setState({ - syncMessage: "Update installed and will be run when the app next resumes.", - progress: false - }); - break; - case CodePush.SyncStatus.UNKNOWN_ERROR: - self.setState({ - syncMessage: "An unknown error occurred.", - progress: false - }); - break; +let CodePushDemoApp = React.createClass({ + async sync() { + let self = this; + try { + return await CodePush.sync( + { + updateDialog: true, + installMode: CodePush.InstallMode.ON_NEXT_RESUME + }, + (syncStatus) => { + switch(syncStatus) { + case CodePush.SyncStatus.CHECKING_FOR_UPDATE: + self.setState({ + syncMessage: "Checking for update." + }); + break; + case CodePush.SyncStatus.DOWNLOADING_PACKAGE: + self.setState({ + syncMessage: "Downloading package." + }); + break; + case CodePush.SyncStatus.AWAITING_USER_ACTION: + self.setState({ + syncMessage: "Awaiting user action." + }); + break; + case CodePush.SyncStatus.INSTALLING_UPDATE: + self.setState({ + syncMessage: "Installing update." + }); + break; + case CodePush.SyncStatus.UP_TO_DATE: + self.setState({ + syncMessage: "App up to date.", + progress: false + }); + break; + case CodePush.SyncStatus.UPDATE_IGNORED: + self.setState({ + syncMessage: "Update cancelled by user.", + progress: false + }); + break; + case CodePush.SyncStatus.UPDATE_INSTALLED: + self.setState({ + syncMessage: "Update installed and will be run when the app next resumes.", + progress: false + }); + break; + case CodePush.SyncStatus.UNKNOWN_ERROR: + self.setState({ + syncMessage: "An unknown error occurred.", + progress: false + }); + break; + } + }, + (progress) => { + self.setState({ + progress: progress + }); } - }, - function(progress) { - self.setState({ - progress: progress - }); - } - ).catch(function(error) { + ); + } catch (error) { CodePush.log(error); - }); + } }, + getInitialState() { return { }; }, + render() { - var syncView; - var syncButton; - var progressView; + let syncView, syncButton, progressView; if (this.state.syncMessage) { syncView = ( @@ -120,7 +120,7 @@ var CodePushDemoApp = React.createClass({ } }); -var styles = StyleSheet.create({ +let styles = StyleSheet.create({ image: { marginTop: 50, width: Dimensions.get('window').width - 100, diff --git a/Examples/CodePushDemoApp/index.android.js b/Examples/CodePushDemoApp/index.android.js index f682949..d946285 100644 --- a/Examples/CodePushDemoApp/index.android.js +++ b/Examples/CodePushDemoApp/index.android.js @@ -1 +1 @@ -require("./crossplatformdemo.js"); \ No newline at end of file +import "./crossplatformdemo.js"; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/index.ios.js b/Examples/CodePushDemoApp/index.ios.js index f682949..d946285 100644 --- a/Examples/CodePushDemoApp/index.ios.js +++ b/Examples/CodePushDemoApp/index.ios.js @@ -1 +1 @@ -require("./crossplatformdemo.js"); \ No newline at end of file +import "./crossplatformdemo.js"; \ No newline at end of file diff --git a/package-mixins.js b/package-mixins.js index d539ab9..6cd8eb5 100644 --- a/package-mixins.js +++ b/package-mixins.js @@ -1,20 +1,18 @@ +"use strict"; + import { DeviceEventEmitter } from "react-native"; // This function is used to augment remote and local // package objects with additional functionality/properties // beyond what is included in the metadata sent by the server. -module.exports = (NativeCodePush) => { +export default (NativeCodePush) => { const remote = { - abortDownload() { - return NativeCodePush.abortDownload(this); - }, - - download(downloadProgressCallback) { + async download(downloadProgressCallback) { if (!this.downloadUrl) { - return Promise.reject(new Error("Cannot download an update without a download url")); + throw new Error("Cannot download an update without a download url"); } - var downloadProgressSubscription; + let downloadProgressSubscription; if (downloadProgressCallback) { // Use event subscription to obtain download progress. downloadProgressSubscription = DeviceEventEmitter.addListener( @@ -25,33 +23,27 @@ module.exports = (NativeCodePush) => { // Use the downloaded package info. Native code will save the package info // so that the client knows what the current package version is. - return NativeCodePush.downloadUpdate(this) - .then((downloadedPackage) => { - downloadProgressSubscription && downloadProgressSubscription.remove(); - return Object.assign({}, downloadedPackage, local); - }) - .catch((error) => { - downloadProgressSubscription && downloadProgressSubscription.remove(); - // Rethrow the error for subsequent handlers down the promise chain. - throw error; - }); + try { + const downloadedPackage = await NativeCodePush.downloadUpdate(this); + return { ...downloadedPackage, ...local }; + } finally { + downloadProgressSubscription && downloadProgressSubscription.remove(); + } }, isPending: false // A remote package could never be in a pending state }; const local = { - install(installMode = NativeCodePush.codePushInstallModeOnNextRestart, updateInstalledCallback) { - let localPackage = this; - return NativeCodePush.installUpdate(this, installMode) - .then(() => { - updateInstalledCallback && updateInstalledCallback(); - if (installMode == NativeCodePush.codePushInstallModeImmediate) { - NativeCodePush.restartApp(); - } else { - localPackage.isPending = true; // Mark the package as pending since it hasn't been applied yet - } - }); + async install(installMode = NativeCodePush.codePushInstallModeOnNextRestart, updateInstalledCallback) { + const localPackage = this; + await NativeCodePush.installUpdate(this, installMode); + updateInstalledCallback && updateInstalledCallback(); + if (installMode == NativeCodePush.codePushInstallModeImmediate) { + NativeCodePush.restartApp(); + } else { + localPackage.isPending = true; // Mark the package as pending since it hasn't been applied yet + } }, isPending: false // A local package wouldn't be pending until it was installed diff --git a/request-fetch-adapter.js b/request-fetch-adapter.js index c3efb88..1ccd3df 100644 --- a/request-fetch-adapter.js +++ b/request-fetch-adapter.js @@ -1,30 +1,33 @@ -module.exports.request = function request(verb, url, body, callback) { +"use strict"; + +export default { + async request(verb, url, body, callback) { if (typeof body === "function") { - callback = body; - body = null; + callback = body; + body = null; } var headers = { - 'Accept': 'application/json', - 'Content-Type': 'application/json' + "Accept": "application/json", + "Content-Type": "application/json" }; if (body && typeof body === "object") { - body = JSON.stringify(body); + body = JSON.stringify(body); } - var statusCode; - - fetch(url, { + try { + const response = await fetch(url, { method: verb, headers: headers, body: body - }).then(function(response) { - statusCode = response.status; - return response.text(); - }).then(function(body) { - callback(null, {statusCode: statusCode, body: body}); - }).catch(function(err) { - callback(err); - }); -} + }); + + const statusCode = response.status; + const body = await response.text(); + callback(null, { statusCode, body }); + } catch (err) { + callback(err); + } + } +};