Skip to content

Commit 9032ab0

Browse files
authored
pass firebase version to firestore (#377)
* pass firebase version to firestore * add trailing comma * add test cases for firebaseVersion
1 parent 14a4706 commit 9032ab0

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/firestore/firestore.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {FirebaseApp} from '../firebase-app';
1818
import {FirebaseFirestoreError} from '../utils/error';
1919
import {FirebaseServiceInterface, FirebaseServiceInternalsInterface} from '../firebase-service';
2020
import {ApplicationDefaultCredential, Certificate} from '../auth/credential';
21-
import {Firestore} from '@google-cloud/firestore';
21+
import {Firestore, Settings} from '@google-cloud/firestore';
2222

2323
import * as validator from '../utils/validator';
2424
import * as utils from '../utils/index';
@@ -63,7 +63,7 @@ export class FirestoreService implements FirebaseServiceInterface {
6363
}
6464
}
6565

66-
export function getFirestoreOptions(app: FirebaseApp): any {
66+
export function getFirestoreOptions(app: FirebaseApp): Settings {
6767
if (!validator.isNonNullObject(app) || !('options' in app)) {
6868
throw new FirebaseFirestoreError({
6969
code: 'invalid-argument',
@@ -73,6 +73,7 @@ export function getFirestoreOptions(app: FirebaseApp): any {
7373

7474
const projectId: string = utils.getProjectId(app);
7575
const cert: Certificate = app.options.credential.getCertificate();
76+
const { version: firebaseVersion } = require('../../package.json');
7677
if (cert != null) {
7778
// cert is available when the SDK has been initialized with a service account JSON file,
7879
// or by setting the GOOGLE_APPLICATION_CREDENTIALS envrionment variable.
@@ -92,12 +93,13 @@ export function getFirestoreOptions(app: FirebaseApp): any {
9293
client_email: cert.clientEmail,
9394
},
9495
projectId,
96+
firebaseVersion,
9597
};
9698
} else if (app.options.credential instanceof ApplicationDefaultCredential) {
9799
// Try to use the Google application default credentials.
98100
// If an explicit project ID is not available, let Firestore client discover one from the
99101
// environment. This prevents the users from having to set GOOGLE_CLOUD_PROJECT in GCP runtimes.
100-
return validator.isNonEmptyString(projectId) ? {projectId} : {};
102+
return validator.isNonEmptyString(projectId) ? {projectId, firebaseVersion} : {firebaseVersion};
101103
}
102104

103105
throw new FirebaseFirestoreError({
@@ -122,5 +124,6 @@ function initFirestore(app: FirebaseApp): Firestore {
122124
+ `Original error: ${err}`,
123125
});
124126
}
127+
125128
return new firestoreDatabase(options);
126129
}

test/unit/firestore/firestore.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe('Firestore', () => {
4242
+ 'credentials to use Cloud Firestore API.';
4343

4444
const mockServiceAccount = path.resolve(__dirname, '../../resources/mock.key.json');
45+
const { version: firebaseVersion } = require('../../../package.json');
4546

4647
beforeEach(() => {
4748
appCredentials = process.env.GOOGLE_APPLICATION_CREDENTIALS;
@@ -179,4 +180,16 @@ describe('Firestore', () => {
179180
expect(options.projectId).to.equal('env-project-id');
180181
});
181182
});
183+
184+
describe('options.firebaseVersion', () => {
185+
it('should return firebaseVersion when using credential with service account certificate', () => {
186+
const options = getFirestoreOptions(mockApp);
187+
expect(options.firebaseVersion).to.equal(firebaseVersion);
188+
});
189+
190+
it('should return firebaseVersion when using credential without service account certificate', () => {
191+
const options = getFirestoreOptions(defaultCredentialApp);
192+
expect(options.firebaseVersion).to.equal(firebaseVersion);
193+
});
194+
});
182195
});

0 commit comments

Comments
 (0)