UNPKG

5.35 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2017 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18import { FirebaseApp } from '@firebase/app-types';
19import { EmulatorMockTokenOptions } from '@firebase/util';
20
21/**
22 * Represents a child snapshot of a `Reference` that is being iterated over. The key will never be undefined.
23 */
24export interface IteratedDataSnapshot extends DataSnapshot {
25 key: string; // key of the location of this snapshot.
26}
27
28export interface DataSnapshot {
29 child(path: string): DataSnapshot;
30 exists(): boolean;
31 exportVal(): any;
32 forEach(action: (a: IteratedDataSnapshot) => boolean | void): boolean;
33 getPriority(): string | number | null;
34 hasChild(path: string): boolean;
35 hasChildren(): boolean;
36 key: string | null;
37 numChildren(): number;
38 ref: Reference;
39 toJSON(): Object | null;
40 val(): any;
41}
42
43export interface Database {
44 app: FirebaseApp;
45 useEmulator(
46 host: string,
47 port: number,
48 options?: {
49 mockUserToken?: EmulatorMockTokenOptions | string;
50 }
51 ): void;
52 goOffline(): void;
53 goOnline(): void;
54 ref(path?: string | Reference): Reference;
55 refFromURL(url: string): Reference;
56}
57
58export class FirebaseDatabase implements Database {
59 private constructor();
60 app: FirebaseApp;
61 useEmulator(
62 host: string,
63 port: number,
64 options?: {
65 mockUserToken?: EmulatorMockTokenOptions | string;
66 }
67 ): void;
68 goOffline(): void;
69 goOnline(): void;
70 ref(path?: string | Reference): Reference;
71 refFromURL(url: string): Reference;
72}
73
74export interface OnDisconnect {
75 cancel(onComplete?: (a: Error | null) => any): Promise<void>;
76 remove(onComplete?: (a: Error | null) => any): Promise<void>;
77 set(value: any, onComplete?: (a: Error | null) => any): Promise<void>;
78 setWithPriority(
79 value: any,
80 priority: number | string | null,
81 onComplete?: (a: Error | null) => any
82 ): Promise<any>;
83 update(values: Object, onComplete?: (a: Error | null) => any): Promise<any>;
84}
85
86type EventType =
87 | 'value'
88 | 'child_added'
89 | 'child_changed'
90 | 'child_moved'
91 | 'child_removed';
92
93export interface Query {
94 endBefore(value: number | string | boolean | null, key?: string): Query;
95 endAt(value: number | string | boolean | null, key?: string): Query;
96 equalTo(value: number | string | boolean | null, key?: string): Query;
97 isEqual(other: Query | null): boolean;
98 limitToFirst(limit: number): Query;
99 limitToLast(limit: number): Query;
100 off(
101 eventType?: EventType,
102 callback?: (a: DataSnapshot, b?: string | null) => any,
103 context?: Object | null
104 ): void;
105 get(): Promise<DataSnapshot>;
106 on(
107 eventType: EventType,
108 callback: (a: DataSnapshot, b?: string | null) => any,
109 cancelCallbackOrContext?: ((a: Error) => any) | Object | null,
110 context?: Object | null
111 ): (a: DataSnapshot | null, b?: string | null) => any;
112 once(
113 eventType: EventType,
114 successCallback?: (a: DataSnapshot, b?: string | null) => any,
115 failureCallbackOrContext?: ((a: Error) => void) | Object | null,
116 context?: Object | null
117 ): Promise<DataSnapshot>;
118 orderByChild(path: string): Query;
119 orderByKey(): Query;
120 orderByPriority(): Query;
121 orderByValue(): Query;
122 ref: Reference;
123 startAt(value: number | string | boolean | null, key?: string): Query;
124 startAfter(value: number | string | boolean | null, key?: string): Query;
125 toJSON(): Object;
126 toString(): string;
127}
128
129export interface Reference extends Query {
130 child(path: string): Reference;
131 key: string | null;
132 onDisconnect(): OnDisconnect;
133 parent: Reference | null;
134 push(value?: any, onComplete?: (a: Error | null) => any): ThenableReference;
135 remove(onComplete?: (a: Error | null) => void): Promise<void>;
136 root: Reference;
137 set(value: any, onComplete?: (a: Error | null) => void): Promise<void>;
138 setPriority(
139 priority: string | number | null,
140 onComplete: (a: Error | null) => void
141 ): Promise<void>;
142 setWithPriority(
143 newVal: any,
144 newPriority: string | number | null,
145 onComplete?: (a: Error | null) => void
146 ): Promise<void>;
147 transaction(
148 transactionUpdate: (a: any) => any,
149 onComplete?: (a: Error | null, b: boolean, c: DataSnapshot | null) => void,
150 applyLocally?: boolean
151 ): Promise<TransactionResult>;
152 update(values: Object, onComplete?: (a: Error | null) => void): Promise<void>;
153}
154
155export interface ServerValue {
156 TIMESTAMP: Object;
157 increment(delta: number): Object;
158}
159
160export interface TransactionResult {
161 committed: boolean;
162 snapshot: DataSnapshot;
163}
164
165export interface ThenableReference
166 extends Reference,
167 Pick<Promise<Reference>, 'then' | 'catch'> {}
168
169export function enableLogging(
170 logger?: boolean | ((a: string) => any),
171 persistent?: boolean
172): any;
173
174declare module '@firebase/component' {
175 interface NameServiceMapping {
176 'database-compat': FirebaseDatabase;
177 }
178}
179
\No newline at end of file