Skip to content

Commit be4b539

Browse files
authored
Allow Firestore to auto-generate typings, separate internal vs external APIs (#986)
1 parent 875d865 commit be4b539

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

gulpfile.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ const TEMPORARY_TYPING_EXCLUDES = [
6969
'!lib/firebase-service.d.ts',
7070
'!lib/auth/*.d.ts',
7171
'!lib/database/*.d.ts',
72-
'!lib/firestore/*.d.ts',
7372
'!lib/machine-learning/*.d.ts',
7473
'!lib/storage/*.d.ts',
7574
'!lib/utils/*.d.ts',

src/firebase-app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { Storage } from './storage/storage';
2828
import { Database } from '@firebase/database';
2929
import { DatabaseService } from './database/database';
3030
import { Firestore } from '@google-cloud/firestore';
31-
import { FirestoreService } from './firestore/firestore';
31+
import { FirestoreService } from './firestore/firestore-internal';
3232
import { InstanceId } from './instance-id/instance-id';
3333

3434
import { ProjectManagement } from './project-management/project-management';
@@ -339,7 +339,7 @@ export class FirebaseApp {
339339

340340
public firestore(): Firestore {
341341
const service: FirestoreService = this.ensureService_('firestore', () => {
342-
const firestoreService: typeof FirestoreService = require('./firestore/firestore').FirestoreService;
342+
const firestoreService: typeof FirestoreService = require('./firestore/firestore-internal').FirestoreService;
343343
return new firestoreService(this);
344344
});
345345
return service.client;
File renamed without changes.

src/firestore/index.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*!
2+
* Copyright 2020 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { FirebaseApp } from '../firebase-app';
18+
import * as _firestore from '@google-cloud/firestore';
19+
import * as firebaseAdmin from '../index';
20+
21+
export function firestore(app?: FirebaseApp): _firestore.Firestore {
22+
if (typeof (app) === 'undefined') {
23+
app = firebaseAdmin.app();
24+
}
25+
return app.firestore();
26+
}
27+
28+
/**
29+
* We must define a namespace to make the typings work correctly. Otherwise
30+
* `admin.firestore()` cannot be called like a function. Temporarily,
31+
* admin.firestore is used as the namespace name because we cannot barrel
32+
* re-export the contents from firestore, and we want it to
33+
* match the namespacing in the re-export inside src/index.d.ts
34+
*/
35+
/* eslint-disable @typescript-eslint/no-namespace */
36+
export namespace admin.firestore {
37+
// See https://github.com/microsoft/TypeScript/issues/4336
38+
/* eslint-disable @typescript-eslint/no-unused-vars */
39+
// See https://github.com/typescript-eslint/typescript-eslint/issues/363
40+
export import v1beta1 = _firestore.v1beta1;
41+
export import v1 = _firestore.v1;
42+
43+
export import CollectionReference = _firestore.CollectionReference;
44+
export import DocumentData = _firestore.DocumentData;
45+
export import DocumentReference = _firestore.DocumentReference;
46+
export import DocumentSnapshot = _firestore.DocumentSnapshot;
47+
export import FieldPath = _firestore.FieldPath;
48+
export import FieldValue = _firestore.FieldValue;
49+
export import Firestore = _firestore.Firestore;
50+
export import GeoPoint = _firestore.GeoPoint;
51+
export import Query = _firestore.Query;
52+
export import QueryDocumentSnapshot = _firestore.QueryDocumentSnapshot;
53+
export import QuerySnapshot = _firestore.QuerySnapshot;
54+
export import Timestamp = _firestore.Timestamp;
55+
export import Transaction = _firestore.Transaction;
56+
export import WriteBatch = _firestore.WriteBatch;
57+
export import WriteResult = _firestore.WriteResult;
58+
59+
export import setLogFunction = _firestore.setLogFunction;
60+
}

test/unit/firestore/firestore.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { expect } from 'chai';
2222
import * as mocks from '../../resources/mocks';
2323
import { FirebaseApp } from '../../../src/firebase-app';
2424
import { ComputeEngineCredential, RefreshTokenCredential } from '../../../src/auth/credential';
25-
import { FirestoreService, getFirestoreOptions } from '../../../src/firestore/firestore';
25+
import { FirestoreService, getFirestoreOptions } from '../../../src/firestore/firestore-internal';
2626

2727
describe('Firestore', () => {
2828
let mockApp: FirebaseApp;

0 commit comments

Comments
 (0)