Skip to content

Commit b118905

Browse files
committed
address getUrl comments
1 parent fd4ffb5 commit b118905

File tree

1 file changed

+73
-30
lines changed

1 file changed

+73
-30
lines changed

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

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ const DATA_CONNECT_CONFIG_HEADERS = {
5656
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`
5757
};
5858

59+
/**
60+
* URL params for requests to an endpoint under services:
61+
* .../services/{serviceId}:endpoint
62+
*/
63+
interface ServicesUrlParams {
64+
version: string;
65+
projectId: string;
66+
locationId: string;
67+
serviceId: string;
68+
endpointId: string;
69+
host?: string; // Present only when using the emulator
70+
}
71+
72+
/**
73+
* URL params for requests to an endpoint under connectors:
74+
* .../services/{serviceId}/connectors/{connectorId}:endpoint
75+
*/
76+
interface ConnectorsUrlParams extends ServicesUrlParams {
77+
connectorId: string;
78+
}
79+
5980
/**
6081
* Class that facilitates sending requests to the Firebase Data Connect backend API.
6182
*
@@ -135,7 +156,12 @@ export class DataConnectApiClient {
135156
...(options?.operationName && { operationName: options?.operationName }),
136157
...(options?.impersonate && { extensions: { impersonate: options?.impersonate } }),
137158
};
138-
const url = await this.getUrl(API_VERSION, this.connectorConfig.location, this.connectorConfig.serviceId, endpoint);
159+
const url = await this.getServicesUrl(
160+
API_VERSION,
161+
this.connectorConfig.location,
162+
this.connectorConfig.serviceId,
163+
endpoint
164+
);
139165
try {
140166
const resp = await this.makeGqlRequest<GraphqlResponse>(url, data);
141167
return resp;
@@ -208,12 +234,12 @@ export class DataConnectApiClient {
208234
operationName: name,
209235
extensions: { impersonate: options?.impersonate },
210236
};
211-
const url = await this.getUrl(
237+
const url = await this.getConnectorsUrl(
212238
API_VERSION,
213239
this.connectorConfig.location,
214240
this.connectorConfig.serviceId,
215-
endpoint,
216241
this.connectorConfig.connector,
242+
endpoint,
217243
);
218244
try {
219245
const resp = await this.makeGqlRequest<GraphqlResponse>(url, data);
@@ -224,52 +250,69 @@ export class DataConnectApiClient {
224250
}
225251

226252
/**
227-
* Constructs the URL for a Data Connect backend request.
228-
*
229-
* If no connectorId is provided, will direct the request to an endpoint under services:
230-
* .../services/{serviceId}:endpoint
253+
* Constructs the URL for a Data Connect request to a service endpoint.
254+
*
255+
* @param version - The API version.
256+
* @param locationId - The location of the Data Connect service.
257+
* @param serviceId - The ID of the Data Connect service.
258+
* @param endpointId - The endpoint to call.
259+
* @returns A promise which resolves to the formatted URL string.
260+
*/
261+
private async getServicesUrl(
262+
version: string,
263+
locationId: string,
264+
serviceId: string,
265+
endpointId: string,
266+
): Promise<string> {
267+
const projectId = await this.getProjectId();
268+
const params: ServicesUrlParams = {
269+
version,
270+
projectId,
271+
locationId,
272+
serviceId,
273+
endpointId,
274+
};
275+
let urlFormat = FIREBASE_DATA_CONNECT_SERVICES_URL_FORMAT;
276+
if (useEmulator()) {
277+
urlFormat = FIREBASE_DATA_CONNECT_EMULATOR_SERVICES_URL_FORMAT;
278+
params.host = emulatorHost();
279+
}
280+
return utils.formatString(urlFormat, params);
281+
}
282+
283+
/**
284+
* Constructs the URL for a Data Connect request to a connector endpoint.
231285
*
232-
* If connectorId is provided, will direct the request to an endpoint under connectors:
233-
* .../services/{serviceId}/connectors/{connectorId}:endpoint
234-
*
235286
* @param version - The API version.
236287
* @param locationId - The location of the Data Connect service.
237288
* @param serviceId - The ID of the Data Connect service.
289+
* @param connectorId - The ID of the Connector.
238290
* @param endpointId - The endpoint to call.
239-
* @param connectorId - The ID of the connector, if applicable.
240-
* @returns A promise that fulfills with the constructed URL.
291+
* @returns A promise which resolves to the formatted URL string.
292+
241293
*/
242-
private async getUrl(
294+
private async getConnectorsUrl(
243295
version: string,
244296
locationId: string,
245297
serviceId: string,
298+
connectorId: string,
246299
endpointId: string,
247-
connectorId?: string,
248300
): Promise<string> {
249301
const projectId = await this.getProjectId();
250-
const urlParams = {
302+
const params: ConnectorsUrlParams = {
251303
version,
252304
projectId,
253305
locationId,
254306
serviceId,
307+
connectorId,
255308
endpointId,
256-
connectorId
257309
};
258-
let urlFormat: string;
310+
let urlFormat = FIREBASE_DATA_CONNECT_CONNECTORS_URL_FORMAT;
259311
if (useEmulator()) {
260-
(urlParams as any).host = emulatorHost();
261-
urlFormat = connectorId === undefined || connectorId === ''
262-
? FIREBASE_DATA_CONNECT_EMULATOR_SERVICES_URL_FORMAT
263-
: FIREBASE_DATA_CONNECT_EMULATOR_CONNECTORS_URL_FORMAT;
264-
} else {
265-
urlFormat = connectorId === undefined || connectorId === ''
266-
? FIREBASE_DATA_CONNECT_SERVICES_URL_FORMAT
267-
: FIREBASE_DATA_CONNECT_CONNECTORS_URL_FORMAT;
268-
}
269-
if (connectorId) {
270-
(urlParams as any).connectorId = connectorId;
271-
}
272-
return utils.formatString(urlFormat, urlParams);
312+
urlFormat = FIREBASE_DATA_CONNECT_EMULATOR_CONNECTORS_URL_FORMAT;
313+
params.host = emulatorHost();
314+
}
315+
return utils.formatString(urlFormat, params);
273316
}
274317

275318
private getProjectId(): Promise<string> {

0 commit comments

Comments
 (0)