15
15
*/
16
16
17
17
import fs = require( 'fs' ) ;
18
+
18
19
import { deepExtend } from './utils/deep-copy' ;
19
20
import { AppErrorCodes , FirebaseAppError } from './utils/error' ;
20
- import { AppOptions } from './firebase-namespace-api' ;
21
+ import { AppOptions , app } from './firebase-namespace-api' ;
21
22
import { AppHook , FirebaseApp } from './firebase-app' ;
22
23
import { FirebaseServiceFactory , FirebaseServiceInterface } from './firebase-service' ;
23
- import {
24
- cert , refreshToken , applicationDefault
25
- } from './credential/credential' ;
24
+ import { cert , refreshToken , applicationDefault } from './credential/credential' ;
26
25
import { getApplicationDefault } from './credential/credential-internal' ;
27
26
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' ;
32
28
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' ;
38
37
39
38
import * as validator from './utils/validator' ;
40
39
import { getSdkVersion } from './utils/index' ;
41
40
41
+ import App = app . App ;
42
+ import Auth = auth . Auth ;
42
43
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 ;
43
52
44
53
const DEFAULT_APP_NAME = '[DEFAULT]' ;
45
54
@@ -51,7 +60,7 @@ const DEFAULT_APP_NAME = '[DEFAULT]';
51
60
export const FIREBASE_CONFIG_VAR = 'FIREBASE_CONFIG' ;
52
61
53
62
export interface FirebaseServiceNamespace < T > {
54
- ( app ?: FirebaseApp ) : T ;
63
+ ( app ?: App ) : T ;
55
64
[ key : string ] : any ;
56
65
}
57
66
@@ -61,24 +70,23 @@ export interface FirebaseServiceNamespace <T> {
61
70
export class FirebaseNamespaceInternals {
62
71
public serviceFactories : { [ serviceName : string ] : FirebaseServiceFactory } = { } ;
63
72
64
- private apps_ : { [ appName : string ] : FirebaseApp } = { } ;
73
+ private apps_ : { [ appName : string ] : App } = { } ;
65
74
private appHooks_ : { [ service : string ] : AppHook } = { } ;
66
75
67
76
constructor ( public firebase_ : { [ key : string ] : any } ) { }
68
77
69
78
/**
70
- * Initializes the FirebaseApp instance.
79
+ * Initializes the App instance.
71
80
*
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.
78
86
*
79
- * @return { FirebaseApp } A new FirebaseApp instance.
87
+ * @return A new App instance.
80
88
*/
81
- public initializeApp ( options ?: AppOptions , appName = DEFAULT_APP_NAME ) : FirebaseApp {
89
+ public initializeApp ( options ?: AppOptions , appName = DEFAULT_APP_NAME ) : App {
82
90
if ( typeof options === 'undefined' ) {
83
91
options = this . loadOptionsFromEnvVar ( ) ;
84
92
options . credential = getApplicationDefault ( ) ;
@@ -118,13 +126,13 @@ export class FirebaseNamespaceInternals {
118
126
}
119
127
120
128
/**
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
122
130
* if no name is provided).
123
131
*
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.
126
134
*/
127
- public app ( appName = DEFAULT_APP_NAME ) : FirebaseApp {
135
+ public app ( appName = DEFAULT_APP_NAME ) : App {
128
136
if ( typeof appName !== 'string' || appName === '' ) {
129
137
throw new FirebaseAppError (
130
138
AppErrorCodes . INVALID_APP_NAME ,
@@ -142,19 +150,15 @@ export class FirebaseNamespaceInternals {
142
150
}
143
151
144
152
/*
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.
148
154
*/
149
- public get apps ( ) : FirebaseApp [ ] {
155
+ public get apps ( ) : App [ ] {
150
156
// Return a copy so the caller cannot mutate the array
151
157
return Object . keys ( this . apps_ ) . map ( ( appName ) => this . apps_ [ appName ] ) ;
152
158
}
153
159
154
160
/*
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.
158
162
*/
159
163
public removeApp ( appName : string ) : void {
160
164
if ( typeof appName === 'undefined' ) {
@@ -169,14 +173,14 @@ export class FirebaseNamespaceInternals {
169
173
delete this . apps_ [ appName ] ;
170
174
}
171
175
172
- /*
176
+ /**
173
177
* Registers a new service on this Firebase namespace.
174
178
*
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.
180
184
*/
181
185
public registerService (
182
186
serviceName : string ,
@@ -206,7 +210,7 @@ export class FirebaseNamespaceInternals {
206
210
207
211
// The service namespace is an accessor function which takes a FirebaseApp instance
208
212
// 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 ) => {
210
214
if ( typeof appArg === 'undefined' ) {
211
215
appArg = this . app ( ) ;
212
216
}
@@ -228,12 +232,12 @@ export class FirebaseNamespaceInternals {
228
232
229
233
/**
230
234
* Calls the app hooks corresponding to the provided event name for each service within the
231
- * provided FirebaseApp instance.
235
+ * provided App instance.
232
236
*
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.
235
239
*/
236
- private callAppHooks_ ( app : FirebaseApp , eventName : string ) : void {
240
+ private callAppHooks_ ( app : App , eventName : string ) : void {
237
241
Object . keys ( this . serviceFactories ) . forEach ( ( serviceName ) => {
238
242
if ( this . appHooks_ [ serviceName ] ) {
239
243
this . appHooks_ [ serviceName ] ( eventName , app ) ;
@@ -297,7 +301,7 @@ export class FirebaseNamespace {
297
301
* `Auth` service for the default app or an explicitly specified app.
298
302
*/
299
303
get auth ( ) : FirebaseServiceNamespace < Auth > {
300
- const fn : FirebaseServiceNamespace < Auth > = ( app ?: FirebaseApp ) => {
304
+ const fn : FirebaseServiceNamespace < Auth > = ( app ?: App ) => {
301
305
return this . ensureApp ( app ) . auth ( ) ;
302
306
} ;
303
307
const auth = require ( './auth/auth' ) . Auth ;
@@ -309,7 +313,7 @@ export class FirebaseNamespace {
309
313
* `Database` service for the default app or an explicitly specified app.
310
314
*/
311
315
get database ( ) : FirebaseServiceNamespace < Database > {
312
- const fn : FirebaseServiceNamespace < Database > = ( app ?: FirebaseApp ) => {
316
+ const fn : FirebaseServiceNamespace < Database > = ( app ?: App ) => {
313
317
return this . ensureApp ( app ) . database ( ) ;
314
318
} ;
315
319
@@ -322,7 +326,7 @@ export class FirebaseNamespace {
322
326
* `Messaging` service for the default app or an explicitly specified app.
323
327
*/
324
328
get messaging ( ) : FirebaseServiceNamespace < Messaging > {
325
- const fn : FirebaseServiceNamespace < Messaging > = ( app ?: FirebaseApp ) => {
329
+ const fn : FirebaseServiceNamespace < Messaging > = ( app ?: App ) => {
326
330
return this . ensureApp ( app ) . messaging ( ) ;
327
331
} ;
328
332
const messaging = require ( './messaging/messaging' ) . Messaging ;
@@ -334,7 +338,7 @@ export class FirebaseNamespace {
334
338
* `Storage` service for the default app or an explicitly specified app.
335
339
*/
336
340
get storage ( ) : FirebaseServiceNamespace < Storage > {
337
- const fn : FirebaseServiceNamespace < Storage > = ( app ?: FirebaseApp ) => {
341
+ const fn : FirebaseServiceNamespace < Storage > = ( app ?: App ) => {
338
342
return this . ensureApp ( app ) . storage ( ) ;
339
343
} ;
340
344
const storage = require ( './storage/storage' ) . Storage ;
@@ -346,7 +350,7 @@ export class FirebaseNamespace {
346
350
* `Firestore` service for the default app or an explicitly specified app.
347
351
*/
348
352
get firestore ( ) : FirebaseServiceNamespace < Firestore > {
349
- let fn : FirebaseServiceNamespace < Firestore > = ( app ?: FirebaseApp ) => {
353
+ let fn : FirebaseServiceNamespace < Firestore > = ( app ?: App ) => {
350
354
return this . ensureApp ( app ) . firestore ( ) ;
351
355
} ;
352
356
@@ -378,7 +382,7 @@ export class FirebaseNamespace {
378
382
*/
379
383
get machineLearning ( ) : FirebaseServiceNamespace < MachineLearning > {
380
384
const fn : FirebaseServiceNamespace < MachineLearning > =
381
- ( app ?: FirebaseApp ) => {
385
+ ( app ?: App ) => {
382
386
return this . ensureApp ( app ) . machineLearning ( ) ;
383
387
} ;
384
388
const machineLearning =
@@ -391,7 +395,7 @@ export class FirebaseNamespace {
391
395
* `Instance` service for the default app or an explicitly specified app.
392
396
*/
393
397
get instanceId ( ) : FirebaseServiceNamespace < InstanceId > {
394
- const fn : FirebaseServiceNamespace < InstanceId > = ( app ?: FirebaseApp ) => {
398
+ const fn : FirebaseServiceNamespace < InstanceId > = ( app ?: App ) => {
395
399
return this . ensureApp ( app ) . instanceId ( ) ;
396
400
} ;
397
401
const instanceId = require ( './instance-id/instance-id' ) . InstanceId ;
@@ -403,7 +407,7 @@ export class FirebaseNamespace {
403
407
* `ProjectManagement` service for the default app or an explicitly specified app.
404
408
*/
405
409
get projectManagement ( ) : FirebaseServiceNamespace < ProjectManagement > {
406
- const fn : FirebaseServiceNamespace < ProjectManagement > = ( app ?: FirebaseApp ) => {
410
+ const fn : FirebaseServiceNamespace < ProjectManagement > = ( app ?: App ) => {
407
411
return this . ensureApp ( app ) . projectManagement ( ) ;
408
412
} ;
409
413
const projectManagement = require ( './project-management/project-management' ) . ProjectManagement ;
@@ -415,7 +419,7 @@ export class FirebaseNamespace {
415
419
* `SecurityRules` service for the default app or an explicitly specified app.
416
420
*/
417
421
get securityRules ( ) : FirebaseServiceNamespace < SecurityRules > {
418
- const fn : FirebaseServiceNamespace < SecurityRules > = ( app ?: FirebaseApp ) => {
422
+ const fn : FirebaseServiceNamespace < SecurityRules > = ( app ?: App ) => {
419
423
return this . ensureApp ( app ) . securityRules ( ) ;
420
424
} ;
421
425
const securityRules = require ( './security-rules/security-rules' ) . SecurityRules ;
@@ -427,7 +431,7 @@ export class FirebaseNamespace {
427
431
* `RemoteConfig` service for the default app or an explicitly specified app.
428
432
*/
429
433
get remoteConfig ( ) : FirebaseServiceNamespace < RemoteConfig > {
430
- const fn : FirebaseServiceNamespace < RemoteConfig > = ( app ?: FirebaseApp ) => {
434
+ const fn : FirebaseServiceNamespace < RemoteConfig > = ( app ?: App ) => {
431
435
return this . ensureApp ( app ) . remoteConfig ( ) ;
432
436
} ;
433
437
const remoteConfig = require ( './remote-config/remote-config' ) . RemoteConfig ;
@@ -447,7 +451,7 @@ export class FirebaseNamespace {
447
451
*
448
452
* @return A new FirebaseApp instance.
449
453
*/
450
- public initializeApp ( options ?: AppOptions , appName ?: string ) : FirebaseApp {
454
+ public initializeApp ( options ?: AppOptions , appName ?: string ) : App {
451
455
return this . INTERNAL . initializeApp ( options , appName ) ;
452
456
}
453
457
@@ -458,20 +462,20 @@ export class FirebaseNamespace {
458
462
* @param appName Optional name of the FirebaseApp instance to return.
459
463
* @return The FirebaseApp instance which has the provided name.
460
464
*/
461
- public app ( appName ?: string ) : FirebaseApp {
465
+ public app ( appName ?: string ) : App {
462
466
return this . INTERNAL . app ( appName ) ;
463
467
}
464
468
465
469
/*
466
470
* Returns an array of all the non-deleted FirebaseApp instances.
467
471
*/
468
- public get apps ( ) : FirebaseApp [ ] {
472
+ public get apps ( ) : App [ ] {
469
473
return this . INTERNAL . apps ;
470
474
}
471
475
472
- private ensureApp ( app ?: FirebaseApp ) : FirebaseApp {
476
+ private ensureApp ( app ?: App ) : App {
473
477
if ( typeof app === 'undefined' ) {
474
- app = this . app ( ) as FirebaseApp ;
478
+ app = this . app ( ) ;
475
479
}
476
480
return app ;
477
481
}
0 commit comments