From fbc47c5cf25ac827726adb0823bbf38769b4061d Mon Sep 17 00:00:00 2001 From: Salakar Date: Mon, 13 Aug 2018 23:17:53 +0100 Subject: [PATCH] [functions][js] add support for multiple firebase apps and specifying a function region --- src/index.d.ts | 22 ++++++++++++--- src/modules/functions/index.js | 49 ++++++++++++++++++++++++++++------ 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 00a3f17d..4f6357c3 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -974,7 +974,10 @@ declare module 'react-native-firebase' { /** * Asynchronously signs in using a phone number. */ - signInWithPhoneNumber(phoneNumber: string, forceResend?: boolean): Promise; + signInWithPhoneNumber( + phoneNumber: string, + forceResend?: boolean + ): Promise; /** * Returns a PhoneAuthListener to listen to phone verification events, @@ -984,7 +987,7 @@ declare module 'react-native-firebase' { verifyPhoneNumber( phoneNumber: string, autoVerifyTimeoutOrForceResend?: number | boolean, - forceResend?: boolean, + forceResend?: boolean ): PhoneAuthListener; /** @@ -1663,7 +1666,7 @@ declare module 'react-native-firebase' { /** * Return an object of key-value attributes. */ - getAttributes(): Promise + getAttributes(): Promise; /** * Set an attribute. Returns true if it was set, false if it was not. @@ -1919,6 +1922,19 @@ declare module 'react-native-firebase' { * @return The `HttpsCallable` instance. */ httpsCallable(name: string): HttpsCallable; + + /** + * Changes this instance to point to a Cloud Functions emulator running + * locally. + * + * See https://firebase.google.com/docs/functions/local-emulator + * + * @param origin the origin string of the local emulator started via firebase tools + * "http://10.0.0.8:1337". + */ + useFunctionsEmulator(origin: string): Promise; + + [key: string]: any; } /** diff --git a/src/modules/functions/index.js b/src/modules/functions/index.js index bf4ac63a..74122146 100644 --- a/src/modules/functions/index.js +++ b/src/modules/functions/index.js @@ -7,6 +7,8 @@ import { isObject } from '../../utils'; import { getNativeModule } from '../../utils/native'; import type App from '../core/app'; +import firebase from '../core/firebase'; + import HttpsError from './HttpsError'; import type { @@ -39,13 +41,28 @@ function errorOrResult(possibleError): HttpsCallablePromise { } export default class Functions extends ModuleBase { - constructor(app: App) { - super(app, { - multiApp: false, - hasShards: false, - namespace: NAMESPACE, - moduleName: MODULE_NAME, - }); + _region: string; + + constructor(appOrRegion: App, region: string = 'us-central1') { + let _app = appOrRegion; + let _region = region; + + if (typeof appOrRegion === 'string') { + _app = firebase.app(); + _region = appOrRegion; + } + + super( + _app, + { + hasMultiAppSupport: true, + hasCustomUrlSupport: false, + hasRegionsSupport: true, + namespace: NAMESPACE, + moduleName: MODULE_NAME, + }, + _region + ); } /** @@ -60,10 +77,26 @@ export default class Functions extends ModuleBase { */ httpsCallable(name: string): HttpsCallable { return (data?: any): HttpsCallablePromise => { - const promise = getNativeModule(this).httpsCallable(name, { data }); + const promise = getNativeModule(this).httpsCallable(this._region, name, { + data, + }); + return promise.then(errorOrResult); }; } + + /** + * Changes this instance to point to a Cloud Functions emulator running + * locally. + * + * See https://firebase.google.com/docs/functions/local-emulator + * + * @param origin the origin string of the local emulator started via firebase tools + * "http://10.0.0.8:1337". + */ + useFunctionsEmulator(origin: string): Promise { + return getNativeModule(this).useFunctionsEmulator(origin); + } } export const statics: { HttpsErrorCode: HttpsErrorCode } = {