diff --git a/gulpfile.js b/gulpfile.js index b4f216a638..9ed848daf7 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -69,7 +69,6 @@ const TEMPORARY_TYPING_EXCLUDES = [ '!lib/firebase-service.d.ts', '!lib/auth/*.d.ts', '!lib/database/*.d.ts', - '!lib/firestore/*.d.ts', '!lib/machine-learning/*.d.ts', '!lib/storage/*.d.ts', '!lib/utils/*.d.ts', diff --git a/src/firebase-app.ts b/src/firebase-app.ts index 4baf2de08d..194b792ce4 100644 --- a/src/firebase-app.ts +++ b/src/firebase-app.ts @@ -28,7 +28,7 @@ import { Storage } from './storage/storage'; import { Database } from '@firebase/database'; import { DatabaseService } from './database/database'; import { Firestore } from '@google-cloud/firestore'; -import { FirestoreService } from './firestore/firestore'; +import { FirestoreService } from './firestore/firestore-internal'; import { InstanceId } from './instance-id/instance-id'; import { ProjectManagement } from './project-management/project-management'; @@ -339,7 +339,7 @@ export class FirebaseApp { public firestore(): Firestore { const service: FirestoreService = this.ensureService_('firestore', () => { - const firestoreService: typeof FirestoreService = require('./firestore/firestore').FirestoreService; + const firestoreService: typeof FirestoreService = require('./firestore/firestore-internal').FirestoreService; return new firestoreService(this); }); return service.client; diff --git a/src/firestore/firestore.ts b/src/firestore/firestore-internal.ts similarity index 100% rename from src/firestore/firestore.ts rename to src/firestore/firestore-internal.ts diff --git a/src/firestore/index.ts b/src/firestore/index.ts new file mode 100644 index 0000000000..fb48b137b8 --- /dev/null +++ b/src/firestore/index.ts @@ -0,0 +1,60 @@ +/*! + * Copyright 2020 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { FirebaseApp } from '../firebase-app'; +import * as _firestore from '@google-cloud/firestore'; +import * as firebaseAdmin from '../index'; + +export function firestore(app?: FirebaseApp): _firestore.Firestore { + if (typeof (app) === 'undefined') { + app = firebaseAdmin.app(); + } + return app.firestore(); +} + +/** + * We must define a namespace to make the typings work correctly. Otherwise + * `admin.firestore()` cannot be called like a function. Temporarily, + * admin.firestore is used as the namespace name because we cannot barrel + * re-export the contents from firestore, and we want it to + * match the namespacing in the re-export inside src/index.d.ts + */ +/* eslint-disable @typescript-eslint/no-namespace */ +export namespace admin.firestore { + // See https://github.com/microsoft/TypeScript/issues/4336 + /* eslint-disable @typescript-eslint/no-unused-vars */ + // See https://github.com/typescript-eslint/typescript-eslint/issues/363 + export import v1beta1 = _firestore.v1beta1; + export import v1 = _firestore.v1; + + export import CollectionReference = _firestore.CollectionReference; + export import DocumentData = _firestore.DocumentData; + export import DocumentReference = _firestore.DocumentReference; + export import DocumentSnapshot = _firestore.DocumentSnapshot; + export import FieldPath = _firestore.FieldPath; + export import FieldValue = _firestore.FieldValue; + export import Firestore = _firestore.Firestore; + export import GeoPoint = _firestore.GeoPoint; + export import Query = _firestore.Query; + export import QueryDocumentSnapshot = _firestore.QueryDocumentSnapshot; + export import QuerySnapshot = _firestore.QuerySnapshot; + export import Timestamp = _firestore.Timestamp; + export import Transaction = _firestore.Transaction; + export import WriteBatch = _firestore.WriteBatch; + export import WriteResult = _firestore.WriteResult; + + export import setLogFunction = _firestore.setLogFunction; +} diff --git a/test/unit/firestore/firestore.spec.ts b/test/unit/firestore/firestore.spec.ts index 236050dfbe..0935f58821 100644 --- a/test/unit/firestore/firestore.spec.ts +++ b/test/unit/firestore/firestore.spec.ts @@ -22,7 +22,7 @@ import { expect } from 'chai'; import * as mocks from '../../resources/mocks'; import { FirebaseApp } from '../../../src/firebase-app'; import { ComputeEngineCredential, RefreshTokenCredential } from '../../../src/auth/credential'; -import { FirestoreService, getFirestoreOptions } from '../../../src/firestore/firestore'; +import { FirestoreService, getFirestoreOptions } from '../../../src/firestore/firestore-internal'; describe('Firestore', () => { let mockApp: FirebaseApp;