33 HttpsCallable ,
44 HttpsCallableOptions ,
55 HttpsErrorCode ,
6- IFunctions ,
6+ IFunctions
77} from "./common" ;
88import { deserialize , firebase , FirebaseApp , serialize } from "@nativescript/firebase-core" ;
99
@@ -23,6 +23,34 @@ Object.defineProperty(fb, 'functions', {
2323 } ,
2424 writable : false ,
2525} ) ;
26+ /**
27+ Firebase Functions Region - Region for which to run HttpsCallable method
28+ Set parameter using firebase().app().functions(regionOrCustomDomain: string)
29+ @link https://firebase.google.com/docs/reference/node/firebase.app.App
30+ @link https://firebase.google.com/docs/reference/ios/firebasefunctions/api/reference/Classes/FIRFunctions
31+ @note If not set, default region is used ('us-central1')
32+ */
33+ let defaultRegionOrCustomDomain : string ;
34+ /**
35+ Add 'functions' method to FirebaseApp class
36+ @param regionOrCustomDomain(string)(Optional): Name of the Region or Custom Domain for which to Functions results
37+ @return Functions
38+ @see FirebaseFunctions
39+ @link https://firebase.google.com/docs/reference/ios/firebasefunctions/api/reference/Classes/FIRFunctions
40+ @see Supported Regions
41+ @see https://firebase.google.com/docs/functions/locations
42+ */
43+ const fbApp = FirebaseApp ;
44+ Object . defineProperty ( fbApp . prototype , 'functions' , {
45+ value : ( regionOrCustomDomain ?: string ) => {
46+ defaultRegionOrCustomDomain = regionOrCustomDomain ;
47+ if ( ! defaultFunctions ) {
48+ defaultFunctions = new Functions ( ) ;
49+ }
50+ return defaultFunctions ;
51+ } ,
52+ writable : false ,
53+ } ) ;
2654
2755function errorToCode ( error : NSError ) {
2856 let code = HttpsErrorCode . UNKNOWN ;
@@ -115,13 +143,29 @@ export class Functions implements IFunctions {
115143
116144 constructor ( app ?: FirebaseApp ) {
117145 if ( app ?. native ) {
118- this . #native = FIRFunctions . functionsForApp ( app . native ) ;
146+ if ( defaultRegionOrCustomDomain ) {
147+ this . #native = isRegion ( defaultRegionOrCustomDomain ) // Check whether a Region has been set
148+ ? FIRFunctions . functionsForAppRegion ( app . native , defaultRegionOrCustomDomain ) // @see https://firebase.google.com/docs/reference/ios/firebasefunctions/api/reference/Classes/FIRFunctions#+functionsforapp:region:
149+ : isCustomDomain ( defaultRegionOrCustomDomain ) // Check whether using a Custom Domain has been set
150+ ? FIRFunctions . functionsForAppCustomDomain ( app . native , defaultRegionOrCustomDomain ) // @see https://firebase.google.com/docs/reference/ios/firebasefunctions/api/reference/Classes/FIRFunctions#+functionsforapp:customdomain:
151+ : FIRFunctions . functionsForApp ( app . native ) ; // @see https://firebase.google.com/docs/reference/ios/firebasefunctions/api/reference/Classes/FIRFunctions#+functionsforapp:
152+ } else {
153+ this . #native = FIRFunctions . functionsForApp ( app . native ) ; // @see https://firebase.google.com/docs/reference/ios/firebasefunctions/api/reference/Classes/FIRFunctions#+functionsforapp:
154+ }
119155 } else {
120156 if ( defaultFunctions ) {
121157 return defaultFunctions ;
122158 }
123159 defaultFunctions = this ;
124- this . #native = FIRFunctions . functions ( ) ;
160+ if ( defaultRegionOrCustomDomain ) {
161+ this . #native = isRegion ( defaultRegionOrCustomDomain ) // Check whether a Region has been set
162+ ? FIRFunctions . functionsForRegion ( defaultRegionOrCustomDomain ) // @see https://firebase.google.com/docs/reference/ios/firebasefunctions/api/reference/Classes/FIRFunctions#+functionsforregion:
163+ : isCustomDomain ( defaultRegionOrCustomDomain ) // Check whether using a Custom Domain has been set
164+ ? FIRFunctions . functionsForCustomDomain ( defaultRegionOrCustomDomain ) // @see https://firebase.google.com/docs/reference/ios/firebasefunctions/api/reference/Classes/FIRFunctions#+functionsforcustomdomain:
165+ : FIRFunctions . functions ( ) ; // @see https://firebase.google.com/docs/reference/ios/firebasefunctions/api/reference/Classes/FIRFunctions#+functions
166+ } else {
167+ this . #native = FIRFunctions . functions ( ) ; // @see https://firebase.google.com/docs/reference/ios/firebasefunctions/api/reference/Classes/FIRFunctions#+functions
168+ }
125169 }
126170 }
127171
@@ -178,3 +222,20 @@ export class Functions implements IFunctions {
178222 return this . #app;
179223 }
180224}
225+ /**
226+ Check whether a regionOrCustomDomain string is a Region for the http trigger, such as “us-central1”.
227+ @param regionOrCustomDomain(string): Text to parse
228+ @return boolean: TRUE if a Region; FALSE if not
229+ */
230+ function isRegion ( regionOrCustomDomain : string ) : boolean {
231+ const elems = regionOrCustomDomain . split ( '.' ) ;
232+ return elems . length === 1 ? true : false ;
233+ }
234+ /**
235+ Check whether a regionOrCustomDomain string is a Custom Domain for the http trigger, such as “https://mydomain.com”
236+ @param regionOrCustomDomain(string): Text to parse
237+ @return boolean: TRUE if a CustomDomain; FALSE if not
238+ */
239+ function isCustomDomain ( regionOrCustomDomain : string ) : boolean {
240+ return ! isRegion ( regionOrCustomDomain ) ;
241+ }
0 commit comments