@@ -184,6 +184,7 @@ export class APIPromise<T> extends Promise<T> {
184
184
185
185
export abstract class APIClient {
186
186
baseURL : string ;
187
+ #baseURLOverridden: boolean ;
187
188
maxRetries : number ;
188
189
timeout : number ;
189
190
httpAgent : Agent | undefined ;
@@ -193,18 +194,21 @@ export abstract class APIClient {
193
194
194
195
constructor ( {
195
196
baseURL,
197
+ baseURLOverridden,
196
198
maxRetries = 5 ,
197
199
timeout = 60000 , // 1 minute
198
200
httpAgent,
199
201
fetch : overriddenFetch ,
200
202
} : {
201
203
baseURL : string ;
204
+ baseURLOverridden : boolean ;
202
205
maxRetries ?: number | undefined ;
203
206
timeout : number | undefined ;
204
207
httpAgent : Agent | undefined ;
205
208
fetch : Fetch | undefined ;
206
209
} ) {
207
210
this . baseURL = baseURL ;
211
+ this . #baseURLOverridden = baseURLOverridden ;
208
212
this . maxRetries = validatePositiveInteger ( 'maxRetries' , maxRetries ) ;
209
213
this . timeout = validatePositiveInteger ( 'timeout' , timeout ) ;
210
214
this . httpAgent = httpAgent ;
@@ -314,7 +318,7 @@ export abstract class APIClient {
314
318
{ retryCount = 0 } : { retryCount ?: number } = { } ,
315
319
) : { req : RequestInit ; url : string ; timeout : number } {
316
320
const options = { ...inputOptions } ;
317
- const { method, path, query, headers : headers = { } } = options ;
321
+ const { method, path, query, defaultBaseURL , headers : headers = { } } = options ;
318
322
319
323
const body =
320
324
ArrayBuffer . isView ( options . body ) || ( options . __binaryRequest && typeof options . body === 'string' ) ?
@@ -324,7 +328,7 @@ export abstract class APIClient {
324
328
: null ;
325
329
const contentLength = this . calculateContentLength ( body ) ;
326
330
327
- const url = this . buildURL ( path ! , query ) ;
331
+ const url = this . buildURL ( path ! , query , defaultBaseURL ) ;
328
332
if ( 'timeout' in options ) validatePositiveInteger ( 'timeout' , options . timeout ) ;
329
333
options . timeout = options . timeout ?? this . timeout ;
330
334
const httpAgent = options . httpAgent ?? this . httpAgent ?? getDefaultAgent ( url ) ;
@@ -517,11 +521,12 @@ export abstract class APIClient {
517
521
return new PagePromise < PageClass , Item > ( this , request , Page ) ;
518
522
}
519
523
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 ;
521
526
const url =
522
527
isAbsoluteURL ( path ) ?
523
528
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 ) ) ;
525
530
526
531
const defaultQuery = this . defaultQuery ( ) ;
527
532
if ( ! isEmptyObj ( defaultQuery ) ) {
@@ -806,6 +811,7 @@ export type RequestOptions<
806
811
query ?: Req | undefined ;
807
812
body ?: Req | null | undefined ;
808
813
headers ?: Headers | undefined ;
814
+ defaultBaseURL ?: string | undefined ;
809
815
810
816
maxRetries ?: number ;
811
817
stream ?: boolean | undefined ;
@@ -828,6 +834,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
828
834
query : true ,
829
835
body : true ,
830
836
headers : true ,
837
+ defaultBaseURL : true ,
831
838
832
839
maxRetries : true ,
833
840
stream : true ,
0 commit comments