Skip to content

Add initializeServerApp() without args #8990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/poor-penguins-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@firebase/app': patch
---

Support zero-args `initializeServerApp()`

* Added overloads to allow initializeServerApp() without parameters.
69 changes: 46 additions & 23 deletions packages/app/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export function initializeApp(
* @example
* ```javascript
*
* // Initialize an instance of `FirebaseServerApp`.
* // Initialize an instance of `FirebaseServerApp` with explicit options.
* // Retrieve your own options values by adding a web app on
* // https://console.firebase.google.com
* initializeServerApp({
Expand Down Expand Up @@ -231,29 +231,52 @@ export function initializeServerApp(
config: FirebaseServerAppSettings
): FirebaseServerApp;

/**
* Creates and initializes a {@link @firebase/app#FirebaseServerApp} instance with default configuration.
*
* @example
* ```javascript
* // Initialize with default configuration
* initializeServerApp();
* ```
*
* @returns The initialized `FirebaseServerApp` with default configuration.
*
* @public
*/
export function initializeServerApp(): FirebaseServerApp;

export function initializeServerApp(
_options: FirebaseOptions | FirebaseApp,
_serverAppConfig: FirebaseServerAppSettings
_options?: FirebaseOptions | FirebaseApp,
_serverAppConfig: FirebaseServerAppSettings = {}
): FirebaseServerApp {
if (isBrowser() && !isWebWorker()) {
// FirebaseServerApp isn't designed to be run in browsers.
throw ERROR_FACTORY.create(AppError.INVALID_SERVER_APP_ENVIRONMENT);
}

if (_serverAppConfig.automaticDataCollectionEnabled === undefined) {
_serverAppConfig.automaticDataCollectionEnabled = false;
let options = _options;
const config: FirebaseServerAppSettings = {
automaticDataCollectionEnabled: false,
..._serverAppConfig
};

options ||= getDefaultAppConfig();

if (!options) {
throw ERROR_FACTORY.create(AppError.NO_OPTIONS);
}

let appOptions: FirebaseOptions;
if (_isFirebaseApp(_options)) {
appOptions = _options.options;
if (_isFirebaseApp(options)) {
appOptions = options.options;
} else {
appOptions = _options;
appOptions = options;
}

// Build an app name based on a hash of the configuration options.
const nameObj = {
..._serverAppConfig,
...config,
...appOptions
};

Expand All @@ -270,7 +293,7 @@ export function initializeServerApp(
);
};

if (_serverAppConfig.releaseOnDeref !== undefined) {
if (config.releaseOnDeref !== undefined) {
if (typeof FinalizationRegistry === 'undefined') {
throw ERROR_FACTORY.create(
AppError.FINALIZATION_REGISTRY_NOT_SUPPORTED,
Expand All @@ -283,7 +306,7 @@ export function initializeServerApp(
const existingApp = _serverApps.get(nameString) as FirebaseServerApp;
if (existingApp) {
(existingApp as FirebaseServerAppImpl).incRefCount(
_serverAppConfig.releaseOnDeref
config.releaseOnDeref
);
return existingApp;
}
Expand All @@ -295,7 +318,7 @@ export function initializeServerApp(

const newApp = new FirebaseServerAppImpl(
appOptions,
_serverAppConfig,
config,
nameString,
container
);
Expand Down Expand Up @@ -375,22 +398,22 @@ export async function deleteApp(app: FirebaseApp): Promise<void> {
let cleanupProviders = false;
const name = app.name;
if (_apps.has(name)) {
cleanupProviders = true;
_apps.delete(name);
} else if (_serverApps.has(name)) {
const firebaseServerApp = app as FirebaseServerAppImpl;
if (firebaseServerApp.decRefCount() <= 0) {
_serverApps.delete(name);
cleanupProviders = true;
}
_apps.delete(name);
} else if (_serverApps.has(name)) {
const firebaseServerApp = app as FirebaseServerAppImpl;
if (firebaseServerApp.decRefCount() <= 0) {
_serverApps.delete(name);
cleanupProviders = true;
}
}

if (cleanupProviders) {
await Promise.all(
await Promise.all(
(app as FirebaseAppImpl).container
.getProviders()
.map(provider => provider.delete())
);
.getProviders()
.map(provider => provider.delete())
);
(app as FirebaseAppImpl).isDeleted = true;
}
}
Expand Down