Skip to content

Commit 5e3454b

Browse files
committed
Implement gen tracking
1 parent 40325df commit 5e3454b

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-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: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ 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+
function getHeaders(isUsingGen: boolean): { [key: string]: string } {
42+
const headerValue = {
43+
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`,
44+
'X-Goog-Api-Client': utils.getMetricsHeader(),
45+
};
46+
if (isUsingGen) {
47+
headerValue['X-Goog-Api-Client'] += ' js/gen';
48+
}
49+
return headerValue;
50+
}
4451

4552
/**
4653
* Class that facilitates sending requests to the Firebase Data Connect backend API.
@@ -50,6 +57,7 @@ const DATA_CONNECT_CONFIG_HEADERS = {
5057
export class DataConnectApiClient {
5158
private readonly httpClient: HttpClient;
5259
private projectId?: string;
60+
private isUsingGen = false;
5361

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

6379
/**
6480
* Execute arbitrary GraphQL, including both read and write queries
@@ -117,7 +133,7 @@ export class DataConnectApiClient {
117133
const request: HttpRequestConfig = {
118134
method: 'POST',
119135
url,
120-
headers: DATA_CONNECT_CONFIG_HEADERS,
136+
headers: getHeaders(this.isUsingGen),
121137
data,
122138
};
123139
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() + ' 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)