@@ -311,7 +311,7 @@ export class BaseAnthropic {
311
311
* Create a new client instance re-using the same options given to the current client with optional overriding.
312
312
*/
313
313
withOptions ( options : Partial < ClientOptions > ) : this {
314
- return new ( this . constructor as any as new ( props : ClientOptions ) => typeof this ) ( {
314
+ const client = new ( this . constructor as any as new ( props : ClientOptions ) => typeof this ) ( {
315
315
...this . _options ,
316
316
baseURL : this . baseURL ,
317
317
maxRetries : this . maxRetries ,
@@ -324,6 +324,7 @@ export class BaseAnthropic {
324
324
authToken : this . authToken ,
325
325
...options ,
326
326
} ) ;
327
+ return client ;
327
328
}
328
329
329
330
/**
@@ -357,18 +358,18 @@ export class BaseAnthropic {
357
358
) ;
358
359
}
359
360
360
- protected authHeaders ( opts : FinalRequestOptions ) : NullableHeaders | undefined {
361
- return buildHeaders ( [ this . apiKeyAuth ( opts ) , this . bearerAuth ( opts ) ] ) ;
361
+ protected async authHeaders ( opts : FinalRequestOptions ) : Promise < NullableHeaders | undefined > {
362
+ return buildHeaders ( [ await this . apiKeyAuth ( opts ) , await this . bearerAuth ( opts ) ] ) ;
362
363
}
363
364
364
- protected apiKeyAuth ( opts : FinalRequestOptions ) : NullableHeaders | undefined {
365
+ protected async apiKeyAuth ( opts : FinalRequestOptions ) : Promise < NullableHeaders | undefined > {
365
366
if ( this . apiKey == null ) {
366
367
return undefined ;
367
368
}
368
369
return buildHeaders ( [ { 'X-Api-Key' : this . apiKey } ] ) ;
369
370
}
370
371
371
- protected bearerAuth ( opts : FinalRequestOptions ) : NullableHeaders | undefined {
372
+ protected async bearerAuth ( opts : FinalRequestOptions ) : Promise < NullableHeaders | undefined > {
372
373
if ( this . authToken == null ) {
373
374
return undefined ;
374
375
}
@@ -515,7 +516,9 @@ export class BaseAnthropic {
515
516
516
517
await this . prepareOptions ( options ) ;
517
518
518
- const { req, url, timeout } = this . buildRequest ( options , { retryCount : maxRetries - retriesRemaining } ) ;
519
+ const { req, url, timeout } = await this . buildRequest ( options , {
520
+ retryCount : maxRetries - retriesRemaining ,
521
+ } ) ;
519
522
520
523
await this . prepareRequest ( req , { url, options } ) ;
521
524
@@ -597,7 +600,7 @@ export class BaseAnthropic {
597
600
} with status ${ response . status } in ${ headersTime - startTime } ms`;
598
601
599
602
if ( ! response . ok ) {
600
- const shouldRetry = this . shouldRetry ( response ) ;
603
+ const shouldRetry = await this . shouldRetry ( response ) ;
601
604
if ( retriesRemaining && shouldRetry ) {
602
605
const retryMessage = `retrying, ${ retriesRemaining } attempts remaining` ;
603
606
@@ -715,7 +718,7 @@ export class BaseAnthropic {
715
718
}
716
719
}
717
720
718
- private shouldRetry ( response : Response ) : boolean {
721
+ private async shouldRetry ( response : Response ) : Promise < boolean > {
719
722
// Note this is not a standard header.
720
723
const shouldRetryHeader = response . headers . get ( 'x-should-retry' ) ;
721
724
@@ -806,18 +809,18 @@ export class BaseAnthropic {
806
809
return defaultTime ;
807
810
}
808
811
809
- buildRequest (
812
+ async buildRequest (
810
813
inputOptions : FinalRequestOptions ,
811
814
{ retryCount = 0 } : { retryCount ?: number } = { } ,
812
- ) : { req : FinalizedRequestInit ; url : string ; timeout : number } {
815
+ ) : Promise < { req : FinalizedRequestInit ; url : string ; timeout : number } > {
813
816
const options = { ...inputOptions } ;
814
817
const { method, path, query, defaultBaseURL } = options ;
815
818
816
819
const url = this . buildURL ( path ! , query as Record < string , unknown > , defaultBaseURL ) ;
817
820
if ( 'timeout' in options ) validatePositiveInteger ( 'timeout' , options . timeout ) ;
818
821
options . timeout = options . timeout ?? this . timeout ;
819
822
const { bodyHeaders, body } = this . buildBody ( { options } ) ;
820
- const reqHeaders = this . buildHeaders ( { options : inputOptions , method, bodyHeaders, retryCount } ) ;
823
+ const reqHeaders = await this . buildHeaders ( { options : inputOptions , method, bodyHeaders, retryCount } ) ;
821
824
822
825
const req : FinalizedRequestInit = {
823
826
method,
@@ -833,7 +836,7 @@ export class BaseAnthropic {
833
836
return { req, url, timeout : options . timeout } ;
834
837
}
835
838
836
- private buildHeaders ( {
839
+ private async buildHeaders ( {
837
840
options,
838
841
method,
839
842
bodyHeaders,
@@ -843,7 +846,7 @@ export class BaseAnthropic {
843
846
method : HTTPMethod ;
844
847
bodyHeaders : HeadersLike ;
845
848
retryCount : number ;
846
- } ) : Headers {
849
+ } ) : Promise < Headers > {
847
850
let idempotencyHeaders : HeadersLike = { } ;
848
851
if ( this . idempotencyHeader && method !== 'get' ) {
849
852
if ( ! options . idempotencyKey ) options . idempotencyKey = this . defaultIdempotencyKey ( ) ;
@@ -863,7 +866,7 @@ export class BaseAnthropic {
863
866
: undefined ) ,
864
867
'anthropic-version' : '2023-06-01' ,
865
868
} ,
866
- this . authHeaders ( options ) ,
869
+ await this . authHeaders ( options ) ,
867
870
this . _options . defaultHeaders ,
868
871
bodyHeaders ,
869
872
options . headers ,
0 commit comments