@@ -63,7 +63,7 @@ export class FirestoreService implements FirebaseServiceInterface {
63
63
}
64
64
}
65
65
66
- function initFirestore ( app : FirebaseApp ) : Firestore {
66
+ export function getFirestoreOptions ( app : FirebaseApp ) : any {
67
67
if ( ! validator . isNonNullObject ( app ) || ! ( 'options' in app ) ) {
68
68
throw new FirebaseFirestoreError ( {
69
69
code : 'invalid-argument' ,
@@ -73,7 +73,6 @@ function initFirestore(app: FirebaseApp): Firestore {
73
73
74
74
const projectId : string = utils . getProjectId ( app ) ;
75
75
const cert : Certificate = app . options . credential . getCertificate ( ) ;
76
- let options : any ;
77
76
if ( cert != null ) {
78
77
// cert is available when the SDK has been initialized with a service account JSON file,
79
78
// or by setting the GOOGLE_APPLICATION_CREDENTIALS envrionment variable.
@@ -87,7 +86,7 @@ function initFirestore(app: FirebaseApp): Firestore {
87
86
+ 'Alternatively set the GCLOUD_PROJECT environment variable.' ,
88
87
} ) ;
89
88
}
90
- options = {
89
+ return {
91
90
credentials : {
92
91
private_key : cert . privateKey ,
93
92
client_email : cert . clientEmail ,
@@ -98,17 +97,20 @@ function initFirestore(app: FirebaseApp): Firestore {
98
97
// Try to use the Google application default credentials.
99
98
// If an explicit project ID is not available, let Firestore client discover one from the
100
99
// environment. This prevents the users from having to set GCLOUD_PROJECT in GCP runtimes.
101
- options = validator . isNonEmptyString ( projectId ) ? { projectId} : { } ;
102
- } else {
103
- throw new FirebaseFirestoreError ( {
104
- code : 'invalid-credential' ,
105
- message : 'Failed to initialize Google Cloud Firestore client with the available credentials. ' +
106
- 'Must initialize the SDK with a certificate credential or application default credentials ' +
107
- 'to use Cloud Firestore API.' ,
108
- } ) ;
100
+ return validator . isNonEmptyString ( projectId ) ? { projectId} : { } ;
109
101
}
110
102
103
+ throw new FirebaseFirestoreError ( {
104
+ code : 'invalid-credential' ,
105
+ message : 'Failed to initialize Google Cloud Firestore client with the available credentials. ' +
106
+ 'Must initialize the SDK with a certificate credential or application default credentials ' +
107
+ 'to use Cloud Firestore API.' ,
108
+ } ) ;
109
+ }
110
+
111
+ function initFirestore ( app : FirebaseApp ) : Firestore {
112
+ const options = getFirestoreOptions ( app ) ;
111
113
// Lazy-load the Firestore implementation here, which in turns loads gRPC.
112
- const firestoreDatabase = require ( '@google-cloud/firestore' ) ;
114
+ const firestoreDatabase : typeof Firestore = require ( '@google-cloud/firestore' ) ;
113
115
return new firestoreDatabase ( options ) ;
114
116
}
0 commit comments