Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/pass-client-logger-to-request-handler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@smithy/core": minor
"@smithy/node-http-handler": minor
"@smithy/undici-http-handler": minor
---

feat: pass client logger to request handlers
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, it, vi } from "vitest";

import { getHttpHandlerExtensionConfiguration } from "./httpExtensionConfiguration";

describe("getHttpHandlerExtensionConfiguration", () => {
const createMockHandler = () => ({
metadata: { handlerProtocol: "http/1.1" },
handle: vi.fn(),
updateHttpClientConfig: vi.fn(),
httpHandlerConfigs: vi.fn().mockReturnValue({}),
});

const createMockLogger = () => ({
trace: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
});

describe("client logger injection", () => {
it("passes logger to httpHandler via updateHttpClientConfig", () => {
const handler = createMockHandler();
const logger = createMockLogger();

getHttpHandlerExtensionConfiguration({ httpHandler: handler, logger } as any);

expect(handler.updateHttpClientConfig).toHaveBeenCalledWith("logger", logger);
});

it("does not call updateHttpClientConfig when logger is not set", () => {
const handler = createMockHandler();

getHttpHandlerExtensionConfiguration({ httpHandler: handler } as any);

expect(handler.updateHttpClientConfig).not.toHaveBeenCalled();
});

it("does not throw when no handler is present", () => {
const logger = createMockLogger();

// should not throw
getHttpHandlerExtensionConfiguration({ logger } as any);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Logger } from "@smithy/types";

import type { HttpHandler } from "../httpHandler";

/**
Expand All @@ -22,9 +24,13 @@ export type HttpHandlerExtensionConfigType<HandlerConfig extends object = {}> =
*
* @internal
*/
export const getHttpHandlerExtensionConfiguration = <HandlerConfig extends object = {}>(
export const getHttpHandlerExtensionConfiguration = <HandlerConfig extends { logger?: Logger }>(
runtimeConfig: HttpHandlerExtensionConfigType<HandlerConfig>
) => {
if ((runtimeConfig as any).logger) {
runtimeConfig.httpHandler?.updateHttpClientConfig("logger" as keyof HandlerConfig, (runtimeConfig as any).logger);
}

return {
setHttpHandler(handler: HttpHandler<HandlerConfig>): void {
runtimeConfig.httpHandler = handler;
Expand Down
34 changes: 34 additions & 0 deletions packages/node-http-handler/src/node-http-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,40 @@ describe("NodeHttpHandler", () => {
});
});

describe("updateHttpClientConfig", () => {
it("updates the logger", async () => {
const handler = new NodeHttpHandler();

const clientLogger = { trace: vi.fn(), debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() };
handler.updateHttpClientConfig("logger", clientLogger);

const request = new HttpRequest({ hostname: "localhost", method: "GET", protocol: "https:", path: "/" });
try {
await handler.handle(request);
} catch {
// ignore request errors
}

const configs = handler.httpHandlerConfigs();
expect(configs.logger).toBe(clientLogger);
});

it("updates non-logger keys", async () => {
const handler = new NodeHttpHandler({ requestTimeout: 1000 });
handler.updateHttpClientConfig("requestTimeout", 5000);

const request = new HttpRequest({ hostname: "localhost", method: "GET", protocol: "https:", path: "/" });
try {
await handler.handle(request);
} catch {
// ignore request errors
}

const configs = handler.httpHandlerConfigs();
expect(configs.requestTimeout).toBe(5000);
});
});

describe("checkSocketUsage", () => {
beforeEach(() => {
vi.spyOn(console, "warn").mockImplementation(vi.fn() as any);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,12 @@ describe("UndiciHttpHandler", () => {
expect(configs.logger).toBe(logger);
});

it("updates config", async () => {
it("updates logger via updateHttpClientConfig", async () => {
const logger = createMockLogger();
const updatedLogger = createMockLogger();
handler = new UndiciHttpHandler({ logger });
await handler.handle(createMockRequest());
handler.updateHttpClientConfig("logger", updatedLogger);
// Config is reset, need another request to resolve
await handler.handle(createMockRequest());
expect(handler.httpHandlerConfigs().logger).toBe(updatedLogger);
});
Expand Down