-
Notifications
You must be signed in to change notification settings - Fork 299
feat(defineWebSocketHandler): support callback with event
#1242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
There's also |
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
pi0
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks ❤️
event
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/utils/ws.ts (1)
24-28: Add@paramJSDoc tag for documentation generation.The JSDoc comment lacks a
@paramtag, which likely causes the auto-generated documentation to show an empty signaturedefineWebSocketHandler(). Adding parameter documentation will improve both the generated docs and IDE intellisense./** * Define WebSocket event handler. * + * @param hooks - WebSocket hooks object or a function that receives an H3Event and returns hooks * @see https://h3.dev/guide/websocket */test/ws.test.ts (1)
23-30: Test looks good; consider adding async function test.The test correctly verifies the synchronous function argument case. Since the type signature supports
Promise<Partial<WebSocketHooks>>, consider adding a test for the async case to ensure the Promise is correctly passed through.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); }); + + it("should attach the provided hooks with async function argument", async () => { + const wsHandler = defineWebSocketHandler(async () => hooks); + const res = wsHandler({} as any); + expect(res).toBeInstanceOf(Response); + expect((res as Response).status).toBe(426); + // crossws should be a Promise that resolves to hooks + await expect((res as any).crossws).resolves.toEqual(hooks); + }); });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
docs/2.utils/9.more.md(1 hunks)src/utils/ws.ts(2 hunks)test/ws.test.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/utils/ws.ts (4)
src/index.ts (4)
WebSocketHooks(230-230)H3Event(24-24)EventHandler(35-35)defineHandler(52-52)src/event.ts (1)
H3Event(33-144)src/types/handler.ts (1)
EventHandler(12-18)src/handler.ts (1)
defineHandler(36-60)
🔇 Additional comments (1)
src/utils/ws.ts (1)
29-47: Implementation is correct; async hooks are properly supported by h3 and crossws.The logic correctly distinguishes between direct hooks and function-based hooks. The type signature supports returning
Promise<Partial<WebSocketHooks>>, and h3's event handler system explicitly awaits Promise results returned from handlers. Similarly, crossws's upgrade and lifecycle hooks support async execution, so passing a Promise to thecrosswsproperty is the intended pattern for lazy or async hook resolution.
| Define WebSocket hooks. | ||
|
|
||
| ### `defineWebSocketHandler(hooks)` | ||
| ### `defineWebSocketHandler()` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation signature appears incomplete.
The auto-generated signature shows defineWebSocketHandler() with no parameters, but the implementation accepts a required hooks parameter. This may be an issue with the JSDoc comment in src/utils/ws.ts not documenting the parameter, causing automd to generate an incomplete signature.
Consider adding a @param JSDoc tag to the function in src/utils/ws.ts to ensure the documentation reflects the actual API.
🤖 Prompt for AI Agents
In docs/2.utils/9.more.md around line 84, the generated signature for
defineWebSocketHandler() is missing its required parameter because the
implementation in src/utils/ws.ts does not document the parameter; update
src/utils/ws.ts by adding a JSDoc @param tag for the required hooks argument
(describe its expected type/shape and whether properties are optional), include
a short description and an example if helpful, and ensure the function JSDoc
block is directly above the function so the docs generator picks it up.
event event
|
Thank you, @dardanbujupaj @pi0 😍 |
This PR adds an alternative parameter type for
defineWebsocketHandler, so that we can accesseventwhen defining crossws hooks.This allows us to use methods like
getRouterParam(event, name)similar to http handlers.It also resolves #715.
I saw that there's also #1149 open, which might relate to this. But I think this should just work together.
Example
Use router param to subscribe/publish to a topic.
Summary by CodeRabbit
New Features
defineWebSocketHandlernow accepts hooks as either static configuration or as a dynamic function that resolves hooks per request.Documentation
Tests
✏️ Tip: You can customize this high-level summary in your review settings.