@@ -25,10 +25,15 @@ import * as utils from '../utils/index';
25
25
import * as validator from '../utils/validator' ;
26
26
import { ConnectorConfig , ExecuteGraphqlResponse , GraphqlOptions } from './data-connect-api' ;
27
27
28
- // Data Connect backend constants
29
- const DATA_CONNECT_HOST = 'https://firebasedataconnect.googleapis.com' ;
30
- const DATA_CONNECT_API_URL_FORMAT =
31
- '{host}/v1alpha/projects/{projectId}/locations/{locationId}/services/{serviceId}:{endpointId}' ;
28
+ const API_VERSION = 'v1alpha' ;
29
+
30
+ /** The Firebase Data Connect backend base URL format. */
31
+ const FIREBASE_DATA_CONNECT_BASE_URL_FORMAT =
32
+ 'https://firebasedataconnect.googleapis.com/{version}/projects/{projectId}/locations/{locationId}/services/{serviceId}:{endpointId}' ;
33
+
34
+ /** Firebase Data Connect base URl format when using the Data Connect emultor. */
35
+ const FIREBASE_DATA_CONNECT_EMULATOR_BASE_URL_FORMAT =
36
+ 'http://{host}/{version}/projects/{projectId}/locations/{locationId}/services/{serviceId}:{endpointId}' ;
32
37
33
38
const EXECUTE_GRAPH_QL_ENDPOINT = 'executeGraphql' ;
34
39
const EXECUTE_GRAPH_QL_READ_ENDPOINT = 'executeGraphqlRead' ;
@@ -52,7 +57,7 @@ export class DataConnectApiClient {
52
57
DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
53
58
'First argument passed to getDataConnect() must be a valid Firebase app instance.' ) ;
54
59
}
55
- this . httpClient = new AuthorizedHttpClient ( app as FirebaseApp ) ;
60
+ this . httpClient = new DataConnectHttpClient ( app as FirebaseApp ) ;
56
61
}
57
62
58
63
/**
@@ -101,13 +106,12 @@ export class DataConnectApiClient {
101
106
'GraphqlOptions must be a non-null object' ) ;
102
107
}
103
108
}
104
- const host = ( process . env . DATA_CONNECT_EMULATOR_HOST || DATA_CONNECT_HOST ) ;
105
109
const data = {
106
110
query,
107
111
...( options ?. variables && { variables : options ?. variables } ) ,
108
112
...( options ?. operationName && { operationName : options ?. operationName } ) ,
109
113
} ;
110
- return this . getUrl ( host , this . connectorConfig . location , this . connectorConfig . serviceId , endpoint )
114
+ return this . getUrl ( API_VERSION , this . connectorConfig . location , this . connectorConfig . serviceId , endpoint )
111
115
. then ( async ( url ) => {
112
116
const request : HttpRequestConfig = {
113
117
method : 'POST' ,
@@ -133,18 +137,25 @@ export class DataConnectApiClient {
133
137
} ) ;
134
138
}
135
139
136
- private async getUrl ( host : string , locationId : string , serviceId : string , endpointId : string ) : Promise < string > {
140
+ private async getUrl ( version : string , locationId : string , serviceId : string , endpointId : string ) : Promise < string > {
137
141
return this . getProjectId ( )
138
142
. then ( ( projectId ) => {
139
143
const urlParams = {
140
- host ,
144
+ version ,
141
145
projectId,
142
146
locationId,
143
147
serviceId,
144
148
endpointId
145
149
} ;
146
- const baseUrl = utils . formatString ( DATA_CONNECT_API_URL_FORMAT , urlParams ) ;
147
- return utils . formatString ( baseUrl ) ;
150
+ let urlFormat : string ;
151
+ if ( useEmulator ( ) ) {
152
+ urlFormat = utils . formatString ( FIREBASE_DATA_CONNECT_EMULATOR_BASE_URL_FORMAT , {
153
+ host : emulatorHost ( )
154
+ } ) ;
155
+ } else {
156
+ urlFormat = FIREBASE_DATA_CONNECT_BASE_URL_FORMAT ;
157
+ }
158
+ return utils . formatString ( urlFormat , urlParams ) ;
148
159
} ) ;
149
160
}
150
161
@@ -188,6 +199,34 @@ export class DataConnectApiClient {
188
199
}
189
200
}
190
201
202
+ /**
203
+ * Data Connect-specific HTTP client which uses the special "owner" token
204
+ * when communicating with the Data Connect Emulator.
205
+ */
206
+ class DataConnectHttpClient extends AuthorizedHttpClient {
207
+
208
+ protected getToken ( ) : Promise < string > {
209
+ if ( useEmulator ( ) ) {
210
+ return Promise . resolve ( 'owner' ) ;
211
+ }
212
+
213
+ return super . getToken ( ) ;
214
+ }
215
+
216
+ }
217
+
218
+ function emulatorHost ( ) : string | undefined {
219
+ return process . env . DATA_CONNECT_EMULATOR_HOST
220
+ }
221
+
222
+ /**
223
+ * When true the SDK should communicate with the Data Connect Emulator for all API
224
+ * calls and also produce unsigned tokens.
225
+ */
226
+ export function useEmulator ( ) : boolean {
227
+ return ! ! emulatorHost ( ) ;
228
+ }
229
+
191
230
interface ErrorResponse {
192
231
error ?: ServerError ;
193
232
}
0 commit comments