17
17
18
18
import { SDK_VERSION } from '../../src/core/version' ;
19
19
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' ;
21
25
import { debugAssert } from '../util/assert' ;
22
26
import { generateUniqueDebugId } from '../util/debug_uid' ;
23
27
import { FirestoreError } from '../util/error' ;
@@ -54,7 +58,8 @@ function getGoogApiClientValue(): string {
54
58
export abstract class RestConnection implements Connection {
55
59
protected readonly databaseId : DatabaseId ;
56
60
protected readonly baseUrl : string ;
57
- private readonly databaseRoot : string ;
61
+ private readonly databasePath : string ;
62
+ private readonly requestParams : string ;
58
63
59
64
get shouldResourcePathBeIncludedInRequest ( ) : boolean {
60
65
// Both `invokeRPC()` and `invokeStreamingRPC()` use their `path` arguments to determine
@@ -65,13 +70,14 @@ export abstract class RestConnection implements Connection {
65
70
constructor ( private readonly databaseInfo : DatabaseInfo ) {
66
71
this . databaseId = databaseInfo . databaseId ;
67
72
const proto = databaseInfo . ssl ? 'https' : 'http' ;
73
+ const projectId = encodeURIComponent ( this . databaseId . projectId ) ;
74
+ const databaseId = encodeURIComponent ( this . databaseId . database ) ;
68
75
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 } ` ;
75
81
}
76
82
77
83
invokeRPC < Req , Resp > (
@@ -85,7 +91,10 @@ export abstract class RestConnection implements Connection {
85
91
const url = this . makeUrl ( rpcName , path ) ;
86
92
logDebug ( LOG_TAG , `Sending RPC '${ rpcName } ' ${ streamId } :` , url , req ) ;
87
93
88
- const headers = { } ;
94
+ const headers : StringMap = {
95
+ 'google-cloud-resource-prefix' : this . databasePath ,
96
+ 'x-goog-request-params' : this . requestParams
97
+ } ;
89
98
this . modifyHeadersForRequest ( headers , authToken , appCheckToken ) ;
90
99
91
100
return this . performRPCRequest < Req , Resp > ( rpcName , url , headers , req ) . then (
0 commit comments