[functions][android][js] implementation - wip

This commit is contained in:
Salakar 2018-05-04 16:33:59 +01:00
parent 8dee086f1f
commit 368b3ad068
6 changed files with 188 additions and 5 deletions

View File

@ -102,6 +102,7 @@ dependencies {
compileOnly "com.google.firebase:firebase-ads:$firebaseVersion"
compileOnly "com.google.firebase:firebase-firestore:$firebaseVersion"
compileOnly "com.google.firebase:firebase-invites:$firebaseVersion"
compileOnly "com.google.firebase:firebase-functions:$firebaseVersion"
compileOnly('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
transitive = true
}

View File

@ -21,7 +21,6 @@ import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
@SuppressWarnings("WeakerAccess")
public class Utils {
private static final String TAG = "Utils";
@ -64,6 +63,10 @@ public class Utils {
Long longVal = (Long) value;
map.putDouble(key, (double) longVal);
break;
case "java.lang.Float":
float floatVal = (float) value;
map.putDouble(key, (double) floatVal);
break;
case "java.lang.Double":
map.putDouble(key, (Double) value);
break;
@ -71,14 +74,12 @@ public class Utils {
map.putString(key, (String) value);
break;
default:
map.putString(key, null);
map.putNull(key);
}
}
/**
*
* @param map
* @return
*/

View File

@ -0,0 +1,130 @@
package io.invertase.firebase.functions;
import android.support.annotation.NonNull;
import android.util.Log;
import android.support.annotation.Nullable;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReadableNativeArray;
import com.facebook.react.bridge.WritableMap;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.functions.FirebaseFunctions;
import com.google.firebase.functions.FirebaseFunctionsException;
import com.google.firebase.functions.HttpsCallableReference;
import com.google.firebase.functions.HttpsCallableResult;
import java.util.List;
import java.util.Map;
import io.invertase.firebase.Utils;
public class RNFirebaseFunctions extends ReactContextBaseJavaModule {
private static final String TAG = "RNFirebaseFunctions";
public RNFirebaseFunctions(ReactApplicationContext reactContext) {
super(reactContext);
Log.d(TAG, "New instance");
}
/**
* @return
*/
@Override
public String getName() {
return TAG;
}
@ReactMethod
public void httpsCallable(final String name, @Nullable final Object data, final Promise promise) {
Object input;
if (data == null
|| data instanceof String
|| data instanceof Boolean
|| data instanceof Integer
|| data instanceof Long
|| data instanceof Float) {
input = data;
} else if (data instanceof ReadableArray) {
input = ((ReadableArray) data).toArrayList();
} else if (data instanceof ReadableMap) {
input = ((ReadableMap) data).toHashMap();
} else {
input = null;
}
HttpsCallableReference httpsCallableReference = FirebaseFunctions
.getInstance()
.getHttpsCallable(name);
httpsCallableReference
.call(input)
.addOnSuccessListener(new OnSuccessListener<HttpsCallableResult>() {
@Override
public void onSuccess(HttpsCallableResult httpsCallableResult) {
Log.d(TAG, "function:call:onSuccess:" + name);
WritableMap map = Arguments.createMap();
Object result = httpsCallableResult.getData();
if (result == null
|| result instanceof String
|| result instanceof Boolean
|| result instanceof Integer
|| result instanceof Long
|| result instanceof Float) {
Utils.mapPutValue("data", result, map);
} else if (result instanceof List) {
map.putArray("data", Arguments.makeNativeArray((List<Object>) result));
} else if (result instanceof Map) {
map.putMap("data", Arguments.makeNativeMap((Map<String, Object>) result));
} else {
// TODO check for other instance types e.g. ArrayList ?
map.putNull("data");
}
promise.resolve(map);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.d(TAG, "function:call:onFailure:" + name, exception);
if (exception instanceof FirebaseFunctionsException) {
FirebaseFunctionsException ffe = (FirebaseFunctionsException) exception;
/*
OK(0),
CANCELLED(1),
UNKNOWN(2),
INVALID_ARGUMENT(3),
DEADLINE_EXCEEDED(4),
NOT_FOUND(5),
ALREADY_EXISTS(6),
PERMISSION_DENIED(7),
RESOURCE_EXHAUSTED(8),
FAILED_PRECONDITION(9),
ABORTED(10),
OUT_OF_RANGE(11),
UNIMPLEMENTED(12),
INTERNAL(13),
UNAVAILABLE(14),
DATA_LOSS(15),
UNAUTHENTICATED(16);
*/
FirebaseFunctionsException.Code code = ffe.getCode();
String message = ffe.getMessage();
Object details = ffe.getDetails();
// TODO promise resolve so we can send details
}
}
});
}
}

View File

@ -0,0 +1,37 @@
package io.invertase.firebase.functions;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.bridge.ReactApplicationContext;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
@SuppressWarnings("unused")
public class RNFirebaseFunctionsPackage implements ReactPackage {
public RNFirebaseFunctionsPackage() {
}
/**
* @param reactContext react application context that can be used to create modules
* @return list of native modules to register with the newly created catalyst instance
*/
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new RNFirebaseFunctions(reactContext));
return modules;
}
/**
* @param reactContext
* @return a list of view managers that should be registered with {@link UIManagerModule}
*/
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}

View File

@ -10,7 +10,11 @@ import type App from '../core/app';
export const MODULE_NAME = 'RNFirebaseFunctions';
export const NAMESPACE = 'functions';
type HttpsCallable = (data?: any) => Promise<any>;
type HttpsCallableResult = {
data: Object,
};
type HttpsCallable = (data?: any) => Promise<HttpsCallableResult>;
export default class Analytics extends ModuleBase {
constructor(app: App) {

View File

@ -15,6 +15,8 @@ import type Database from '../modules/database';
import { typeof statics as DatabaseStatics } from '../modules/database';
import type Firestore from '../modules/firestore';
import { typeof statics as FirestoreStatics } from '../modules/firestore';
import type Functions from '../modules/functions';
import { typeof statics as FunctionsStatics } from '../modules/functions';
import type InstanceId from '../modules/iid';
import { typeof statics as InstanceIdStatics } from '../modules/iid';
import type Invites from '../modules/invites';
@ -63,6 +65,7 @@ export type FirebaseModuleName =
| 'RNFirebaseCrashlytics'
| 'RNFirebaseDatabase'
| 'RNFirebaseFirestore'
| 'RNFirebaseFunctions'
| 'RNFirebaseInstanceId'
| 'RNFirebaseInvites'
| 'RNFirebaseLinks'
@ -171,6 +174,13 @@ export type FirestoreModule = {
nativeModuleExists: boolean,
} & FirestoreStatics;
/* Functions types */
export type FunctionsModule = {
(): Functions,
nativeModuleExists: boolean,
} & FunctionsStatics;
/* InstanceId types */
export type InstanceIdModule = {