@@ -40,21 +40,26 @@ export class AppStore {
4040 options . credential = getApplicationDefault ( ) ;
4141 }
4242
43- // Check to see if an App already exists. And validate that we can return it
44- // given the AppOptions parameter provided to initializeApp.
43+ // Check if an App already exists and, if so, ensure its AppOptions match
44+ // those of this initializeApp request.
4545 if ( this . appStore . has ( appName ) ) {
4646 const currentApp = this . appStore . get ( appName ) ! ;
47+ // Ensure the autoInit state matches the existing app's. If not, throw.
4748 if ( currentApp . autoInit ( ) !== autoInit ) {
4849 throw new FirebaseAppError (
4950 AppErrorCodes . INVALID_APP_OPTIONS ,
5051 `Firebase app named "${ appName } " attempted mismatch between custom AppOptions` +
5152 ' and an App created via Auto Init.'
5253 )
5354 } else if ( autoInit ) {
55+ // Auto-initialization is triggered when no options were passed to
56+ // initializeApp. With no options to compare, simply return the App.
5457 return currentApp ;
5558 } else {
56- // httpAgent breaks idempotency. Throw if the AppOptions parameter or the
57- // existing app contains a httpAgent.
59+ // Auto-initialization was not used.
60+
61+ // httpAgent breaks idempotency since the objects cannot be compared.
62+ // Throw if a httpAgent exists in AppOptions or the existing App.
5863 if ( typeof options . httpAgent !== 'undefined' ) {
5964 throw new FirebaseAppError (
6065 AppErrorCodes . INVALID_APP_OPTIONS ,
@@ -70,10 +75,8 @@ export class AppStore {
7075 ' options configuration: httpAgent'
7176 ) ;
7277 }
73-
74- // Credential breaks idempotency. Throw if the AppOptions parameter contains a
75- // Credential, or if the existing app's credential was provided during its
76- // construction.
78+ // Credential breaks idempotency since the objects cannot be compared.
79+ // Throw if a Credential exists in AppOptions or the existing App.
7780 if ( typeof options . credential !== 'undefined' ) {
7881 throw new FirebaseAppError (
7982 AppErrorCodes . INVALID_APP_OPTIONS ,
@@ -82,16 +85,17 @@ export class AppStore {
8285 ' of Credential objects with the existing app. Please use getApp or getApps' +
8386 ' to reuse the existing app instead.'
8487 ) ;
85- } else if ( currentApp . initializedWithCustomCredential ( ) ) {
88+ } else if ( currentApp . customCredential ( ) ) {
8689 throw new FirebaseAppError (
8790 AppErrorCodes . INVALID_APP_OPTIONS ,
8891 `An existing app named "${ appName } " already exists with a different` +
8992 ' options configuration: Credential'
9093 ) ;
9194 }
9295
93- // FirebaseApp appends credentials to the options upon construction. Remove
94- // those generated credentials for the sake of AppOptions parameter comparison.
96+ // FirebaseApp() appends an instance of Credential to the `options`
97+ // field upon construction (below). Run a comparison of the app's
98+ // options without this auto generated Credential.
9599 const currentAppOptions = { ...currentApp . options } ;
96100 delete currentAppOptions . credential ;
97101 if ( deepEqual ( options , currentAppOptions ) ) {
@@ -161,6 +165,16 @@ export class AppStore {
161165 }
162166}
163167
168+ /**
169+ * Checks to see if the provided appName is a non-empty string and throws if it
170+ * is not.
171+ *
172+ * @param appName A string representation of an App name.
173+ *
174+ * @throws FirebaseAppError if appName is not of type string or is empty.
175+ *
176+ * @internal
177+ */
164178function validateAppNameFormat ( appName : string ) : void {
165179 if ( typeof appName !== 'string' || appName === '' ) {
166180 throw new FirebaseAppError (
@@ -205,10 +219,26 @@ export function initializeApp(options?: AppOptions, appName: string = DEFAULT_AP
205219 return defaultAppStore . initializeApp ( options , appName ) ;
206220}
207221
222+ /**
223+ * Returns an existing App instance for the provided name. If no name is provided
224+ * the the default app name is queried.
225+ *
226+ * @param appName - Optional name of the FirebaseApp instance.
227+ *
228+ * @returns An existing App instance that matches the name provided.
229+ *
230+ * @throws FirebaseAppError if no {@link App} exists for the given name.
231+ * @throws FirebaseAppError if the `appName` is malformed.
232+ */
208233export function getApp ( appName : string = DEFAULT_APP_NAME ) : App {
209234 return defaultAppStore . getApp ( appName ) ;
210235}
211236
237+ /**
238+ * A (read-only) array of all initialized apps.
239+ *
240+ * @returns An array containing all initialized apps.
241+ */
212242export function getApps ( ) : App [ ] {
213243 return defaultAppStore . getApps ( ) ;
214244}
0 commit comments