UNPKG

8.01 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.AcquisitionManager = exports.AcquisitionStatus = void 0;
4var code_push_error_1 = require("./code-push-error");
5var AcquisitionStatus = /** @class */ (function () {
6 function AcquisitionStatus() {
7 }
8 AcquisitionStatus.DeploymentSucceeded = "DeploymentSucceeded";
9 AcquisitionStatus.DeploymentFailed = "DeploymentFailed";
10 return AcquisitionStatus;
11}());
12exports.AcquisitionStatus = AcquisitionStatus;
13var AcquisitionManager = /** @class */ (function () {
14 function AcquisitionManager(httpRequester, configuration) {
15 this._publicPrefixUrl = "v0.1/public/codepush/";
16 this._httpRequester = httpRequester;
17 this._serverUrl = configuration.serverUrl;
18 if (this._serverUrl.slice(-1) !== "/") {
19 this._serverUrl += "/";
20 }
21 this._appVersion = configuration.appVersion;
22 this._clientUniqueId = configuration.clientUniqueId;
23 this._deploymentKey = configuration.deploymentKey;
24 this._ignoreAppVersion = configuration.ignoreAppVersion;
25 }
26 AcquisitionManager.prototype.queryUpdateWithCurrentPackage = function (currentPackage, callback) {
27 var _this = this;
28 if (!currentPackage || !currentPackage.appVersion) {
29 throw new code_push_error_1.CodePushPackageError("Calling common acquisition SDK with incorrect package"); // Unexpected; indicates error in our implementation
30 }
31 var updateRequest = {
32 deployment_key: this._deploymentKey,
33 app_version: currentPackage.appVersion,
34 package_hash: currentPackage.packageHash,
35 is_companion: this._ignoreAppVersion,
36 label: currentPackage.label,
37 client_unique_id: this._clientUniqueId
38 };
39 var requestUrl = this._serverUrl + this._publicPrefixUrl + "update_check?" + queryStringify(updateRequest);
40 this._httpRequester.request(0 /* GET */, requestUrl, function (error, response) {
41 if (error) {
42 callback(error, /*remotePackage=*/ null);
43 return;
44 }
45 if (response.statusCode !== 200) {
46 var errorMessage = void 0;
47 if (response.statusCode === 0) {
48 errorMessage = "Couldn't send request to " + requestUrl + ", xhr.statusCode = 0 was returned. One of the possible reasons for that might be connection problems. Please, check your internet connection.";
49 }
50 else {
51 errorMessage = response.statusCode + ": " + response.body;
52 }
53 callback(new code_push_error_1.CodePushHttpError(errorMessage), /*remotePackage=*/ null);
54 return;
55 }
56 try {
57 var responseObject = JSON.parse(response.body);
58 var updateInfo = responseObject.update_info;
59 }
60 catch (error) {
61 callback(error, /*remotePackage=*/ null);
62 return;
63 }
64 if (!updateInfo) {
65 callback(error, /*remotePackage=*/ null);
66 return;
67 }
68 else if (updateInfo.update_app_version) {
69 callback(/*error=*/ null, { updateAppVersion: true, appVersion: updateInfo.target_binary_range });
70 return;
71 }
72 else if (!updateInfo.is_available) {
73 callback(/*error=*/ null, /*remotePackage=*/ null);
74 return;
75 }
76 var remotePackage = {
77 deploymentKey: _this._deploymentKey,
78 description: updateInfo.description,
79 label: updateInfo.label,
80 appVersion: updateInfo.target_binary_range,
81 isMandatory: updateInfo.is_mandatory,
82 packageHash: updateInfo.package_hash,
83 packageSize: updateInfo.package_size,
84 downloadUrl: updateInfo.download_url
85 };
86 callback(/*error=*/ null, remotePackage);
87 });
88 };
89 AcquisitionManager.prototype.reportStatusDeploy = function (deployedPackage, status, previousLabelOrAppVersion, previousDeploymentKey, callback) {
90 var url = this._serverUrl + this._publicPrefixUrl + "report_status/deploy";
91 var body = {
92 app_version: this._appVersion,
93 deployment_key: this._deploymentKey
94 };
95 if (this._clientUniqueId) {
96 body.client_unique_id = this._clientUniqueId;
97 }
98 if (deployedPackage) {
99 body.label = deployedPackage.label;
100 body.app_version = deployedPackage.appVersion;
101 switch (status) {
102 case AcquisitionStatus.DeploymentSucceeded:
103 case AcquisitionStatus.DeploymentFailed:
104 body.status = status;
105 break;
106 default:
107 if (callback) {
108 if (!status) {
109 callback(new code_push_error_1.CodePushDeployStatusError("Missing status argument."), /*not used*/ null);
110 }
111 else {
112 callback(new code_push_error_1.CodePushDeployStatusError("Unrecognized status \"" + status + "\"."), /*not used*/ null);
113 }
114 }
115 return;
116 }
117 }
118 if (previousLabelOrAppVersion) {
119 body.previous_label_or_app_version = previousLabelOrAppVersion;
120 }
121 if (previousDeploymentKey) {
122 body.previous_deployment_key = previousDeploymentKey;
123 }
124 callback = typeof arguments[arguments.length - 1] === "function" && arguments[arguments.length - 1];
125 this._httpRequester.request(2 /* POST */, url, JSON.stringify(body), function (error, response) {
126 if (callback) {
127 if (error) {
128 callback(error, /*not used*/ null);
129 return;
130 }
131 if (response.statusCode !== 200) {
132 callback(new code_push_error_1.CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null);
133 return;
134 }
135 callback(/*error*/ null, /*not used*/ null);
136 }
137 });
138 };
139 AcquisitionManager.prototype.reportStatusDownload = function (downloadedPackage, callback) {
140 var url = this._serverUrl + this._publicPrefixUrl + "report_status/download";
141 var body = {
142 client_unique_id: this._clientUniqueId,
143 deployment_key: this._deploymentKey,
144 label: downloadedPackage.label
145 };
146 this._httpRequester.request(2 /* POST */, url, JSON.stringify(body), function (error, response) {
147 if (callback) {
148 if (error) {
149 callback(error, /*not used*/ null);
150 return;
151 }
152 if (response.statusCode !== 200) {
153 callback(new code_push_error_1.CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null);
154 return;
155 }
156 callback(/*error*/ null, /*not used*/ null);
157 }
158 });
159 };
160 return AcquisitionManager;
161}());
162exports.AcquisitionManager = AcquisitionManager;
163function queryStringify(object) {
164 var queryString = "";
165 var isFirst = true;
166 for (var property in object) {
167 if (object.hasOwnProperty(property)) {
168 var value = object[property];
169 if (value !== null && typeof value !== "undefined") {
170 if (!isFirst) {
171 queryString += "&";
172 }
173 queryString += encodeURIComponent(property) + "=";
174 queryString += encodeURIComponent(value);
175 }
176 isFirst = false;
177 }
178 }
179 return queryString;
180}