From daf5a77197ed3347c75ca1382701e2a40382c779 Mon Sep 17 00:00:00 2001 From: Hiranya Jayathilaka Date: Mon, 20 Nov 2017 11:19:43 -0800 Subject: [PATCH 1/2] Adding the RTDB typings back --- src/index.d.ts | 125 ++++++++++++++++-- .../typescript/src/example.test.ts | 4 +- 2 files changed, 114 insertions(+), 15 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 4fa9f5548e..38aea0a7a3 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -15,7 +15,6 @@ */ import {Bucket} from '@google-cloud/storage'; -import * as _rtdb from '@firebase/database'; import * as _firestore from '@google-cloud/firestore'; declare namespace admin { @@ -175,6 +174,118 @@ declare namespace admin.credential { function refreshToken(refreshTokenPathOrObject: string|Object): admin.credential.Credential; } +declare namespace admin.database { + interface Database { + app: admin.app.App; + + goOffline(): void; + goOnline(): void; + ref(path?: string): admin.database.Reference; + refFromURL(url: string): admin.database.Reference; + } + + interface DataSnapshot { + key: string|null; + ref: admin.database.Reference; + + child(path: string): admin.database.DataSnapshot; + exists(): boolean; + exportVal(): any; + forEach(action: (a: admin.database.DataSnapshot) => boolean): boolean; + getPriority(): string|number|null; + hasChild(path: string): boolean; + hasChildren(): boolean; + numChildren(): number; + toJSON(): Object; + val(): any; + } + + interface OnDisconnect { + cancel(onComplete?: (a: Error|null) => any): Promise; + remove(onComplete?: (a: Error|null) => any): Promise; + set(value: any, onComplete?: (a: Error|null) => any): Promise; + setWithPriority( + value: any, + priority: number|string|null, + onComplete?: (a: Error|null) => any + ): Promise; + update(values: Object, onComplete?: (a: Error|null) => any): Promise; + } + + type EventType = 'value' | 'child_added' | 'child_changed' | 'child_moved' | 'child_removed'; + + interface Query { + ref: admin.database.Reference; + + endAt(value: number|string|boolean|null, key?: string): admin.database.Query; + equalTo(value: number|string|boolean|null, key?: string): admin.database.Query; + isEqual(other: admin.database.Query|null): boolean; + limitToFirst(limit: number): admin.database.Query; + limitToLast(limit: number): admin.database.Query; + off( + eventType?: admin.database.EventType, + callback?: (a: admin.database.DataSnapshot, b?: string|null) => any, + context?: Object|null + ): void; + on( + eventType: admin.database.EventType, + callback: (a: admin.database.DataSnapshot|null, b?: string) => any, + cancelCallbackOrContext?: Object|null, + context?: Object|null + ): (a: admin.database.DataSnapshot|null, b?: string) => any; + once( + eventType: admin.database.EventType, + successCallback?: (a: admin.database.DataSnapshot, b?: string) => any, + failureCallbackOrContext?: Object|null, + context?: Object|null + ): Promise; + orderByChild(path: string): admin.database.Query; + orderByKey(): admin.database.Query; + orderByPriority(): admin.database.Query; + orderByValue(): admin.database.Query; + startAt(value: number|string|boolean|null, key?: string): admin.database.Query; + toJSON(): Object; + toString(): string; + } + + interface Reference extends admin.database.Query { + key: string|null; + parent: admin.database.Reference|null; + root: admin.database.Reference; + + child(path: string): admin.database.Reference; + onDisconnect(): admin.database.OnDisconnect; + push(value?: any, onComplete?: (a: Error|null) => any): admin.database.ThenableReference; + remove(onComplete?: (a: Error|null) => any): Promise; + set(value: any, onComplete?: (a: Error|null) => any): Promise; + setPriority( + priority: string|number|null, + onComplete: (a: Error|null) => any + ): Promise; + setWithPriority( + newVal: any, newPriority: string|number|null, + onComplete?: (a: Error|null) => any + ): Promise; + transaction( + transactionUpdate: (a: any) => any, + onComplete?: (a: Error|null, b: boolean, c: admin.database.DataSnapshot|null) => any, + applyLocally?: boolean + ): Promise<{ + committed: boolean, + snapshot: admin.database.DataSnapshot|null + }>; + update(values: Object, onComplete?: (a: Error|null) => any): Promise; + } + + interface ThenableReference extends admin.database.Reference, Promise {} + + function enableLogging(logger?: boolean|((message: string) => any), persistent?: boolean): any; +} + +declare namespace admin.database.ServerValue { + var TIMESTAMP: number; +} + declare namespace admin.messaging { type DataMessagePayload = { [key: string]: string; @@ -295,18 +406,6 @@ declare namespace admin.storage { } } -declare namespace admin.database { - export import Database = _rtdb.Database; - export import Reference = _rtdb.Reference; - export import Query = _rtdb.Query; - export import ServerValue = _rtdb.ServerValue; - export import enableLogging = _rtdb.enableLogging; - export import OnDisconnect = _rtdb.OnDisconnect; - export import DataSnapshot = _rtdb.DataSnapshot; - - type EventType = 'value' | 'child_added' | 'child_changed' | 'child_moved' | 'child_removed'; -} - declare namespace admin.firestore { export import FieldPath = _firestore.FieldPath; export import FieldValue = _firestore.FieldValue; diff --git a/test/integration/typescript/src/example.test.ts b/test/integration/typescript/src/example.test.ts index 8f345bf113..98d03056aa 100644 --- a/test/integration/typescript/src/example.test.ts +++ b/test/integration/typescript/src/example.test.ts @@ -32,12 +32,12 @@ describe('Init App', () => { it('Should return a Database client', () => { const db = admin.database(app); - expect(db).to.be.instanceOf(admin.database.Database); + expect(db).to.be.instanceOf((admin.database as any).Database); }); it('Should return a Database client for URL', () => { const db = app.database('https://other-mock.firebaseio.com'); - expect(db).to.be.instanceOf(admin.database.Database); + expect(db).to.be.instanceOf((admin.database as any).Database); }); it('Should return a Database ServerValue', () => { From 9b4cd43f5cada064d845cd708397697cc3f4e802 Mon Sep 17 00:00:00 2001 From: Hiranya Jayathilaka Date: Mon, 20 Nov 2017 11:43:10 -0800 Subject: [PATCH 2/2] Updated RTDB typings --- src/index.d.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 38aea0a7a3..77a88e03cf 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -196,7 +196,7 @@ declare namespace admin.database { hasChild(path: string): boolean; hasChildren(): boolean; numChildren(): number; - toJSON(): Object; + toJSON(): Object | null; val(): any; } @@ -213,7 +213,7 @@ declare namespace admin.database { } type EventType = 'value' | 'child_added' | 'child_changed' | 'child_moved' | 'child_removed'; - + interface Query { ref: admin.database.Reference; @@ -252,6 +252,7 @@ declare namespace admin.database { key: string|null; parent: admin.database.Reference|null; root: admin.database.Reference; + path: string; child(path: string): admin.database.Reference; onDisconnect(): admin.database.OnDisconnect; @@ -277,7 +278,7 @@ declare namespace admin.database { update(values: Object, onComplete?: (a: Error|null) => any): Promise; } - interface ThenableReference extends admin.database.Reference, Promise {} + interface ThenableReference extends admin.database.Reference, PromiseLike {} function enableLogging(logger?: boolean|((message: string) => any), persistent?: boolean): any; }