|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 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 | + |
| 18 | +import { FirebaseApp } from '@firebase/app-types'; |
| 19 | + |
| 20 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 21 | + |
| 22 | +export interface DocumentData { |
| 23 | + [field: string]: any; |
| 24 | +} |
| 25 | + |
| 26 | +export interface UpdateData { |
| 27 | + [fieldPath: string]: any; |
| 28 | +} |
| 29 | + |
| 30 | +export interface Settings { |
| 31 | + host?: string; |
| 32 | + ssl?: boolean; |
| 33 | + ignoreUndefinedProperties?: boolean; |
| 34 | +} |
| 35 | + |
| 36 | +export type LogLevel = 'debug' | 'error' | 'silent'; |
| 37 | + |
| 38 | +export function setLogLevel(logLevel: LogLevel): void; |
| 39 | + |
| 40 | +export interface FirestoreDataConverter<T> { |
| 41 | + toFirestore(modelObject: T): DocumentData; |
| 42 | + fromFirestore(snapshot: QueryDocumentSnapshot): T; |
| 43 | +} |
| 44 | + |
| 45 | +export class FirebaseFirestore { |
| 46 | + private constructor(); |
| 47 | +} |
| 48 | + |
| 49 | +export function initializeFirestore( |
| 50 | + app: FirebaseApp, |
| 51 | + settings: Settings |
| 52 | +): FirebaseFirestore; |
| 53 | +export function getFirestore(app: FirebaseApp): FirebaseFirestore; |
| 54 | +export function terminate(firestore: FirebaseFirestore): Promise<void>; |
| 55 | +export function writeBatch(firestore: FirebaseFirestore): WriteBatch; |
| 56 | +export function runTransaction<T>( |
| 57 | + firestore: FirebaseFirestore, |
| 58 | + updateFunction: (transaction: Transaction) => Promise<T> |
| 59 | +): Promise<T>; |
| 60 | + |
| 61 | +export function collection( |
| 62 | + firestore: FirebaseFirestore, |
| 63 | + collectionPath: string |
| 64 | +): CollectionReference<DocumentData>; |
| 65 | +export function collection( |
| 66 | + reference: DocumentReference, |
| 67 | + collectionPath: string |
| 68 | +): CollectionReference<DocumentData>; |
| 69 | +export function doc( |
| 70 | + firestore: FirebaseFirestore, |
| 71 | + documentPath: string |
| 72 | +): DocumentReference<DocumentData>; |
| 73 | +export function doc<T>( |
| 74 | + reference: CollectionReference<T>, |
| 75 | + documentPath?: string |
| 76 | +): DocumentReference<T>; |
| 77 | +export function parent( |
| 78 | + reference: CollectionReference<unknown> |
| 79 | +): DocumentReference<DocumentData> | null; |
| 80 | +export function parent<T>( |
| 81 | + reference: DocumentReference<T> |
| 82 | +): CollectionReference<T>; |
| 83 | +export function collectionGroup( |
| 84 | + firestore: FirebaseFirestore, |
| 85 | + collectionId: string |
| 86 | +): Query<DocumentData>; |
| 87 | + |
| 88 | +export class GeoPoint { |
| 89 | + constructor(latitude: number, longitude: number); |
| 90 | + |
| 91 | + readonly latitude: number; |
| 92 | + readonly longitude: number; |
| 93 | + |
| 94 | + isEqual(other: GeoPoint): boolean; |
| 95 | +} |
| 96 | + |
| 97 | +export class Timestamp { |
| 98 | + constructor(seconds: number, nanoseconds: number); |
| 99 | + |
| 100 | + static now(): Timestamp; |
| 101 | + |
| 102 | + static fromDate(date: Date): Timestamp; |
| 103 | + |
| 104 | + static fromMillis(milliseconds: number): Timestamp; |
| 105 | + |
| 106 | + readonly seconds: number; |
| 107 | + readonly nanoseconds: number; |
| 108 | + |
| 109 | + toDate(): Date; |
| 110 | + |
| 111 | + toMillis(): number; |
| 112 | + |
| 113 | + isEqual(other: Timestamp): boolean; |
| 114 | + |
| 115 | + valueOf(): string; |
| 116 | +} |
| 117 | + |
| 118 | +export class Blob { |
| 119 | + private constructor(); |
| 120 | + |
| 121 | + static fromBase64String(base64: string): Blob; |
| 122 | + |
| 123 | + static fromUint8Array(array: Uint8Array): Blob; |
| 124 | + |
| 125 | + toBase64(): string; |
| 126 | + |
| 127 | + toUint8Array(): Uint8Array; |
| 128 | + |
| 129 | + isEqual(other: Blob): boolean; |
| 130 | +} |
| 131 | + |
| 132 | +export class Transaction { |
| 133 | + private constructor(); |
| 134 | + |
| 135 | + get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>>; |
| 136 | + |
| 137 | + set<T>( |
| 138 | + documentRef: DocumentReference<T>, |
| 139 | + data: T, |
| 140 | + options?: SetOptions |
| 141 | + ): Transaction; |
| 142 | + |
| 143 | + update(documentRef: DocumentReference<any>, data: UpdateData): Transaction; |
| 144 | + update( |
| 145 | + documentRef: DocumentReference<any>, |
| 146 | + field: string | FieldPath, |
| 147 | + value: any, |
| 148 | + ...moreFieldsAndValues: any[] |
| 149 | + ): Transaction; |
| 150 | + |
| 151 | + delete(documentRef: DocumentReference<any>): Transaction; |
| 152 | +} |
| 153 | + |
| 154 | +export class WriteBatch { |
| 155 | + private constructor(); |
| 156 | + |
| 157 | + set<T>( |
| 158 | + documentRef: DocumentReference<T>, |
| 159 | + data: T, |
| 160 | + options?: SetOptions |
| 161 | + ): WriteBatch; |
| 162 | + |
| 163 | + update(documentRef: DocumentReference<any>, data: UpdateData): WriteBatch; |
| 164 | + update( |
| 165 | + documentRef: DocumentReference<any>, |
| 166 | + field: string | FieldPath, |
| 167 | + value: any, |
| 168 | + ...moreFieldsAndValues: any[] |
| 169 | + ): WriteBatch; |
| 170 | + |
| 171 | + delete(documentRef: DocumentReference<any>): WriteBatch; |
| 172 | + |
| 173 | + commit(): Promise<void>; |
| 174 | +} |
| 175 | + |
| 176 | +export type SetOptions = |
| 177 | + | { merge: true } |
| 178 | + | { mergeFields: Array<string | FieldPath> }; |
| 179 | + |
| 180 | +export class DocumentReference<T = DocumentData> { |
| 181 | + private constructor(); |
| 182 | + readonly id: string; |
| 183 | + readonly firestore: FirebaseFirestore; |
| 184 | + readonly path: string; |
| 185 | + withConverter<U>(converter: FirestoreDataConverter<U>): DocumentReference<U>; |
| 186 | +} |
| 187 | + |
| 188 | +export class DocumentSnapshot<T = DocumentData> { |
| 189 | + readonly ref: DocumentReference<T>; |
| 190 | + readonly id: string; |
| 191 | + exists(): this is QueryDocumentSnapshot<T>; |
| 192 | + data(): T | undefined; |
| 193 | + get(fieldPath: string | FieldPath): any; |
| 194 | +} |
| 195 | + |
| 196 | +export class QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot< |
| 197 | + T |
| 198 | +> { |
| 199 | + data(): T; |
| 200 | +} |
| 201 | + |
| 202 | +export type OrderByDirection = 'desc' | 'asc'; |
| 203 | + |
| 204 | +export type WhereFilterOp = |
| 205 | + | '<' |
| 206 | + | '<=' |
| 207 | + | '==' |
| 208 | + | '>=' |
| 209 | + | '>' |
| 210 | + | 'array-contains' |
| 211 | + | 'in' |
| 212 | + | 'array-contains-any'; |
| 213 | + |
| 214 | +export class Query<T = DocumentData> { |
| 215 | + protected constructor(); |
| 216 | + readonly firestore: FirebaseFirestore; |
| 217 | + where( |
| 218 | + fieldPath: string | FieldPath, |
| 219 | + opStr: WhereFilterOp, |
| 220 | + value: any |
| 221 | + ): Query<T>; |
| 222 | + orderBy( |
| 223 | + fieldPath: string | FieldPath, |
| 224 | + directionStr?: OrderByDirection |
| 225 | + ): Query<T>; |
| 226 | + limit(limit: number): Query<T>; |
| 227 | + limitToLast(limit: number): Query<T>; |
| 228 | + startAt(snapshot: DocumentSnapshot<any>): Query<T>; |
| 229 | + startAt(...fieldValues: any[]): Query<T>; |
| 230 | + startAfter(snapshot: DocumentSnapshot<any>): Query<T>; |
| 231 | + startAfter(...fieldValues: any[]): Query<T>; |
| 232 | + endBefore(snapshot: DocumentSnapshot<any>): Query<T>; |
| 233 | + endBefore(...fieldValues: any[]): Query<T>; |
| 234 | + endAt(snapshot: DocumentSnapshot<any>): Query<T>; |
| 235 | + endAt(...fieldValues: any[]): Query<T>; |
| 236 | + withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>; |
| 237 | +} |
| 238 | + |
| 239 | +export class QuerySnapshot<T = DocumentData> { |
| 240 | + readonly query: Query<T>; |
| 241 | + readonly docs: Array<QueryDocumentSnapshot<T>>; |
| 242 | + readonly size: number; |
| 243 | + readonly empty: boolean; |
| 244 | + forEach( |
| 245 | + callback: (result: QueryDocumentSnapshot<T>) => void, |
| 246 | + thisArg?: any |
| 247 | + ): void; |
| 248 | +} |
| 249 | + |
| 250 | +export class CollectionReference<T = DocumentData> extends Query<T> { |
| 251 | + readonly id: string; |
| 252 | + readonly path: string; |
| 253 | + withConverter<U>( |
| 254 | + converter: FirestoreDataConverter<U> |
| 255 | + ): CollectionReference<U>; |
| 256 | +} |
| 257 | + |
| 258 | +export function getDoc<T>( |
| 259 | + reference: DocumentReference<T> |
| 260 | +): Promise<DocumentSnapshot<T>>; |
| 261 | +export function getQuery<T>(query: Query<T>): Promise<QuerySnapshot<T>>; |
| 262 | + |
| 263 | +export function addDoc<T>( |
| 264 | + reference: CollectionReference<T>, |
| 265 | + data: T |
| 266 | +): Promise<DocumentSnapshot<T>>; |
| 267 | +export function setDoc<T>( |
| 268 | + reference: DocumentReference<T>, |
| 269 | + data: T |
| 270 | +): Promise<void>; |
| 271 | +export function setDoc<T>( |
| 272 | + reference: DocumentReference<T>, |
| 273 | + data: Partial<T>, |
| 274 | + options: SetOptions |
| 275 | +): Promise<void>; |
| 276 | +export function updateDoc( |
| 277 | + reference: DocumentReference, |
| 278 | + data: UpdateData |
| 279 | +): Promise<void>; |
| 280 | +export function updateDoc( |
| 281 | + field: string | FieldPath, |
| 282 | + value: any, |
| 283 | + ...moreFieldsAndValues: any[] |
| 284 | +): Promise<void>; |
| 285 | +export function deleteDoc(reference: DocumentReference): Promise<void>; |
| 286 | + |
| 287 | +export class FieldValue { |
| 288 | + private constructor(); |
| 289 | + isEqual(other: FieldValue): boolean; |
| 290 | +} |
| 291 | + |
| 292 | +export function serverTimestamp(): FieldValue; |
| 293 | +export function deleteField(): FieldValue; |
| 294 | +export function arrayUnion(...elements: any[]): FieldValue; |
| 295 | +export function arrayRemove(...elements: any[]): FieldValue; |
| 296 | +export function increment(n: number): FieldValue; |
| 297 | + |
| 298 | +export class FieldPath { |
| 299 | + constructor(...fieldNames: string[]); |
| 300 | + isEqual(other: FieldPath): boolean; |
| 301 | +} |
| 302 | + |
| 303 | +export function documentId(): FieldPath; |
| 304 | + |
| 305 | +export function refEqual( |
| 306 | + l: DocumentReference | CollectionReference, |
| 307 | + r: DocumentReference | CollectionReference |
| 308 | +): boolean; |
| 309 | +export function queryEqual(l: Query, r: Query): boolean; |
| 310 | +export function snapshotEqual( |
| 311 | + l: DocumentSnapshot | QuerySnapshot, |
| 312 | + r: DocumentSnapshot | QuerySnapshot |
| 313 | +): boolean; |
| 314 | + |
| 315 | +export type FirestoreErrorCode = |
| 316 | + | 'cancelled' |
| 317 | + | 'unknown' |
| 318 | + | 'invalid-argument' |
| 319 | + | 'deadline-exceeded' |
| 320 | + | 'not-found' |
| 321 | + | 'already-exists' |
| 322 | + | 'permission-denied' |
| 323 | + | 'resource-exhausted' |
| 324 | + | 'failed-precondition' |
| 325 | + | 'aborted' |
| 326 | + | 'out-of-range' |
| 327 | + | 'unimplemented' |
| 328 | + | 'internal' |
| 329 | + | 'unavailable' |
| 330 | + | 'data-loss' |
| 331 | + | 'unauthenticated'; |
| 332 | + |
| 333 | +export interface FirestoreError { |
| 334 | + code: FirestoreErrorCode; |
| 335 | + message: string; |
| 336 | + name: string; |
| 337 | + stack?: string; |
| 338 | +} |
| 339 | + |
| 340 | +declare module '@firebase/component' { |
| 341 | + interface NameServiceMapping { |
| 342 | + 'firestore/lite': FirebaseFirestore; |
| 343 | + } |
| 344 | +} |
0 commit comments