@@ -63,7 +63,7 @@ export class FirestoreService implements FirebaseServiceInterface {
6363 }
6464}
6565
66- function initFirestore ( app : FirebaseApp ) : Firestore {
66+ export function getFirestoreOptions ( app : FirebaseApp ) : any {
6767 if ( ! validator . isNonNullObject ( app ) || ! ( 'options' in app ) ) {
6868 throw new FirebaseFirestoreError ( {
6969 code : 'invalid-argument' ,
@@ -73,7 +73,6 @@ function initFirestore(app: FirebaseApp): Firestore {
7373
7474 const projectId : string = utils . getProjectId ( app ) ;
7575 const cert : Certificate = app . options . credential . getCertificate ( ) ;
76- let options : any ;
7776 if ( cert != null ) {
7877 // cert is available when the SDK has been initialized with a service account JSON file,
7978 // or by setting the GOOGLE_APPLICATION_CREDENTIALS envrionment variable.
@@ -87,7 +86,7 @@ function initFirestore(app: FirebaseApp): Firestore {
8786 + 'Alternatively set the GCLOUD_PROJECT environment variable.' ,
8887 } ) ;
8988 }
90- options = {
89+ return {
9190 credentials : {
9291 private_key : cert . privateKey ,
9392 client_email : cert . clientEmail ,
@@ -98,17 +97,20 @@ function initFirestore(app: FirebaseApp): Firestore {
9897 // Try to use the Google application default credentials.
9998 // If an explicit project ID is not available, let Firestore client discover one from the
10099 // 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} : { } ;
109101 }
110102
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 ) ;
111113 // 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' ) ;
113115 return new firestoreDatabase ( options ) ;
114116}
0 commit comments