Skip to content

Commit c5518c8

Browse files
Add x-goog-request-params to request header. (#7440)
* Add `x-goog-request-params` to request header. * Create eighty-roses-appear.md * Pretty * Fix * Pretty * Pretty * Pretty * Failing CI fix (maybe) * Fix test * Fix test * Complete solution * Complete solution * Update testing emulator to 1.18.1 (#7445) * Attempt to make backward compatible with emulator. * Pretty * Pretty * Improve changeset message. * Encode values on 'x-goog-request-params' header. * Try slightly older version of emulator will pass auth test. * Revert emulator change --------- Co-authored-by: Mark Duckworth <[email protected]>
1 parent 52b17b4 commit c5518c8

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

.changeset/eighty-roses-appear.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/firestore": patch
3+
---
4+
5+
Fixed issue where count and lite API queries did not work with named databases.

packages/firestore/src/remote/rest_connection.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
import { SDK_VERSION } from '../../src/core/version';
1919
import { Token } from '../api/credentials';
20-
import { DatabaseId, DatabaseInfo } from '../core/database_info';
20+
import {
21+
DatabaseId,
22+
DatabaseInfo,
23+
DEFAULT_DATABASE_NAME
24+
} from '../core/database_info';
2125
import { debugAssert } from '../util/assert';
2226
import { generateUniqueDebugId } from '../util/debug_uid';
2327
import { FirestoreError } from '../util/error';
@@ -54,7 +58,8 @@ function getGoogApiClientValue(): string {
5458
export abstract class RestConnection implements Connection {
5559
protected readonly databaseId: DatabaseId;
5660
protected readonly baseUrl: string;
57-
private readonly databaseRoot: string;
61+
private readonly databasePath: string;
62+
private readonly requestParams: string;
5863

5964
get shouldResourcePathBeIncludedInRequest(): boolean {
6065
// Both `invokeRPC()` and `invokeStreamingRPC()` use their `path` arguments to determine
@@ -65,13 +70,14 @@ export abstract class RestConnection implements Connection {
6570
constructor(private readonly databaseInfo: DatabaseInfo) {
6671
this.databaseId = databaseInfo.databaseId;
6772
const proto = databaseInfo.ssl ? 'https' : 'http';
73+
const projectId = encodeURIComponent(this.databaseId.projectId);
74+
const databaseId = encodeURIComponent(this.databaseId.database);
6875
this.baseUrl = proto + '://' + databaseInfo.host;
69-
this.databaseRoot =
70-
'projects/' +
71-
this.databaseId.projectId +
72-
'/databases/' +
73-
this.databaseId.database +
74-
'/documents';
76+
this.databasePath = `projects/${projectId}/databases/${databaseId}`;
77+
this.requestParams =
78+
this.databaseId.database === DEFAULT_DATABASE_NAME
79+
? `project_id=${projectId}`
80+
: `project_id=${projectId}&database_id=${databaseId}`;
7581
}
7682

7783
invokeRPC<Req, Resp>(
@@ -85,7 +91,10 @@ export abstract class RestConnection implements Connection {
8591
const url = this.makeUrl(rpcName, path);
8692
logDebug(LOG_TAG, `Sending RPC '${rpcName}' ${streamId}:`, url, req);
8793

88-
const headers = {};
94+
const headers: StringMap = {
95+
'google-cloud-resource-prefix': this.databasePath,
96+
'x-goog-request-params': this.requestParams
97+
};
8998
this.modifyHeadersForRequest(headers, authToken, appCheckToken);
9099

91100
return this.performRPCRequest<Req, Resp>(rpcName, url, headers, req).then(

packages/firestore/test/unit/remote/rest_connection.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ describe('RestConnection', () => {
9696
'Content-Type': 'text/plain',
9797
'X-Firebase-GMPID': 'test-app-id',
9898
'X-Goog-Api-Client': `gl-js/ fire/${SDK_VERSION}`,
99-
'x-firebase-appcheck': 'some-app-check-token'
99+
'x-firebase-appcheck': 'some-app-check-token',
100+
'x-goog-request-params': 'project_id=testproject',
101+
'google-cloud-resource-prefix': 'projects/testproject/databases/(default)'
100102
});
101103
});
102104

@@ -111,7 +113,9 @@ describe('RestConnection', () => {
111113
expect(connection.lastHeaders).to.deep.equal({
112114
'Content-Type': 'text/plain',
113115
'X-Firebase-GMPID': 'test-app-id',
114-
'X-Goog-Api-Client': `gl-js/ fire/${SDK_VERSION}`
116+
'X-Goog-Api-Client': `gl-js/ fire/${SDK_VERSION}`,
117+
'x-goog-request-params': 'project_id=testproject',
118+
'google-cloud-resource-prefix': 'projects/testproject/databases/(default)'
115119
// Note: AppCheck token should not exist here.
116120
});
117121
});

0 commit comments

Comments
 (0)