verbose logging option for our custom fetch
This commit is contained in:
parent
1d02a3092b
commit
12e2bdb203
3 changed files with 92 additions and 5 deletions
4
packages/fetch/package-lock.json
generated
4
packages/fetch/package-lock.json
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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, {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue