From 2bd4a667a99412f7e83810fce91eb3b6afa5f05b Mon Sep 17 00:00:00 2001 From: Paul Huynh Date: Mon, 29 Jan 2018 16:14:06 +1100 Subject: [PATCH 1/3] Fix TS definitions so modules can be accessed from the default export --- lib/index.d.ts | 161 ++++++++++++++++++++++++++----------------------- 1 file changed, 85 insertions(+), 76 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 8d300c6f..b18ebd4c 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -5,88 +5,68 @@ declare module "react-native-firebase" { - type AuthProvider = { - PROVIDER_ID: string, - credential: (token: string, secret?: string) => AuthCredential, + type FirebaseModuleAndStatics = { + (): M; + nativeModuleExists: boolean; + } & S + + // Modules commented-out do not currently have type definitions + export class Firebase { + private constructor(); + // admob: FirebaseModuleAndStatics; + analytics: FirebaseModuleAndStatics; + auth: FirebaseModuleAndStatics; + // config: FirebaseModule; + crash: FirebaseModuleAndStatics; + database: FirebaseModuleAndStatics; + fabric: { + crashlytics: FirebaseModuleAndStatics; }; + // firestore: FirebaseModuleAndStatics; + links: FirebaseModuleAndStatics; + messaging: FirebaseModuleAndStatics; + // perf: FirebaseModuleAndStatics; + storage: FirebaseModuleAndStatics; + // utils: FirebaseModuleAndStatics; + initializeApp(options: Firebase.Options, name: string): App; + app(name?: string): App; + apps(): App[]; + SDK_VERSION(): string; + } + namespace Firebase { + interface Options { + apiKey: string; + appId: string; + databaseURL: string; + messagingSenderId: string; + projectId: string; + storageBucket: string; + } + } + const firebase: Firebase; + export default firebase; - export default class FireBase { - constructor(config?: RNFirebase.configurationOptions) - - log: any; - + // Modules commented-out do not currently have type definitions + export class App { + private constructor(); + // admob(): RNFirebase.admob.AdMob; analytics(): RNFirebase.Analytics; - - on(type: string, handler: (msg: any) => void): any; - - database: { - (): RNFirebase.database.Database - ServerValue: { - TIMESTAMP: number - } - }; - - auth: { - (): RNFirebase.auth.Auth - EmailAuthProvider: AuthProvider, - PhoneAuthProvider: AuthProvider, - GoogleAuthProvider: AuthProvider, - GithubAuthProvider: AuthProvider, - TwitterAuthProvider: AuthProvider, - FacebookAuthProvider: AuthProvider, - PhoneAuthState: { - CODE_SENT: string, - AUTO_VERIFY_TIMEOUT: string, - AUTO_VERIFIED: string, - ERROR: string, - }, - }; - - /**RNFirebase mimics the Web Firebase SDK Storage, - * whilst providing some iOS and Android specific functionality. - */ - storage(): RNFirebase.storage.Storage; - - /** - * Firebase Cloud Messaging (FCM) allows you to send push messages at no cost to both Android & iOS platforms. - * Assuming the installation instructions have been followed, FCM is ready to go. - * As the Firebase Web SDK has limited messaging functionality, - * the following methods within react-native-firebase have been created to handle FCM in the React Native environment. - */ - messaging(): RNFirebase.messaging.Messaging; - - /** - * RNFirebase provides crash reporting for your app out of the box. - * Please note crashes do not appear in real-time on the console, - * they tend to take a number of hours to appear - * If you want to manually report a crash, - * such as a pre-caught exception this is possible by using the report method. - */ + auth(): RNFirebase.auth.Auth; + // config(): RNFirebase.config.Config; crash(): RNFirebase.crash.Crash; - - /** - * Firebase Dynamic Links are links that work the way you want, on multiple - * platforms, and whether or not your app is already installed. - * See the official Firebase docs: - * https://firebase.google.com/docs/dynamic-links/ - */ - links(): RNFirebase.links.Links; - - static fabric: { - crashlytics(): RNFirebase.crashlytics.Crashlytics; + database(): RNFirebase.database.Database; + fabric: { + crashlytics(): RNFirebase.crashlytics.Crashlytics, }; - - apps: Array; - googleApiAvailability: RNFirebase.GoogleApiAvailabilityType; - - static initializeApp(options?: any | RNFirebase.configurationOptions, name?: string): FireBase; - - static app(name?: string): FireBase; - - [key: string]: any; + // firestore(): RNFirebase.firestore.Firestore; + links(): RNFirebase.links.Links; + messaging(): RNFirebase.messaging.Messaging; + // perf(): RNFirebase.perf.Performance; + storage(): RNFirebase.storage.Storage; + // utils(): RNFirebase.utils.Utils; } - namespace RNFirebase { + export namespace RNFirebase { interface RnError extends Error { code?: string; } @@ -484,6 +464,15 @@ declare module "react-native-firebase" { update(values: Object, onComplete?: (a: RnError | null) => any): Promise; } + + interface DatabaseStatics { + /** @see https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/ServerValue.html#TIMESTAMP */ + ServerValue: { + TIMESTAMP: { + [key: string]: string + } + } + } } /** @@ -643,7 +632,7 @@ declare module "react-native-firebase" { } /** 3rd party provider Credentials */ - type AuthCredential { + type AuthCredential = { providerId: string, token: string, secret: string @@ -711,6 +700,11 @@ declare module "react-native-firebase" { user: object | null } | null; + type AuthProvider = { + PROVIDER_ID: string, + credential: (token: string, secret?: string) => AuthCredential, + }; + interface Auth { /** * Returns the current Firebase authentication state. @@ -828,6 +822,21 @@ declare module "react-native-firebase" { [key: string]: any; } + + interface AuthStatics { + EmailAuthProvider: AuthProvider; + PhoneAuthProvider: AuthProvider; + GoogleAuthProvider: AuthProvider; + GithubAuthProvider: AuthProvider; + TwitterAuthProvider: AuthProvider; + FacebookAuthProvider: AuthProvider; + PhoneAuthState: { + CODE_SENT: string; + AUTO_VERIFY_TIMEOUT: string; + AUTO_VERIFIED: string; + ERROR: string; + }; + } } namespace messaging { From 7f394b56e597f6cfc919cdea941dcafe03218a53 Mon Sep 17 00:00:00 2001 From: Paul Huynh Date: Tue, 30 Jan 2018 10:44:57 +1100 Subject: [PATCH 2/3] Remove implicit 'any' type on QuerySnapshot.forEach() --- lib/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 8624fbd3..f5be87fd 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1270,7 +1270,7 @@ declare module "react-native-firebase" { readonly metadata: Types.SnapshotMetadata; readonly query: Query; readonly size: number; - forEach(callback: (snapshot: DocumentSnapshot) => any); + forEach(callback: (snapshot: DocumentSnapshot) => any): void; } namespace QuerySnapshot { interface NativeData { From 90195769fdbcdc1edad0ee7ce96a1542d73530f0 Mon Sep 17 00:00:00 2001 From: Paul Huynh Date: Tue, 30 Jan 2018 21:14:35 +1100 Subject: [PATCH 3/3] Fix implicit 'any' and incorrect placement of optional boolean in getIdToken --- lib/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index f5be87fd..3082f7a4 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -583,7 +583,7 @@ declare module "react-native-firebase" { * * @param forceRefresh: boolean - default to false */ - getIdToken(forceRefresh: boolean?): Promise + getIdToken(forceRefresh?: boolean): Promise /** * Link the user with a 3rd party credential provider. @@ -1041,7 +1041,7 @@ declare module "react-native-firebase" { * Returns an unsubscribe function, call the returned function to * unsubscribe from all future events. */ - onLink(listener: (url) => void): () => void; + onLink(listener: (url: string) => void): () => void; } /**