Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add firebaseAdminVersion to Firestore settings #2795

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
"uuid": "^11.0.2"
},
"optionalDependencies": {
"@google-cloud/firestore": "^7.10.0",
"@google-cloud/firestore": "^7.11.0",
"@google-cloud/storage": "^7.14.0"
},
"devDependencies": {
Expand Down
22 changes: 16 additions & 6 deletions src/firestore/firestore-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function getFirestoreOptions(app: App, firestoreSettings?: FirestoreSetti
const projectId: string | null = utils.getExplicitProjectId(app);
const credential = app.options.credential;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { version: firebaseVersion } = require('../../package.json');
const sdkVersion = utils.getSdkVersion();
const preferRest = firestoreSettings?.preferRest;
if (credential instanceof ServiceAccountCredential) {
return {
Expand All @@ -124,16 +124,26 @@ export function getFirestoreOptions(app: App, firestoreSettings?: FirestoreSetti
// When the SDK is initialized with ServiceAccountCredentials an explicit projectId is
// guaranteed to be available.
projectId: projectId!,
firebaseVersion,
firebaseVersion: sdkVersion,
firebaseAdminVersion: sdkVersion,
preferRest,
};
} else if (isApplicationDefault(app.options.credential)) {
// Try to use the Google application default credentials.
// If an explicit project ID is not available, let Firestore client discover one from the
// environment. This prevents the users from having to set GOOGLE_CLOUD_PROJECT in GCP runtimes.
return validator.isNonEmptyString(projectId)
? { projectId, firebaseVersion, preferRest }
: { firebaseVersion, preferRest };
? {
projectId,
firebaseVersion: sdkVersion,
firebaseAdminVersion: sdkVersion,
preferRest
}
: {
firebaseVersion: sdkVersion,
firebaseAdminVersion: sdkVersion,
preferRest
};
}

throw new FirebaseFirestoreError({
Expand All @@ -155,8 +165,8 @@ function initFirestore(app: App, databaseId: string, firestoreSettings?: Firesto
throw new FirebaseFirestoreError({
code: 'missing-dependencies',
message: 'Failed to import the Cloud Firestore client library for Node.js. '
+ 'Make sure to install the "@google-cloud/firestore" npm package. '
+ `Original error: ${err}`,
+ 'Make sure to install the "@google-cloud/firestore" npm package. '
+ `Original error: ${err}`,
});
}

Expand Down
21 changes: 18 additions & 3 deletions test/unit/firestore/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
RefreshTokenCredential
} from '../../../src/app/credential-internal';
import { FirestoreService, getFirestoreOptions } from '../../../src/firestore/firestore-internal';
import { getSdkVersion } from '../../../src/utils/index';
import { DEFAULT_DATABASE_ID } from '@google-cloud/firestore/build/src/path';

describe('Firestore', () => {
Expand All @@ -44,7 +45,7 @@ describe('Firestore', () => {
+ 'credentials to use Cloud Firestore API.';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { version: firebaseVersion } = require('../../../package.json');
const sdkVersion = getSdkVersion();
const defaultCredentialApps = [
{
name: 'ComputeEngineCredentials',
Expand Down Expand Up @@ -191,13 +192,27 @@ describe('Firestore', () => {
describe('options.firebaseVersion', () => {
it('should return firebaseVersion when using credential with service account certificate', () => {
const options = getFirestoreOptions(mockApp);
expect(options.firebaseVersion).to.equal(firebaseVersion);
expect(options.firebaseVersion).to.equal(sdkVersion);
});

defaultCredentialApps.forEach((config) => {
it(`should return firebaseVersion when using default ${config.name}`, () => {
const options = getFirestoreOptions(config.app);
expect(options.firebaseVersion).to.equal(firebaseVersion);
expect(options.firebaseVersion).to.equal(sdkVersion);
});
});
});

describe('options.firebaseAdminVersion', () => {
it('should return firebaseAdminVersion when using credential with service account certificate', () => {
const options = getFirestoreOptions(mockApp);
expect(options.firebaseAdminVersion).to.equal(sdkVersion);
});

defaultCredentialApps.forEach((config) => {
it(`should return firebaseAdminVersion when using default ${config.name}`, () => {
const options = getFirestoreOptions(config.app);
expect(options.firebaseAdminVersion).to.equal(sdkVersion);
});
});
});
Expand Down
Loading