Skip to content

Allow Firestore to auto-generate typings, separate internal vs external APIs #986

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

Merged
merged 5 commits into from
Aug 11, 2020
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
1 change: 0 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/firebase-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
File renamed without changes.
60 changes: 60 additions & 0 deletions src/firestore/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion test/unit/firestore/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down