2018-05-06 13:21:38 +00:00
|
|
|
#import "RNFirebaseFunctions.h"
|
|
|
|
|
|
|
|
#if __has_include(<FirebaseFunctions/FIRFunctions.h>)
|
|
|
|
#import <FirebaseFunctions/FIRFunctions.h>
|
2018-05-06 14:36:03 +00:00
|
|
|
#import <FirebaseFunctions/FIRHTTPSCallable.h>
|
|
|
|
#import <FirebaseFunctions/FIRError.h>
|
2018-05-06 13:21:38 +00:00
|
|
|
|
|
|
|
@implementation RNFirebaseFunctions
|
|
|
|
RCT_EXPORT_MODULE();
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(httpsCallable:
|
2018-05-06 14:36:03 +00:00
|
|
|
(NSString *) name
|
2018-05-06 13:21:38 +00:00
|
|
|
wrapper:
|
2018-05-06 14:36:03 +00:00
|
|
|
(NSDictionary *) wrapper
|
2018-05-06 13:21:38 +00:00
|
|
|
resolver:
|
|
|
|
(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:
|
|
|
|
(RCTPromiseRejectBlock) reject
|
|
|
|
) {
|
2018-05-06 14:36:03 +00:00
|
|
|
FIRFunctions *functions = [FIRFunctions functions];
|
|
|
|
|
|
|
|
[[functions HTTPSCallableWithName:name] callWithObject:[wrapper valueForKey:@"data"] completion:^(FIRHTTPSCallableResult * _Nullable result, NSError * _Nullable error) {
|
|
|
|
if (error) {
|
|
|
|
NSObject *details = [NSNull null];
|
|
|
|
NSString *message = error.localizedDescription;
|
|
|
|
if (error.domain == FIRFunctionsErrorDomain) {
|
|
|
|
details = error.userInfo[FIRFunctionsErrorDetailsKey];
|
|
|
|
if (details == nil) {
|
|
|
|
details = [NSNull null];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(@{
|
|
|
|
@"__error": @true,
|
|
|
|
@"code": [self getErrorCodeName:error],
|
|
|
|
@"message": message,
|
|
|
|
@"details": details
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve(@{ @"data": [result data] });
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)getErrorCodeName:(NSError *)error {
|
|
|
|
NSString *code = @"UNKNOWN";
|
|
|
|
switch (error.code) {
|
|
|
|
case FIRFunctionsErrorCodeOK:
|
|
|
|
code = @"OK";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeCancelled:
|
|
|
|
code = @"CANCELLED";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeUnknown:
|
|
|
|
code = @"UNKNOWN";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeInvalidArgument:
|
|
|
|
code = @"INVALID_ARGUMENT";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeDeadlineExceeded:
|
|
|
|
code = @"DEADLINE_EXCEEDED";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeNotFound:
|
|
|
|
code = @"NOT_FOUND";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeAlreadyExists:
|
|
|
|
code = @"ALREADY_EXISTS";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodePermissionDenied:
|
|
|
|
code = @"PERMISSION_DENIED";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeResourceExhausted:
|
|
|
|
code = @"RESOURCE_EXHAUSTED";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeFailedPrecondition:
|
|
|
|
code = @"FAILED_PRECONDITION";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeAborted:
|
|
|
|
code = @"ABORTED";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeOutOfRange:
|
|
|
|
code = @"OUT_OF_RANGE";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeUnimplemented:
|
|
|
|
code = @"UNIMPLEMENTED";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeInternal:
|
|
|
|
code = @"INTERNAL";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeUnavailable:
|
|
|
|
code = @"UNAVAILABLE";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeDataLoss:
|
|
|
|
code = @"DATA_LOSS";
|
|
|
|
break;
|
|
|
|
case FIRFunctionsErrorCodeUnauthenticated:
|
|
|
|
code = @"UNAUTHENTICATED";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return code;
|
2018-05-06 13:21:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#else
|
|
|
|
@implementation RNFirebaseFunctions
|
|
|
|
@end
|
|
|
|
#endif
|
|
|
|
|