mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-22 19:57:51 +08:00
[functions] useFunctionsEmulator now stores origin in JS to be sent with each httpsCallable for that instance
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
2
packages/functions/lib/index.d.ts
vendored
2
packages/functions/lib/index.d.ts
vendored
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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' {
|
||||
|
||||
Reference in New Issue
Block a user