Skip to content

Commit 555f38e

Browse files
committed
Add initializeServerApp() without args
1 parent 080a90d commit 555f38e

File tree

1 file changed

+46
-23
lines changed

1 file changed

+46
-23
lines changed

packages/app/src/api.ts

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export function initializeApp(
203203
* @example
204204
* ```javascript
205205
*
206-
* // Initialize an instance of `FirebaseServerApp`.
206+
* // Initialize an instance of `FirebaseServerApp` with explicit options.
207207
* // Retrieve your own options values by adding a web app on
208208
* // https://console.firebase.google.com
209209
* initializeServerApp({
@@ -231,29 +231,52 @@ export function initializeServerApp(
231231
config: FirebaseServerAppSettings
232232
): FirebaseServerApp;
233233

234+
/**
235+
* Creates and initializes a {@link @firebase/app#FirebaseServerApp} instance with default configuration.
236+
*
237+
* @example
238+
* ```javascript
239+
* // Initialize with default configuration
240+
* initializeServerApp();
241+
* ```
242+
*
243+
* @returns The initialized `FirebaseServerApp` with default configuration.
244+
*
245+
* @public
246+
*/
247+
export function initializeServerApp(): FirebaseServerApp;
248+
234249
export function initializeServerApp(
235-
_options: FirebaseOptions | FirebaseApp,
236-
_serverAppConfig: FirebaseServerAppSettings
250+
_options?: FirebaseOptions | FirebaseApp,
251+
_serverAppConfig: FirebaseServerAppSettings = {}
237252
): FirebaseServerApp {
238253
if (isBrowser() && !isWebWorker()) {
239254
// FirebaseServerApp isn't designed to be run in browsers.
240255
throw ERROR_FACTORY.create(AppError.INVALID_SERVER_APP_ENVIRONMENT);
241256
}
242257

243-
if (_serverAppConfig.automaticDataCollectionEnabled === undefined) {
244-
_serverAppConfig.automaticDataCollectionEnabled = false;
258+
let options = _options;
259+
const config: FirebaseServerAppSettings = {
260+
automaticDataCollectionEnabled: false,
261+
..._serverAppConfig
262+
};
263+
264+
options ||= getDefaultAppConfig();
265+
266+
if (!options) {
267+
throw ERROR_FACTORY.create(AppError.NO_OPTIONS);
245268
}
246269

247270
let appOptions: FirebaseOptions;
248-
if (_isFirebaseApp(_options)) {
249-
appOptions = _options.options;
271+
if (_isFirebaseApp(options)) {
272+
appOptions = options.options;
250273
} else {
251-
appOptions = _options;
274+
appOptions = options;
252275
}
253276

254277
// Build an app name based on a hash of the configuration options.
255278
const nameObj = {
256-
..._serverAppConfig,
279+
...config,
257280
...appOptions
258281
};
259282

@@ -270,7 +293,7 @@ export function initializeServerApp(
270293
);
271294
};
272295

273-
if (_serverAppConfig.releaseOnDeref !== undefined) {
296+
if (config.releaseOnDeref !== undefined) {
274297
if (typeof FinalizationRegistry === 'undefined') {
275298
throw ERROR_FACTORY.create(
276299
AppError.FINALIZATION_REGISTRY_NOT_SUPPORTED,
@@ -283,7 +306,7 @@ export function initializeServerApp(
283306
const existingApp = _serverApps.get(nameString) as FirebaseServerApp;
284307
if (existingApp) {
285308
(existingApp as FirebaseServerAppImpl).incRefCount(
286-
_serverAppConfig.releaseOnDeref
309+
config.releaseOnDeref
287310
);
288311
return existingApp;
289312
}
@@ -295,7 +318,7 @@ export function initializeServerApp(
295318

296319
const newApp = new FirebaseServerAppImpl(
297320
appOptions,
298-
_serverAppConfig,
321+
config,
299322
nameString,
300323
container
301324
);
@@ -375,22 +398,22 @@ export async function deleteApp(app: FirebaseApp): Promise<void> {
375398
let cleanupProviders = false;
376399
const name = app.name;
377400
if (_apps.has(name)) {
378-
cleanupProviders = true;
379-
_apps.delete(name);
380-
} else if (_serverApps.has(name)) {
381-
const firebaseServerApp = app as FirebaseServerAppImpl;
382-
if (firebaseServerApp.decRefCount() <= 0) {
383-
_serverApps.delete(name);
384401
cleanupProviders = true;
385-
}
402+
_apps.delete(name);
403+
} else if (_serverApps.has(name)) {
404+
const firebaseServerApp = app as FirebaseServerAppImpl;
405+
if (firebaseServerApp.decRefCount() <= 0) {
406+
_serverApps.delete(name);
407+
cleanupProviders = true;
408+
}
386409
}
387410

388411
if (cleanupProviders) {
389-
await Promise.all(
412+
await Promise.all(
390413
(app as FirebaseAppImpl).container
391-
.getProviders()
392-
.map(provider => provider.delete())
393-
);
414+
.getProviders()
415+
.map(provider => provider.delete())
416+
);
394417
(app as FirebaseAppImpl).isDeleted = true;
395418
}
396419
}

0 commit comments

Comments
 (0)