@@ -13,15 +13,15 @@ import {
1313import { errorMessage } from "./error.js" ;
1414import { logVerbose } from "./util.js" ;
1515import { CancellationOptions } from "./cancellation.js" ;
16- import { resolveHttpProxyAgent } from "./proxy.js" ;
16+ import { resolveHttpsProxyAgent } from "./proxy.js" ;
1717import { host } from "./host.js" ;
1818import { renderWithPrecision } from "./precision.js" ;
1919import crossFetch from "cross-fetch" ;
20- import debug from "debug" ;
2120import { prettyStrings } from "./pretty.js" ;
2221import type { FetchOptions , RetryOptions } from "./types.js" ;
22+ import { genaiscriptDebug } from "./debug.js" ;
2323
24- const dbg = debug ( "genaiscript: fetch") ;
24+ const dbg = genaiscriptDebug ( " fetch") ;
2525
2626/**
2727 * Parses the retry-after header value.
@@ -51,7 +51,7 @@ export function parseRetryAfter(retryAfterHeader: string): number | null {
5151 const delaySeconds = Math . max ( 0 , Math . ceil ( delayMs / 1000 ) ) ;
5252 return delaySeconds ;
5353 }
54- } catch ( e ) {
54+ } catch ( e ) {
5555 dbg ( `failed to parse retry-after header as date: %s` , errorMessage ( e ) ) ;
5656 }
5757 }
@@ -84,31 +84,38 @@ export type FetchType = (
8484export async function createFetch (
8585 options ?: TraceOptions & CancellationOptions & RetryOptions ,
8686) : Promise < FetchType > {
87+ options = options || { } ;
8788 const {
8889 retries = FETCH_RETRY_DEFAULT ,
8990 retryOn = FETCH_RETRY_ON_DEFAULT ,
9091 trace,
9192 retryDelay = FETCH_RETRY_DEFAULT_DEFAULT ,
9293 maxDelay = FETCH_RETRY_MAX_DELAY_DEFAULT ,
9394 cancellationToken,
94- } = options || { } ;
95+ } = options ;
9596
97+ dbg ( `create fetch` ) ;
9698 // We create a proxy based on Node.js environment variables.
97- const agent = await resolveHttpProxyAgent ( ) ;
99+ const agent = await resolveHttpsProxyAgent ( ) ;
98100
99101 // We enrich crossFetch with the proxy.
100102 const crossFetchWithProxy : typeof fetch = agent
101- ? ( url , options ) => crossFetch ( url , { ...( options || { } ) , dispatcher : agent } as any )
103+ ? ( url , options ) => crossFetch ( url , { ...options , agent } as RequestInit )
102104 : crossFetch ;
103105
106+ const loggingFetch : typeof fetch = ( url , options ) => {
107+ dbg ( `fetch: %s %s` , options ?. method || "GET" , url ) ;
108+ return crossFetchWithProxy ( url , options ) ;
109+ } ;
110+
104111 // Return the default fetch if no retry status codes are specified
105112 if ( ! retryOn ?. length ) {
106113 dbg ( "no retry logic applied, using crossFetchWithProxy directly" ) ;
107- return crossFetchWithProxy ;
114+ return loggingFetch ;
108115 }
109116
110117 // Create a fetch function with retry logic
111- const fetchRetry = wrapFetch ( crossFetchWithProxy , {
118+ const fetchRetry = wrapFetch ( loggingFetch , {
112119 retryOn,
113120 retries,
114121 retryDelay : ( attempt , error , response ) => {
0 commit comments