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

Adding the RTDB typings back #140

Merged
merged 2 commits into from
Nov 20, 2017
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
126 changes: 113 additions & 13 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -175,6 +174,119 @@ 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 | null;
val(): any;
}

interface OnDisconnect {
cancel(onComplete?: (a: Error|null) => any): Promise<void>;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS SDK does Promise<any> here and on the rest of the functions in this interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will keep this unchanged for now, since we already seem to have some other (non-DB) APIs that return Promise<void>

remove(onComplete?: (a: Error|null) => any): Promise<void>;
set(value: any, onComplete?: (a: Error|null) => any): Promise<void>;
setWithPriority(
value: any,
priority: number|string|null,
onComplete?: (a: Error|null) => any
): Promise<void>;
update(values: Object, onComplete?: (a: Error|null) => any): Promise<void>;
}

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<any>;
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;
path: string;

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<void>;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise<any>?

set(value: any, onComplete?: (a: Error|null) => any): Promise<void>;
setPriority(
priority: string|number|null,
onComplete: (a: Error|null) => any
): Promise<void>;
setWithPriority(
newVal: any, newPriority: string|number|null,
onComplete?: (a: Error|null) => any
): Promise<void>;
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<void>;
}

interface ThenableReference extends admin.database.Reference, PromiseLike<any> {}

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;
Expand Down Expand Up @@ -295,18 +407,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;
Expand Down
4 changes: 2 additions & 2 deletions test/integration/typescript/src/example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down