@@ -86,17 +86,21 @@ export class Service implements FirebaseFunctions, FirebaseService {
86
86
private emulatorOrigin : string | null = null ;
87
87
private cancelAllRequests : Promise < void > ;
88
88
private deleteService ! : ( ) => void ;
89
+ private region : string ;
90
+ private customDomain : string | null ;
89
91
90
92
/**
91
- * Creates a new Functions service for the given app and (optional) region.
93
+ * Creates a new Functions service for the given app and (optional) region or custom domain .
92
94
* @param app_ The FirebaseApp to use.
93
- * @param region_ The region to call functions in.
95
+ * @param regionOrCustomDomain_ one of:
96
+ * a) A region to call functions from, such as us-central1
97
+ * b) A custom domain to use as a functions prefix, such as https://mydomain.com
94
98
*/
95
99
constructor (
96
100
private app_ : FirebaseApp ,
97
101
authProvider : Provider < FirebaseAuthInternalName > ,
98
102
messagingProvider : Provider < FirebaseMessagingName > ,
99
- private region_ : string = 'us-central1' ,
103
+ regionOrCustomDomain_ : string = 'us-central1' ,
100
104
readonly fetchImpl : typeof fetch
101
105
) {
102
106
this . contextProvider = new ContextProvider ( authProvider , messagingProvider ) ;
@@ -106,6 +110,16 @@ export class Service implements FirebaseFunctions, FirebaseService {
106
110
return resolve ( ) ;
107
111
} ;
108
112
} ) ;
113
+
114
+ // Resolve the region or custom domain overload by attempting to parse it.
115
+ try {
116
+ const url = new URL ( regionOrCustomDomain_ ) ;
117
+ this . customDomain = url . origin ;
118
+ this . region = 'us-central1' ;
119
+ } catch ( e ) {
120
+ this . customDomain = null ;
121
+ this . region = regionOrCustomDomain_ ;
122
+ }
109
123
}
110
124
111
125
get app ( ) : FirebaseApp {
@@ -124,12 +138,16 @@ export class Service implements FirebaseFunctions, FirebaseService {
124
138
*/
125
139
_url ( name : string ) : string {
126
140
const projectId = this . app_ . options . projectId ;
127
- const region = this . region_ ;
128
141
if ( this . emulatorOrigin !== null ) {
129
142
const origin = this . emulatorOrigin ;
130
- return `${ origin } /${ projectId } /${ region } /${ name } ` ;
143
+ return `${ origin } /${ projectId } /${ this . region } /${ name } ` ;
131
144
}
132
- return `https://${ region } -${ projectId } .cloudfunctions.net/${ name } ` ;
145
+
146
+ if ( this . customDomain !== null ) {
147
+ return `${ this . customDomain } /${ name } ` ;
148
+ }
149
+
150
+ return `https://${ this . region } -${ projectId } .cloudfunctions.net/${ name } ` ;
133
151
}
134
152
135
153
/**
0 commit comments