Skip to content

Commit 386d656

Browse files
authored
chore: Using public API types in FirebaseNamespace impl (#1057)
1 parent 5aebf6a commit 386d656

File tree

9 files changed

+165
-136
lines changed

9 files changed

+165
-136
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
[![Build Status](https://github.com/firebase/firebase-admin-node/workflows/Continuous%20Integration/badge.svg)](https://github.com/firebase/firebase-admin-node/actions)
22

3-
# TODO
4-
5-
* Copy generated d.ts files to the build directory.
6-
* Update return types in the methods of `FirebaseNamespace` class.
7-
83
# Firebase Admin Node.js SDK
94

105

gulpfile.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,9 @@ gulp.task('compile', function() {
8383

8484
const configuration = [
8585
'lib/**/*.js',
86-
'lib/auth/index.d.ts',
87-
'lib/credential/index.d.ts',
86+
'lib/**/index.d.ts',
8887
'lib/firebase-namespace-api.d.ts',
89-
'lib/database/index.d.ts',
90-
'lib/firestore/index.d.ts',
91-
'lib/instance-id/index.d.ts',
92-
'lib/machine-learning/index.d.ts',
93-
'lib/messaging/index.d.ts',
94-
'lib/project-management/index.d.ts',
95-
'lib/remote-config/index.d.ts',
96-
'lib/security-rules/index.d.ts',
97-
'lib/storage/index.d.ts',
88+
'!lib/utils/index.d.ts',
9889
];
9990

10091
workflow = workflow.pipe(filter(configuration));

src/firebase-app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import Database = database.Database;
4242
/**
4343
* Type representing a callback which is called every time an app lifecycle event occurs.
4444
*/
45-
export type AppHook = (event: string, app: FirebaseApp) => void;
45+
export type AppHook = (event: string, app: app.App) => void;
4646

4747
/**
4848
* Type representing a Firebase OAuth access token (derived from a Google OAuth2 access token) which

src/firebase-namespace.ts

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,40 @@
1515
*/
1616

1717
import fs = require('fs');
18+
1819
import { deepExtend } from './utils/deep-copy';
1920
import { AppErrorCodes, FirebaseAppError } from './utils/error';
20-
import { AppOptions } from './firebase-namespace-api';
21+
import { AppOptions, app } from './firebase-namespace-api';
2122
import { AppHook, FirebaseApp } from './firebase-app';
2223
import { FirebaseServiceFactory, FirebaseServiceInterface } from './firebase-service';
23-
import {
24-
cert, refreshToken, applicationDefault
25-
} from './credential/credential';
24+
import { cert, refreshToken, applicationDefault } from './credential/credential';
2625
import { getApplicationDefault } from './credential/credential-internal';
2726

28-
import { Auth } from './auth/auth';
29-
import { MachineLearning } from './machine-learning/machine-learning';
30-
import { Messaging } from './messaging/messaging';
31-
import { Storage } from './storage/storage';
27+
import { auth } from './auth/index';
3228
import { database } from './database/index';
33-
import { Firestore } from '@google-cloud/firestore';
34-
import { InstanceId } from './instance-id/instance-id';
35-
import { ProjectManagement } from './project-management/project-management';
36-
import { SecurityRules } from './security-rules/security-rules';
37-
import { RemoteConfig } from './remote-config/remote-config';
29+
import { firestore } from './firestore/index';
30+
import { instanceId } from './instance-id/index';
31+
import { machineLearning } from './machine-learning/index';
32+
import { messaging } from './messaging/index';
33+
import { projectManagement } from './project-management/index';
34+
import { remoteConfig } from './remote-config/index';
35+
import { securityRules } from './security-rules/index';
36+
import { storage } from './storage/index';
3837

3938
import * as validator from './utils/validator';
4039
import { getSdkVersion } from './utils/index';
4140

41+
import App = app.App;
42+
import Auth = auth.Auth;
4243
import Database = database.Database;
44+
import Firestore = firestore.Firestore;
45+
import InstanceId = instanceId.InstanceId;
46+
import MachineLearning = machineLearning.MachineLearning;
47+
import Messaging = messaging.Messaging;
48+
import ProjectManagement = projectManagement.ProjectManagement;
49+
import RemoteConfig = remoteConfig.RemoteConfig;
50+
import SecurityRules = securityRules.SecurityRules;
51+
import Storage = storage.Storage;
4352

4453
const DEFAULT_APP_NAME = '[DEFAULT]';
4554

@@ -51,7 +60,7 @@ const DEFAULT_APP_NAME = '[DEFAULT]';
5160
export const FIREBASE_CONFIG_VAR = 'FIREBASE_CONFIG';
5261

5362
export interface FirebaseServiceNamespace <T> {
54-
(app?: FirebaseApp): T;
63+
(app?: App): T;
5564
[key: string]: any;
5665
}
5766

@@ -61,24 +70,23 @@ export interface FirebaseServiceNamespace <T> {
6170
export class FirebaseNamespaceInternals {
6271
public serviceFactories: {[serviceName: string]: FirebaseServiceFactory} = {};
6372

64-
private apps_: {[appName: string]: FirebaseApp} = {};
73+
private apps_: {[appName: string]: App} = {};
6574
private appHooks_: {[service: string]: AppHook} = {};
6675

6776
constructor(public firebase_: {[key: string]: any}) {}
6877

6978
/**
70-
* Initializes the FirebaseApp instance.
79+
* Initializes the App instance.
7180
*
72-
* @param {FirebaseAppOptions} options Optional options for the FirebaseApp instance. If none present
73-
* will try to initialize from the FIREBASE_CONFIG environment variable.
74-
* If the environment variable contains a string that starts with '{'
75-
* it will be parsed as JSON,
76-
* otherwise it will be assumed to be pointing to a file.
77-
* @param {string} [appName] Optional name of the FirebaseApp instance.
81+
* @param options Optional options for the App instance. If none present will try to initialize
82+
* from the FIREBASE_CONFIG environment variable. If the environment variable contains a string
83+
* that starts with '{' it will be parsed as JSON, otherwise it will be assumed to be pointing
84+
* to a file.
85+
* @param appName Optional name of the FirebaseApp instance.
7886
*
79-
* @return {FirebaseApp} A new FirebaseApp instance.
87+
* @return A new App instance.
8088
*/
81-
public initializeApp(options?: AppOptions, appName = DEFAULT_APP_NAME): FirebaseApp {
89+
public initializeApp(options?: AppOptions, appName = DEFAULT_APP_NAME): App {
8290
if (typeof options === 'undefined') {
8391
options = this.loadOptionsFromEnvVar();
8492
options.credential = getApplicationDefault();
@@ -118,13 +126,13 @@ export class FirebaseNamespaceInternals {
118126
}
119127

120128
/**
121-
* Returns the FirebaseApp instance with the provided name (or the default FirebaseApp instance
129+
* Returns the App instance with the provided name (or the default App instance
122130
* if no name is provided).
123131
*
124-
* @param {string} [appName=DEFAULT_APP_NAME] Optional name of the FirebaseApp instance to return.
125-
* @return {FirebaseApp} The FirebaseApp instance which has the provided name.
132+
* @param appName Optional name of the FirebaseApp instance to return.
133+
* @return The App instance which has the provided name.
126134
*/
127-
public app(appName = DEFAULT_APP_NAME): FirebaseApp {
135+
public app(appName = DEFAULT_APP_NAME): App {
128136
if (typeof appName !== 'string' || appName === '') {
129137
throw new FirebaseAppError(
130138
AppErrorCodes.INVALID_APP_NAME,
@@ -142,19 +150,15 @@ export class FirebaseNamespaceInternals {
142150
}
143151

144152
/*
145-
* Returns an array of all the non-deleted FirebaseApp instances.
146-
*
147-
* @return {Array<FirebaseApp>} An array of all the non-deleted FirebaseApp instances
153+
* Returns an array of all the non-deleted App instances.
148154
*/
149-
public get apps(): FirebaseApp[] {
155+
public get apps(): App[] {
150156
// Return a copy so the caller cannot mutate the array
151157
return Object.keys(this.apps_).map((appName) => this.apps_[appName]);
152158
}
153159

154160
/*
155-
* Removes the specified FirebaseApp instance.
156-
*
157-
* @param {string} appName The name of the FirebaseApp instance to remove.
161+
* Removes the specified App instance.
158162
*/
159163
public removeApp(appName: string): void {
160164
if (typeof appName === 'undefined') {
@@ -169,14 +173,14 @@ export class FirebaseNamespaceInternals {
169173
delete this.apps_[appName];
170174
}
171175

172-
/*
176+
/**
173177
* Registers a new service on this Firebase namespace.
174178
*
175-
* @param {string} serviceName The name of the Firebase service to register.
176-
* @param {FirebaseServiceFactory} createService A factory method to generate an instance of the Firebase service.
177-
* @param {object} [serviceProperties] Optional properties to extend this Firebase namespace with.
178-
* @param {AppHook} [appHook] Optional callback that handles app-related events like app creation and deletion.
179-
* @return {FirebaseServiceNamespace<FirebaseServiceInterface>} The Firebase service's namespace.
179+
* @param serviceName The name of the Firebase service to register.
180+
* @param createService A factory method to generate an instance of the Firebase service.
181+
* @param serviceProperties Optional properties to extend this Firebase namespace with.
182+
* @param appHook Optional callback that handles app-related events like app creation and deletion.
183+
* @return The Firebase service's namespace.
180184
*/
181185
public registerService(
182186
serviceName: string,
@@ -206,7 +210,7 @@ export class FirebaseNamespaceInternals {
206210

207211
// The service namespace is an accessor function which takes a FirebaseApp instance
208212
// or uses the default app if no FirebaseApp instance is provided
209-
const serviceNamespace: FirebaseServiceNamespace<FirebaseServiceInterface> = (appArg?: FirebaseApp) => {
213+
const serviceNamespace: FirebaseServiceNamespace<FirebaseServiceInterface> = (appArg?: App) => {
210214
if (typeof appArg === 'undefined') {
211215
appArg = this.app();
212216
}
@@ -228,12 +232,12 @@ export class FirebaseNamespaceInternals {
228232

229233
/**
230234
* Calls the app hooks corresponding to the provided event name for each service within the
231-
* provided FirebaseApp instance.
235+
* provided App instance.
232236
*
233-
* @param {FirebaseApp} app The FirebaseApp instance whose app hooks to call.
234-
* @param {string} eventName The event name representing which app hooks to call.
237+
* @param app The App instance whose app hooks to call.
238+
* @param eventName The event name representing which app hooks to call.
235239
*/
236-
private callAppHooks_(app: FirebaseApp, eventName: string): void {
240+
private callAppHooks_(app: App, eventName: string): void {
237241
Object.keys(this.serviceFactories).forEach((serviceName) => {
238242
if (this.appHooks_[serviceName]) {
239243
this.appHooks_[serviceName](eventName, app);
@@ -297,7 +301,7 @@ export class FirebaseNamespace {
297301
* `Auth` service for the default app or an explicitly specified app.
298302
*/
299303
get auth(): FirebaseServiceNamespace<Auth> {
300-
const fn: FirebaseServiceNamespace<Auth> = (app?: FirebaseApp) => {
304+
const fn: FirebaseServiceNamespace<Auth> = (app?: App) => {
301305
return this.ensureApp(app).auth();
302306
};
303307
const auth = require('./auth/auth').Auth;
@@ -309,7 +313,7 @@ export class FirebaseNamespace {
309313
* `Database` service for the default app or an explicitly specified app.
310314
*/
311315
get database(): FirebaseServiceNamespace<Database> {
312-
const fn: FirebaseServiceNamespace<Database> = (app?: FirebaseApp) => {
316+
const fn: FirebaseServiceNamespace<Database> = (app?: App) => {
313317
return this.ensureApp(app).database();
314318
};
315319

@@ -322,7 +326,7 @@ export class FirebaseNamespace {
322326
* `Messaging` service for the default app or an explicitly specified app.
323327
*/
324328
get messaging(): FirebaseServiceNamespace<Messaging> {
325-
const fn: FirebaseServiceNamespace<Messaging> = (app?: FirebaseApp) => {
329+
const fn: FirebaseServiceNamespace<Messaging> = (app?: App) => {
326330
return this.ensureApp(app).messaging();
327331
};
328332
const messaging = require('./messaging/messaging').Messaging;
@@ -334,7 +338,7 @@ export class FirebaseNamespace {
334338
* `Storage` service for the default app or an explicitly specified app.
335339
*/
336340
get storage(): FirebaseServiceNamespace<Storage> {
337-
const fn: FirebaseServiceNamespace<Storage> = (app?: FirebaseApp) => {
341+
const fn: FirebaseServiceNamespace<Storage> = (app?: App) => {
338342
return this.ensureApp(app).storage();
339343
};
340344
const storage = require('./storage/storage').Storage;
@@ -346,7 +350,7 @@ export class FirebaseNamespace {
346350
* `Firestore` service for the default app or an explicitly specified app.
347351
*/
348352
get firestore(): FirebaseServiceNamespace<Firestore> {
349-
let fn: FirebaseServiceNamespace<Firestore> = (app?: FirebaseApp) => {
353+
let fn: FirebaseServiceNamespace<Firestore> = (app?: App) => {
350354
return this.ensureApp(app).firestore();
351355
};
352356

@@ -378,7 +382,7 @@ export class FirebaseNamespace {
378382
*/
379383
get machineLearning(): FirebaseServiceNamespace<MachineLearning> {
380384
const fn: FirebaseServiceNamespace<MachineLearning> =
381-
(app?: FirebaseApp) => {
385+
(app?: App) => {
382386
return this.ensureApp(app).machineLearning();
383387
};
384388
const machineLearning =
@@ -391,7 +395,7 @@ export class FirebaseNamespace {
391395
* `Instance` service for the default app or an explicitly specified app.
392396
*/
393397
get instanceId(): FirebaseServiceNamespace<InstanceId> {
394-
const fn: FirebaseServiceNamespace<InstanceId> = (app?: FirebaseApp) => {
398+
const fn: FirebaseServiceNamespace<InstanceId> = (app?: App) => {
395399
return this.ensureApp(app).instanceId();
396400
};
397401
const instanceId = require('./instance-id/instance-id').InstanceId;
@@ -403,7 +407,7 @@ export class FirebaseNamespace {
403407
* `ProjectManagement` service for the default app or an explicitly specified app.
404408
*/
405409
get projectManagement(): FirebaseServiceNamespace<ProjectManagement> {
406-
const fn: FirebaseServiceNamespace<ProjectManagement> = (app?: FirebaseApp) => {
410+
const fn: FirebaseServiceNamespace<ProjectManagement> = (app?: App) => {
407411
return this.ensureApp(app).projectManagement();
408412
};
409413
const projectManagement = require('./project-management/project-management').ProjectManagement;
@@ -415,7 +419,7 @@ export class FirebaseNamespace {
415419
* `SecurityRules` service for the default app or an explicitly specified app.
416420
*/
417421
get securityRules(): FirebaseServiceNamespace<SecurityRules> {
418-
const fn: FirebaseServiceNamespace<SecurityRules> = (app?: FirebaseApp) => {
422+
const fn: FirebaseServiceNamespace<SecurityRules> = (app?: App) => {
419423
return this.ensureApp(app).securityRules();
420424
};
421425
const securityRules = require('./security-rules/security-rules').SecurityRules;
@@ -427,7 +431,7 @@ export class FirebaseNamespace {
427431
* `RemoteConfig` service for the default app or an explicitly specified app.
428432
*/
429433
get remoteConfig(): FirebaseServiceNamespace<RemoteConfig> {
430-
const fn: FirebaseServiceNamespace<RemoteConfig> = (app?: FirebaseApp) => {
434+
const fn: FirebaseServiceNamespace<RemoteConfig> = (app?: App) => {
431435
return this.ensureApp(app).remoteConfig();
432436
};
433437
const remoteConfig = require('./remote-config/remote-config').RemoteConfig;
@@ -447,7 +451,7 @@ export class FirebaseNamespace {
447451
*
448452
* @return A new FirebaseApp instance.
449453
*/
450-
public initializeApp(options?: AppOptions, appName?: string): FirebaseApp {
454+
public initializeApp(options?: AppOptions, appName?: string): App {
451455
return this.INTERNAL.initializeApp(options, appName);
452456
}
453457

@@ -458,20 +462,20 @@ export class FirebaseNamespace {
458462
* @param appName Optional name of the FirebaseApp instance to return.
459463
* @return The FirebaseApp instance which has the provided name.
460464
*/
461-
public app(appName?: string): FirebaseApp {
465+
public app(appName?: string): App {
462466
return this.INTERNAL.app(appName);
463467
}
464468

465469
/*
466470
* Returns an array of all the non-deleted FirebaseApp instances.
467471
*/
468-
public get apps(): FirebaseApp[] {
472+
public get apps(): App[] {
469473
return this.INTERNAL.apps;
470474
}
471475

472-
private ensureApp(app?: FirebaseApp): FirebaseApp {
476+
private ensureApp(app?: App): App {
473477
if (typeof app === 'undefined') {
474-
app = this.app() as FirebaseApp;
478+
app = this.app();
475479
}
476480
return app;
477481
}

src/firebase-service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { app } from './firebase-namespace-api';
1718
import { FirebaseApp } from './firebase-app';
1819

20+
import App = app.App;
21+
1922
/**
2023
* Internals of a FirebaseService instance.
2124
*/
@@ -27,7 +30,7 @@ export interface FirebaseServiceInternalsInterface {
2730
* Services are exposed through instances, each of which is associated with a FirebaseApp.
2831
*/
2932
export interface FirebaseServiceInterface {
30-
app: FirebaseApp;
33+
app: App;
3134
INTERNAL: FirebaseServiceInternalsInterface;
3235
}
3336

test/resources/mocks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { AppOptions } from '../../src/firebase-namespace-api';
2828
import { FirebaseNamespace } from '../../src/firebase-namespace';
2929
import { FirebaseServiceInterface } from '../../src/firebase-service';
3030
import { FirebaseApp } from '../../src/firebase-app';
31+
import { app as _app } from '../../src/firebase-namespace-api';
3132
import { credential as _credential, GoogleOAuthAccessToken } from '../../src/credential/index';
3233
import { ServiceAccountCredential } from '../../src/credential/credential-internal';
3334

@@ -223,7 +224,7 @@ export function generateSessionCookie(overrides?: object, expiresIn?: number): s
223224

224225
/* eslint-disable @typescript-eslint/no-unused-vars */
225226
export function firebaseServiceFactory(
226-
firebaseApp: FirebaseApp,
227+
firebaseApp: _app.App,
227228
_extendApp?: (props: object) => void,
228229
): FirebaseServiceInterface {
229230
const result = {

0 commit comments

Comments
 (0)