From 12e2bdb2034f864c3d2af5edffcf94d440cd0aba Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 28 May 2025 15:19:27 -0700 Subject: [PATCH] verbose logging option for our custom fetch --- packages/fetch/package-lock.json | 4 +- packages/fetch/package.json | 2 +- packages/fetch/src/fetch.ts | 91 +++++++++++++++++++++++++++++++- 3 files changed, 92 insertions(+), 5 deletions(-) diff --git a/packages/fetch/package-lock.json b/packages/fetch/package-lock.json index 04fdc5f2f..f2d19bcc3 100644 --- a/packages/fetch/package-lock.json +++ b/packages/fetch/package-lock.json @@ -1,12 +1,12 @@ { "name": "@continuedev/fetch", - "version": "1.0.9", + "version": "1.0.11", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@continuedev/fetch", - "version": "1.0.9", + "version": "1.0.11", "license": "Apache-2.0", "dependencies": { "@continuedev/config-types": "^1.0.5", diff --git a/packages/fetch/package.json b/packages/fetch/package.json index f1cb93f06..ae088c1ba 100644 --- a/packages/fetch/package.json +++ b/packages/fetch/package.json @@ -1,6 +1,6 @@ { "name": "@continuedev/fetch", - "version": "1.0.10", + "version": "1.0.11", "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/packages/fetch/src/fetch.ts b/packages/fetch/src/fetch.ts index 59e0a887c..66903cabe 100644 --- a/packages/fetch/src/fetch.ts +++ b/packages/fetch/src/fetch.ts @@ -2,12 +2,81 @@ import { RequestOptions } from "@continuedev/config-types"; import * as followRedirects from "follow-redirects"; import { HttpProxyAgent } from "http-proxy-agent"; import { HttpsProxyAgent } from "https-proxy-agent"; -import fetch, { RequestInit, Response } from "node-fetch"; +import fetch, { BodyInit, RequestInit, Response } from "node-fetch"; import { getAgentOptions } from "./getAgentOptions.js"; import { getProxyFromEnv, shouldBypassProxy } from "./util.js"; const { http, https } = (followRedirects as any).default; +function logRequest( + method: string, + url: URL, + headers: { [key: string]: string }, + body: BodyInit | null | undefined, + proxy?: string, + shouldBypass?: boolean, +) { + console.log("=== FETCH REQUEST ==="); + console.log(`Method: ${method}`); + console.log(`URL: ${url.toString()}`); + + // Log headers in curl format + console.log("Headers:"); + for (const [key, value] of Object.entries(headers)) { + console.log(` -H '${key}: ${value}'`); + } + + // Log proxy information + if (proxy && !shouldBypass) { + console.log(`Proxy: ${proxy}`); + } + + // Log body + if (body) { + console.log(`Body: ${body}`); + } + + // Generate equivalent curl command + let curlCommand = `curl -X ${method}`; + for (const [key, value] of Object.entries(headers)) { + curlCommand += ` -H '${key}: ${value}'`; + } + if (body) { + curlCommand += ` -d '${body}'`; + } + if (proxy && !shouldBypass) { + curlCommand += ` --proxy '${proxy}'`; + } + curlCommand += ` '${url.toString()}'`; + console.log(`Equivalent curl: ${curlCommand}`); + console.log("====================="); +} + +async function logResponse(resp: Response) { + console.log("=== FETCH RESPONSE ==="); + console.log(`Status: ${resp.status} ${resp.statusText}`); + console.log("Response Headers:"); + resp.headers.forEach((value, key) => { + console.log(` ${key}: ${value}`); + }); + + // Clone response to read body without consuming it + const respClone = resp.clone(); + try { + const responseText = await respClone.text(); + console.log(`Response Body: ${responseText}`); + } catch (e) { + console.log("Could not read response body:", e); + } + console.log("======================"); +} + +function logError(error: unknown) { + console.log("=== FETCH ERROR ==="); + console.log(`Error: ${error}`); + console.log("==================="); +} + export async function fetchwithRequestOptions( url_: URL | string, init?: RequestInit, @@ -68,15 +137,28 @@ export async function fetchwithRequestOptions( console.log("Unable to parse HTTP request body: ", e); } + const finalBody = updatedBody ?? init?.body; + const method = init?.method || "GET"; + + // Verbose logging for debugging - log request details + if (process.env.VERBOSE_FETCH) { + logRequest(method, url, headers, finalBody, proxy, shouldBypass); + } + // fetch the request with the provided options try { const resp = await fetch(url, { ...init, - body: updatedBody ?? init?.body, + body: finalBody, headers: headers, agent: agent, }); + // Verbose logging for debugging - log response details + if (process.env.VERBOSE_FETCH) { + await logResponse(resp); + } + if (!resp.ok) { const requestId = resp.headers.get("x-request-id"); if (requestId) { @@ -86,6 +168,11 @@ export async function fetchwithRequestOptions( return resp; } catch (error) { + // Verbose logging for errors + if (process.env.VERBOSE_FETCH) { + logError(error); + } + if (error instanceof Error && error.name === "AbortError") { // Return a Response object that streamResponse etc can handle return new Response(null, {