1 |
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.ProjectManagement = void 0;
|
20 | var error_1 = require("../utils/error");
|
21 | var utils = require("../utils/index");
|
22 | var validator = require("../utils/validator");
|
23 | var android_app_1 = require("./android-app");
|
24 | var ios_app_1 = require("./ios-app");
|
25 | var project_management_api_request_internal_1 = require("./project-management-api-request-internal");
|
26 | var app_metadata_1 = require("./app-metadata");
|
27 |
|
28 |
|
29 |
|
30 | var ProjectManagement = (function () {
|
31 | |
32 |
|
33 |
|
34 |
|
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 |
|
46 |
|
47 |
|
48 |
|
49 | ProjectManagement.prototype.listAndroidApps = function () {
|
50 | return this.listPlatformApps('android', 'listAndroidApps()');
|
51 | };
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 | ProjectManagement.prototype.listIosApps = function () {
|
58 | return this.listPlatformApps('ios', 'listIosApps()');
|
59 | };
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | ProjectManagement.prototype.androidApp = function (appId) {
|
71 | return new android_app_1.AndroidApp(appId, this.requestHandler);
|
72 | };
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | ProjectManagement.prototype.iosApp = function (appId) {
|
84 | return new ios_app_1.IosApp(appId, this.requestHandler);
|
85 | };
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | ProjectManagement.prototype.shaCertificate = function (shaHash) {
|
96 | return new android_app_1.ShaCertificate(shaHash);
|
97 | };
|
98 | |
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
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 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
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 |
|
143 |
|
144 |
|
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 |
|
161 |
|
162 |
|
163 |
|
164 |
|
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 |
|
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 |
|
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 | }());
|
251 | exports.ProjectManagement = ProjectManagement;
|