@@ -184,6 +184,7 @@ export class APIPromise<T> extends Promise<T> {
184184
185185export abstract class APIClient {
186186 baseURL : string ;
187+ #baseURLOverridden: boolean ;
187188 maxRetries : number ;
188189 timeout : number ;
189190 httpAgent : Agent | undefined ;
@@ -193,18 +194,21 @@ export abstract class APIClient {
193194
194195 constructor ( {
195196 baseURL,
197+ baseURLOverridden,
196198 maxRetries = 2 ,
197199 timeout = 60000 , // 1 minute
198200 httpAgent,
199201 fetch : overriddenFetch ,
200202 } : {
201203 baseURL : string ;
204+ baseURLOverridden : boolean ;
202205 maxRetries ?: number | undefined ;
203206 timeout : number | undefined ;
204207 httpAgent : Agent | undefined ;
205208 fetch : Fetch | undefined ;
206209 } ) {
207210 this . baseURL = baseURL ;
211+ this . #baseURLOverridden = baseURLOverridden ;
208212 this . maxRetries = validatePositiveInteger ( 'maxRetries' , maxRetries ) ;
209213 this . timeout = validatePositiveInteger ( 'timeout' , timeout ) ;
210214 this . httpAgent = httpAgent ;
@@ -314,7 +318,7 @@ export abstract class APIClient {
314318 { retryCount = 0 } : { retryCount ?: number } = { } ,
315319 ) : { req : RequestInit ; url : string ; timeout : number } {
316320 const options = { ...inputOptions } ;
317- const { method, path, query, headers : headers = { } } = options ;
321+ const { method, path, query, defaultBaseURL , headers : headers = { } } = options ;
318322
319323 const body =
320324 ArrayBuffer . isView ( options . body ) || ( options . __binaryRequest && typeof options . body === 'string' ) ?
@@ -324,7 +328,7 @@ export abstract class APIClient {
324328 : null ;
325329 const contentLength = this . calculateContentLength ( body ) ;
326330
327- const url = this . buildURL ( path ! , query ) ;
331+ const url = this . buildURL ( path ! , query , defaultBaseURL ) ;
328332 if ( 'timeout' in options ) validatePositiveInteger ( 'timeout' , options . timeout ) ;
329333 options . timeout = options . timeout ?? this . timeout ;
330334 const httpAgent = options . httpAgent ?? this . httpAgent ?? getDefaultAgent ( url ) ;
@@ -517,11 +521,12 @@ export abstract class APIClient {
517521 return new PagePromise < PageClass , Item > ( this , request , Page ) ;
518522 }
519523
520- buildURL < Req > ( path : string , query : Req | null | undefined ) : string {
524+ buildURL < Req > ( path : string , query : Req | null | undefined , defaultBaseURL ?: string | undefined ) : string {
525+ const baseURL = ( ! this . #baseURLOverridden && defaultBaseURL ) || this . baseURL ;
521526 const url =
522527 isAbsoluteURL ( path ) ?
523528 new URL ( path )
524- : new URL ( this . baseURL + ( this . baseURL . endsWith ( '/' ) && path . startsWith ( '/' ) ? path . slice ( 1 ) : path ) ) ;
529+ : new URL ( baseURL + ( baseURL . endsWith ( '/' ) && path . startsWith ( '/' ) ? path . slice ( 1 ) : path ) ) ;
525530
526531 const defaultQuery = this . defaultQuery ( ) ;
527532 if ( ! isEmptyObj ( defaultQuery ) ) {
@@ -806,6 +811,7 @@ export type RequestOptions<
806811 query ?: Req | undefined ;
807812 body ?: Req | null | undefined ;
808813 headers ?: Headers | undefined ;
814+ defaultBaseURL ?: string | undefined ;
809815
810816 maxRetries ?: number ;
811817 stream ?: boolean | undefined ;
@@ -828,6 +834,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
828834 query : true ,
829835 body : true ,
830836 headers : true ,
837+ defaultBaseURL : true ,
831838
832839 maxRetries : true ,
833840 stream : true ,
0 commit comments