Skip to content

feat: migrate API client and codebase to native fetch (Part 1)#10723

Open
joehan wants to merge 1 commit into
mainfrom
refactor/migrate-to-native-fetch-part1
Open

feat: migrate API client and codebase to native fetch (Part 1)#10723
joehan wants to merge 1 commit into
mainfrom
refactor/migrate-to-native-fetch-part1

Conversation

@joehan

@joehan joehan commented Jun 25, 2026

Copy link
Copy Markdown
Member

Description

This is Part 1 of the native fetch migration. It contains the core codebase changes.

  • Migrated the central API client (src/apiv2.ts) to use modern, native globalThis.fetch in production.
  • Implemented robust request/response proxying, multiple Set-Cookie header forwarding, and location redirection fixes in the hosting proxy middleware.
  • Updated production modules to native fetch.
  • Restored node-fetch and nock devDependencies to maintain 100% backward compatibility for all existing unit tests under the test runner.
  • Aligned lockfile/shrinkwrap with the projects node/npm configuration to pass the lockfile check.

This PR is the base for the stacked PRs:

  • Part 1 (This PR): Core codebase migration.
  • Part 2: Unit test suite migration and custom mock helper.
  • Part 3: Integration and VS Code extension tests.

@wiz-9635d3485b

wiz-9635d3485b Bot commented Jun 25, 2026

Copy link
Copy Markdown

Wiz Scan Summary

Scanner Findings
Vulnerability Finding Vulnerabilities -
Data Finding Sensitive Data -
Secret Finding Secrets -
IaC Misconfiguration IaC Misconfigurations -
SAST Finding SAST Findings 1 Medium
Software Management Finding Software Management Findings -
Total 1 Medium

View scan details in Wiz

To detect these findings earlier in the dev lifecycle, try using Wiz Code VS Code Extension.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request migrates the codebase from 'node-fetch' and 'abort-controller' to 'undici' for HTTP requests, updating proxy agent configurations, request signals, stream handling, and error mapping across multiple modules. Feedback on these changes highlights opportunities to improve type safety by avoiding 'any' in 'FetchOptions', adhering to the repository style guide by replacing 'console.error' with the central 'logger' in 'src/hosting/proxy.ts', and adding a defensive check in 'UndiciResponseCompat.text()' to prevent potential runtime errors when the response body is undefined.

Comment thread src/apiv2.ts
Comment on lines +200 to +204
interface FetchOptions extends RequestInit {
dispatcher?: ProxyAgent;
duplex?: "half";
agent?: any;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Avoid using any as an escape hatch for the agent property. Since noKeepAliveAgent is a function returning http.Agent | https.Agent, we can type this property more precisely to adhere to the repository style guide.

Suggested change
interface FetchOptions extends RequestInit {
dispatcher?: ProxyAgent;
duplex?: "half";
agent?: any;
}
interface FetchOptions extends RequestInit {
dispatcher?: ProxyAgent;
duplex?: "half";
agent?: http.Agent | https.Agent | ((parsedURL: URL) => http.Agent | https.Agent);
}
References
  1. Never use any or unknown as an escape hatch. Define proper interfaces/types or use type guards. (link)

Comment thread src/hosting/proxy.ts
Comment on lines 107 to 109
} catch (err: any) {
console.error("[PROXY ERROR]", err);
const isAbortError =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use the central logger instead of console.error for logging errors to maintain consistency and adhere to the repository style guide.

Suggested change
} catch (err: any) {
console.error("[PROXY ERROR]", err);
const isAbortError =
} catch (err: any) {
logger.error("[PROXY ERROR]", err);
const isAbortError =
References
  1. Use the central logger (src/logger.ts); never use console.log() for user-facing output. (link)

Comment thread src/apiv2.ts
Comment on lines +710 to +713
async text(): Promise<string> {
const { streamToString } = require("./utils");
return streamToString(this.body);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add a defensive check to ensure this.body is defined before passing it to streamToString. If this.body is null or undefined (e.g., for a 204 No Content response), calling streamToString(this.body) would throw a TypeError.

  async text(): Promise<string> {
    if (!this.body) {
      return "";
    }
    const { streamToString } = require("./utils");
    return streamToString(this.body);
  }

@joehan joehan force-pushed the refactor/migrate-to-native-fetch-part1 branch from 4d2c3cd to 17625de Compare June 25, 2026 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants