Skip to content

fix: Decoupled FirebaseNamespace from new module entry points #1432

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

Merged
merged 3 commits into from
Sep 21, 2021
Merged
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
4 changes: 2 additions & 2 deletions etc/firebase-admin.app.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface FirebaseError {
}

// @public (undocumented)
export function getApp(name?: string): App;
export function getApp(appName?: string): App;

// @public (undocumented)
export function getApps(): App[];
Expand All @@ -68,7 +68,7 @@ export interface GoogleOAuthAccessToken {
}

// @public (undocumented)
export function initializeApp(options?: AppOptions, name?: string): App;
export function initializeApp(options?: AppOptions, appName?: string): App;

// @public
export function refreshToken(refreshTokenPathOrObject: string | object, httpAgent?: Agent): Credential;
Expand Down
148 changes: 10 additions & 138 deletions src/app/firebase-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,16 @@
* limitations under the License.
*/

import { AppOptions, app } from '../firebase-namespace-api';
import { AppOptions, App } from './core';
import { AppStore } from './lifecycle';
import { Credential } from './credential';
import { getApplicationDefault } from './credential-internal';
import * as validator from '../utils/validator';
import { deepCopy } from '../utils/deep-copy';
import { FirebaseNamespaceInternals } from './firebase-namespace';
import { AppErrorCodes, FirebaseAppError } from '../utils/error';

import { Auth } from '../auth/index';
import { AppCheck } from '../app-check/index';
import { MachineLearning } from '../machine-learning/index';
import { Messaging } from '../messaging/index';
import { Storage } from '../storage/index';
import { Database } from '../database/index';
import { Firestore } from '../firestore/index';
import { InstanceId } from '../instance-id/index';
import { Installations } from '../installations/index';
import { ProjectManagement } from '../project-management/index';
import { SecurityRules } from '../security-rules/index';
import { RemoteConfig } from '../remote-config/index';

const TOKEN_EXPIRY_THRESHOLD_MILLIS = 5 * 60 * 1000;

/**
* Type representing a callback which is called every time an app lifecycle event occurs.
*/
export type AppHook = (event: string, app: app.App) => void;

/**
* Type representing a Firebase OAuth access token (derived from a Google OAuth2 access token) which
* can be used to authenticate to Firebase services such as the Realtime Database and Auth.
Expand Down Expand Up @@ -159,15 +141,16 @@ export class FirebaseAppInternals {
*
* @internal
*/
export class FirebaseApp implements app.App {
export class FirebaseApp implements App {

public INTERNAL: FirebaseAppInternals;

private name_: string;
private options_: AppOptions;
private services_: {[name: string]: unknown} = {};
private isDeleted_ = false;

constructor(options: AppOptions, name: string, private firebaseInternals_: FirebaseNamespaceInternals) {
constructor(options: AppOptions, name: string, private readonly appStore?: AppStore) {
this.name_ = name;
this.options_ = deepCopy(options);

Expand Down Expand Up @@ -197,121 +180,6 @@ export class FirebaseApp implements app.App {
this.INTERNAL = new FirebaseAppInternals(credential);
}

/**
* Returns the AppCheck service instance associated with this app.
*
* @returns The AppCheck service instance of this app.
*/
public appCheck(): AppCheck {
const fn = require('../app-check/index').getAppCheck;
return fn(this);
}

/**
* Returns the Auth service instance associated with this app.
*
* @returns The Auth service instance of this app.
*/
public auth(): Auth {
const fn = require('../auth/index').getAuth;
return fn(this);
}

/**
* Returns the Database service for the specified URL, and the current app.
*
* @returns The Database service instance of this app.
*/
public database(url?: string): Database {
const fn = require('../database/index').getDatabaseWithUrl;
return fn(url, this);
}

/**
* Returns the Messaging service instance associated with this app.
*
* @returns The Messaging service instance of this app.
*/
public messaging(): Messaging {
const fn = require('../messaging/index').getMessaging;
return fn(this);
}

/**
* Returns the Storage service instance associated with this app.
*
* @returns The Storage service instance of this app.
*/
public storage(): Storage {
const fn = require('../storage/index').getStorage;
return fn(this);
}

public firestore(): Firestore {
const fn = require('../firestore/index').getFirestore;
return fn(this);
}

/**
* Returns the InstanceId service instance associated with this app.
*
* @returns The InstanceId service instance of this app.
*/
public instanceId(): InstanceId {
const fn = require('../instance-id/index').getInstanceId;
return fn(this);
}

/**
* Returns the InstanceId service instance associated with this app.
*
* @returns The InstanceId service instance of this app.
*/
public installations(): Installations {
const fn = require('../installations/index').getInstallations;
return fn(this);
}

/**
* Returns the MachineLearning service instance associated with this app.
*
* @returns The Machine Learning service instance of this app
*/
public machineLearning(): MachineLearning {
const fn = require('../machine-learning/index').getMachineLearning;
return fn(this);
}

/**
* Returns the ProjectManagement service instance associated with this app.
*
* @returns The ProjectManagement service instance of this app.
*/
public projectManagement(): ProjectManagement {
const fn = require('../project-management/index').getProjectManagement;
return fn(this);
}

/**
* Returns the SecurityRules service instance associated with this app.
*
* @returns The SecurityRules service instance of this app.
*/
public securityRules(): SecurityRules {
const fn = require('../security-rules/index').getSecurityRules;
return fn(this);
}

/**
* Returns the RemoteConfig service instance associated with this app.
*
* @returns The RemoteConfig service instance of this app.
*/
public remoteConfig(): RemoteConfig {
const fn = require('../remote-config/index').getRemoteConfig;
return fn(this);
}

/**
* Returns the name of the FirebaseApp instance.
*
Expand Down Expand Up @@ -346,7 +214,11 @@ export class FirebaseApp implements app.App {
*/
public delete(): Promise<void> {
this.checkDestroyed_();
this.firebaseInternals_.removeApp(this.name_);

// Also remove the instance from the AppStore. This is needed to support the existing
// app.delete() use case. In the future we can remove this API, and deleteApp() will
// become the only way to tear down an App.
this.appStore?.removeApp(this.name);

return Promise.all(Object.keys(this.services_).map((serviceName) => {
const service = this.services_[serviceName];
Expand Down
Loading