1 | import { WebSocketClientConnectionProtocol, WebSocketData } from '@mswjs/interceptors/WebSocket';
|
2 | export { WebSocketData } from '@mswjs/interceptors/WebSocket';
|
3 | import { WebSocketHandlerEventMap, WebSocketHandler } from './handlers/WebSocketHandler.js';
|
4 | import { Path } from './utils/matching/matchRequestUrl.js';
|
5 | import 'strict-event-emitter';
|
6 |
|
7 | type WebSocketEventListener<EventType extends keyof WebSocketHandlerEventMap> = (...args: WebSocketHandlerEventMap[EventType]) => void;
|
8 | type WebSocketLink = {
|
9 | /**
|
10 | * A set of all WebSocket clients connected
|
11 | * to this link.
|
12 | *
|
13 | * @see {@link https://mswjs.io/docs/api/ws#clients `clients` API reference}
|
14 | */
|
15 | clients: Set<WebSocketClientConnectionProtocol>;
|
16 | /**
|
17 | * Adds an event listener to this WebSocket link.
|
18 | *
|
19 | * @example
|
20 | * const chat = ws.link('wss://chat.example.com')
|
21 | * chat.addEventListener('connection', listener)
|
22 | *
|
23 | * @see {@link https://mswjs.io/docs/api/ws#onevent-listener `on()` API reference}
|
24 | */
|
25 | addEventListener<EventType extends keyof WebSocketHandlerEventMap>(event: EventType, listener: WebSocketEventListener<EventType>): WebSocketHandler;
|
26 | /**
|
27 | * Broadcasts the given data to all WebSocket clients.
|
28 | *
|
29 | * @example
|
30 | * const service = ws.link('wss://example.com')
|
31 | * service.addEventListener('connection', () => {
|
32 | * service.broadcast('hello, everyone!')
|
33 | * })
|
34 | *
|
35 | * @see {@link https://mswjs.io/docs/api/ws#broadcastdata `broadcast()` API reference}
|
36 | */
|
37 | broadcast(data: WebSocketData): void;
|
38 | /**
|
39 | * Broadcasts the given data to all WebSocket clients
|
40 | * except the ones provided in the `clients` argument.
|
41 | *
|
42 | * @example
|
43 | * const service = ws.link('wss://example.com')
|
44 | * service.addEventListener('connection', ({ client }) => {
|
45 | * service.broadcastExcept(client, 'hi, the rest of you!')
|
46 | * })
|
47 | *
|
48 | * @see {@link https://mswjs.io/docs/api/ws#broadcastexceptclients-data `broadcast()` API reference}
|
49 | */
|
50 | broadcastExcept(clients: WebSocketClientConnectionProtocol | Array<WebSocketClientConnectionProtocol>, data: WebSocketData): void;
|
51 | };
|
52 | /**
|
53 | * Intercepts outgoing WebSocket connections to the given URL.
|
54 | *
|
55 | * @example
|
56 | * const chat = ws.link('wss://chat.example.com')
|
57 | * chat.addEventListener('connection', ({ client }) => {
|
58 | * client.send('hello from server!')
|
59 | * })
|
60 | */
|
61 | declare function createWebSocketLinkHandler(url: Path): WebSocketLink;
|
62 | /**
|
63 | * A namespace to intercept and mock WebSocket connections.
|
64 | *
|
65 | * @example
|
66 | * const chat = ws.link('wss://chat.example.com')
|
67 | *
|
68 | * @see {@link https://mswjs.io/docs/api/ws `ws` API reference}
|
69 | * @see {@link https://mswjs.io/docs/basics/handling-websocket-events Handling WebSocket events}
|
70 | */
|
71 | declare const ws: {
|
72 | link: typeof createWebSocketLinkHandler;
|
73 | };
|
74 |
|
75 | export { type WebSocketEventListener, type WebSocketLink, ws };
|