firebase-admin 6.2.0 → 6.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- /*! firebase-admin v6.2.0 */
1
+ /*! firebase-admin v6.5.1 */
2
2
  "use strict";
3
3
  /*!
4
4
  * Copyright 2017 Google Inc.
@@ -155,6 +155,7 @@ function validateAps(aps) {
155
155
  throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, 'apns.payload.aps must be a non-null object');
156
156
  }
157
157
  validateApsAlert(aps.alert);
158
+ validateApsSound(aps.sound);
158
159
  var propertyMappings = {
159
160
  contentAvailable: 'content-available',
160
161
  mutableContent: 'mutable-content',
@@ -185,6 +186,37 @@ function validateAps(aps) {
185
186
  }
186
187
  }
187
188
  }
189
+ function validateApsSound(sound) {
190
+ if (typeof sound === 'undefined' || validator.isNonEmptyString(sound)) {
191
+ return;
192
+ }
193
+ else if (!validator.isNonNullObject(sound)) {
194
+ throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, 'apns.payload.aps.sound must be a non-empty string or a non-null object');
195
+ }
196
+ if (!validator.isNonEmptyString(sound.name)) {
197
+ throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, 'apns.payload.aps.sound.name must be a non-empty string');
198
+ }
199
+ var volume = sound.volume;
200
+ if (typeof volume !== 'undefined') {
201
+ if (!validator.isNumber(volume)) {
202
+ throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, 'apns.payload.aps.sound.volume must be a number');
203
+ }
204
+ if (volume < 0 || volume > 1) {
205
+ throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, 'apns.payload.aps.sound.volume must be in the interval [0, 1]');
206
+ }
207
+ }
208
+ var soundObject = sound;
209
+ var key = 'critical';
210
+ var critical = soundObject[key];
211
+ if (typeof critical !== 'undefined' && critical !== 1) {
212
+ if (critical === true) {
213
+ soundObject[key] = 1;
214
+ }
215
+ else {
216
+ delete soundObject[key];
217
+ }
218
+ }
219
+ }
188
220
  /**
189
221
  * Checks if the given alert object is valid. Alert could be a string or a complex object.
190
222
  * If specified as an object, it must have valid localization parameters. If successful, transforms
@@ -208,11 +240,17 @@ function validateApsAlert(alert) {
208
240
  !validator.isNonEmptyString(apsAlert.titleLocKey)) {
209
241
  throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, 'apns.payload.aps.alert.titleLocKey is required when specifying titleLocArgs');
210
242
  }
243
+ if (validator.isNonEmptyArray(apsAlert.subtitleLocArgs) &&
244
+ !validator.isNonEmptyString(apsAlert.subtitleLocKey)) {
245
+ throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, 'apns.payload.aps.alert.subtitleLocKey is required when specifying subtitleLocArgs');
246
+ }
211
247
  var propertyMappings = {
212
248
  locKey: 'loc-key',
213
249
  locArgs: 'loc-args',
214
250
  titleLocKey: 'title-loc-key',
215
251
  titleLocArgs: 'title-loc-args',
252
+ subtitleLocKey: 'subtitle-loc-key',
253
+ subtitleLocArgs: 'subtitle-loc-args',
216
254
  actionLocKey: 'action-loc-key',
217
255
  launchImage: 'launch-image',
218
256
  };
@@ -290,6 +328,7 @@ function validateAndroidNotification(notification) {
290
328
  bodyLocArgs: 'body_loc_args',
291
329
  titleLocKey: 'title_loc_key',
292
330
  titleLocArgs: 'title_loc_args',
331
+ channelId: 'channel_id',
293
332
  };
294
333
  index_1.renameProperties(notification, propertyMappings);
295
334
  }
@@ -0,0 +1,116 @@
1
+ /*! firebase-admin v6.5.1 */
2
+ "use strict";
3
+ /*!
4
+ * Copyright 2018 Google Inc.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ var error_1 = require("../utils/error");
20
+ var validator = require("../utils/validator");
21
+ var project_management_api_request_1 = require("./project-management-api-request");
22
+ var AndroidApp = /** @class */ (function () {
23
+ function AndroidApp(appId, requestHandler) {
24
+ this.appId = appId;
25
+ this.requestHandler = requestHandler;
26
+ if (!validator.isNonEmptyString(appId)) {
27
+ throw new error_1.FirebaseProjectManagementError('invalid-argument', 'appId must be a non-empty string.');
28
+ }
29
+ this.resourceName = "projects/-/androidApps/" + appId;
30
+ }
31
+ AndroidApp.prototype.getMetadata = function () {
32
+ return this.requestHandler.getResource(this.resourceName)
33
+ .then(function (responseData) {
34
+ project_management_api_request_1.assertServerResponse(validator.isNonNullObject(responseData), responseData, 'getMetadata()\'s responseData must be a non-null object.');
35
+ var requiredFieldsList = ['name', 'appId', 'projectId', 'packageName'];
36
+ requiredFieldsList.forEach(function (requiredField) {
37
+ project_management_api_request_1.assertServerResponse(validator.isNonEmptyString(responseData[requiredField]), responseData, "getMetadata()'s responseData." + requiredField + " must be a non-empty string.");
38
+ });
39
+ var metadata = {
40
+ resourceName: responseData.name,
41
+ appId: responseData.appId,
42
+ displayName: responseData.displayName || null,
43
+ projectId: responseData.projectId,
44
+ packageName: responseData.packageName,
45
+ };
46
+ return metadata;
47
+ });
48
+ };
49
+ AndroidApp.prototype.setDisplayName = function (newDisplayName) {
50
+ return this.requestHandler.setDisplayName(this.resourceName, newDisplayName);
51
+ };
52
+ AndroidApp.prototype.getShaCertificates = function () {
53
+ return this.requestHandler.getAndroidShaCertificates(this.resourceName)
54
+ .then(function (responseData) {
55
+ project_management_api_request_1.assertServerResponse(validator.isNonNullObject(responseData), responseData, 'getShaCertificates()\'s responseData must be a non-null object.');
56
+ if (!responseData.certificates) {
57
+ return [];
58
+ }
59
+ project_management_api_request_1.assertServerResponse(validator.isArray(responseData.certificates), responseData, '"certificates" field must be present in the getShaCertificates() response data.');
60
+ var requiredFieldsList = ['name', 'shaHash'];
61
+ return responseData.certificates.map(function (certificateJson) {
62
+ requiredFieldsList.forEach(function (requiredField) {
63
+ project_management_api_request_1.assertServerResponse(validator.isNonEmptyString(certificateJson[requiredField]), responseData, "getShaCertificates()'s responseData.certificates[]." + requiredField + " must be a "
64
+ + "non-empty string.");
65
+ });
66
+ return new ShaCertificate(certificateJson.shaHash, certificateJson.name);
67
+ });
68
+ });
69
+ };
70
+ AndroidApp.prototype.addShaCertificate = function (certificateToAdd) {
71
+ return this.requestHandler.addAndroidShaCertificate(this.resourceName, certificateToAdd);
72
+ };
73
+ AndroidApp.prototype.deleteShaCertificate = function (certificateToDelete) {
74
+ return this.requestHandler.deleteResource(certificateToDelete.resourceName);
75
+ };
76
+ /**
77
+ * @return {Promise<string>} A promise that resolves to a UTF-8 JSON string, typically intended to
78
+ * be written to a JSON file.
79
+ */
80
+ AndroidApp.prototype.getConfig = function () {
81
+ return this.requestHandler.getConfig(this.resourceName)
82
+ .then(function (responseData) {
83
+ project_management_api_request_1.assertServerResponse(validator.isNonNullObject(responseData), responseData, 'getConfig()\'s responseData must be a non-null object.');
84
+ var base64ConfigFileContents = responseData.configFileContents;
85
+ project_management_api_request_1.assertServerResponse(validator.isBase64String(base64ConfigFileContents), responseData, "getConfig()'s responseData.configFileContents must be a base64 string.");
86
+ return Buffer.from(base64ConfigFileContents, 'base64').toString('utf8');
87
+ });
88
+ };
89
+ return AndroidApp;
90
+ }());
91
+ exports.AndroidApp = AndroidApp;
92
+ var ShaCertificate = /** @class */ (function () {
93
+ /**
94
+ * Creates a ShaCertificate using the given hash. The ShaCertificate's type (eg. 'sha256') is
95
+ * automatically determined from the hash itself.
96
+ *
97
+ * @param shaHash The sha256 or sha1 hash for this certificate.
98
+ * @param resourceName The Firebase resource name for this certificate. This does not need to be
99
+ * set when creating a new certificate.
100
+ */
101
+ function ShaCertificate(shaHash, resourceName) {
102
+ this.shaHash = shaHash;
103
+ this.resourceName = resourceName;
104
+ if (/^[a-fA-F0-9]{40}$/.test(shaHash)) {
105
+ this.certType = 'sha1';
106
+ }
107
+ else if (/^[a-fA-F0-9]{64}$/.test(shaHash)) {
108
+ this.certType = 'sha256';
109
+ }
110
+ else {
111
+ throw new error_1.FirebaseProjectManagementError('invalid-argument', 'shaHash must be either a sha256 hash or a sha1 hash.');
112
+ }
113
+ }
114
+ return ShaCertificate;
115
+ }());
116
+ exports.ShaCertificate = ShaCertificate;
@@ -0,0 +1,67 @@
1
+ /*! firebase-admin v6.5.1 */
2
+ "use strict";
3
+ /*!
4
+ * Copyright 2018 Google Inc.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ var error_1 = require("../utils/error");
20
+ var validator = require("../utils/validator");
21
+ var project_management_api_request_1 = require("./project-management-api-request");
22
+ var IosApp = /** @class */ (function () {
23
+ function IosApp(appId, requestHandler) {
24
+ this.appId = appId;
25
+ this.requestHandler = requestHandler;
26
+ if (!validator.isNonEmptyString(appId)) {
27
+ throw new error_1.FirebaseProjectManagementError('invalid-argument', 'appId must be a non-empty string.');
28
+ }
29
+ this.resourceName = "projects/-/iosApps/" + appId;
30
+ }
31
+ IosApp.prototype.getMetadata = function () {
32
+ return this.requestHandler.getResource(this.resourceName)
33
+ .then(function (responseData) {
34
+ project_management_api_request_1.assertServerResponse(validator.isNonNullObject(responseData), responseData, 'getMetadata()\'s responseData must be a non-null object.');
35
+ var requiredFieldsList = ['name', 'appId', 'projectId', 'bundleId'];
36
+ requiredFieldsList.forEach(function (requiredField) {
37
+ project_management_api_request_1.assertServerResponse(validator.isNonEmptyString(responseData[requiredField]), responseData, "getMetadata()'s responseData." + requiredField + " must be a non-empty string.");
38
+ });
39
+ var metadata = {
40
+ resourceName: responseData.name,
41
+ appId: responseData.appId,
42
+ displayName: responseData.displayName || null,
43
+ projectId: responseData.projectId,
44
+ bundleId: responseData.bundleId,
45
+ };
46
+ return metadata;
47
+ });
48
+ };
49
+ IosApp.prototype.setDisplayName = function (newDisplayName) {
50
+ return this.requestHandler.setDisplayName(this.resourceName, newDisplayName);
51
+ };
52
+ /**
53
+ * @return {Promise<string>} A promise that resolves to a UTF-8 XML string, typically intended to
54
+ * be written to a plist file.
55
+ */
56
+ IosApp.prototype.getConfig = function () {
57
+ return this.requestHandler.getConfig(this.resourceName)
58
+ .then(function (responseData) {
59
+ project_management_api_request_1.assertServerResponse(validator.isNonNullObject(responseData), responseData, 'getConfig()\'s responseData must be a non-null object.');
60
+ var base64ConfigFileContents = responseData.configFileContents;
61
+ project_management_api_request_1.assertServerResponse(validator.isBase64String(base64ConfigFileContents), responseData, "getConfig()'s responseData.configFileContents must be a base64 string.");
62
+ return Buffer.from(base64ConfigFileContents, 'base64').toString('utf8');
63
+ });
64
+ };
65
+ return IosApp;
66
+ }());
67
+ exports.IosApp = IosApp;
@@ -0,0 +1,260 @@
1
+ /*! firebase-admin v6.5.1 */
2
+ "use strict";
3
+ /*!
4
+ * Copyright 2018 Google Inc.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ var api_request_1 = require("../utils/api-request");
20
+ var error_1 = require("../utils/error");
21
+ var validator = require("../utils/validator");
22
+ /** Project management backend host and port. */
23
+ var PROJECT_MANAGEMENT_HOST_AND_PORT = 'firebase.googleapis.com:443';
24
+ /** Project management backend path. */
25
+ var PROJECT_MANAGEMENT_PATH = '/v1/';
26
+ /** Project management beta backend path. */
27
+ var PROJECT_MANAGEMENT_BETA_PATH = '/v1beta1/';
28
+ /** Project management request header. */
29
+ var PROJECT_MANAGEMENT_HEADERS = {
30
+ 'X-Client-Version': 'Node/Admin/6.5.1',
31
+ };
32
+ /** Project management request timeout duration in milliseconds. */
33
+ var PROJECT_MANAGEMENT_TIMEOUT_MILLIS = 10000;
34
+ var LIST_APPS_MAX_PAGE_SIZE = 100;
35
+ var CERT_TYPE_API_MAP = {
36
+ sha1: 'SHA_1',
37
+ sha256: 'SHA_256',
38
+ };
39
+ function assertServerResponse(condition, responseData, message) {
40
+ if (!condition) {
41
+ throw new error_1.FirebaseProjectManagementError('invalid-server-response', message + " Response data: " + JSON.stringify(responseData, null, 2));
42
+ }
43
+ }
44
+ exports.assertServerResponse = assertServerResponse;
45
+ /**
46
+ * Class that provides mechanism to send requests to the Firebase project management backend
47
+ * endpoints.
48
+ *
49
+ * @private
50
+ */
51
+ var ProjectManagementRequestHandler = /** @class */ (function () {
52
+ /**
53
+ * @param {FirebaseApp} app The app used to fetch access tokens to sign API requests.
54
+ * @constructor
55
+ */
56
+ function ProjectManagementRequestHandler(app) {
57
+ this.baseUrl = "https://" + PROJECT_MANAGEMENT_HOST_AND_PORT + PROJECT_MANAGEMENT_PATH;
58
+ this.baseBetaUrl = "https://" + PROJECT_MANAGEMENT_HOST_AND_PORT + PROJECT_MANAGEMENT_BETA_PATH;
59
+ this.httpClient = new api_request_1.AuthorizedHttpClient(app);
60
+ }
61
+ ProjectManagementRequestHandler.wrapAndRethrowHttpError = function (errStatusCode, errText) {
62
+ var errorCode;
63
+ var errorMessage;
64
+ switch (errStatusCode) {
65
+ case 400:
66
+ errorCode = 'invalid-argument';
67
+ errorMessage = 'Invalid argument provided.';
68
+ break;
69
+ case 401:
70
+ case 403:
71
+ errorCode = 'authentication-error';
72
+ errorMessage = 'An error occurred when trying to authenticate. Make sure the credential '
73
+ + 'used to authenticate this SDK has the proper permissions. See '
74
+ + 'https://firebase.google.com/docs/admin/setup for setup instructions.';
75
+ break;
76
+ case 404:
77
+ errorCode = 'not-found';
78
+ errorMessage = 'The specified entity could not be found.';
79
+ break;
80
+ case 409:
81
+ errorCode = 'already-exists';
82
+ errorMessage = 'The specified entity already exists.';
83
+ break;
84
+ case 500:
85
+ errorCode = 'internal-error';
86
+ errorMessage = 'An internal error has occurred. Please retry the request.';
87
+ break;
88
+ case 503:
89
+ errorCode = 'service-unavailable';
90
+ errorMessage = 'The server could not process the request in time. See the error '
91
+ + 'documentation for more details.';
92
+ break;
93
+ default:
94
+ errorCode = 'unknown-error';
95
+ errorMessage = 'An unknown server error was returned.';
96
+ }
97
+ throw new error_1.FirebaseProjectManagementError(errorCode, errorMessage + " Status code: " + errStatusCode + ". Raw server response: \"" + errText + "\".");
98
+ };
99
+ /**
100
+ * @param {string} parentResourceName Fully-qualified resource name of the project whose Android
101
+ * apps you want to list.
102
+ */
103
+ ProjectManagementRequestHandler.prototype.listAndroidApps = function (parentResourceName) {
104
+ return this.invokeRequestHandler('GET', parentResourceName + "/androidApps?page_size=" + LIST_APPS_MAX_PAGE_SIZE,
105
+ /* requestData */ null, 'v1beta1');
106
+ };
107
+ /**
108
+ * @param {string} parentResourceName Fully-qualified resource name of the project whose iOS apps
109
+ * you want to list.
110
+ */
111
+ ProjectManagementRequestHandler.prototype.listIosApps = function (parentResourceName) {
112
+ return this.invokeRequestHandler('GET', parentResourceName + "/iosApps?page_size=" + LIST_APPS_MAX_PAGE_SIZE,
113
+ /* requestData */ null, 'v1beta1');
114
+ };
115
+ /**
116
+ * @param {string} parentResourceName Fully-qualified resource name of the project that you want
117
+ * to create the Android app within.
118
+ */
119
+ ProjectManagementRequestHandler.prototype.createAndroidApp = function (parentResourceName, packageName, displayName) {
120
+ var _this = this;
121
+ var requestData = {
122
+ packageName: packageName,
123
+ };
124
+ if (validator.isNonEmptyString(displayName)) {
125
+ requestData.displayName = displayName;
126
+ }
127
+ return this
128
+ .invokeRequestHandler('POST', parentResourceName + "/androidApps", requestData, 'v1beta1')
129
+ .then(function (responseData) {
130
+ assertServerResponse(validator.isNonNullObject(responseData), responseData, "createAndroidApp's responseData must be a non-null object.");
131
+ assertServerResponse(validator.isNonEmptyString(responseData.name), responseData, "createAndroidApp's responseData.name must be a non-empty string.");
132
+ return _this.pollRemoteOperationWithExponentialBackoff(responseData.name);
133
+ });
134
+ };
135
+ /**
136
+ * @param {string} parentResourceName Fully-qualified resource name of the project that you want
137
+ * to create the iOS app within.
138
+ */
139
+ ProjectManagementRequestHandler.prototype.createIosApp = function (parentResourceName, bundleId, displayName) {
140
+ var _this = this;
141
+ var requestData = {
142
+ bundleId: bundleId,
143
+ };
144
+ if (validator.isNonEmptyString(displayName)) {
145
+ requestData.displayName = displayName;
146
+ }
147
+ return this
148
+ .invokeRequestHandler('POST', parentResourceName + "/iosApps", requestData, 'v1beta1')
149
+ .then(function (responseData) {
150
+ assertServerResponse(validator.isNonNullObject(responseData), responseData, "createIosApp's responseData must be a non-null object.");
151
+ assertServerResponse(validator.isNonEmptyString(responseData.name), responseData, "createIosApp's responseData.name must be a non-empty string.");
152
+ return _this.pollRemoteOperationWithExponentialBackoff(responseData.name);
153
+ });
154
+ };
155
+ /**
156
+ * @param {string} resourceName Fully-qualified resource name of the entity whose display name you
157
+ * want to set.
158
+ */
159
+ ProjectManagementRequestHandler.prototype.setDisplayName = function (resourceName, newDisplayName) {
160
+ var requestData = {
161
+ displayName: newDisplayName,
162
+ };
163
+ return this
164
+ .invokeRequestHandler('PATCH', resourceName + "?update_mask=display_name", requestData, 'v1beta1')
165
+ .then(function () { return null; });
166
+ };
167
+ /**
168
+ * @param {string} parentResourceName Fully-qualified resource name of the Android app whose SHA
169
+ * certificates you want to get.
170
+ */
171
+ ProjectManagementRequestHandler.prototype.getAndroidShaCertificates = function (parentResourceName) {
172
+ return this.invokeRequestHandler('GET', parentResourceName + "/sha", /* requestData */ null, 'v1beta1');
173
+ };
174
+ /**
175
+ * @param {string} parentResourceName Fully-qualified resource name of the Android app that you
176
+ * want to add the given SHA certificate to.
177
+ */
178
+ ProjectManagementRequestHandler.prototype.addAndroidShaCertificate = function (parentResourceName, certificate) {
179
+ var requestData = {
180
+ shaHash: certificate.shaHash,
181
+ certType: CERT_TYPE_API_MAP[certificate.certType],
182
+ };
183
+ return this
184
+ .invokeRequestHandler('POST', parentResourceName + "/sha", requestData, 'v1beta1')
185
+ .then(function () { return null; });
186
+ };
187
+ /**
188
+ * @param {string} parentResourceName Fully-qualified resource name of the app whose config you
189
+ * want to get.
190
+ */
191
+ ProjectManagementRequestHandler.prototype.getConfig = function (parentResourceName) {
192
+ return this.invokeRequestHandler('GET', parentResourceName + "/config", /* requestData */ null, 'v1beta1');
193
+ };
194
+ /**
195
+ * @param {string} parentResourceName Fully-qualified resource name of the entity that you want to
196
+ * get.
197
+ */
198
+ ProjectManagementRequestHandler.prototype.getResource = function (parentResourceName) {
199
+ return this.invokeRequestHandler('GET', parentResourceName, /* requestData */ null, 'v1beta1');
200
+ };
201
+ /**
202
+ * @param {string} resourceName Fully-qualified resource name of the entity that you want to
203
+ * delete.
204
+ */
205
+ ProjectManagementRequestHandler.prototype.deleteResource = function (resourceName) {
206
+ return this
207
+ .invokeRequestHandler('DELETE', resourceName, /* requestData */ null, 'v1beta1')
208
+ .then(function () { return null; });
209
+ };
210
+ ProjectManagementRequestHandler.prototype.pollRemoteOperationWithExponentialBackoff = function (operationResourceName) {
211
+ var _this = this;
212
+ var poller = new api_request_1.ExponentialBackoffPoller();
213
+ return poller.poll(function () {
214
+ return _this.invokeRequestHandler('GET', operationResourceName, /* requestData */ null)
215
+ .then(function (responseData) {
216
+ if (responseData.error) {
217
+ var errStatusCode = responseData.error.code || 500;
218
+ var errText = responseData.error.message || JSON.stringify(responseData.error);
219
+ ProjectManagementRequestHandler.wrapAndRethrowHttpError(errStatusCode, errText);
220
+ }
221
+ if (!responseData.done) {
222
+ // Continue polling.
223
+ return null;
224
+ }
225
+ // Polling complete. Resolve with operation response JSON.
226
+ return responseData.response;
227
+ });
228
+ });
229
+ };
230
+ /**
231
+ * Invokes the request handler with the provided request data.
232
+ */
233
+ ProjectManagementRequestHandler.prototype.invokeRequestHandler = function (method, path, requestData, apiVersion) {
234
+ if (apiVersion === void 0) { apiVersion = 'v1'; }
235
+ var baseUrlToUse = (apiVersion === 'v1') ? this.baseUrl : this.baseBetaUrl;
236
+ var request = {
237
+ method: method,
238
+ url: "" + baseUrlToUse + path,
239
+ headers: PROJECT_MANAGEMENT_HEADERS,
240
+ data: requestData,
241
+ timeout: PROJECT_MANAGEMENT_TIMEOUT_MILLIS,
242
+ };
243
+ return this.httpClient.send(request)
244
+ .then(function (response) {
245
+ // Send non-JSON responses to the catch() below, where they will be treated as errors.
246
+ if (!response.isJson()) {
247
+ throw new api_request_1.HttpError(response);
248
+ }
249
+ return response.data;
250
+ })
251
+ .catch(function (err) {
252
+ if (err instanceof api_request_1.HttpError) {
253
+ ProjectManagementRequestHandler.wrapAndRethrowHttpError(err.response.status, err.response.text);
254
+ }
255
+ throw err;
256
+ });
257
+ };
258
+ return ProjectManagementRequestHandler;
259
+ }());
260
+ exports.ProjectManagementRequestHandler = ProjectManagementRequestHandler;
@@ -0,0 +1,149 @@
1
+ /*! firebase-admin v6.5.1 */
2
+ "use strict";
3
+ /*!
4
+ * Copyright 2018 Google Inc.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ var error_1 = require("../utils/error");
20
+ var utils = require("../utils/index");
21
+ var validator = require("../utils/validator");
22
+ var android_app_1 = require("./android-app");
23
+ var ios_app_1 = require("./ios-app");
24
+ var project_management_api_request_1 = require("./project-management-api-request");
25
+ /**
26
+ * Internals of a Project Management instance.
27
+ */
28
+ var ProjectManagementInternals = /** @class */ (function () {
29
+ function ProjectManagementInternals() {
30
+ }
31
+ /**
32
+ * Deletes the service and its associated resources.
33
+ *
34
+ * @return {Promise<void>} An empty Promise that will be resolved when the service is deleted.
35
+ */
36
+ ProjectManagementInternals.prototype.delete = function () {
37
+ // There are no resources to clean up.
38
+ return Promise.resolve();
39
+ };
40
+ return ProjectManagementInternals;
41
+ }());
42
+ /**
43
+ * ProjectManagement service bound to the provided app.
44
+ */
45
+ var ProjectManagement = /** @class */ (function () {
46
+ /**
47
+ * @param {object} app The app for this ProjectManagement service.
48
+ * @constructor
49
+ */
50
+ function ProjectManagement(app) {
51
+ this.app = app;
52
+ this.INTERNAL = new ProjectManagementInternals();
53
+ if (!validator.isNonNullObject(app) || !('options' in app)) {
54
+ throw new error_1.FirebaseProjectManagementError('invalid-argument', 'First argument passed to admin.projectManagement() must be a valid Firebase app '
55
+ + 'instance.');
56
+ }
57
+ // Assert that a specific project ID was provided within the app.
58
+ this.projectId = utils.getProjectId(app);
59
+ if (!validator.isNonEmptyString(this.projectId)) {
60
+ throw new error_1.FirebaseProjectManagementError('invalid-project-id', 'Failed to determine project ID. Initialize the SDK with service account credentials, or '
61
+ + 'set project ID as an app option. Alternatively, set the GOOGLE_CLOUD_PROJECT '
62
+ + 'environment variable.');
63
+ }
64
+ this.resourceName = "projects/" + this.projectId;
65
+ this.requestHandler = new project_management_api_request_1.ProjectManagementRequestHandler(app);
66
+ }
67
+ /**
68
+ * Lists up to 100 Firebase Android apps associated with this Firebase project.
69
+ */
70
+ ProjectManagement.prototype.listAndroidApps = function () {
71
+ return this.listPlatformApps('android', 'listAndroidApps()');
72
+ };
73
+ /**
74
+ * Lists up to 100 Firebase iOS apps associated with this Firebase project.
75
+ */
76
+ ProjectManagement.prototype.listIosApps = function () {
77
+ return this.listPlatformApps('ios', 'listIosApps()');
78
+ };
79
+ /**
80
+ * Returns an AndroidApp object for the given appId. No RPC is made.
81
+ */
82
+ ProjectManagement.prototype.androidApp = function (appId) {
83
+ return new android_app_1.AndroidApp(appId, this.requestHandler);
84
+ };
85
+ /**
86
+ * Returns an IosApp object for the given appId. No RPC is made.
87
+ */
88
+ ProjectManagement.prototype.iosApp = function (appId) {
89
+ return new ios_app_1.IosApp(appId, this.requestHandler);
90
+ };
91
+ /**
92
+ * Returns a ShaCertificate object for the given shaHash. No RPC is made.
93
+ */
94
+ ProjectManagement.prototype.shaCertificate = function (shaHash) {
95
+ return new android_app_1.ShaCertificate(shaHash);
96
+ };
97
+ /**
98
+ * Creates a new Firebase Android app, associated with this Firebase project.
99
+ */
100
+ ProjectManagement.prototype.createAndroidApp = function (packageName, displayName) {
101
+ var _this = this;
102
+ return this.requestHandler.createAndroidApp(this.resourceName, packageName, displayName)
103
+ .then(function (responseData) {
104
+ project_management_api_request_1.assertServerResponse(validator.isNonNullObject(responseData), responseData, 'createAndroidApp()\'s responseData must be a non-null object.');
105
+ project_management_api_request_1.assertServerResponse(validator.isNonEmptyString(responseData.appId), responseData, "\"responseData.appId\" field must be present in createAndroidApp()'s response data.");
106
+ return new android_app_1.AndroidApp(responseData.appId, _this.requestHandler);
107
+ });
108
+ };
109
+ /**
110
+ * Creates a new Firebase iOS app, associated with this Firebase project.
111
+ */
112
+ ProjectManagement.prototype.createIosApp = function (bundleId, displayName) {
113
+ var _this = this;
114
+ return this.requestHandler.createIosApp(this.resourceName, bundleId, displayName)
115
+ .then(function (responseData) {
116
+ project_management_api_request_1.assertServerResponse(validator.isNonNullObject(responseData), responseData, 'createIosApp()\'s responseData must be a non-null object.');
117
+ project_management_api_request_1.assertServerResponse(validator.isNonEmptyString(responseData.appId), responseData, "\"responseData.appId\" field must be present in createIosApp()'s response data.");
118
+ return new ios_app_1.IosApp(responseData.appId, _this.requestHandler);
119
+ });
120
+ };
121
+ /**
122
+ * Lists up to 100 Firebase apps for a specified platform, associated with this Firebase project.
123
+ */
124
+ ProjectManagement.prototype.listPlatformApps = function (platform, callerName) {
125
+ var _this = this;
126
+ var listPromise = (platform === 'android') ?
127
+ this.requestHandler.listAndroidApps(this.resourceName)
128
+ : this.requestHandler.listIosApps(this.resourceName);
129
+ return listPromise
130
+ .then(function (responseData) {
131
+ project_management_api_request_1.assertServerResponse(validator.isNonNullObject(responseData), responseData, callerName + "'s responseData must be a non-null object.");
132
+ if (!responseData.apps) {
133
+ return [];
134
+ }
135
+ project_management_api_request_1.assertServerResponse(validator.isArray(responseData.apps), responseData, "\"apps\" field must be present in the " + callerName + " response data.");
136
+ return responseData.apps.map(function (appJson) {
137
+ project_management_api_request_1.assertServerResponse(validator.isNonEmptyString(appJson.appId), responseData, "\"apps[].appId\" field must be present in the " + callerName + " response data.");
138
+ if (platform === 'android') {
139
+ return new android_app_1.AndroidApp(appJson.appId, _this.requestHandler);
140
+ }
141
+ else {
142
+ return new ios_app_1.IosApp(appJson.appId, _this.requestHandler);
143
+ }
144
+ });
145
+ });
146
+ };
147
+ return ProjectManagement;
148
+ }());
149
+ exports.ProjectManagement = ProjectManagement;