[functions] useFunctionsEmulator now stores origin in JS to be sent with each httpsCallable for that instance

This commit is contained in:
Salakar
2019-02-10 05:14:58 +00:00
parent 6a18129028
commit 7fd60acce3
5 changed files with 20 additions and 33 deletions

View File

@@ -49,23 +49,11 @@ public class ReactNativeFirebaseFunctionsModule extends ReactNativeFirebaseModul
super(reactContext, TAG);
}
@ReactMethod
public void useFunctionsEmulator(
String appName,
String region,
String origin,
Promise promise
) {
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseFunctions functionsInstance = FirebaseFunctions.getInstance(firebaseApp, region);
functionsInstance.useFunctionsEmulator(origin);
promise.resolve(null);
}
@ReactMethod
public void httpsCallable(
String appName,
String region,
String origin,
String name,
ReadableMap wrapper,
Promise promise
@@ -74,6 +62,11 @@ public class ReactNativeFirebaseFunctionsModule extends ReactNativeFirebaseModul
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseFunctions functionsInstance = FirebaseFunctions.getInstance(firebaseApp, region);
if (origin != null) {
functionsInstance.useFunctionsEmulator(origin);
}
HttpsCallableReference httpsCallableReference = functionsInstance.getHttpsCallable(name);
httpsCallableReference

View File

@@ -35,6 +35,8 @@
(FIRApp *) firebaseApp
region:
(NSString *) region
origin:
(NSString *) origin
name:
(NSString *) name
wrapper:
@@ -45,6 +47,11 @@
(RCTPromiseRejectBlock) reject
) {
FIRFunctions *functions = [FIRFunctions functionsForApp:firebaseApp region:region];
if (origin != nil) {
[functions useFunctionsEmulatorOrigin:origin];
}
FIRHTTPSCallable *callable = [functions HTTPSCallableWithName:name];
[callable callWithObject:[wrapper valueForKey:@"data"] completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
@@ -70,22 +77,6 @@
}];
}
RCT_EXPORT_METHOD(useFunctionsEmulator:
(FIRApp *) firebaseApp
region:
(NSString *) region
origin:
(NSString *) origin
resolver:
(RCTPromiseResolveBlock) resolve
rejecter:
(RCTPromiseRejectBlock) reject
) {
FIRFunctions *functions = [FIRFunctions functionsForApp:firebaseApp region:region];
[functions useFunctionsEmulatorOrigin:origin];
resolve([NSNull null]);
}
- (NSString *)getErrorCodeName:(NSError *)error {
NSString *code = @"UNKNOWN";
switch (error.code) {

View File

@@ -166,7 +166,7 @@ export namespace Functions {
* @param origin the origin string of the local emulator started via firebase tools
* "http://10.0.0.8:1337".
*/
useFunctionsEmulator(origin: string): Promise<void>;
useFunctionsEmulator(origin: string): void;
}
}

View File

@@ -65,11 +65,14 @@ class FirebaseFunctionsModule extends FirebaseModule {
constructor(...args) {
super(...args);
this._customUrlOrRegion = this._customUrlOrRegion || 'us-central1';
this._useFunctionsEmulatorOrigin = null;
}
httpsCallable(name) {
return data => {
const nativePromise = this.native.httpsCallable(name, { data });
const nativePromise = this.native.httpsCallable(this._useFunctionsEmulatorOrigin, name, {
data,
});
return nativePromise.catch(nativeError => {
const { code, message, details } = nativeError.userInfo || {};
return Promise.reject(
@@ -85,7 +88,7 @@ class FirebaseFunctionsModule extends FirebaseModule {
}
useFunctionsEmulator(origin) {
return this.native.useFunctionsEmulator(origin);
this._useFunctionsEmulatorOrigin = origin || null;
}
}

View File

@@ -156,7 +156,7 @@ interface Module extends ReactNativeFirebaseModule {
* @param origin the origin string of the local emulator started via firebase tools
* "http://10.0.0.8:1337".
*/
useFunctionsEmulator(origin: string): Promise<void>;
useFunctionsEmulator(origin: string): void;
}
declare module '@react-native-firebase/functions' {