From 07f0968b996ae0baef013b9cfb4b6370c6930914 Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 10 Feb 2019 05:48:24 +0000 Subject: [PATCH] [types] add 'withApp' module type --- packages/app-types/index.d.ts | 30 +++++++++------- packages/app-types/index.js.flow | 5 +++ packages/functions/lib/index.d.ts | 24 +++++-------- packages/functions/lib/index.js.flow | 25 ++++--------- packages/iid/lib/index.d.ts | 11 +++--- packages/iid/lib/index.js.flow | 54 ++++++++++++++++++++++++---- tests/type-test.ts | 5 ++- 7 files changed, 98 insertions(+), 56 deletions(-) diff --git a/packages/app-types/index.d.ts b/packages/app-types/index.d.ts index c1a3e756..3872451a 100644 --- a/packages/app-types/index.d.ts +++ b/packages/app-types/index.d.ts @@ -15,8 +15,6 @@ * */ -import {FirebaseApp} from "./index"; - export interface NativeFirebaseError extends Error { /** * Firebase error code, e.g. `auth/invalid-email` @@ -44,15 +42,6 @@ export interface NativeFirebaseError extends Error { readonly nativeErrorMessage: string; } -export type ReactNativeFirebaseModuleAndStatics = { - (): M; - - /** - * This React Native Firebase module version. - */ - readonly SDK_VERSION: string; -} & S; - export type FirebaseOptions = { /** * The Google App ID that is used to uniquely identify an instance of an app. @@ -85,7 +74,6 @@ export type FirebaseOptions = { */ storageBucket?: string; - /** * The Project Number from the Google Developer's console, for example "012345678901", used to * configure Google Cloud Messaging. @@ -254,3 +242,21 @@ export namespace App { readonly SDK_VERSION: string; } } + +export type ReactNativeFirebaseModuleAndStatics = { + (): M; + + /** + * This React Native Firebase module version. + */ + readonly SDK_VERSION: string; +} & S; + +export type ReactNativeFirebaseModuleAndStaticsWithApp = { + (app?: FirebaseApp): M; + + /** + * This React Native Firebase module version. + */ + readonly SDK_VERSION: string; +} & S; diff --git a/packages/app-types/index.js.flow b/packages/app-types/index.js.flow index 84f7a226..cb2c0376 100644 --- a/packages/app-types/index.js.flow +++ b/packages/app-types/index.js.flow @@ -19,6 +19,11 @@ export type ReactNativeFirebaseModuleAndStatics = { +SDK_VERSION: string; } & S; +export type ReactNativeFirebaseModuleAndStaticsWithApp = { + (): M; + +SDK_VERSION: string; +} & S; + export type FirebaseOptions = { appId: string; apiKey?: string; diff --git a/packages/functions/lib/index.d.ts b/packages/functions/lib/index.d.ts index 463e9ef8..b8e36926 100644 --- a/packages/functions/lib/index.d.ts +++ b/packages/functions/lib/index.d.ts @@ -17,6 +17,7 @@ import { ReactNativeFirebaseModule, + ReactNativeFirebaseModuleAndStaticsWithApp, ReactNativeFirebaseNamespace, } from '@react-native-firebase/app-types'; @@ -141,7 +142,7 @@ export namespace Functions { /** * Uppercase + underscored variables of @Functions.FunctionsErrorCode */ - HttpsErrorCode: {} & HttpsErrorCode, + HttpsErrorCode: {} & HttpsErrorCode; } /** @@ -185,13 +186,10 @@ declare module '@react-native-firebase/functions' { */ export const firebase: {} & ReactNativeFirebaseNamespace; - const FunctionsDefaultExport: { - (app?: FirebaseApp): Functions.Module; - /** - * This React Native Firebase Functions module version. - */ - readonly SDK_VERSION: string; - } & Functions.Statics; + const FunctionsDefaultExport: ReactNativeFirebaseModuleAndStaticsWithApp< + Functions.Module, + Functions.Statics + >; /** * @example * ```js @@ -213,13 +211,7 @@ declare module '@react-native-firebase/app-types' { * write and deploy an HTTPS Callable function in Cloud Functions, * and then add client logic to call the function from your app. */ - functions: { - (app?: FirebaseApp): Functions.Module; - /** - * This React Native Firebase Functions module version. - */ - readonly SDK_VERSION: string; - } & Functions.Statics; + functions: ReactNativeFirebaseModuleAndStaticsWithApp; } interface FirebaseApp { @@ -228,6 +220,8 @@ declare module '@react-native-firebase/app-types' { * directly from a Firebase app. To call a function from your app in this way, * write and deploy an HTTPS Callable function in Cloud Functions, * and then add client logic to call the function from your app. + * + * @param region The region you deployed your functions to. Defaults to 'us-central1'. */ functions?(region?: string): Functions.Module; } diff --git a/packages/functions/lib/index.js.flow b/packages/functions/lib/index.js.flow index 248d3379..3d47408a 100644 --- a/packages/functions/lib/index.js.flow +++ b/packages/functions/lib/index.js.flow @@ -15,9 +15,7 @@ * */ -import { - ReactNativeFirebaseModule, -} from '@react-native-firebase/app-types/index.js.flow'; +import { ReactNativeFirebaseModule } from '@react-native-firebase/app-types/index.js.flow'; /** * The set of Firebase Functions status codes. The codes are the same at the @@ -160,7 +158,10 @@ interface Module extends ReactNativeFirebaseModule { } declare module '@react-native-firebase/functions' { - import type { FirebaseApp, ReactNativeFirebaseNamespace } from '@react-native-firebase/app-types'; + import type { + ReactNativeFirebaseNamespace, + ReactNativeFirebaseModuleAndStaticsWithApp, + } from '@react-native-firebase/app-types/index.js.flow'; declare export var HttpsErrorCode: {} & HttpsErrorCode; @@ -173,13 +174,7 @@ declare module '@react-native-firebase/functions' { */ declare export var firebase: {} & ReactNativeFirebaseNamespace; - declare var FunctionsDefaultExport: { - (app?: FirebaseApp): Module, - /** - * This React Native Firebase Functions module version. - */ - SDK_VERSION: string, - } & Statics; + declare var FunctionsDefaultExport: ReactNativeFirebaseModuleAndStaticsWithApp; /** * @example * ```js @@ -201,13 +196,7 @@ declare module '@react-native-firebase/app-types' { * write and deploy an HTTPS Callable function in Cloud Functions, * and then add client logic to call the function from your app. */ - functions: { - (app?: FirebaseApp): Module, - /** - * This React Native Firebase Functions module version. - */ - SDK_VERSION: string, - } & Statics; + functions: ReactNativeFirebaseModuleAndStaticsWithApp; } declare interface FirebaseApp { diff --git a/packages/iid/lib/index.d.ts b/packages/iid/lib/index.d.ts index c0a02d6d..727abcf0 100644 --- a/packages/iid/lib/index.d.ts +++ b/packages/iid/lib/index.d.ts @@ -18,7 +18,7 @@ import { ReactNativeFirebaseModule, ReactNativeFirebaseNamespace, - ReactNativeFirebaseModuleAndStatics, + ReactNativeFirebaseModuleAndStaticsWithApp, } from '@react-native-firebase/app-types'; /** @@ -69,7 +69,10 @@ export namespace Iid { } declare module '@react-native-firebase/iid' { - import { ReactNativeFirebaseNamespace } from '@react-native-firebase/app-types'; + import { + ReactNativeFirebaseModuleAndStaticsWithApp, + ReactNativeFirebaseNamespace +} from '@react-native-firebase/app-types'; const FirebaseNamespaceExport: {} & ReactNativeFirebaseNamespace; @@ -82,7 +85,7 @@ declare module '@react-native-firebase/iid' { */ export const firebase = FirebaseNamespaceExport; - const IidDefaultExport: ReactNativeFirebaseModuleAndStatics; + const IidDefaultExport: ReactNativeFirebaseModuleAndStaticsWithApp; /** * @example * ```js @@ -105,7 +108,7 @@ declare module '@react-native-firebase/app-types' { * An Instance ID is long lived except when you call delete, the app is restored on a new device, the user * uninstalls/reinstall the app or the user clears the app data (clearing data applies to Android only). */ - iid: ReactNativeFirebaseModuleAndStatics; + iid: ReactNativeFirebaseModuleAndStaticsWithApp; } interface FirebaseApp { diff --git a/packages/iid/lib/index.js.flow b/packages/iid/lib/index.js.flow index 5c6bcf49..7ed55b93 100644 --- a/packages/iid/lib/index.js.flow +++ b/packages/iid/lib/index.js.flow @@ -21,13 +21,43 @@ import type { ReactNativeFirebaseModule } from '@react-native-firebase/app-types export interface Statics {} export interface Module extends ReactNativeFirebaseModule { + /** + * Returns a identifier that uniquely identifies the app instance. + * + * Once an Instance ID is generated, Firebase periodically sends information about the application + * and the device it's running on to the Firebase backend. To stop this, see `delete()`. + */ + get(): Promise; + /** + * Delete the Instance ID and all data associated with it. This stops the periodic sending of data to the Firebase + * backend that was started when the Instance ID was generated. + * + * A new Instance ID is asynchronously generated unless auto initialisation is turned off. + */ + delete(): Promise; + + /** + * Returns a token that authorizes an Entity to perform an action on behalf of the application. + * + * @param authorizedEntity Entity authorized by the token. Defaults to the apps `messagingSenderId` option. + * @param scope Action authorized for authorizedEntity. Defaults to '*'. + */ + getToken(authorizedEntity?: string, scope?: string): Promise; + + /** + * Revokes access to a scope for an entity previously authorized by `getToken()`. + * + * @param authorizedEntity Entity authorized by the token. Defaults to the apps' `messagingSenderId` option. + * @param scope Action authorized for authorizedEntity. Defaults to '*'. + */ + deleteToken(authorizedEntity?: string, scope?: string): Promise; } declare module '@react-native-firebase/iid' { import type { ReactNativeFirebaseNamespace, - ReactNativeFirebaseModuleAndStatics, + ReactNativeFirebaseModuleAndStaticsWithApp, } from '@react-native-firebase/app-types/index.js.flow'; /** * @example @@ -45,25 +75,37 @@ declare module '@react-native-firebase/iid' { * iid().X(...); * ``` */ - declare export default ReactNativeFirebaseModuleAndStatics; + declare export default ReactNativeFirebaseModuleAndStaticsWithApp; } /** * Attach namespace to `firebase.` and `FirebaseApp.`. */ declare module '@react-native-firebase/app-types' { - import type { ReactNativeFirebaseModuleAndStatics } from '@react-native-firebase/app-types/index.js.flow'; + import type { ReactNativeFirebaseModuleAndStaticsWithApp } from '@react-native-firebase/app-types/index.js.flow'; declare interface ReactNativeFirebaseNamespace { /** - * Iid + * Firebase Instance ID provides a unique identifier for each instance of your app and a mechanism to authenticate + * and authorize actions for it (for example: sending FCM messages). + * + * An Instance ID is long lived except when you call delete, the app is restored on a new device, the user + * uninstalls/reinstall the app or the user clears the app data (clearing data applies to Android only). + * + * @firebase iid */ - iid: ReactNativeFirebaseModuleAndStatics; + iid: ReactNativeFirebaseModuleAndStaticsWithApp; } declare interface FirebaseApp { /** - * Iid + * Firebase Instance ID provides a unique identifier for each instance of your app and a mechanism to authenticate + * and authorize actions for it (for example: sending FCM messages). + * + * An Instance ID is long lived except when you call delete, the app is restored on a new device, the user + * uninstalls/reinstall the app or the user clears the app data (clearing data applies to Android only). + * + * @firebase iid */ iid(): Module; } diff --git a/tests/type-test.ts b/tests/type-test.ts index 3412e6cf..3f1dcc75 100644 --- a/tests/type-test.ts +++ b/tests/type-test.ts @@ -1,4 +1,5 @@ import firebase from 'react-native-firebase'; +import '@react-native-firebase/iid'; import analytics, { Analytics } from '@react-native-firebase/analytics'; import functions, { firebase as boopy, @@ -6,13 +7,15 @@ import functions, { HttpsErrorCode, } from '@react-native-firebase/functions'; -firebase.app; boopy.apps[0].options.projectId; analytics.SDK_VERSION; functions.SDK_VERSION; const httpsCallable = firebase.functions(firebase.app()).httpsCallable('foo'); functions; +firebase.iid().get(); +firebase.analytics().resetAnalyticsData(); + httpsCallable({ foo: 1 }) .then(result => { result.data;