|
| 1 | +// WebSocket API types (outgoing / client-side) |
| 2 | + |
| 3 | +export {}; |
| 4 | + |
| 5 | +declare global { |
| 6 | + /** |
| 7 | + * Event delivered when a WebSocket receives a message. |
| 8 | + */ |
| 9 | + interface WebSocketMessageEvent { |
| 10 | + /** The event type, always "message". */ |
| 11 | + readonly type: "message"; |
| 12 | + |
| 13 | + /** The message data (string for text frames, ArrayBuffer for binary). */ |
| 14 | + readonly data: string | ArrayBuffer; |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Event delivered when a WebSocket connection is closed. |
| 19 | + */ |
| 20 | + interface WebSocketCloseEvent { |
| 21 | + /** The event type, always "close". */ |
| 22 | + readonly type: "close"; |
| 23 | + |
| 24 | + /** The close code (e.g. 1000 for normal closure). */ |
| 25 | + readonly code: number; |
| 26 | + |
| 27 | + /** The close reason string. */ |
| 28 | + readonly reason: string; |
| 29 | + |
| 30 | + /** Whether the close was clean (proper close handshake). */ |
| 31 | + readonly wasClean: boolean; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Event delivered when a WebSocket encounters an error. |
| 36 | + */ |
| 37 | + interface WebSocketErrorEvent { |
| 38 | + /** The event type, always "error". */ |
| 39 | + readonly type: "error"; |
| 40 | + |
| 41 | + /** The error message. */ |
| 42 | + readonly message: string; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Listener types for WebSocket events. |
| 47 | + */ |
| 48 | + interface WebSocketEventMap { |
| 49 | + message: WebSocketMessageEvent; |
| 50 | + close: WebSocketCloseEvent; |
| 51 | + error: WebSocketErrorEvent; |
| 52 | + open: Event; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * A client-side WebSocket connection. |
| 57 | + * |
| 58 | + * Obtained from a fetch() response when using the `Upgrade: websocket` header. |
| 59 | + * Must call `accept()` before sending or receiving messages. |
| 60 | + * |
| 61 | + * @example |
| 62 | + * ```ts |
| 63 | + * const resp = await fetch("wss://echo.example.com", { |
| 64 | + * headers: { Upgrade: "websocket" }, |
| 65 | + * }); |
| 66 | + * |
| 67 | + * const ws = resp.webSocket; |
| 68 | + * if (!ws) throw new Error("Server didn't accept WebSocket"); |
| 69 | + * |
| 70 | + * ws.accept(); |
| 71 | + * ws.send("hello"); |
| 72 | + * |
| 73 | + * ws.addEventListener("message", (event) => { |
| 74 | + * console.log("Received:", event.data); |
| 75 | + * }); |
| 76 | + * |
| 77 | + * ws.addEventListener("close", (event) => { |
| 78 | + * console.log("Closed:", event.code, event.reason); |
| 79 | + * }); |
| 80 | + * ``` |
| 81 | + */ |
| 82 | + class WebSocket { |
| 83 | + /** |
| 84 | + * Accept the WebSocket connection and start receiving messages. |
| 85 | + * Must be called before `send()` or messages will be buffered. |
| 86 | + */ |
| 87 | + accept(): void; |
| 88 | + |
| 89 | + /** |
| 90 | + * Send a message to the server. |
| 91 | + * @param data Text string or binary data to send. |
| 92 | + * @throws If `accept()` has not been called yet. |
| 93 | + */ |
| 94 | + send(data: string | ArrayBuffer | ArrayBufferView): void; |
| 95 | + |
| 96 | + /** |
| 97 | + * Close the WebSocket connection. |
| 98 | + * @param code Close status code (default: 1000). |
| 99 | + * @param reason Human-readable close reason (default: ""). |
| 100 | + */ |
| 101 | + close(code?: number, reason?: string): void; |
| 102 | + |
| 103 | + /** |
| 104 | + * Register an event listener. |
| 105 | + * @param type The event type: "message", "close", "error", or "open". |
| 106 | + * @param listener The callback function. |
| 107 | + */ |
| 108 | + addEventListener<K extends keyof WebSocketEventMap>( |
| 109 | + type: K, |
| 110 | + listener: (event: WebSocketEventMap[K]) => void |
| 111 | + ): void; |
| 112 | + |
| 113 | + /** |
| 114 | + * Remove a previously registered event listener. |
| 115 | + * @param type The event type. |
| 116 | + * @param listener The callback to remove. |
| 117 | + */ |
| 118 | + removeEventListener<K extends keyof WebSocketEventMap>( |
| 119 | + type: K, |
| 120 | + listener: (event: WebSocketEventMap[K]) => void |
| 121 | + ): void; |
| 122 | + } |
| 123 | + |
| 124 | + interface Response { |
| 125 | + /** |
| 126 | + * The WebSocket connection, present when the response is a WebSocket upgrade |
| 127 | + * (status 101). `null` or `undefined` for normal HTTP responses. |
| 128 | + * |
| 129 | + * @example |
| 130 | + * ```ts |
| 131 | + * const resp = await fetch(url, { headers: { Upgrade: "websocket" } }); |
| 132 | + * if (resp.webSocket) { |
| 133 | + * resp.webSocket.accept(); |
| 134 | + * } |
| 135 | + * ``` |
| 136 | + */ |
| 137 | + readonly webSocket?: WebSocket; |
| 138 | + } |
| 139 | +} |
0 commit comments