@@ -170,6 +170,7 @@ export class APIPromise<T> extends Promise<T> {
170170
171171export abstract class APIClient {
172172 baseURL : string ;
173+ #baseURLOverridden: boolean ;
173174 maxRetries : number ;
174175 timeout : number ;
175176 httpAgent : Agent | undefined ;
@@ -179,18 +180,21 @@ export abstract class APIClient {
179180
180181 constructor ( {
181182 baseURL,
183+ baseURLOverridden,
182184 maxRetries = 2 ,
183185 timeout = 60000 , // 1 minute
184186 httpAgent,
185187 fetch : overriddenFetch ,
186188 } : {
187189 baseURL : string ;
190+ baseURLOverridden : boolean ;
188191 maxRetries ?: number | undefined ;
189192 timeout : number | undefined ;
190193 httpAgent : Agent | undefined ;
191194 fetch : Fetch | undefined ;
192195 } ) {
193196 this . baseURL = baseURL ;
197+ this . #baseURLOverridden = baseURLOverridden ;
194198 this . maxRetries = validatePositiveInteger ( 'maxRetries' , maxRetries ) ;
195199 this . timeout = validatePositiveInteger ( 'timeout' , timeout ) ;
196200 this . httpAgent = httpAgent ;
@@ -300,7 +304,7 @@ export abstract class APIClient {
300304 { retryCount = 0 } : { retryCount ?: number } = { } ,
301305 ) : { req : RequestInit ; url : string ; timeout : number } {
302306 const options = { ...inputOptions } ;
303- const { method, path, query, headers : headers = { } } = options ;
307+ const { method, path, query, defaultBaseURL , headers : headers = { } } = options ;
304308
305309 const body =
306310 ArrayBuffer . isView ( options . body ) || ( options . __binaryRequest && typeof options . body === 'string' ) ?
@@ -310,7 +314,7 @@ export abstract class APIClient {
310314 : null ;
311315 const contentLength = this . calculateContentLength ( body ) ;
312316
313- const url = this . buildURL ( path ! , query ) ;
317+ const url = this . buildURL ( path ! , query , defaultBaseURL ) ;
314318 if ( 'timeout' in options ) validatePositiveInteger ( 'timeout' , options . timeout ) ;
315319 options . timeout = options . timeout ?? this . timeout ;
316320 const httpAgent = options . httpAgent ?? this . httpAgent ?? getDefaultAgent ( url ) ;
@@ -503,11 +507,12 @@ export abstract class APIClient {
503507 return new PagePromise < PageClass , Item > ( this , request , Page ) ;
504508 }
505509
506- buildURL < Req > ( path : string , query : Req | null | undefined ) : string {
510+ buildURL < Req > ( path : string , query : Req | null | undefined , defaultBaseURL ?: string | undefined ) : string {
511+ const baseURL = ( ! this . #baseURLOverridden && defaultBaseURL ) || this . baseURL ;
507512 const url =
508513 isAbsoluteURL ( path ) ?
509514 new URL ( path )
510- : new URL ( this . baseURL + ( this . baseURL . endsWith ( '/' ) && path . startsWith ( '/' ) ? path . slice ( 1 ) : path ) ) ;
515+ : new URL ( baseURL + ( baseURL . endsWith ( '/' ) && path . startsWith ( '/' ) ? path . slice ( 1 ) : path ) ) ;
511516
512517 const defaultQuery = this . defaultQuery ( ) ;
513518 if ( ! isEmptyObj ( defaultQuery ) ) {
@@ -792,6 +797,7 @@ export type RequestOptions<
792797 query ?: Req | undefined ;
793798 body ?: Req | null | undefined ;
794799 headers ?: Headers | undefined ;
800+ defaultBaseURL ?: string | undefined ;
795801
796802 maxRetries ?: number ;
797803 stream ?: boolean | undefined ;
@@ -813,6 +819,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
813819 query : true ,
814820 body : true ,
815821 headers : true ,
822+ defaultBaseURL : true ,
816823
817824 maxRetries : true ,
818825 stream : true ,
0 commit comments