-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathdata-connect-api-client-internal.ts
More file actions
691 lines (636 loc) · 23.2 KB
/
Copy pathdata-connect-api-client-internal.ts
File metadata and controls
691 lines (636 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/*!
* @license
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { App } from '../app';
import { FirebaseApp } from '../app/firebase-app';
import {
HttpRequestConfig, HttpClient, RequestResponseError, AuthorizedHttpClient
} from '../utils/api-request';
import { FirebaseError, toHttpResponse } from '../utils/error';
import {
FirebaseDataConnectError,
DataConnectErrorCode,
DATA_CONNECT_ERROR_CODE_MAPPING,
EMULATOR_GRPC_STATUS_CODE_TO_STRING
} from './error';
import * as utils from '../utils/index';
import * as validator from '../utils/validator';
import { ConnectorConfig, ExecuteGraphqlResponse, GraphqlOptions, OperationOptions } from './data-connect-api';
const API_VERSION = 'v1';
const FIREBASE_DATA_CONNECT_PROD_URL = 'https://firebasedataconnect.googleapis.com';
/** The Firebase Data Connect backend service URL format. */
const FIREBASE_DATA_CONNECT_SERVICES_URL_FORMAT =
FIREBASE_DATA_CONNECT_PROD_URL +
'/{version}' +
'/projects/{projectId}' +
'/locations/{locationId}' +
'/services/{serviceId}' +
':{endpointId}';
/** The Firebase Data Connect backend connector URL format. */
const FIREBASE_DATA_CONNECT_CONNECTORS_URL_FORMAT =
FIREBASE_DATA_CONNECT_PROD_URL +
'/{version}' +
'/projects/{projectId}' +
'/locations/{locationId}' +
'/services/{serviceId}' +
'/connectors/{connectorId}' +
':{endpointId}';
/** Firebase Data Connect service URL format when using the Data Connect emulator. */
const FIREBASE_DATA_CONNECT_EMULATOR_SERVICES_URL_FORMAT =
'http://{host}/{version}/projects/{projectId}/locations/{locationId}/services/{serviceId}:{endpointId}';
/** Firebase Data Connect connector URL format when using the Data Connect emulator. */
const FIREBASE_DATA_CONNECT_EMULATOR_CONNECTORS_URL_FORMAT =
'http://{host}/{version}/projects/{projectId}/locations/{locationId}/services/{serviceId}/connectors/{connectorId}:{endpointId}';
const EXECUTE_GRAPH_QL_ENDPOINT = 'executeGraphql';
const EXECUTE_GRAPH_QL_READ_ENDPOINT = 'executeGraphqlRead';
const IMPERSONATE_QUERY_ENDPOINT = 'impersonateQuery';
const IMPERSONATE_MUTATION_ENDPOINT = 'impersonateMutation';
function getHeaders(isUsingGen: boolean): { [key: string]: string } {
const headerValue = {
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`,
'X-Goog-Api-Client': utils.getMetricsHeader(),
};
if (isUsingGen) {
headerValue['X-Goog-Api-Client'] += ' admin-js/gen';
}
return headerValue;
}
/**
* URL params for requests to an endpoint under services:
* .../services/{serviceId}:endpoint
*/
interface ServicesUrlParams {
version: string;
projectId: string;
locationId: string;
serviceId: string;
endpointId: string;
host?: string; // Present only when using the emulator
}
/**
* URL params for requests to an endpoint under connectors:
* .../services/{serviceId}/connectors/{connectorId}:endpoint
*/
interface ConnectorsUrlParams extends ServicesUrlParams {
connectorId: string;
}
/**
* Class that facilitates sending requests to the Firebase Data Connect backend API.
*
* @internal
*/
export class DataConnectApiClient {
private readonly httpClient: HttpClient;
private projectId?: string;
private isUsingGen = false;
constructor(private readonly connectorConfig: ConnectorConfig, private readonly app: App) {
if (!validator.isNonNullObject(app) || !('options' in app)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: 'First argument passed to getDataConnect() must be a valid Firebase app instance.'
});
}
this.httpClient = new DataConnectHttpClient(app as FirebaseApp);
}
/**
* Update whether the SDK is using a generated one or not.
* @param isUsingGen
*/
setIsUsingGen(isUsingGen: boolean): void {
this.isUsingGen = isUsingGen;
}
/**
* Execute arbitrary GraphQL, including both read and write queries
*
* @param query - The GraphQL string to be executed.
* @param options - GraphQL Options
* @returns A promise that fulfills with a `ExecuteGraphqlResponse`.
*/
public async executeGraphql<GraphqlResponse, Variables>(
query: string,
options?: GraphqlOptions<Variables>,
): Promise<ExecuteGraphqlResponse<GraphqlResponse>> {
return this.executeGraphqlHelper(query, EXECUTE_GRAPH_QL_ENDPOINT, options);
}
/**
* Execute arbitrary read-only GraphQL queries
*
* @param query - The GraphQL (read-only) string to be executed.
* @param options - GraphQL Options
* @returns A promise that fulfills with a `ExecuteGraphqlResponse`.
* @throws FirebaseDataConnectError
*/
public async executeGraphqlRead<GraphqlResponse, Variables>(
query: string,
options?: GraphqlOptions<Variables>,
): Promise<ExecuteGraphqlResponse<GraphqlResponse>> {
return this.executeGraphqlHelper(query, EXECUTE_GRAPH_QL_READ_ENDPOINT, options);
}
/**
* A helper function to execute GraphQL queries.
*
* @param query - The arbitrary GraphQL query to execute.
* @param endpoint - The endpoint to call.
* @param options - The GraphQL options.
* @returns A promise that fulfills with the GraphQL response, or throws an error.
*/
private async executeGraphqlHelper<GraphqlResponse, Variables>(
query: string,
endpoint: string,
options?: GraphqlOptions<Variables>,
): Promise<ExecuteGraphqlResponse<GraphqlResponse>> {
if (!validator.isNonEmptyString(query)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: '`query` must be a non-empty string.'
});
}
if (typeof options !== 'undefined') {
if (!validator.isNonNullObject(options)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: 'GraphqlOptions must be a non-null object'
});
}
}
const data = {
query,
...(options?.variables && { variables: options?.variables }),
...(options?.operationName && { operationName: options?.operationName }),
...(options?.impersonate && { extensions: { impersonate: options?.impersonate } }),
};
const url = await this.getServicesUrl(
API_VERSION,
this.connectorConfig.location,
this.connectorConfig.serviceId,
endpoint
);
try {
const resp = await this.makeGqlRequest<GraphqlResponse>(url, data);
return resp;
} catch (err: any) {
throw this.toFirebaseError(err);
}
}
/**
* Executes a GraphQL query with impersonation.
*
* @param options - The GraphQL options. Must include impersonation details.
* @returns A promise that fulfills with the GraphQL response.
*/
public async executeQuery<GraphqlResponse, Variables>(
name: string,
variables: Variables,
options?: OperationOptions
): Promise<ExecuteGraphqlResponse<GraphqlResponse>> {
return this.executeOperationHelper(IMPERSONATE_QUERY_ENDPOINT, name, variables, options);
}
/**
* Executes a GraphQL mutation with impersonation.
*
* @param options - The GraphQL options. Must include impersonation details.
* @returns A promise that fulfills with the GraphQL response.
*/
public async executeMutation<GraphqlResponse, Variables>(
name: string,
variables: Variables,
options?: OperationOptions
): Promise<ExecuteGraphqlResponse<GraphqlResponse>> {
return this.executeOperationHelper(IMPERSONATE_MUTATION_ENDPOINT, name, variables, options);
}
/**
* A helper function to execute operations by making requests to FDC's impersonate
* operations endpoints.
*
* @param endpoint - The endpoint to call.
* @param options - The GraphQL options, including impersonation details.
* @returns A promise that fulfills with the GraphQL response.
*/
private async executeOperationHelper<GraphqlResponse, Variables>(
endpoint: string,
name: string,
variables: Variables,
options?: OperationOptions
): Promise<ExecuteGraphqlResponse<GraphqlResponse>> {
if (
typeof name === 'undefined' ||
!validator.isNonEmptyString(name)
) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: '`name` must be a non-empty string.'
});
}
if (this.connectorConfig.connector === undefined || this.connectorConfig.connector === '') {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: `The 'connectorConfig.connector' field used to instantiate your Data Connect
instance must be a non-empty string (the connectorId) when calling executeQuery or executeMutation.`
});
}
const data = {
...(variables && { variables: variables }),
operationName: name,
extensions: { impersonate: options?.impersonate },
};
const url = await this.getConnectorsUrl(
API_VERSION,
this.connectorConfig.location,
this.connectorConfig.serviceId,
this.connectorConfig.connector,
endpoint,
);
try {
const resp = await this.makeGqlRequest<GraphqlResponse>(url, data);
return resp;
} catch (err: any) {
throw this.toFirebaseError(err);
}
}
/**
* Constructs the URL for a Data Connect request to a service endpoint.
*
* @param version - The API version.
* @param locationId - The location of the Data Connect service.
* @param serviceId - The ID of the Data Connect service.
* @param endpointId - The endpoint to call.
* @returns A promise which resolves to the formatted URL string.
*/
private async getServicesUrl(
version: string,
locationId: string,
serviceId: string,
endpointId: string,
): Promise<string> {
const projectId = await this.getProjectId();
const params: ServicesUrlParams = {
version,
projectId,
locationId,
serviceId,
endpointId,
};
let urlFormat = FIREBASE_DATA_CONNECT_SERVICES_URL_FORMAT;
if (useEmulator()) {
urlFormat = FIREBASE_DATA_CONNECT_EMULATOR_SERVICES_URL_FORMAT;
params.host = emulatorHost();
}
return utils.formatString(urlFormat, params);
}
/**
* Constructs the URL for a Data Connect request to a connector endpoint.
*
* @param version - The API version.
* @param locationId - The location of the Data Connect service.
* @param serviceId - The ID of the Data Connect service.
* @param connectorId - The ID of the Connector.
* @param endpointId - The endpoint to call.
* @returns A promise which resolves to the formatted URL string.
*/
private async getConnectorsUrl(
version: string,
locationId: string,
serviceId: string,
connectorId: string,
endpointId: string,
): Promise<string> {
const projectId = await this.getProjectId();
const params: ConnectorsUrlParams = {
version,
projectId,
locationId,
serviceId,
connectorId,
endpointId,
};
let urlFormat = FIREBASE_DATA_CONNECT_CONNECTORS_URL_FORMAT;
if (useEmulator()) {
urlFormat = FIREBASE_DATA_CONNECT_EMULATOR_CONNECTORS_URL_FORMAT;
params.host = emulatorHost();
}
return utils.formatString(urlFormat, params);
}
private getProjectId(): Promise<string> {
if (this.projectId) {
return Promise.resolve(this.projectId);
}
return utils.findProjectId(this.app)
.then((projectId) => {
if (!validator.isNonEmptyString(projectId)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.UNKNOWN,
message: 'Failed to determine project ID. Initialize the '
+ 'SDK with service account credentials or set project ID as an app option. '
+ 'Alternatively, set the GOOGLE_CLOUD_PROJECT environment variable.'
});
}
this.projectId = projectId;
return projectId;
});
}
/**
* Makes a GraphQL request to the specified url.
*
* @param url - The URL to send the request to.
* @param data - The GraphQL request payload.
* @returns A promise that fulfills with the GraphQL response, or throws an error.
*/
private async makeGqlRequest<GraphqlResponse>(url: string, data: object):
Promise<ExecuteGraphqlResponse<GraphqlResponse>> {
const request: HttpRequestConfig = {
method: 'POST',
url,
headers: getHeaders(this.isUsingGen),
data,
};
const resp = await this.httpClient.send(request);
if (resp.data.errors && validator.isNonEmptyArray(resp.data.errors)) {
const allMessages = resp.data.errors.map((error: { message: any; }) => error.message).join(' ');
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.QUERY_ERROR,
message: allMessages,
httpResponse: toHttpResponse(resp),
});
}
return Promise.resolve({
data: resp.data.data as GraphqlResponse,
});
}
private toFirebaseError(err: RequestResponseError): FirebaseError {
if (err instanceof FirebaseError) {
return err;
}
const response = err.response;
if (!response.isJson()) {
return new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.UNKNOWN,
message: `Unexpected response with status: ${response.status} and body: ${response.text}`,
httpResponse: toHttpResponse(response),
cause: err
});
}
const data = response.data as any;
const error: ServerError = (validator.isNonNullObject(data) && validator.isNonNullObject(data.error))
? data.error
: (validator.isNonNullObject(data) ? data : {});
let status = error.status;
if (!status && validator.isNumber(error.code)) {
status = EMULATOR_GRPC_STATUS_CODE_TO_STRING[error.code as number];
}
let code: DataConnectErrorCode = DATA_CONNECT_ERROR_CODE_MAPPING.UNKNOWN;
if (status && status in DATA_CONNECT_ERROR_CODE_MAPPING) {
code = DATA_CONNECT_ERROR_CODE_MAPPING[status];
}
const message = error.message || 'Unknown server error';
return new FirebaseDataConnectError({
code,
message,
httpResponse: toHttpResponse(response),
cause: err,
});
}
/**
* Converts JSON data into a GraphQL literal string.
* Handles nested objects, arrays, strings, numbers, and booleans.
* Ensures strings are properly escaped.
*/
private objectToString(data: unknown): string {
if (typeof data === 'string') {
return JSON.stringify(data);
}
if (typeof data === 'number' || typeof data === 'boolean' || data === null) {
return String(data);
}
if (validator.isArray(data)) {
const elements = data.map(item => this.objectToString(item)).join(', ');
return `[${elements}]`;
}
if (typeof data === 'object' && data !== null) {
// Filter out properties where the value is undefined BEFORE mapping
const kvPairs = Object.entries(data)
.filter(([, val]) => val !== undefined)
.map(([key, val]) => {
// GraphQL object keys are typically unquoted.
return `${key}: ${this.objectToString(val)}`;
});
if (kvPairs.length === 0) {
return '{}'; // Represent an object with no defined properties as {}
}
return `{ ${kvPairs.join(', ')} }`;
}
// If value is undefined (and not an object property, which is handled above,
// e.g., if objectToString(undefined) is called directly or for an array element)
// it should be represented as 'null'.
if (typeof data === 'undefined') {
return 'null';
}
// Fallback for any other types (e.g., Symbol, BigInt - though less common in GQL contexts)
// Consider how these should be handled or if an error should be thrown.
// For now, simple string conversion.
return String(data);
}
private formatTableName(tableName: string): string {
// Format tableName: first character to lowercase
if (tableName && tableName.length > 0) {
return tableName.charAt(0).toLowerCase() + tableName.slice(1);
}
return tableName;
}
private handleBulkImportErrors(err: FirebaseDataConnectError): never {
if (err.code === `data-connect/${DATA_CONNECT_ERROR_CODE_MAPPING.QUERY_ERROR}`){
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.QUERY_ERROR,
message: `${err.message}. Make sure that your table name passed in matches the type name in your `
+ 'GraphQL schema file.',
cause: err,
});
}
throw err;
}
/**
* Insert a single row into the specified table.
*/
public async insert<GraphQlResponse, Variables extends object>(
tableName: string,
data: Variables,
): Promise<ExecuteGraphqlResponse<GraphQlResponse>> {
if (!validator.isNonEmptyString(tableName)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: '`tableName` must be a non-empty string.'
});
}
if (validator.isArray(data)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: '`data` must be an object, not an array, for single insert. For arrays, please use '
+ '`insertMany` function.'
});
}
if (!validator.isNonNullObject(data)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: '`data` must be a non-null object.'
});
}
try {
tableName = this.formatTableName(tableName);
const gqlDataString = this.objectToString(data);
const mutation = `mutation { ${tableName}_insert(data: ${gqlDataString}) }`;
// Use internal executeGraphql
return this.executeGraphql<GraphQlResponse, Variables>(mutation).catch(this.handleBulkImportErrors);
} catch (e: any) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INTERNAL,
message: `Failed to construct insert mutation: ${e.message}`,
cause: e,
});
}
}
/**
* Insert multiple rows into the specified table.
*/
public async insertMany<GraphQlResponse, Variables extends Array<unknown>>(
tableName: string,
data: Variables,
): Promise<ExecuteGraphqlResponse<GraphQlResponse>> {
if (!validator.isNonEmptyString(tableName)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: '`tableName` must be a non-empty string.'
});
}
if (!validator.isNonEmptyArray(data)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: '`data` must be a non-empty array for insertMany.',
});
}
try {
tableName = this.formatTableName(tableName);
const gqlDataString = this.objectToString(data);
const mutation = `mutation { ${tableName}_insertMany(data: ${gqlDataString}) }`;
// Use internal executeGraphql
return this.executeGraphql<GraphQlResponse, Variables>(mutation).catch(this.handleBulkImportErrors);
} catch (e: any) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INTERNAL,
message: `Failed to construct insertMany mutation: ${e.message}`,
cause: e,
});
}
}
/**
* Insert a single row into the specified table, or update it if it already exists.
*/
public async upsert<GraphQlResponse, Variables extends object>(
tableName: string,
data: Variables,
): Promise<ExecuteGraphqlResponse<GraphQlResponse>> {
if (!validator.isNonEmptyString(tableName)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: '`tableName` must be a non-empty string.'
});
}
if (validator.isArray(data)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: '`data` must be an object, not an array, for single upsert. For arrays, please use '
+ '`upsertMany` function.'
});
}
if (!validator.isNonNullObject(data)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: '`data` must be a non-null object.'
});
}
try {
tableName = this.formatTableName(tableName);
const gqlDataString = this.objectToString(data);
const mutation = `mutation { ${tableName}_upsert(data: ${gqlDataString}) }`;
// Use internal executeGraphql
return this.executeGraphql<GraphQlResponse, Variables>(mutation).catch(this.handleBulkImportErrors);
} catch (e: any) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INTERNAL,
message: `Failed to construct upsert mutation: ${e.message}`,
cause: e,
});
}
}
/**
* Insert multiple rows into the specified table, or update them if they already exist.
*/
public async upsertMany<GraphQlResponse, Variables extends Array<unknown>>(
tableName: string,
data: Variables,
): Promise<ExecuteGraphqlResponse<GraphQlResponse>> {
if (!validator.isNonEmptyString(tableName)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: '`tableName` must be a non-empty string.'
});
}
if (!validator.isNonEmptyArray(data)) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
message: '`data` must be a non-empty array for upsertMany.'
});
}
try {
tableName = this.formatTableName(tableName);
const gqlDataString = this.objectToString(data);
const mutation = `mutation { ${tableName}_upsertMany(data: ${gqlDataString}) }`;
// Use internal executeGraphql
return this.executeGraphql<GraphQlResponse, Variables>(mutation).catch(this.handleBulkImportErrors);
} catch (e: any) {
throw new FirebaseDataConnectError({
code: DATA_CONNECT_ERROR_CODE_MAPPING.INTERNAL,
message: `Failed to construct upsertMany mutation: ${e.message}`,
cause: e,
});
}
}
}
/**
* Data Connect-specific HTTP client which uses the special "owner" token
* when communicating with the Data Connect Emulator.
*/
class DataConnectHttpClient extends AuthorizedHttpClient {
protected getToken(): Promise<string> {
if (useEmulator()) {
return Promise.resolve('owner');
}
return super.getToken();
}
}
function emulatorHost(): string | undefined {
return process.env.DATA_CONNECT_EMULATOR_HOST
}
/**
* When true the SDK should communicate with the Data Connect Emulator for all API
* calls and also produce unsigned tokens.
*/
export function useEmulator(): boolean {
return !!emulatorHost();
}
interface ServerError {
code?: number;
message?: string;
status?: string;
}