@continuedev/fetch 1.0.10 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/fetch.js CHANGED
@@ -5,6 +5,61 @@ import fetch, { Response } from "node-fetch";
5
5
  import { getAgentOptions } from "./getAgentOptions.js";
6
6
  import { getProxyFromEnv, shouldBypassProxy } from "./util.js";
7
7
  const { http, https } = followRedirects.default;
8
+ function logRequest(method, url, headers, body, proxy, shouldBypass) {
9
+ console.log("=== FETCH REQUEST ===");
10
+ console.log(`Method: ${method}`);
11
+ console.log(`URL: ${url.toString()}`);
12
+ // Log headers in curl format
13
+ console.log("Headers:");
14
+ for (const [key, value] of Object.entries(headers)) {
15
+ console.log(` -H '${key}: ${value}'`);
16
+ }
17
+ // Log proxy information
18
+ if (proxy && !shouldBypass) {
19
+ console.log(`Proxy: ${proxy}`);
20
+ }
21
+ // Log body
22
+ if (body) {
23
+ console.log(`Body: ${body}`);
24
+ }
25
+ // Generate equivalent curl command
26
+ let curlCommand = `curl -X ${method}`;
27
+ for (const [key, value] of Object.entries(headers)) {
28
+ curlCommand += ` -H '${key}: ${value}'`;
29
+ }
30
+ if (body) {
31
+ curlCommand += ` -d '${body}'`;
32
+ }
33
+ if (proxy && !shouldBypass) {
34
+ curlCommand += ` --proxy '${proxy}'`;
35
+ }
36
+ curlCommand += ` '${url.toString()}'`;
37
+ console.log(`Equivalent curl: ${curlCommand}`);
38
+ console.log("=====================");
39
+ }
40
+ async function logResponse(resp) {
41
+ console.log("=== FETCH RESPONSE ===");
42
+ console.log(`Status: ${resp.status} ${resp.statusText}`);
43
+ console.log("Response Headers:");
44
+ resp.headers.forEach((value, key) => {
45
+ console.log(` ${key}: ${value}`);
46
+ });
47
+ // TODO: For streamed responses, this caused the response to be consumed and the connection would just hang open
48
+ // Clone response to read body without consuming it
49
+ // const respClone = resp.clone();
50
+ // try {
51
+ // const responseText = await respClone.text();
52
+ // console.log(`Response Body: ${responseText}`);
53
+ // } catch (e) {
54
+ // console.log("Could not read response body:", e);
55
+ // }
56
+ console.log("======================");
57
+ }
58
+ function logError(error) {
59
+ console.log("=== FETCH ERROR ===");
60
+ console.log(`Error: ${error}`);
61
+ console.log("===================");
62
+ }
8
63
  export async function fetchwithRequestOptions(url_, init, requestOptions) {
9
64
  const url = typeof url_ === "string" ? new URL(url_) : url_;
10
65
  if (url.host === "localhost") {
@@ -52,14 +107,24 @@ export async function fetchwithRequestOptions(url_, init, requestOptions) {
52
107
  catch (e) {
53
108
  console.log("Unable to parse HTTP request body: ", e);
54
109
  }
110
+ const finalBody = updatedBody ?? init?.body;
111
+ const method = init?.method || "GET";
112
+ // Verbose logging for debugging - log request details
113
+ if (process.env.VERBOSE_FETCH) {
114
+ logRequest(method, url, headers, finalBody, proxy, shouldBypass);
115
+ }
55
116
  // fetch the request with the provided options
56
117
  try {
57
118
  const resp = await fetch(url, {
58
119
  ...init,
59
- body: updatedBody ?? init?.body,
120
+ body: finalBody,
60
121
  headers: headers,
61
122
  agent: agent,
62
123
  });
124
+ // Verbose logging for debugging - log response details
125
+ if (process.env.VERBOSE_FETCH) {
126
+ await logResponse(resp);
127
+ }
63
128
  if (!resp.ok) {
64
129
  const requestId = resp.headers.get("x-request-id");
65
130
  if (requestId) {
@@ -69,6 +134,10 @@ export async function fetchwithRequestOptions(url_, init, requestOptions) {
69
134
  return resp;
70
135
  }
71
136
  catch (error) {
137
+ // Verbose logging for errors
138
+ if (process.env.VERBOSE_FETCH) {
139
+ logError(error);
140
+ }
72
141
  if (error instanceof Error && error.name === "AbortError") {
73
142
  // Return a Response object that streamResponse etc can handle
74
143
  return new Response(null, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@continuedev/fetch",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/fetch.ts CHANGED
@@ -2,12 +2,82 @@ import { RequestOptions } from "@continuedev/config-types";
2
2
  import * as followRedirects from "follow-redirects";
3
3
  import { HttpProxyAgent } from "http-proxy-agent";
4
4
  import { HttpsProxyAgent } from "https-proxy-agent";
5
- import fetch, { RequestInit, Response } from "node-fetch";
5
+ import fetch, { BodyInit, RequestInit, Response } from "node-fetch";
6
6
  import { getAgentOptions } from "./getAgentOptions.js";
7
7
  import { getProxyFromEnv, shouldBypassProxy } from "./util.js";
8
8
 
9
9
  const { http, https } = (followRedirects as any).default;
10
10
 
11
+ function logRequest(
12
+ method: string,
13
+ url: URL,
14
+ headers: { [key: string]: string },
15
+ body: BodyInit | null | undefined,
16
+ proxy?: string,
17
+ shouldBypass?: boolean,
18
+ ) {
19
+ console.log("=== FETCH REQUEST ===");
20
+ console.log(`Method: ${method}`);
21
+ console.log(`URL: ${url.toString()}`);
22
+
23
+ // Log headers in curl format
24
+ console.log("Headers:");
25
+ for (const [key, value] of Object.entries(headers)) {
26
+ console.log(` -H '${key}: ${value}'`);
27
+ }
28
+
29
+ // Log proxy information
30
+ if (proxy && !shouldBypass) {
31
+ console.log(`Proxy: ${proxy}`);
32
+ }
33
+
34
+ // Log body
35
+ if (body) {
36
+ console.log(`Body: ${body}`);
37
+ }
38
+
39
+ // Generate equivalent curl command
40
+ let curlCommand = `curl -X ${method}`;
41
+ for (const [key, value] of Object.entries(headers)) {
42
+ curlCommand += ` -H '${key}: ${value}'`;
43
+ }
44
+ if (body) {
45
+ curlCommand += ` -d '${body}'`;
46
+ }
47
+ if (proxy && !shouldBypass) {
48
+ curlCommand += ` --proxy '${proxy}'`;
49
+ }
50
+ curlCommand += ` '${url.toString()}'`;
51
+ console.log(`Equivalent curl: ${curlCommand}`);
52
+ console.log("=====================");
53
+ }
54
+
55
+ async function logResponse(resp: Response) {
56
+ console.log("=== FETCH RESPONSE ===");
57
+ console.log(`Status: ${resp.status} ${resp.statusText}`);
58
+ console.log("Response Headers:");
59
+ resp.headers.forEach((value, key) => {
60
+ console.log(` ${key}: ${value}`);
61
+ });
62
+
63
+ // TODO: For streamed responses, this caused the response to be consumed and the connection would just hang open
64
+ // Clone response to read body without consuming it
65
+ // const respClone = resp.clone();
66
+ // try {
67
+ // const responseText = await respClone.text();
68
+ // console.log(`Response Body: ${responseText}`);
69
+ // } catch (e) {
70
+ // console.log("Could not read response body:", e);
71
+ // }
72
+ console.log("======================");
73
+ }
74
+
75
+ function logError(error: unknown) {
76
+ console.log("=== FETCH ERROR ===");
77
+ console.log(`Error: ${error}`);
78
+ console.log("===================");
79
+ }
80
+
11
81
  export async function fetchwithRequestOptions(
12
82
  url_: URL | string,
13
83
  init?: RequestInit,
@@ -68,15 +138,28 @@ export async function fetchwithRequestOptions(
68
138
  console.log("Unable to parse HTTP request body: ", e);
69
139
  }
70
140
 
141
+ const finalBody = updatedBody ?? init?.body;
142
+ const method = init?.method || "GET";
143
+
144
+ // Verbose logging for debugging - log request details
145
+ if (process.env.VERBOSE_FETCH) {
146
+ logRequest(method, url, headers, finalBody, proxy, shouldBypass);
147
+ }
148
+
71
149
  // fetch the request with the provided options
72
150
  try {
73
151
  const resp = await fetch(url, {
74
152
  ...init,
75
- body: updatedBody ?? init?.body,
153
+ body: finalBody,
76
154
  headers: headers,
77
155
  agent: agent,
78
156
  });
79
157
 
158
+ // Verbose logging for debugging - log response details
159
+ if (process.env.VERBOSE_FETCH) {
160
+ await logResponse(resp);
161
+ }
162
+
80
163
  if (!resp.ok) {
81
164
  const requestId = resp.headers.get("x-request-id");
82
165
  if (requestId) {
@@ -86,6 +169,11 @@ export async function fetchwithRequestOptions(
86
169
 
87
170
  return resp;
88
171
  } catch (error) {
172
+ // Verbose logging for errors
173
+ if (process.env.VERBOSE_FETCH) {
174
+ logError(error);
175
+ }
176
+
89
177
  if (error instanceof Error && error.name === "AbortError") {
90
178
  // Return a Response object that streamResponse etc can handle
91
179
  return new Response(null, {