@@ -90,6 +90,7 @@ export interface StorageTransportCallback<T> {
9090 fullResponse ?: GaxiosResponse ,
9191 ) : void ;
9292}
93+ let projectId : string ;
9394
9495export class StorageTransport {
9596 authClient : GoogleAuth < AuthClient > ;
@@ -98,6 +99,7 @@ export class StorageTransport {
9899 private retryOptions : RetryOptions ;
99100 private baseUrl : string ;
100101 private timeout ?: number ;
102+ private projectId ?: string ;
101103 private useAuthWithCustomEndpoint ?: boolean ;
102104
103105 constructor ( options : TransportParameters ) {
@@ -115,6 +117,7 @@ export class StorageTransport {
115117 this . retryOptions = options . retryOptions ;
116118 this . baseUrl = options . baseUrl ;
117119 this . timeout = options . timeout ;
120+ this . projectId = options . projectId ;
118121 this . useAuthWithCustomEndpoint = options . useAuthWithCustomEndpoint ;
119122 }
120123
@@ -134,19 +137,39 @@ export class StorageTransport {
134137 transport . instance . interceptors . request . add ( inter ) ;
135138 }
136139 }
137- const requestPromise = this . authClient . request < T > ( {
138- retryConfig : {
139- retry : this . retryOptions . maxRetries ,
140- noResponseRetries : this . retryOptions . maxRetries ,
141- maxRetryDelay : this . retryOptions . maxRetryDelay ,
142- retryDelayMultiplier : this . retryOptions . retryDelayMultiplier ,
143- shouldRetry : this . retryOptions . retryableErrorFn ,
144- totalTimeout : this . retryOptions . totalTimeout ,
145- } ,
146- ...reqOpts ,
147- headers,
148- url : this . #buildUrl( reqOpts . url ?. toString ( ) , reqOpts . queryParameters ) ,
149- timeout : this . timeout ,
140+ const prepareRequest = async ( ) => {
141+ try {
142+ const getProjectId = async ( ) => {
143+ if ( reqOpts . projectId ) return reqOpts . projectId ;
144+ projectId = await this . authClient . getProjectId ( ) ;
145+ return projectId ;
146+ } ;
147+ const _projectId = await getProjectId ( ) ;
148+ if ( _projectId ) {
149+ projectId = _projectId ;
150+ this . projectId = projectId ;
151+ }
152+ return projectId ;
153+ } catch ( e ) {
154+ if ( callback ) return callback ( e as GaxiosError ) ;
155+ throw e ;
156+ }
157+ } ;
158+ const requestPromise = prepareRequest ( ) . then ( ( ) => {
159+ return this . authClient . request < T > ( {
160+ retryConfig : {
161+ retry : this . retryOptions . maxRetries ,
162+ noResponseRetries : this . retryOptions . maxRetries ,
163+ maxRetryDelay : this . retryOptions . maxRetryDelay ,
164+ retryDelayMultiplier : this . retryOptions . retryDelayMultiplier ,
165+ shouldRetry : this . retryOptions . retryableErrorFn ,
166+ totalTimeout : this . retryOptions . totalTimeout ,
167+ } ,
168+ ...reqOpts ,
169+ headers,
170+ url : this . #buildUrl( reqOpts . url ?. toString ( ) , reqOpts . queryParameters ) ,
171+ timeout : this . timeout ,
172+ } ) ;
150173 } ) ;
151174
152175 return callback
@@ -157,6 +180,13 @@ export class StorageTransport {
157180 }
158181
159182 #buildUrl( pathUri = '' , queryParameters : StorageQueryParameters = { } ) : URL {
183+ if (
184+ 'project' in queryParameters &&
185+ ( queryParameters . project !== this . projectId ||
186+ queryParameters . project !== projectId )
187+ ) {
188+ queryParameters . project = this . projectId ;
189+ }
160190 const qp = this . #buildRequestQueryParams( queryParameters ) ;
161191 let url : URL ;
162192 if ( this . #isValidUrl( pathUri ) ) {
0 commit comments