-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathproject-management.ts
More file actions
299 lines (271 loc) · 9.97 KB
/
project-management.ts
File metadata and controls
299 lines (271 loc) · 9.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*!
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { App } from '../app';
import { FirebaseProjectManagementError } from '../utils/error';
import * as utils from '../utils/index';
import * as validator from '../utils/validator';
import { AndroidApp, ShaCertificate } from './android-app';
import { IosApp } from './ios-app';
import { ProjectManagementRequestHandler, assertServerResponse } from './project-management-api-request-internal';
import { AppMetadata, AppPlatform } from './app-metadata';
/**
* The Firebase ProjectManagement service interface.
*/
export class ProjectManagement {
private readonly requestHandler: ProjectManagementRequestHandler;
private projectId!: string;
/**
* @param app - The app for this ProjectManagement service.
* @constructor
* @internal
*/
constructor(readonly app: App) {
if (!validator.isNonNullObject(app) || !('options' in app)) {
throw new FirebaseProjectManagementError(
'invalid-argument',
'First argument passed to admin.projectManagement() must be a valid Firebase app '
+ 'instance.');
}
this.requestHandler = new ProjectManagementRequestHandler(app);
}
/**
* Lists up to 100 Firebase Android apps associated with this Firebase project.
*
* @returns The list of Android apps.
*/
public listAndroidApps(): Promise<AndroidApp[]> {
return this.listPlatformApps<AndroidApp>('android', 'listAndroidApps()');
}
/**
* Lists up to 100 Firebase iOS apps associated with this Firebase project.
*
* @returns The list of iOS apps.
*/
public listIosApps(): Promise<IosApp[]> {
return this.listPlatformApps<IosApp>('ios', 'listIosApps()');
}
/**
* Creates an `AndroidApp` object, referencing the specified Android app within
* this Firebase project.
*
* This method does not perform an RPC.
*
* @param appId - The `appId` of the Android app to reference.
*
* @returns An `AndroidApp` object that references the specified Firebase Android app.
*/
public androidApp(appId: string): AndroidApp {
return new AndroidApp(appId, this.requestHandler);
}
/**
* Creates an `iOSApp` object, referencing the specified iOS app within
* this Firebase project.
*
* This method does not perform an RPC.
*
* @param appId - The `appId` of the iOS app to reference.
*
* @returns An `iOSApp` object that references the specified Firebase iOS app.
*/
public iosApp(appId: string): IosApp {
return new IosApp(appId, this.requestHandler);
}
/**
* Creates a `ShaCertificate` object.
*
* This method does not perform an RPC.
*
* @param shaHash - The SHA-1 or SHA-256 hash for this certificate.
*
* @returns A `ShaCertificate` object contains the specified SHA hash.
*/
public shaCertificate(shaHash: string): ShaCertificate {
return new ShaCertificate(shaHash);
}
/**
* Creates a new Firebase Android app associated with this Firebase project.
*
* @param packageName - The canonical package name of the Android App,
* as would appear in the Google Play Developer Console.
* @param displayName - An optional user-assigned display name for this
* new app.
*
* @returns A promise that resolves to the newly created Android app.
*/
public createAndroidApp(packageName: string, displayName?: string): Promise<AndroidApp> {
return this.getResourceName()
.then((resourceName) => {
return this.requestHandler.createAndroidApp(resourceName, packageName, displayName);
})
.then((responseData: any) => {
assertServerResponse(
validator.isNonNullObject(responseData),
responseData,
'createAndroidApp()\'s responseData must be a non-null object.');
assertServerResponse(
validator.isNonEmptyString(responseData.appId),
responseData,
'"responseData.appId" field must be present in createAndroidApp()\'s response data.');
return new AndroidApp(responseData.appId, this.requestHandler);
});
}
/**
* Creates a new Firebase iOS app associated with this Firebase project.
*
* @param bundleId - The iOS app bundle ID to use for this new app.
* @param displayName - An optional user-assigned display name for this
* new app.
*
* @returns A promise that resolves to the newly created iOS app.
*/
public createIosApp(bundleId: string, displayName?: string): Promise<IosApp> {
return this.getResourceName()
.then((resourceName) => {
return this.requestHandler.createIosApp(resourceName, bundleId, displayName);
})
.then((responseData: any) => {
assertServerResponse(
validator.isNonNullObject(responseData),
responseData,
'createIosApp()\'s responseData must be a non-null object.');
assertServerResponse(
validator.isNonEmptyString(responseData.appId),
responseData,
'"responseData.appId" field must be present in createIosApp()\'s response data.');
return new IosApp(responseData.appId, this.requestHandler);
});
}
/**
* Lists up to 100 Firebase apps associated with this Firebase project.
*
* @returns A promise that resolves to the metadata list of the apps.
*/
public listAppMetadata(): Promise<AppMetadata[]> {
return this.getResourceName()
.then((resourceName) => {
return this.requestHandler.listAppMetadata(resourceName);
})
.then((responseData) => {
return this.getProjectId()
.then((projectId) => {
return this.transformResponseToAppMetadata(responseData, projectId);
});
});
}
/**
* Update the display name of this Firebase project.
*
* @param newDisplayName - The new display name to be updated.
*
* @returns A promise that resolves when the project display name has been updated.
*/
public setDisplayName(newDisplayName: string): Promise<void> {
return this.getResourceName()
.then((resourceName) => {
return this.requestHandler.setDisplayName(resourceName, newDisplayName);
});
}
private transformResponseToAppMetadata(responseData: any, projectId: string): AppMetadata[] {
this.assertListAppsResponseData(responseData, 'listAppMetadata()');
if (!responseData.apps) {
return [];
}
return responseData.apps.map((appJson: any) => {
assertServerResponse(
validator.isNonEmptyString(appJson.appId),
responseData,
'"apps[].appId" field must be present in the listAppMetadata() response data.');
assertServerResponse(
validator.isNonEmptyString(appJson.platform),
responseData,
'"apps[].platform" field must be present in the listAppMetadata() response data.');
const metadata: AppMetadata = {
appId: appJson.appId,
platform: (AppPlatform as any)[appJson.platform] || AppPlatform.PLATFORM_UNKNOWN,
projectId,
resourceName: appJson.name,
};
if (appJson.displayName) {
metadata.displayName = appJson.displayName;
}
return metadata;
});
}
private getResourceName(): Promise<string> {
return this.getProjectId()
.then((projectId) => {
return `projects/${projectId}`;
});
}
private getProjectId(): Promise<string> {
if (this.projectId) {
return Promise.resolve(this.projectId);
}
return utils.findProjectId(this.app)
.then((projectId) => {
// Assert that a specific project ID was provided within the app.
if (!validator.isNonEmptyString(projectId)) {
throw new FirebaseProjectManagementError(
'invalid-project-id',
'Failed to determine project ID. Initialize the SDK with service account credentials, or '
+ 'set project ID as an app option. Alternatively, set the GOOGLE_CLOUD_PROJECT '
+ 'environment variable.');
}
this.projectId = projectId;
return this.projectId;
});
}
/**
* Lists up to 100 Firebase apps for a specified platform, associated with this Firebase project.
*/
private listPlatformApps<T>(platform: 'android' | 'ios', callerName: string): Promise<T[]> {
return this.getResourceName()
.then((resourceName) => {
return (platform === 'android') ?
this.requestHandler.listAndroidApps(resourceName)
: this.requestHandler.listIosApps(resourceName);
})
.then((responseData: any) => {
this.assertListAppsResponseData(responseData, callerName);
if (!responseData.apps) {
return [];
}
return responseData.apps.map((appJson: any) => {
assertServerResponse(
validator.isNonEmptyString(appJson.appId),
responseData,
`"apps[].appId" field must be present in the ${callerName} response data.`);
if (platform === 'android') {
return new AndroidApp(appJson.appId, this.requestHandler);
} else {
return new IosApp(appJson.appId, this.requestHandler);
}
});
});
}
private assertListAppsResponseData(responseData: any, callerName: string): void {
assertServerResponse(
validator.isNonNullObject(responseData),
responseData,
`${callerName}'s responseData must be a non-null object.`);
if (responseData.apps) {
assertServerResponse(
validator.isArray(responseData.apps),
responseData,
`"apps" field must be present in the ${callerName} response data.`);
}
}
}