Skip to content

Commit e72c0cd

Browse files
authored
chore(dc): Implement gen tracking (#2985)
1 parent 40325df commit e72c0cd

File tree

5 files changed

+72
-6
lines changed

5 files changed

+72
-6
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
"test:integration": "mocha test/integration/*.ts --slow 5000 --timeout 20000 --require ts-node/register",
2020
"test:coverage": "nyc npm run test:unit",
2121
"lint:src": "eslint src/ --ext .ts",
22+
"lint:src:fix": "eslint src/ --ext .ts --fix",
2223
"lint:test": "eslint test/ --ext .ts",
24+
"lint:test:fix": "eslint test/ --ext .ts --fix",
2325
"apidocs": "run-s api-extractor:local api-documenter",
2426
"api-extractor": "node generate-reports.js",
2527
"api-extractor:local": "npm run build && node generate-reports.js --local",

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,17 @@ const FIREBASE_DATA_CONNECT_EMULATOR_BASE_URL_FORMAT =
3838
const EXECUTE_GRAPH_QL_ENDPOINT = 'executeGraphql';
3939
const EXECUTE_GRAPH_QL_READ_ENDPOINT = 'executeGraphqlRead';
4040

41-
const DATA_CONNECT_CONFIG_HEADERS = {
42-
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`
43-
};
41+
42+
function getHeaders(isUsingGen: boolean): { [key: string]: string } {
43+
const headerValue = {
44+
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`,
45+
'X-Goog-Api-Client': utils.getMetricsHeader(),
46+
};
47+
if (isUsingGen) {
48+
headerValue['X-Goog-Api-Client'] += ' admin-js/gen';
49+
}
50+
return headerValue;
51+
}
4452

4553
/**
4654
* Class that facilitates sending requests to the Firebase Data Connect backend API.
@@ -50,6 +58,7 @@ const DATA_CONNECT_CONFIG_HEADERS = {
5058
export class DataConnectApiClient {
5159
private readonly httpClient: HttpClient;
5260
private projectId?: string;
61+
private isUsingGen = false;
5362

5463
constructor(private readonly connectorConfig: ConnectorConfig, private readonly app: App) {
5564
if (!validator.isNonNullObject(app) || !('options' in app)) {
@@ -59,6 +68,14 @@ export class DataConnectApiClient {
5968
}
6069
this.httpClient = new DataConnectHttpClient(app as FirebaseApp);
6170
}
71+
72+
/**
73+
* Update whether the SDK is using a generated one or not.
74+
* @param isUsingGen
75+
*/
76+
setIsUsingGen(isUsingGen: boolean): void {
77+
this.isUsingGen = isUsingGen;
78+
}
6279

6380
/**
6481
* Execute arbitrary GraphQL, including both read and write queries
@@ -117,7 +134,7 @@ export class DataConnectApiClient {
117134
const request: HttpRequestConfig = {
118135
method: 'POST',
119136
url,
120-
headers: DATA_CONNECT_CONFIG_HEADERS,
137+
headers: getHeaders(this.isUsingGen),
121138
data,
122139
};
123140
const resp = await this.httpClient.send(request);

src/data-connect/data-connect.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ export class DataConnect {
7272
this.client = new DataConnectApiClient(connectorConfig, app);
7373
}
7474

75+
/**
76+
* @param isUsingGen
77+
* @internal
78+
*/
79+
useGen(isUsingGen: boolean): void {
80+
this.client.setIsUsingGen(isUsingGen);
81+
}
82+
7583
/**
7684
* Execute an arbitrary GraphQL query or mutation
7785
*

src/utils/api-request.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,9 @@ export class AuthorizedHttpClient extends HttpClient {
10921092
requestCopy.httpAgent = this.app.options.httpAgent;
10931093
}
10941094

1095-
requestCopy.headers['X-Goog-Api-Client'] = getMetricsHeader()
1095+
if (!requestCopy.headers['X-Goog-Api-Client']) {
1096+
requestCopy.headers['X-Goog-Api-Client'] = getMetricsHeader()
1097+
}
10961098

10971099
return super.send(requestCopy);
10981100
});
@@ -1126,7 +1128,9 @@ export class AuthorizedHttp2Client extends Http2Client {
11261128
requestCopy.headers['x-goog-user-project'] = quotaProjectId;
11271129
}
11281130

1129-
requestCopy.headers['X-Goog-Api-Client'] = getMetricsHeader()
1131+
if (!requestCopy.headers['X-Goog-Api-Client']) {
1132+
requestCopy.headers['X-Goog-Api-Client'] = getMetricsHeader()
1133+
}
11301134

11311135
return super.send(requestCopy);
11321136
});

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ describe('DataConnectApiClient', () => {
4646
'X-Goog-Api-Client': getMetricsHeader(),
4747
};
4848

49+
const EXPECTED_HEADERS_WITH_GEN = {
50+
'Authorization': 'Bearer mock-token',
51+
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
52+
'X-Goog-Api-Client': getMetricsHeader() + ' admin-js/gen',
53+
};
54+
4955
const EMULATOR_EXPECTED_HEADERS = {
5056
'Authorization': 'Bearer owner',
5157
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
@@ -229,6 +235,35 @@ describe('DataConnectApiClient', () => {
229235
});
230236
});
231237
});
238+
it('should use gen headers if set on success', () => {
239+
interface UsersResponse {
240+
users: [
241+
user: {
242+
id: string;
243+
name: string;
244+
address: string;
245+
}
246+
];
247+
}
248+
apiClient.setIsUsingGen(true);
249+
const stub = sandbox
250+
.stub(HttpClient.prototype, 'send')
251+
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
252+
return apiClient.executeGraphql<UsersResponse, unknown>('query', {})
253+
.then((resp) => {
254+
expect(resp.data.users).to.be.not.empty;
255+
expect(resp.data.users[0].name).to.be.not.undefined;
256+
expect(resp.data.users[0].address).to.be.not.undefined;
257+
expect(resp.data.users).to.deep.equal(TEST_RESPONSE.data.users);
258+
expect(stub).to.have.been.calledOnce.and.calledWith({
259+
method: 'POST',
260+
url: `https://firebasedataconnect.googleapis.com/v1/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}:executeGraphql`,
261+
headers: EXPECTED_HEADERS_WITH_GEN,
262+
data: { query: 'query' }
263+
});
264+
apiClient.setIsUsingGen(false);
265+
});
266+
});
232267
});
233268
});
234269

0 commit comments

Comments
 (0)