Skip to content

Commit b95a3d0

Browse files
committed
update connectorId to be optional
1 parent e94bde7 commit b95a3d0

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

src/data-connect/data-connect-api-client-internal.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,25 @@ export class DataConnectApiClient {
195195
'`options.impersonate` must be a non-null object.'
196196
);
197197
}
198+
199+
if (this.connectorConfig.connector === undefined || this.connectorConfig.connector === '') {
200+
throw new FirebaseDataConnectError(
201+
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
202+
'`connectorConfig.connector` field used to instantiate Data Connect instance \
203+
must be a non-empty string (the connectorId) when calling impersonate APIs.');
204+
}
205+
198206
const data = {
199207
...(options.variables && { variables: options?.variables }),
200208
operationName: options.operationName,
201209
extensions: { impersonate: options.impersonate },
202210
};
203-
const connectorId = `${this.connectorConfig.location}-${this.connectorConfig.serviceId}`;
204211
const url = await this.getUrl(
205212
API_VERSION,
206213
this.connectorConfig.location,
207214
this.connectorConfig.serviceId,
208215
endpoint,
209-
connectorId,
216+
this.connectorConfig.connector,
210217
);
211218
return this.makeGqlRequest<GraphqlResponse>(url, data)
212219
.then((resp) => {
@@ -247,17 +254,18 @@ export class DataConnectApiClient {
247254
locationId,
248255
serviceId,
249256
endpointId,
257+
connectorId
250258
};
251259
let urlFormat: string;
252260
if (useEmulator()) {
253261
(urlParams as any).host = emulatorHost();
254-
urlFormat = connectorId
255-
? FIREBASE_DATA_CONNECT_EMULATOR_CONNECTORS_URL_FORMAT
256-
: FIREBASE_DATA_CONNECT_EMULATOR_SERVICES_URL_FORMAT;
262+
urlFormat = connectorId === undefined || connectorId === ''
263+
? FIREBASE_DATA_CONNECT_EMULATOR_SERVICES_URL_FORMAT
264+
: FIREBASE_DATA_CONNECT_EMULATOR_CONNECTORS_URL_FORMAT;
257265
} else {
258-
urlFormat = connectorId
259-
? FIREBASE_DATA_CONNECT_CONNECTORS_URL_FORMAT
260-
: FIREBASE_DATA_CONNECT_SERVICES_URL_FORMAT;
266+
urlFormat = connectorId === undefined || connectorId === ''
267+
? FIREBASE_DATA_CONNECT_SERVICES_URL_FORMAT
268+
: FIREBASE_DATA_CONNECT_CONNECTORS_URL_FORMAT;
261269
}
262270
if (connectorId) {
263271
(urlParams as any).connectorId = connectorId;

src/data-connect/data-connect-api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export interface ConnectorConfig {
3030
* Service ID of the Data Connect service.
3131
*/
3232
serviceId: string;
33+
34+
/**
35+
* Name of the Data Connect connector.
36+
* Required for operations that interact with connectors, such as impersonateQuery and impersonateMutation.
37+
*/
38+
connector?: string;
3339
}
3440

3541
/**

src/data-connect/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export {
5151
* const connectorConfig: ConnectorConfig = {
5252
* location: 'us-west2',
5353
* serviceId: 'my-service',
54+
* connectorName: 'my-connector',
5455
* };
5556
*
5657
* // Get the `DataConnect` service for the default app

test/unit/data-connect/data-connect-api-client-internal.spec.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ describe('DataConnectApiClient', () => {
6969
const connectorConfig: ConnectorConfig = {
7070
location: 'us-west2',
7171
serviceId: 'my-service',
72+
connector: 'my-connector',
7273
};
7374

7475
const clientWithoutProjectId = new DataConnectApiClient(
@@ -251,6 +252,16 @@ describe('DataConnectApiClient', () => {
251252
clientWithoutProjectId.impersonateQuery(unauthenticatedOptions)
252253
.should.eventually.be.rejectedWith(noProjectId);
253254
});
255+
it('should reject when no connectorId is provided', () => {
256+
apiClient = new DataConnectApiClient(
257+
{ location: connectorConfig.location, serviceId: connectorConfig.serviceId },
258+
app
259+
);
260+
apiClient.impersonateQuery({ impersonate: { unauthenticated: true } })
261+
.should.eventually.be.rejectedWith(
262+
'`connectorConfig.connector` field used to instantiate Data Connect instance \
263+
must be a non-empty string (the connectorId) when calling impersonate APIs.');
264+
});
254265

255266
it('should reject when a full platform error response is received', () => {
256267
sandbox
@@ -310,7 +321,7 @@ describe('DataConnectApiClient', () => {
310321
expect(resp.data.users).to.deep.equal(TEST_RESPONSE.data.users);
311322
expect(stub).to.have.been.calledOnce.and.calledWith({
312323
method: 'POST',
313-
url: `https://firebasedataconnect.googleapis.com/v1alpha/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.location}-${connectorConfig.serviceId}:impersonateQuery`,
324+
url: `https://firebasedataconnect.googleapis.com/v1alpha/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateQuery`,
314325
headers: EXPECTED_HEADERS,
315326
data: {
316327
operationName: unauthenticatedOptions.operationName,
@@ -329,7 +340,7 @@ describe('DataConnectApiClient', () => {
329340
.then(() => {
330341
expect(stub).to.have.been.calledOnce.and.calledWith({
331342
method: 'POST',
332-
url: `http://localhost:9399/v1alpha/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.location}-${connectorConfig.serviceId}:impersonateQuery`,
343+
url: `http://localhost:9399/v1alpha/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateQuery`,
333344
headers: EMULATOR_EXPECTED_HEADERS,
334345
data: {
335346
operationName: unauthenticatedOptions.operationName,
@@ -360,6 +371,16 @@ describe('DataConnectApiClient', () => {
360371
clientWithoutProjectId.impersonateMutation(unauthenticatedOptions)
361372
.should.eventually.be.rejectedWith(noProjectId);
362373
});
374+
it('should reject when no connectorId is provided', () => {
375+
apiClient = new DataConnectApiClient(
376+
{ location: connectorConfig.location, serviceId: connectorConfig.serviceId },
377+
app
378+
);
379+
apiClient.impersonateMutation({ impersonate: { unauthenticated: true } })
380+
.should.eventually.be.rejectedWith(
381+
'`connectorConfig.connector` field used to instantiate Data Connect instance \
382+
must be a non-empty string (the connectorId) when calling impersonate APIs.');
383+
});
363384

364385
it('should reject when a full platform error response is received', () => {
365386
sandbox
@@ -419,7 +440,7 @@ describe('DataConnectApiClient', () => {
419440
expect(resp.data.users).to.deep.equal(TEST_RESPONSE.data.users);
420441
expect(stub).to.have.been.calledOnce.and.calledWith({
421442
method: 'POST',
422-
url: `https://firebasedataconnect.googleapis.com/v1alpha/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.location}-${connectorConfig.serviceId}:impersonateMutation`,
443+
url: `https://firebasedataconnect.googleapis.com/v1alpha/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateMutation`,
423444
headers: EXPECTED_HEADERS,
424445
data: {
425446
operationName: unauthenticatedOptions.operationName,
@@ -438,7 +459,7 @@ describe('DataConnectApiClient', () => {
438459
.then(() => {
439460
expect(stub).to.have.been.calledOnce.and.calledWith({
440461
method: 'POST',
441-
url: `http://localhost:9399/v1alpha/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.location}-${connectorConfig.serviceId}:impersonateMutation`,
462+
url: `http://localhost:9399/v1alpha/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateMutation`,
442463
headers: EMULATOR_EXPECTED_HEADERS,
443464
data: {
444465
operationName: unauthenticatedOptions.operationName,
@@ -459,6 +480,7 @@ describe('DataConnectApiClient CRUD helpers', () => {
459480
const connectorConfig: ConnectorConfig = {
460481
location: 'us-west1',
461482
serviceId: 'my-crud-service',
483+
connector: 'my-crud-connector',
462484
};
463485

464486
const mockOptions = {

0 commit comments

Comments
 (0)