3
3
HttpsCallable ,
4
4
HttpsCallableOptions ,
5
5
HttpsErrorCode ,
6
- IFunctions ,
6
+ IFunctions
7
7
} from "./common" ;
8
8
import { deserialize , firebase , FirebaseApp , serialize } from "@nativescript/firebase-core" ;
9
9
@@ -23,6 +23,34 @@ Object.defineProperty(fb, 'functions', {
23
23
} ,
24
24
writable : false ,
25
25
} ) ;
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
+ } ) ;
26
54
27
55
function errorToCode ( error : NSError ) {
28
56
let code = HttpsErrorCode . UNKNOWN ;
@@ -115,13 +143,29 @@ export class Functions implements IFunctions {
115
143
116
144
constructor ( app ?: FirebaseApp ) {
117
145
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
+ }
119
155
} else {
120
156
if ( defaultFunctions ) {
121
157
return defaultFunctions ;
122
158
}
123
159
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
+ }
125
169
}
126
170
}
127
171
@@ -178,3 +222,20 @@ export class Functions implements IFunctions {
178
222
return this . #app;
179
223
}
180
224
}
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