Skip to content

Commit 786c75f

Browse files
committed
merge master into rosa/impersonate
2 parents 4dad488 + 8c9895e commit 786c75f

6 files changed

Lines changed: 76 additions & 10 deletions

File tree

.github/actions/send-email/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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
@@ -49,9 +49,17 @@ const EXECUTE_GRAPH_QL_READ_ENDPOINT = 'executeGraphqlRead';
4949
const IMPERSONATE_QUERY_ENDPOINT = 'impersonateQuery';
5050
const IMPERSONATE_MUTATION_ENDPOINT = 'impersonateMutation';
5151

52-
const DATA_CONNECT_CONFIG_HEADERS = {
53-
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`
54-
};
52+
53+
function getHeaders(isUsingGen: boolean): { [key: string]: string } {
54+
const headerValue = {
55+
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`,
56+
'X-Goog-Api-Client': utils.getMetricsHeader(),
57+
};
58+
if (isUsingGen) {
59+
headerValue['X-Goog-Api-Client'] += ' admin-js/gen';
60+
}
61+
return headerValue;
62+
}
5563

5664
/**
5765
* URL params for requests to an endpoint under services:
@@ -82,6 +90,7 @@ interface ConnectorsUrlParams extends ServicesUrlParams {
8290
export class DataConnectApiClient {
8391
private readonly httpClient: HttpClient;
8492
private projectId?: string;
93+
private isUsingGen = false;
8594

8695
constructor(private readonly connectorConfig: ConnectorConfig, private readonly app: App) {
8796
if (!validator.isNonNullObject(app) || !('options' in app)) {
@@ -91,6 +100,14 @@ export class DataConnectApiClient {
91100
}
92101
this.httpClient = new DataConnectHttpClient(app as FirebaseApp);
93102
}
103+
104+
/**
105+
* Update whether the SDK is using a generated one or not.
106+
* @param isUsingGen
107+
*/
108+
setIsUsingGen(isUsingGen: boolean): void {
109+
this.isUsingGen = isUsingGen;
110+
}
94111

95112
/**
96113
* Execute arbitrary GraphQL, including both read and write queries
@@ -342,7 +359,7 @@ export class DataConnectApiClient {
342359
const request: HttpRequestConfig = {
343360
method: 'POST',
344361
url,
345-
headers: DATA_CONNECT_CONFIG_HEADERS,
362+
headers: getHeaders(this.isUsingGen),
346363
data,
347364
};
348365
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
@@ -73,6 +73,14 @@ export class DataConnect {
7373
this.client = new DataConnectApiClient(connectorConfig, app);
7474
}
7575

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

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
@@ -48,6 +48,12 @@ describe('DataConnectApiClient', () => {
4848
'X-Goog-Api-Client': getMetricsHeader(),
4949
};
5050

51+
const EXPECTED_HEADERS_WITH_GEN = {
52+
'Authorization': 'Bearer mock-token',
53+
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
54+
'X-Goog-Api-Client': getMetricsHeader() + ' admin-js/gen',
55+
};
56+
5157
const EMULATOR_EXPECTED_HEADERS = {
5258
'Authorization': 'Bearer owner',
5359
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
@@ -516,6 +522,35 @@ describe('DataConnectApiClient', () => {
516522
}
517523
});
518524
});
525+
it('should use gen headers if set on success', () => {
526+
interface UsersResponse {
527+
users: [
528+
user: {
529+
id: string;
530+
name: string;
531+
address: string;
532+
}
533+
];
534+
}
535+
apiClient.setIsUsingGen(true);
536+
const stub = sandbox
537+
.stub(HttpClient.prototype, 'send')
538+
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
539+
return apiClient.executeGraphql<UsersResponse, unknown>('query', {})
540+
.then((resp) => {
541+
expect(resp.data.users).to.be.not.empty;
542+
expect(resp.data.users[0].name).to.be.not.undefined;
543+
expect(resp.data.users[0].address).to.be.not.undefined;
544+
expect(resp.data.users).to.deep.equal(TEST_RESPONSE.data.users);
545+
expect(stub).to.have.been.calledOnce.and.calledWith({
546+
method: 'POST',
547+
url: `https://firebasedataconnect.googleapis.com/v1/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}:executeGraphql`,
548+
headers: EXPECTED_HEADERS_WITH_GEN,
549+
data: { query: 'query' }
550+
});
551+
apiClient.setIsUsingGen(false);
552+
});
553+
});
519554
});
520555
});
521556

0 commit comments

Comments
 (0)