Skip to content

Commit

Permalink
feat(functions): support Cloud Functions locations (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored Jul 1, 2024
1 parent 2df9693 commit 549585d
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 14 deletions.
7 changes: 4 additions & 3 deletions packages/functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ Call a callable function by URL.

#### CallByNameOptions

| Prop | Type | Description | Since |
| ---------- | ------------------- | ---------------------------------- | ----- |
| **`name`** | <code>string</code> | The name of the callable function. | 6.1.0 |
| Prop | Type | Description | Since |
| ------------ | ------------------- | ------------------------------------ | ----- |
| **`name`** | <code>string</code> | The name of the callable function. | 6.1.0 |
| **`region`** | <code>string</code> | The region of the callable function. | 6.1.0 |


#### CallByUrlOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.capawesome.capacitorjs.plugins.firebase.functions;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.capawesome.capacitorjs.plugins.firebase.functions.classes.options.CallByNameOptions;
import io.capawesome.capacitorjs.plugins.firebase.functions.classes.options.CallByUrlOptions;
import io.capawesome.capacitorjs.plugins.firebase.functions.classes.results.CallResult;
Expand All @@ -16,9 +17,10 @@ public FirebaseFunctions(FirebaseFunctionsPlugin plugin) {

public void callByName(@NonNull CallByNameOptions options, @NonNull NonEmptyResultCallback callback) {
String name = options.getName();
String region = options.getRegion();
Object data = options.getData();

getFirebaseFunctionsInstance()
getFirebaseFunctionsInstance(region)
.getHttpsCallable(name)
.call(data)
.addOnSuccessListener(
Expand All @@ -38,7 +40,7 @@ public void callByUrl(@NonNull CallByUrlOptions options, @NonNull NonEmptyResult
String url = options.getUrl();
Object data = options.getData();

getFirebaseFunctionsInstance()
getFirebaseFunctionsInstance(null)
.getHttpsCallable(url)
.call(data)
.addOnSuccessListener(
Expand All @@ -54,7 +56,11 @@ public void callByUrl(@NonNull CallByUrlOptions options, @NonNull NonEmptyResult
);
}

private com.google.firebase.functions.FirebaseFunctions getFirebaseFunctionsInstance() {
return com.google.firebase.functions.FirebaseFunctions.getInstance();
private com.google.firebase.functions.FirebaseFunctions getFirebaseFunctionsInstance(@Nullable String regionOrCustomDomain) {
if (regionOrCustomDomain == null) {
return com.google.firebase.functions.FirebaseFunctions.getInstance();
} else {
return com.google.firebase.functions.FirebaseFunctions.getInstance(regionOrCustomDomain);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ public void load() {
public void callByName(PluginCall call) {
try {
JSObject json = call.getData();
String name = json.getString("name");
String name = json.getString("name", null);
if (name == null) {
call.reject(ERROR_NAME_MISSING);
return;
}
String region = json.getString("region", null);
Object data = json.opt("data");

CallByNameOptions options = new CallByNameOptions(name, data);
CallByNameOptions options = new CallByNameOptions(name, region, data);
NonEmptyResultCallback callback = new NonEmptyResultCallback() {
@Override
public void success(Result result) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package io.capawesome.capacitorjs.plugins.firebase.functions.classes.options;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class CallByNameOptions extends CallOptions {

@NonNull
private String name;

public CallByNameOptions(@NonNull String name, Object data) {
@Nullable
private String region;

public CallByNameOptions(@NonNull String name, @Nullable String region, Object data) {
super(data);
this.name = name;
this.region = region;
}

@NonNull
public String getName() {
return name;
}

@Nullable
public String getRegion() {
return region;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import Foundation

@objc public class CallByNameOptions: CallOptions {
private var name: String
private var region: String?

init(name: String, data: Any?) {
init(name: String, region: String?, data: Any?) {
self.name = name
self.region = region
super.init(data: data)
}

func getName() -> String {
return name
}

func getRegion() -> String? {
return region
}
}
8 changes: 7 additions & 1 deletion packages/functions/ios/Plugin/FirebaseFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ import FirebaseFunctions

@objc public func callByName(_ options: CallByNameOptions, completion: @escaping (Result?, Error?) -> Void) {
let name = options.getName()
let region = options.getRegion()
let data = options.getData()

let functions = Functions.functions()
let functions: Functions
if let region = region {
functions = Functions.functions(region: region)
} else {
functions = Functions.functions()
}
let callable = functions.httpsCallable(name)
callable.call(data) { (result, error) in
if let error = error {
Expand Down
3 changes: 2 additions & 1 deletion packages/functions/ios/Plugin/FirebaseFunctionsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public class FirebaseFunctionsPlugin: CAPPlugin {
call.reject(errorNameMissing)
return
}
let region = call.getString("region")
let data = call.getValue("data")

let options = CallByNameOptions(name: name, data: data)
let options = CallByNameOptions(name: name, region: region, data: data)

implementation?.callByName(options, completion: { result, error in
if let error = error {
Expand Down
7 changes: 7 additions & 0 deletions packages/functions/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export interface CallByNameOptions extends CallOptions {
* @since 6.1.0
*/
name: string;
/**
* The region of the callable function.
*
* @example 'us-central1'
* @since 6.1.0
*/
region?: string;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/functions/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class FirebaseFunctionsWeb
public async callByName(
options: CallByNameOptions,
): Promise<CallByNameResult> {
const functions = getFunctions();
const functions = getFunctions(undefined, options.region);
const callable = httpsCallable(functions, options.name);
const result = await callable(options.data);
return {
Expand Down

0 comments on commit 549585d

Please sign in to comment.