Compare commits

...

2 Commits

Author SHA1 Message Date
autofix-ci[bot]
53eaedac70 [autofix.ci] apply automated fixes 2025-08-19 01:31:46 +00:00
Claude Bot
782592520d Fix undici Client.request() returning undefined
The undici Client class had an empty request() method that was not properly
implemented, causing client.request() to return undefined instead of the
expected response object.

Fixed by:
- Adding constructor to accept base URL
- Implementing request() method to resolve paths against the base URL
- Delegating to the global request() function for actual HTTP handling

Fixes #21944

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-19 01:25:47 +00:00
2 changed files with 67 additions and 2 deletions

View File

@@ -269,7 +269,27 @@ class Pool extends Dispatcher {
}
class BalancedPool extends Dispatcher {}
class Client extends Dispatcher {
request() {}
#url;
constructor(url, options = {}) {
super();
this.#url = url;
}
async request(options = {}) {
// If path is provided, resolve it against the base URL
let requestUrl = this.#url;
if (options.path) {
if (typeof this.#url === "string") {
requestUrl = new URL(options.path, this.#url).toString();
} else {
requestUrl = new URL(options.path, this.#url);
}
}
// Call the global request function with the resolved URL
return await request(requestUrl, options);
}
}
class DispatcherBase extends EventEmitter {}

View File

@@ -1,5 +1,5 @@
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
import { request } from "undici";
import { Client, request } from "undici";
import { createServer } from "../../../http-test-server";
@@ -155,4 +155,49 @@ describe("undici", () => {
// expect(json.form.foo).toBe("bar");
// });
});
describe("Client", () => {
it("should create a client with a base URL and make requests", async () => {
const client = new Client(hostUrl);
const { body, statusCode } = await client.request({
path: "/get",
method: "GET",
});
expect(statusCode).toBe(200);
expect(body).toBeDefined();
const json = (await body.json()) as { url: string };
expect(json.url).toBe(`${hostUrl}/get`);
});
it("should handle relative paths correctly", async () => {
const client = new Client(hostUrl);
const { body, statusCode } = await client.request({
path: "/headers",
method: "GET",
headers: {
"x-test": "client-test",
},
});
expect(statusCode).toBe(200);
expect(body).toBeDefined();
const json = (await body.json()) as { headers: { "x-test": string } };
expect(json.headers["x-test"]).toBe("client-test");
});
it("should work with POST requests", async () => {
const client = new Client(hostUrl);
const { body, statusCode } = await client.request({
path: "/post",
method: "POST",
body: "test body",
});
expect(statusCode).toBe(201);
expect(body).toBeDefined();
const json = (await body.json()) as { data: string };
expect(json.data).toBe("test body");
});
});
});