From 8779d46b418f78ead8e203379118bcc9a971082e Mon Sep 17 00:00:00 2001 From: Dardan Bujupaj Date: Sun, 16 Nov 2025 20:58:30 +0100 Subject: [PATCH 1/3] feat: allow event callback as parameter to defineWebSocketHandler --- src/utils/ws.ts | 13 +++++++++---- test/ws.test.ts | 9 +++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/utils/ws.ts b/src/utils/ws.ts index e50e809fa..09b63ecff 100644 --- a/src/utils/ws.ts +++ b/src/utils/ws.ts @@ -1,12 +1,13 @@ import { defineHandler } from "../handler.ts"; import type { Hooks as WebSocketHooks } from "crossws"; +import type { H3Event } from "../event.ts"; import type { EventHandler } from "../types/handler.ts"; export type { Hooks as WebSocketHooks, - Peer as WebSocketPeer, Message as WebSocketMessage, + Peer as WebSocketPeer, } from "crossws"; /** @@ -26,15 +27,19 @@ export function defineWebSocket( * @see https://h3.dev/guide/websocket */ export function defineWebSocketHandler( - hooks: Partial, + hooks: + | Partial + | ((event: H3Event) => Partial), ): EventHandler { - return defineHandler(function _webSocketHandler() { + return defineHandler(function _webSocketHandler(event) { + const crossws = typeof hooks === "function" ? hooks(event) : hooks; + return Object.assign( new Response("WebSocket upgrade is required.", { status: 426, }), { - crossws: hooks, + crossws, }, ); }); diff --git a/test/ws.test.ts b/test/ws.test.ts index 87136bf63..3c666d1f4 100644 --- a/test/ws.test.ts +++ b/test/ws.test.ts @@ -19,4 +19,13 @@ describe("defineWebSocketHandler", () => { // expect((res as Response).statusText).toBe("Upgrade Required"); expect((res as any).crossws).toEqual(hooks); }); + + it("should attach the provided hooks with function argument", () => { + const wsHandler = defineWebSocketHandler(() => hooks); + const res = wsHandler({} as any); + expect(res).toBeInstanceOf(Response); + expect((res as Response).status).toBe(426); + // expect((res as Response).statusText).toBe("Upgrade Required"); + expect((res as any).crossws).toEqual(hooks); + }); }); From 67621cfa86142aa11de5821a0c6a18904062cc8a Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sun, 16 Nov 2025 22:27:29 +0000 Subject: [PATCH 2/3] chore: apply automated updates --- docs/2.utils/9.more.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/2.utils/9.more.md b/docs/2.utils/9.more.md index 925922ca6..8e22cf648 100644 --- a/docs/2.utils/9.more.md +++ b/docs/2.utils/9.more.md @@ -81,7 +81,7 @@ You can return a new Response from the handler to replace the original response. Define WebSocket hooks. -### `defineWebSocketHandler(hooks)` +### `defineWebSocketHandler()` Define WebSocket event handler. From 1f30c7475dc89dc62f734a8545089d438eb022f8 Mon Sep 17 00:00:00 2001 From: Dardan Bujupaj Date: Mon, 17 Nov 2025 00:10:57 +0100 Subject: [PATCH 3/3] fix: add promise return type for websocket handler hooks --- src/utils/ws.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/ws.ts b/src/utils/ws.ts index 09b63ecff..cd87cc954 100644 --- a/src/utils/ws.ts +++ b/src/utils/ws.ts @@ -29,7 +29,9 @@ export function defineWebSocket( export function defineWebSocketHandler( hooks: | Partial - | ((event: H3Event) => Partial), + | (( + event: H3Event, + ) => Partial | Promise>), ): EventHandler { return defineHandler(function _webSocketHandler(event) { const crossws = typeof hooks === "function" ? hooks(event) : hooks;