UNPKG

11.6 kBJavaScriptView Raw
1/*! firebase-admin v10.0.0 */
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 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.ProjectManagement = void 0;
20var error_1 = require("../utils/error");
21var utils = require("../utils/index");
22var validator = require("../utils/validator");
23var android_app_1 = require("./android-app");
24var ios_app_1 = require("./ios-app");
25var project_management_api_request_internal_1 = require("./project-management-api-request-internal");
26var app_metadata_1 = require("./app-metadata");
27/**
28 * The Firebase ProjectManagement service interface.
29 */
30var ProjectManagement = /** @class */ (function () {
31 /**
32 * @param app - The app for this ProjectManagement service.
33 * @constructor
34 * @internal
35 */
36 function ProjectManagement(app) {
37 this.app = app;
38 if (!validator.isNonNullObject(app) || !('options' in app)) {
39 throw new error_1.FirebaseProjectManagementError('invalid-argument', 'First argument passed to admin.projectManagement() must be a valid Firebase app '
40 + 'instance.');
41 }
42 this.requestHandler = new project_management_api_request_internal_1.ProjectManagementRequestHandler(app);
43 }
44 /**
45 * Lists up to 100 Firebase Android apps associated with this Firebase project.
46 *
47 * @returns The list of Android apps.
48 */
49 ProjectManagement.prototype.listAndroidApps = function () {
50 return this.listPlatformApps('android', 'listAndroidApps()');
51 };
52 /**
53 * Lists up to 100 Firebase iOS apps associated with this Firebase project.
54 *
55 * @returns The list of iOS apps.
56 */
57 ProjectManagement.prototype.listIosApps = function () {
58 return this.listPlatformApps('ios', 'listIosApps()');
59 };
60 /**
61 * Creates an `AndroidApp` object, referencing the specified Android app within
62 * this Firebase project.
63 *
64 * This method does not perform an RPC.
65 *
66 * @param appId - The `appId` of the Android app to reference.
67 *
68 * @returns An `AndroidApp` object that references the specified Firebase Android app.
69 */
70 ProjectManagement.prototype.androidApp = function (appId) {
71 return new android_app_1.AndroidApp(appId, this.requestHandler);
72 };
73 /**
74 * Creates an `iOSApp` object, referencing the specified iOS app within
75 * this Firebase project.
76 *
77 * This method does not perform an RPC.
78 *
79 * @param appId - The `appId` of the iOS app to reference.
80 *
81 * @returns An `iOSApp` object that references the specified Firebase iOS app.
82 */
83 ProjectManagement.prototype.iosApp = function (appId) {
84 return new ios_app_1.IosApp(appId, this.requestHandler);
85 };
86 /**
87 * Creates a `ShaCertificate` object.
88 *
89 * This method does not perform an RPC.
90 *
91 * @param shaHash - The SHA-1 or SHA-256 hash for this certificate.
92 *
93 * @returns A `ShaCertificate` object contains the specified SHA hash.
94 */
95 ProjectManagement.prototype.shaCertificate = function (shaHash) {
96 return new android_app_1.ShaCertificate(shaHash);
97 };
98 /**
99 * Creates a new Firebase Android app associated with this Firebase project.
100 *
101 * @param packageName - The canonical package name of the Android App,
102 * as would appear in the Google Play Developer Console.
103 * @param displayName - An optional user-assigned display name for this
104 * new app.
105 *
106 * @returns A promise that resolves to the newly created Android app.
107 */
108 ProjectManagement.prototype.createAndroidApp = function (packageName, displayName) {
109 var _this = this;
110 return this.getResourceName()
111 .then(function (resourceName) {
112 return _this.requestHandler.createAndroidApp(resourceName, packageName, displayName);
113 })
114 .then(function (responseData) {
115 project_management_api_request_internal_1.assertServerResponse(validator.isNonNullObject(responseData), responseData, 'createAndroidApp()\'s responseData must be a non-null object.');
116 project_management_api_request_internal_1.assertServerResponse(validator.isNonEmptyString(responseData.appId), responseData, '"responseData.appId" field must be present in createAndroidApp()\'s response data.');
117 return new android_app_1.AndroidApp(responseData.appId, _this.requestHandler);
118 });
119 };
120 /**
121 * Creates a new Firebase iOS app associated with this Firebase project.
122 *
123 * @param bundleId - The iOS app bundle ID to use for this new app.
124 * @param displayName - An optional user-assigned display name for this
125 * new app.
126 *
127 * @returns A promise that resolves to the newly created iOS app.
128 */
129 ProjectManagement.prototype.createIosApp = function (bundleId, displayName) {
130 var _this = this;
131 return this.getResourceName()
132 .then(function (resourceName) {
133 return _this.requestHandler.createIosApp(resourceName, bundleId, displayName);
134 })
135 .then(function (responseData) {
136 project_management_api_request_internal_1.assertServerResponse(validator.isNonNullObject(responseData), responseData, 'createIosApp()\'s responseData must be a non-null object.');
137 project_management_api_request_internal_1.assertServerResponse(validator.isNonEmptyString(responseData.appId), responseData, '"responseData.appId" field must be present in createIosApp()\'s response data.');
138 return new ios_app_1.IosApp(responseData.appId, _this.requestHandler);
139 });
140 };
141 /**
142 * Lists up to 100 Firebase apps associated with this Firebase project.
143 *
144 * @returns A promise that resolves to the metadata list of the apps.
145 */
146 ProjectManagement.prototype.listAppMetadata = function () {
147 var _this = this;
148 return this.getResourceName()
149 .then(function (resourceName) {
150 return _this.requestHandler.listAppMetadata(resourceName);
151 })
152 .then(function (responseData) {
153 return _this.getProjectId()
154 .then(function (projectId) {
155 return _this.transformResponseToAppMetadata(responseData, projectId);
156 });
157 });
158 };
159 /**
160 * Update the display name of this Firebase project.
161 *
162 * @param newDisplayName - The new display name to be updated.
163 *
164 * @returns A promise that resolves when the project display name has been updated.
165 */
166 ProjectManagement.prototype.setDisplayName = function (newDisplayName) {
167 var _this = this;
168 return this.getResourceName()
169 .then(function (resourceName) {
170 return _this.requestHandler.setDisplayName(resourceName, newDisplayName);
171 });
172 };
173 ProjectManagement.prototype.transformResponseToAppMetadata = function (responseData, projectId) {
174 this.assertListAppsResponseData(responseData, 'listAppMetadata()');
175 if (!responseData.apps) {
176 return [];
177 }
178 return responseData.apps.map(function (appJson) {
179 project_management_api_request_internal_1.assertServerResponse(validator.isNonEmptyString(appJson.appId), responseData, '"apps[].appId" field must be present in the listAppMetadata() response data.');
180 project_management_api_request_internal_1.assertServerResponse(validator.isNonEmptyString(appJson.platform), responseData, '"apps[].platform" field must be present in the listAppMetadata() response data.');
181 var metadata = {
182 appId: appJson.appId,
183 platform: app_metadata_1.AppPlatform[appJson.platform] || app_metadata_1.AppPlatform.PLATFORM_UNKNOWN,
184 projectId: projectId,
185 resourceName: appJson.name,
186 };
187 if (appJson.displayName) {
188 metadata.displayName = appJson.displayName;
189 }
190 return metadata;
191 });
192 };
193 ProjectManagement.prototype.getResourceName = function () {
194 return this.getProjectId()
195 .then(function (projectId) {
196 return "projects/" + projectId;
197 });
198 };
199 ProjectManagement.prototype.getProjectId = function () {
200 var _this = this;
201 if (this.projectId) {
202 return Promise.resolve(this.projectId);
203 }
204 return utils.findProjectId(this.app)
205 .then(function (projectId) {
206 // Assert that a specific project ID was provided within the app.
207 if (!validator.isNonEmptyString(projectId)) {
208 throw new error_1.FirebaseProjectManagementError('invalid-project-id', 'Failed to determine project ID. Initialize the SDK with service account credentials, or '
209 + 'set project ID as an app option. Alternatively, set the GOOGLE_CLOUD_PROJECT '
210 + 'environment variable.');
211 }
212 _this.projectId = projectId;
213 return _this.projectId;
214 });
215 };
216 /**
217 * Lists up to 100 Firebase apps for a specified platform, associated with this Firebase project.
218 */
219 ProjectManagement.prototype.listPlatformApps = function (platform, callerName) {
220 var _this = this;
221 return this.getResourceName()
222 .then(function (resourceName) {
223 return (platform === 'android') ?
224 _this.requestHandler.listAndroidApps(resourceName)
225 : _this.requestHandler.listIosApps(resourceName);
226 })
227 .then(function (responseData) {
228 _this.assertListAppsResponseData(responseData, callerName);
229 if (!responseData.apps) {
230 return [];
231 }
232 return responseData.apps.map(function (appJson) {
233 project_management_api_request_internal_1.assertServerResponse(validator.isNonEmptyString(appJson.appId), responseData, "\"apps[].appId\" field must be present in the " + callerName + " response data.");
234 if (platform === 'android') {
235 return new android_app_1.AndroidApp(appJson.appId, _this.requestHandler);
236 }
237 else {
238 return new ios_app_1.IosApp(appJson.appId, _this.requestHandler);
239 }
240 });
241 });
242 };
243 ProjectManagement.prototype.assertListAppsResponseData = function (responseData, callerName) {
244 project_management_api_request_internal_1.assertServerResponse(validator.isNonNullObject(responseData), responseData, callerName + "'s responseData must be a non-null object.");
245 if (responseData.apps) {
246 project_management_api_request_internal_1.assertServerResponse(validator.isArray(responseData.apps), responseData, "\"apps\" field must be present in the " + callerName + " response data.");
247 }
248 };
249 return ProjectManagement;
250}());
251exports.ProjectManagement = ProjectManagement;