[functions][js] add support for multiple firebase apps and specifying a function region

This commit is contained in:
Salakar 2018-08-13 23:17:53 +01:00
parent 7632da1809
commit fbc47c5cf2
2 changed files with 60 additions and 11 deletions

22
src/index.d.ts vendored
View File

@ -974,7 +974,10 @@ declare module 'react-native-firebase' {
/** /**
* Asynchronously signs in using a phone number. * Asynchronously signs in using a phone number.
*/ */
signInWithPhoneNumber(phoneNumber: string, forceResend?: boolean): Promise<ConfirmationResult>; signInWithPhoneNumber(
phoneNumber: string,
forceResend?: boolean
): Promise<ConfirmationResult>;
/** /**
* Returns a PhoneAuthListener to listen to phone verification events, * Returns a PhoneAuthListener to listen to phone verification events,
@ -984,7 +987,7 @@ declare module 'react-native-firebase' {
verifyPhoneNumber( verifyPhoneNumber(
phoneNumber: string, phoneNumber: string,
autoVerifyTimeoutOrForceResend?: number | boolean, autoVerifyTimeoutOrForceResend?: number | boolean,
forceResend?: boolean, forceResend?: boolean
): PhoneAuthListener; ): PhoneAuthListener;
/** /**
@ -1663,7 +1666,7 @@ declare module 'react-native-firebase' {
/** /**
* Return an object of key-value attributes. * Return an object of key-value attributes.
*/ */
getAttributes(): Promise<Object> getAttributes(): Promise<Object>;
/** /**
* Set an attribute. Returns true if it was set, false if it was not. * 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. * @return The `HttpsCallable` instance.
*/ */
httpsCallable(name: string): HttpsCallable; 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<null>;
[key: string]: any;
} }
/** /**

View File

@ -7,6 +7,8 @@ import { isObject } from '../../utils';
import { getNativeModule } from '../../utils/native'; import { getNativeModule } from '../../utils/native';
import type App from '../core/app'; import type App from '../core/app';
import firebase from '../core/firebase';
import HttpsError from './HttpsError'; import HttpsError from './HttpsError';
import type { import type {
@ -39,13 +41,28 @@ function errorOrResult(possibleError): HttpsCallablePromise {
} }
export default class Functions extends ModuleBase { export default class Functions extends ModuleBase {
constructor(app: App) { _region: string;
super(app, {
multiApp: false, constructor(appOrRegion: App, region: string = 'us-central1') {
hasShards: false, 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, namespace: NAMESPACE,
moduleName: MODULE_NAME, moduleName: MODULE_NAME,
}); },
_region
);
} }
/** /**
@ -60,10 +77,26 @@ export default class Functions extends ModuleBase {
*/ */
httpsCallable(name: string): HttpsCallable { httpsCallable(name: string): HttpsCallable {
return (data?: any): HttpsCallablePromise => { return (data?: any): HttpsCallablePromise => {
const promise = getNativeModule(this).httpsCallable(name, { data }); const promise = getNativeModule(this).httpsCallable(this._region, name, {
data,
});
return promise.then(errorOrResult); 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<null> {
return getNativeModule(this).useFunctionsEmulator(origin);
}
} }
export const statics: { HttpsErrorCode: HttpsErrorCode } = { export const statics: { HttpsErrorCode: HttpsErrorCode } = {