/* * Copyright (c) 2016-present Invertase Limited & Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this library except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import type { ReactNativeFirebase } from '@react-native-firebase/app'; import type { FieldPath } from '../FieldPath'; import type { FieldValue } from '../FieldValue'; import type { AggregateField } from '../FirestoreAggregate'; import type { Query as QueryClass } from '../FirestoreQuery'; import type { sum, average, count } from '../index'; // Canonical app/module aliases used by modular declarations. export type FirebaseApp = ReactNativeFirebase.FirebaseApp; export declare class Firestore { /** * Whether it's a Firestore or Firestore Lite instance. */ type: 'firestore'; /** * The FirebaseApp associated with this Firestore instance. */ get app(): FirebaseApp; /** * Returns a JSON-serializable representation of this Firestore instance. */ toJSON(): object; } export type LogLevel = 'debug' | 'error' | 'silent'; // web/other platform only export interface ExperimentalLongPollingOptions { timeoutSeconds?: number; } // web/other platform only export interface PersistenceSettings { forceOwnership?: boolean; } // web/other platform only export type MemoryLocalCache = { kind: 'memory'; _onlineComponentProvider: unknown; _offlineComponentProvider: unknown; }; // web/other platform only export type PersistentLocalCache = { kind: 'persistent'; _onlineComponentProvider: unknown; _offlineComponentProvider: unknown; }; // web/other platform only export type FirestoreLocalCache = MemoryLocalCache | PersistentLocalCache; export interface FirestoreSettings { host?: string; ssl?: boolean; ignoreUndefinedProperties?: boolean; cacheSizeBytes?: number; // web/other platform only localCache?: FirestoreLocalCache; // web/other platform only experimentalForceLongPolling?: boolean; // web/other platform only experimentalAutoDetectLongPolling?: boolean; // web/other platform only experimentalLongPollingOptions?: ExperimentalLongPollingOptions; // React Native native SDK-specific settings. persistence?: boolean; serverTimestampBehavior?: 'estimate' | 'previous' | 'none'; } export type AggregateType = 'count' | 'avg' | 'sum'; export type AggregateFieldType = | ReturnType | ReturnType | ReturnType; export interface AggregateSpec { [field: string]: AggregateFieldType; } export type AggregateSpecData = { [P in keyof T]: T[P] extends AggregateField ? U : never; }; /** * Provider identifier for Firebase Auth (from @firebase/util). * Must match the union used by EmulatorMockTokenOptions / FirebaseIdToken. */ export type FirebaseSignInProvider = | 'custom' | 'email' | 'password' | 'phone' | 'anonymous' | 'google.com' | 'facebook.com' | 'github.com' | 'twitter.com' | 'microsoft.com' | 'apple.com'; /** * Shape of a decoded Firebase ID token (JWT) used for emulator mock auth. * @see https://firebase.google.com/docs/reference/js/auth#firebaseidtoken */ export interface FirebaseIdToken { iss: string; aud: string; sub: string; iat: number; exp: number; user_id: string; auth_time: number; provider_id?: 'anonymous'; email?: string; email_verified?: boolean; phone_number?: string; name?: string; picture?: string; firebase: { sign_in_provider: FirebaseSignInProvider; identities?: { [provider in FirebaseSignInProvider]?: string[] }; }; [claim: string]: unknown; uid?: never; } /** * Options for mock auth token when using the Firestore emulator. * Must include either `user_id` or `sub`; may include other partial ID token claims. */ export type EmulatorMockTokenOptions = ({ user_id: string } | { sub: string }) & Partial; export type TaskState = 'Error' | 'Running' | 'Success'; export interface LoadBundleTaskProgress { documentsLoaded: number; totalDocuments: number; bytesLoaded: number; totalBytes: number; taskState: TaskState; } export declare class LoadBundleTask implements PromiseLike { onProgress( next?: (progress: LoadBundleTaskProgress) => unknown, error?: (err: Error) => unknown, complete?: () => void, ): void; catch(onRejected: (a: Error) => R | PromiseLike): Promise; then( onFulfilled?: (a: LoadBundleTaskProgress) => T | PromiseLike, onRejected?: (a: Error) => R | PromiseLike, ): Promise; } export type SetOptions = | { readonly merge?: boolean; } | { readonly mergeFields?: Array; }; export type WhereFilterOp = | '<' | '<=' | '==' | '!=' | '>=' | '>' | 'array-contains' | 'in' | 'array-contains-any' | 'not-in'; export type OrderByDirection = 'desc' | 'asc'; export type QueryConstraintType = | 'where' | 'orderBy' | 'limit' | 'limitToLast' | 'startAt' | 'startAfter' | 'endAt' | 'endBefore'; /** * Describe the source a query listens to. * * Set to `default` to listen to both cache and server changes. Set to `cache` * to listen to changes in cache only. */ export type ListenSource = 'default' | 'cache'; export interface SnapshotListenOptions { readonly includeMetadataChanges?: boolean; readonly source?: ListenSource; } /** * Options that configure how data is retrieved from a `DocumentSnapshot` (for * example the desired behavior for server timestamps that have not yet been set * to their final value). */ export declare interface SnapshotOptions { /** * If set, controls the return value for server timestamps that have not yet * been set to their final value. * * By specifying 'estimate', pending server timestamps return an estimate * based on the local clock. This estimate will differ from the final value * and cause these values to change once the server result becomes available. * * By specifying 'previous', pending timestamps will be ignored and return * their previous value instead. * * If omitted or set to 'none', `null` will be returned by default until the * server value becomes available. */ readonly serverTimestamps?: 'estimate' | 'previous' | 'none'; } export declare class SnapshotMetadata { readonly fromCache: boolean; readonly hasPendingWrites: boolean; isEqual(other: SnapshotMetadata): boolean; } export type DocumentChangeType = 'added' | 'removed' | 'modified'; export type FirestoreError = Error; export interface Unsubscribe { (): void; } export interface DocumentChange< AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData, > { readonly type: DocumentChangeType; readonly doc: QueryDocumentSnapshot; readonly oldIndex: number; readonly newIndex: number; } // Re-exported helpers mirrored from modular declarations. export type Primitive = string | number | boolean | undefined | null; export interface DocumentData { [key: string]: any; } export type PartialWithFieldValue = | Partial | (T extends Primitive ? T : T extends {} ? { [K in keyof T]?: PartialWithFieldValue | FieldValue } : never); /** * Given a union type `U = T1 | T2 | ...`, returns an intersected type * `(T1 & T2 & ...)`. * * Uses distributive conditional types and inference from conditional types. * This works because multiple candidates for the same type variable in * contra-variant positions causes an intersection type to be inferred. * https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-inference-in-conditional-types * https://stackoverflow.com/questions/50374908/transform-union-type-to-intersection-type */ export declare type UnionToIntersection = (U extends unknown ? (k: U) => void : never) extends ( k: infer I, ) => void ? I : never; /** * For the given type, return a union type of T and the types of all child * properties of T. Matches firebase-js-sdk UpdateData helper typing. */ export type ChildTypes = T extends Record ? { [K in keyof T & string]: ChildTypes }[keyof T & string] | T : T; /** * Returns a new map where every key is prefixed with the outer key appended * to a dot. Index signatures widen via PartialWithFieldValue of ChildTypes. */ export type AddPrefixToKeys> = { [K in keyof T & string as `${Prefix}.${K}`]+?: string extends K ? PartialWithFieldValue> | FieldValue : T[K]; }; export type ChildUpdateFields = V extends Record ? AddPrefixToKeys> : never; /** * For each field (e.g. 'bar'), find all nested keys (e.g. {'bar.baz': T1, * 'bar.qux': T2}). Intersect them together to make a single map containing * all possible keys that are all marked as optional. */ export type NestedUpdateFields = UnionToIntersection< { [K in keyof T & string]: string extends K ? never : ChildUpdateFields; }[keyof T & string] >; /** * Update data (for use with updateDoc) that consists of field paths mapped to * values. Index-signature fields widen via PartialWithFieldValue of ChildTypes. */ export type UpdateData = T extends Primitive ? T : T extends {} ? { [K in keyof T]?: string extends K ? PartialWithFieldValue> | FieldValue : UpdateData | FieldValue; } & NestedUpdateFields : Partial; export type WithFieldValue = | T | (T extends Primitive ? T : T extends {} ? { [K in keyof T]: WithFieldValue | FieldValue } : never); export interface FirestoreDataConverter< AppModelType, DbModelType extends DocumentData = DocumentData, > { toFirestore(modelObject: WithFieldValue): WithFieldValue; toFirestore( modelObject: PartialWithFieldValue, options: SetOptions, ): PartialWithFieldValue; fromFirestore( snapshot: QueryDocumentSnapshot, options?: SnapshotOptions, ): AppModelType; } export interface Query< AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData, > { readonly converter: FirestoreDataConverter | null; readonly type: 'query' | 'collection'; readonly firestore: Firestore; withConverter(converter: null): Query; withConverter( converter: FirestoreDataConverter, ): Query; } export declare class CollectionReference< AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData, > extends QueryClass { readonly type: 'collection'; id: string; parent: DocumentReference | null; path: string; withConverter(converter: null): CollectionReference; withConverter( converter: FirestoreDataConverter, ): CollectionReference; } export declare class DocumentReference< AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData, > { readonly type: 'document'; firestore: Firestore; converter: FirestoreDataConverter | null; id: string; parent: CollectionReference; path: string; withConverter(converter: null): DocumentReference; withConverter( converter: FirestoreDataConverter, ): DocumentReference; } export declare class WriteBatch { set( documentRef: DocumentReference, data: WithFieldValue, ): WriteBatch; set( documentRef: DocumentReference, data: PartialWithFieldValue, options: SetOptions, ): WriteBatch; update( documentRef: DocumentReference, data: UpdateData, ): WriteBatch; update( documentRef: DocumentReference, field: string | FieldPath, value: unknown, ...moreFieldsAndValues: unknown[] ): WriteBatch; update( documentRef: DocumentReference, fieldOrUpdateData: string | FieldPath | UpdateData, value?: unknown, ...moreFieldsAndValues: unknown[] ): WriteBatch; delete( documentRef: DocumentReference, ): WriteBatch; commit(): Promise; } export interface TransactionOptions { readonly maxAttempts?: number; } export declare class LiteTransaction {} export declare class Transaction extends LiteTransaction { get( documentRef: DocumentReference, ): Promise>; set( documentRef: DocumentReference, data: WithFieldValue, ): this; set( documentRef: DocumentReference, data: PartialWithFieldValue, options: SetOptions, ): this; update( documentRef: DocumentReference, data: UpdateData, ): this; update( documentRef: DocumentReference, field: string | FieldPath, value: unknown, ...moreFieldsAndValues: unknown[] ): this; update( documentRef: DocumentReference, fieldOrUpdateData: string | FieldPath | UpdateData, value?: unknown, ...moreFieldsAndValues: unknown[] ): this; delete( documentRef: DocumentReference, ): this; } export declare class DocumentSnapshot< AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData, > { readonly metadata: SnapshotMetadata; exists(): this is QueryDocumentSnapshot; data(options?: SnapshotOptions): AppModelType | undefined; get(fieldPath: string | FieldPath, options?: SnapshotOptions): any; get id(): string; get ref(): DocumentReference; } export declare class QueryDocumentSnapshot< AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData, > extends DocumentSnapshot { data(options?: SnapshotOptions): AppModelType; } export declare class QuerySnapshot< AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData, > { readonly metadata: SnapshotMetadata; readonly query: Query; get docs(): Array>; get size(): number; get empty(): boolean; forEach( callback: (result: QueryDocumentSnapshot) => void, thisArg?: unknown, ): void; docChanges(options?: SnapshotListenOptions): Array>; }