-
Notifications
You must be signed in to change notification settings - Fork 338
feat: experimental defineWebSocketRoute #1149
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
86308f0
902f8c7
fd6f13d
4369b94
81eec94
2ca3e4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ import type { HTTPMethod } from "../types/h3.ts"; | |
| import type { EventHandler, Middleware } from "../types/handler.ts"; | ||
| import type { H3Plugin, H3 } from "../types/h3.ts"; | ||
| import type { StandardSchemaV1 } from "./internal/standard-schema.ts"; | ||
| import type { Hooks as WSHooks } from "crossws"; | ||
| import { defineValidatedHandler } from "../handler.ts"; | ||
| import { defineWebSocketHandler } from "./ws.ts"; | ||
|
|
||
| /** | ||
| * Route definition options | ||
|
|
@@ -69,3 +71,65 @@ export function defineRoute(def: RouteDefinition): H3Plugin { | |
| h3.on(def.method, def.route, handler); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * WebSocket route definition options | ||
| */ | ||
| export interface WebSocketRouteDefinition { | ||
| /** | ||
| * HTTP method for the route (typically 'GET' for WebSocket upgrades) | ||
| */ | ||
| method?: HTTPMethod; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WebSocket handlers should not accept a method. |
||
|
|
||
| /** | ||
| * Route pattern, e.g. '/api/ws' | ||
| */ | ||
| route: string; | ||
|
|
||
| /** | ||
| * WebSocket hooks | ||
| */ | ||
| websocket: Partial<WSHooks>; | ||
|
pi0 marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Optional middleware to run before WebSocket upgrade | ||
| */ | ||
| middleware?: Middleware[]; | ||
|
|
||
| /** | ||
| * Additional route metadata | ||
| */ | ||
| meta?: Record<string, unknown>; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Middleware and meta are not supported. Types should be removed (at least untill they are supported by
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should take the comment line and leave a TODO ? |
||
| } | ||
|
|
||
| /** | ||
| * Define a WebSocket route as a plugin that can be registered with app.register() | ||
| * | ||
| * @example | ||
| * ```js | ||
| * const wsRoute = defineWebSocketRoute({ | ||
| * route: '/api/ws', | ||
| * websocket: { | ||
| * open: (peer) => { | ||
| * console.log('WebSocket connected:', peer.id); | ||
| * peer.send('Welcome!'); | ||
| * }, | ||
| * message: (peer, message) => { | ||
| * console.log('Received:', message); | ||
| * peer.send(`Echo: ${message}`); | ||
| * }, | ||
| * close: (peer) => { | ||
| * console.log('WebSocket closed:', peer.id); | ||
| * } | ||
| * } | ||
| * }); | ||
| * | ||
| * app.register(wsRoute); | ||
| * ``` | ||
| */ | ||
| export function defineWebSocketRoute(def: WebSocketRouteDefinition): H3Plugin { | ||
| const handler = defineWebSocketHandler(def.websocket); | ||
| return (h3: H3) => { | ||
| h3.on(def.method || "GET", def.route, handler); | ||
| }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.