@@ -43,11 +43,11 @@ class ProjectManagementInternals implements FirebaseServiceInternalsInterface {
43
43
* ProjectManagement service bound to the provided app.
44
44
*/
45
45
export class ProjectManagement implements FirebaseServiceInterface {
46
+
46
47
public readonly INTERNAL : ProjectManagementInternals = new ProjectManagementInternals ( ) ;
47
48
48
- private readonly resourceName : string ;
49
- private readonly projectId : string ;
50
49
private readonly requestHandler : ProjectManagementRequestHandler ;
50
+ private projectId : string ;
51
51
52
52
/**
53
53
* @param {object } app The app for this ProjectManagement service.
@@ -61,18 +61,6 @@ export class ProjectManagement implements FirebaseServiceInterface {
61
61
+ 'instance.' ) ;
62
62
}
63
63
64
- // Assert that a specific project ID was provided within the app.
65
- const projectId = utils . getProjectId ( app ) ;
66
- if ( ! validator . isNonEmptyString ( projectId ) ) {
67
- throw new FirebaseProjectManagementError (
68
- 'invalid-project-id' ,
69
- 'Failed to determine project ID. Initialize the SDK with service account credentials, or '
70
- + 'set project ID as an app option. Alternatively, set the GOOGLE_CLOUD_PROJECT '
71
- + 'environment variable.' ) ;
72
- }
73
- this . projectId = projectId ;
74
- this . resourceName = `projects/${ this . projectId } ` ;
75
-
76
64
this . requestHandler = new ProjectManagementRequestHandler ( app ) ;
77
65
}
78
66
@@ -115,56 +103,73 @@ export class ProjectManagement implements FirebaseServiceInterface {
115
103
* Creates a new Firebase Android app, associated with this Firebase project.
116
104
*/
117
105
public createAndroidApp ( packageName : string , displayName ?: string ) : Promise < AndroidApp > {
118
- return this . requestHandler . createAndroidApp ( this . resourceName , packageName , displayName )
119
- . then ( ( responseData : any ) => {
120
- assertServerResponse (
121
- validator . isNonNullObject ( responseData ) ,
122
- responseData ,
123
- 'createAndroidApp()\'s responseData must be a non-null object.' ) ;
106
+ return this . getResourceName ( )
107
+ . then ( ( resourceName ) => {
108
+ return this . requestHandler . createAndroidApp ( resourceName , packageName , displayName ) ;
109
+ } )
110
+ . then ( ( responseData : any ) => {
111
+ assertServerResponse (
112
+ validator . isNonNullObject ( responseData ) ,
113
+ responseData ,
114
+ 'createAndroidApp()\'s responseData must be a non-null object.' ) ;
124
115
125
- assertServerResponse (
126
- validator . isNonEmptyString ( responseData . appId ) ,
127
- responseData ,
128
- `"responseData.appId" field must be present in createAndroidApp()'s response data.` ) ;
129
- return new AndroidApp ( responseData . appId , this . requestHandler ) ;
130
- } ) ;
116
+ assertServerResponse (
117
+ validator . isNonEmptyString ( responseData . appId ) ,
118
+ responseData ,
119
+ `"responseData.appId" field must be present in createAndroidApp()'s response data.` ) ;
120
+ return new AndroidApp ( responseData . appId , this . requestHandler ) ;
121
+ } ) ;
131
122
}
132
123
133
124
/**
134
125
* Creates a new Firebase iOS app, associated with this Firebase project.
135
126
*/
136
127
public createIosApp ( bundleId : string , displayName ?: string ) : Promise < IosApp > {
137
- return this . requestHandler . createIosApp ( this . resourceName , bundleId , displayName )
138
- . then ( ( responseData : any ) => {
139
- assertServerResponse (
140
- validator . isNonNullObject ( responseData ) ,
141
- responseData ,
142
- 'createIosApp()\'s responseData must be a non-null object.' ) ;
128
+ return this . getResourceName ( )
129
+ . then ( ( resourceName ) => {
130
+ return this . requestHandler . createIosApp ( resourceName , bundleId , displayName ) ;
131
+ } )
132
+ . then ( ( responseData : any ) => {
133
+ assertServerResponse (
134
+ validator . isNonNullObject ( responseData ) ,
135
+ responseData ,
136
+ 'createIosApp()\'s responseData must be a non-null object.' ) ;
143
137
144
- assertServerResponse (
145
- validator . isNonEmptyString ( responseData . appId ) ,
146
- responseData ,
147
- `"responseData.appId" field must be present in createIosApp()'s response data.` ) ;
148
- return new IosApp ( responseData . appId , this . requestHandler ) ;
149
- } ) ;
138
+ assertServerResponse (
139
+ validator . isNonEmptyString ( responseData . appId ) ,
140
+ responseData ,
141
+ `"responseData.appId" field must be present in createIosApp()'s response data.` ) ;
142
+ return new IosApp ( responseData . appId , this . requestHandler ) ;
143
+ } ) ;
150
144
}
151
145
152
146
/**
153
147
* Lists up to 100 Firebase apps associated with this Firebase project.
154
148
*/
155
149
public listAppMetadata ( ) : Promise < AppMetadata [ ] > {
156
- return this . requestHandler . listAppMetadata ( this . resourceName )
157
- . then ( ( responseData ) => this . transformResponseToAppMetadata ( responseData ) ) ;
150
+ return this . getResourceName ( )
151
+ . then ( ( resourceName ) => {
152
+ return this . requestHandler . listAppMetadata ( resourceName ) ;
153
+ } )
154
+ . then ( ( responseData ) => {
155
+ return this . getProjectId ( )
156
+ . then ( ( projectId ) => {
157
+ return this . transformResponseToAppMetadata ( responseData , projectId ) ;
158
+ } ) ;
159
+ } ) ;
158
160
}
159
161
160
162
/**
161
163
* Update display name of the project
162
164
*/
163
165
public setDisplayName ( newDisplayName : string ) : Promise < void > {
164
- return this . requestHandler . setDisplayName ( this . resourceName , newDisplayName ) ;
166
+ return this . getResourceName ( )
167
+ . then ( ( resourceName ) => {
168
+ return this . requestHandler . setDisplayName ( resourceName , newDisplayName ) ;
169
+ } ) ;
165
170
}
166
171
167
- private transformResponseToAppMetadata ( responseData : any ) : AppMetadata [ ] {
172
+ private transformResponseToAppMetadata ( responseData : any , projectId : string ) : AppMetadata [ ] {
168
173
this . assertListAppsResponseData ( responseData , 'listAppMetadata()' ) ;
169
174
170
175
if ( ! responseData . apps ) {
@@ -183,7 +188,7 @@ export class ProjectManagement implements FirebaseServiceInterface {
183
188
const metadata : AppMetadata = {
184
189
appId : appJson . appId ,
185
190
platform : ( AppPlatform as any ) [ appJson . platform ] || AppPlatform . PLATFORM_UNKNOWN ,
186
- projectId : this . projectId ,
191
+ projectId,
187
192
resourceName : appJson . name ,
188
193
} ;
189
194
if ( appJson . displayName ) {
@@ -193,34 +198,63 @@ export class ProjectManagement implements FirebaseServiceInterface {
193
198
} ) ;
194
199
}
195
200
201
+ private getResourceName ( ) : Promise < string > {
202
+ return this . getProjectId ( )
203
+ . then ( ( projectId ) => {
204
+ return `projects/${ projectId } ` ;
205
+ } ) ;
206
+ }
207
+
208
+ private getProjectId ( ) : Promise < string > {
209
+ if ( this . projectId ) {
210
+ return Promise . resolve ( this . projectId ) ;
211
+ }
212
+
213
+ return utils . findProjectId ( this . app )
214
+ . then ( ( projectId ) => {
215
+ // Assert that a specific project ID was provided within the app.
216
+ if ( ! validator . isNonEmptyString ( projectId ) ) {
217
+ throw new FirebaseProjectManagementError (
218
+ 'invalid-project-id' ,
219
+ 'Failed to determine project ID. Initialize the SDK with service account credentials, or '
220
+ + 'set project ID as an app option. Alternatively, set the GOOGLE_CLOUD_PROJECT '
221
+ + 'environment variable.' ) ;
222
+ }
223
+
224
+ this . projectId = projectId ;
225
+ return this . projectId ;
226
+ } ) ;
227
+ }
228
+
196
229
/**
197
230
* Lists up to 100 Firebase apps for a specified platform, associated with this Firebase project.
198
231
*/
199
232
private listPlatformApps < T > ( platform : 'android' | 'ios' , callerName : string ) : Promise < T [ ] > {
200
- const listPromise : Promise < object > = ( platform === 'android' ) ?
201
- this . requestHandler . listAndroidApps ( this . resourceName )
202
- : this . requestHandler . listIosApps ( this . resourceName ) ;
233
+ return this . getResourceName ( )
234
+ . then ( ( resourceName ) => {
235
+ return ( platform === 'android' ) ?
236
+ this . requestHandler . listAndroidApps ( resourceName )
237
+ : this . requestHandler . listIosApps ( resourceName ) ;
238
+ } )
239
+ . then ( ( responseData : any ) => {
240
+ this . assertListAppsResponseData ( responseData , callerName ) ;
203
241
204
- return listPromise
205
- . then ( ( responseData : any ) => {
206
- this . assertListAppsResponseData ( responseData , callerName ) ;
242
+ if ( ! responseData . apps ) {
243
+ return [ ] ;
244
+ }
207
245
208
- if ( ! responseData . apps ) {
209
- return [ ] ;
246
+ return responseData . apps . map ( ( appJson : any ) => {
247
+ assertServerResponse (
248
+ validator . isNonEmptyString ( appJson . appId ) ,
249
+ responseData ,
250
+ `"apps[].appId" field must be present in the ${ callerName } response data.` ) ;
251
+ if ( platform === 'android' ) {
252
+ return new AndroidApp ( appJson . appId , this . requestHandler ) ;
253
+ } else {
254
+ return new IosApp ( appJson . appId , this . requestHandler ) ;
210
255
}
211
-
212
- return responseData . apps . map ( ( appJson : any ) => {
213
- assertServerResponse (
214
- validator . isNonEmptyString ( appJson . appId ) ,
215
- responseData ,
216
- `"apps[].appId" field must be present in the ${ callerName } response data.` ) ;
217
- if ( platform === 'android' ) {
218
- return new AndroidApp ( appJson . appId , this . requestHandler ) ;
219
- } else {
220
- return new IosApp ( appJson . appId , this . requestHandler ) ;
221
- }
222
- } ) ;
223
256
} ) ;
257
+ } ) ;
224
258
}
225
259
226
260
private assertListAppsResponseData ( responseData : any , callerName : string ) : void {
0 commit comments