@@ -171,6 +171,7 @@ export class APIPromise<T> extends Promise<T> {
171171export abstract class APIClient {
172172 baseURL : string ;
173173 apiVersion : string ;
174+ #baseURLOverridden: boolean ;
174175 maxRetries : number ;
175176 timeout : number ;
176177 httpAgent : Agent | undefined ;
@@ -181,20 +182,23 @@ export abstract class APIClient {
181182 constructor ( {
182183 baseURL,
183184 apiVersion,
185+ baseURLOverridden,
184186 maxRetries = 2 ,
185187 timeout = 60000 , // 1 minute
186188 httpAgent,
187189 fetch : overriddenFetch ,
188190 } : {
189191 baseURL : string ;
190192 apiVersion : string ;
193+ baseURLOverridden : boolean ;
191194 maxRetries ?: number | undefined ;
192195 timeout : number | undefined ;
193196 httpAgent : Agent | undefined ;
194197 fetch : Fetch | undefined ;
195198 } ) {
196199 this . baseURL = baseURL ;
197200 this . apiVersion = apiVersion ;
201+ this . #baseURLOverridden = baseURLOverridden ;
198202 this . maxRetries = validatePositiveInteger ( 'maxRetries' , maxRetries ) ;
199203 this . timeout = validatePositiveInteger ( 'timeout' , timeout ) ;
200204 this . httpAgent = httpAgent ;
@@ -305,7 +309,7 @@ export abstract class APIClient {
305309 { retryCount = 0 } : { retryCount ?: number } = { } ,
306310 ) : { req : RequestInit ; url : string ; timeout : number } {
307311 const options = { ...inputOptions } ;
308- const { method, path, query, headers : headers = { } } = options ;
312+ const { method, path, query, defaultBaseURL , headers : headers = { } } = options ;
309313
310314 const body =
311315 ArrayBuffer . isView ( options . body ) || ( options . __binaryRequest && typeof options . body === 'string' ) ?
@@ -315,7 +319,7 @@ export abstract class APIClient {
315319 : null ;
316320 const contentLength = this . calculateContentLength ( body ) ;
317321
318- const url = this . buildURL ( path ! , query ) ;
322+ const url = this . buildURL ( path ! , query , defaultBaseURL ) ;
319323 if ( 'timeout' in options ) validatePositiveInteger ( 'timeout' , options . timeout ) ;
320324 options . timeout = options . timeout ?? this . timeout ;
321325 const httpAgent = options . httpAgent ?? this . httpAgent ?? getDefaultAgent ( url ) ;
@@ -508,11 +512,12 @@ export abstract class APIClient {
508512 return new PagePromise < PageClass , Item > ( this , request , Page ) ;
509513 }
510514
511- buildURL < Req > ( path : string , query : Req | null | undefined ) : string {
515+ buildURL < Req > ( path : string , query : Req | null | undefined , defaultBaseURL ?: string | undefined ) : string {
516+ const baseURL = ( ! this . #baseURLOverridden && defaultBaseURL ) || this . baseURL ;
512517 const url =
513518 isAbsoluteURL ( path ) ?
514519 new URL ( path )
515- : new URL ( this . baseURL + ( this . baseURL . endsWith ( '/' ) && path . startsWith ( '/' ) ? path . slice ( 1 ) : path ) ) ;
520+ : new URL ( baseURL + ( baseURL . endsWith ( '/' ) && path . startsWith ( '/' ) ? path . slice ( 1 ) : path ) ) ;
516521
517522 const defaultQuery = this . defaultQuery ( ) ;
518523 if ( ! isEmptyObj ( defaultQuery ) ) {
@@ -801,6 +806,7 @@ export type RequestOptions<
801806 query ?: Req | undefined ;
802807 body ?: Req | null | undefined ;
803808 headers ?: Headers | undefined ;
809+ defaultBaseURL ?: string | undefined ;
804810
805811 maxRetries ?: number ;
806812 stream ?: boolean | undefined ;
@@ -824,6 +830,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
824830 query : true ,
825831 body : true ,
826832 headers : true ,
833+ defaultBaseURL : true ,
827834
828835 maxRetries : true ,
829836 stream : true ,
0 commit comments