@@ -171,6 +171,7 @@ export class APIPromise<T> extends Promise<T> {
171
171
export abstract class APIClient {
172
172
baseURL : string ;
173
173
apiVersion : string ;
174
+ #baseURLOverridden: boolean ;
174
175
maxRetries : number ;
175
176
timeout : number ;
176
177
httpAgent : Agent | undefined ;
@@ -181,20 +182,23 @@ export abstract class APIClient {
181
182
constructor ( {
182
183
baseURL,
183
184
apiVersion,
185
+ baseURLOverridden,
184
186
maxRetries = 2 ,
185
187
timeout = 60000 , // 1 minute
186
188
httpAgent,
187
189
fetch : overriddenFetch ,
188
190
} : {
189
191
baseURL : string ;
190
192
apiVersion : string ;
193
+ baseURLOverridden : boolean ;
191
194
maxRetries ?: number | undefined ;
192
195
timeout : number | undefined ;
193
196
httpAgent : Agent | undefined ;
194
197
fetch : Fetch | undefined ;
195
198
} ) {
196
199
this . baseURL = baseURL ;
197
200
this . apiVersion = apiVersion ;
201
+ this . #baseURLOverridden = baseURLOverridden ;
198
202
this . maxRetries = validatePositiveInteger ( 'maxRetries' , maxRetries ) ;
199
203
this . timeout = validatePositiveInteger ( 'timeout' , timeout ) ;
200
204
this . httpAgent = httpAgent ;
@@ -305,7 +309,7 @@ export abstract class APIClient {
305
309
{ retryCount = 0 } : { retryCount ?: number } = { } ,
306
310
) : { req : RequestInit ; url : string ; timeout : number } {
307
311
const options = { ...inputOptions } ;
308
- const { method, path, query, headers : headers = { } } = options ;
312
+ const { method, path, query, defaultBaseURL , headers : headers = { } } = options ;
309
313
310
314
const body =
311
315
ArrayBuffer . isView ( options . body ) || ( options . __binaryRequest && typeof options . body === 'string' ) ?
@@ -315,7 +319,7 @@ export abstract class APIClient {
315
319
: null ;
316
320
const contentLength = this . calculateContentLength ( body ) ;
317
321
318
- const url = this . buildURL ( path ! , query ) ;
322
+ const url = this . buildURL ( path ! , query , defaultBaseURL ) ;
319
323
if ( 'timeout' in options ) validatePositiveInteger ( 'timeout' , options . timeout ) ;
320
324
options . timeout = options . timeout ?? this . timeout ;
321
325
const httpAgent = options . httpAgent ?? this . httpAgent ?? getDefaultAgent ( url ) ;
@@ -508,11 +512,12 @@ export abstract class APIClient {
508
512
return new PagePromise < PageClass , Item > ( this , request , Page ) ;
509
513
}
510
514
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 ;
512
517
const url =
513
518
isAbsoluteURL ( path ) ?
514
519
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 ) ) ;
516
521
517
522
const defaultQuery = this . defaultQuery ( ) ;
518
523
if ( ! isEmptyObj ( defaultQuery ) ) {
@@ -801,6 +806,7 @@ export type RequestOptions<
801
806
query ?: Req | undefined ;
802
807
body ?: Req | null | undefined ;
803
808
headers ?: Headers | undefined ;
809
+ defaultBaseURL ?: string | undefined ;
804
810
805
811
maxRetries ?: number ;
806
812
stream ?: boolean | undefined ;
@@ -824,6 +830,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
824
830
query : true ,
825
831
body : true ,
826
832
headers : true ,
833
+ defaultBaseURL : true ,
827
834
828
835
maxRetries : true ,
829
836
stream : true ,
0 commit comments