From 96fc4d811e94ab4e02c5ada8c689ffe4ef864efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominykas=20Blyz=CC=8Ce=CC=87?= Date: Mon, 6 Jan 2025 18:00:34 +0200 Subject: [PATCH 1/2] fix: declare correct ws options type The original string | string[] seems to have been a mistake because the ws package client takes 3 arguments, of which the middle one (protocols) is the string | string[], but it is optional. When it is left out - it becomes the options object instead: https://github.com/websockets/ws/blob/c798dd4ee20efb2d7591b5659839ad05cdb3eb70/lib/websocket.js#L80 The type information is covered in jsdoc: https://github.com/websockets/ws/blob/c798dd4ee20efb2d7591b5659839ad05cdb3eb70/lib/websocket.js#L627 A more complete typing information is exposed by @types/ws, though. --- lib/client.d.ts | 7 +++++-- package.json | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/client.d.ts b/lib/client.d.ts index 76a622c..69c20cf 100644 --- a/lib/client.d.ts +++ b/lib/client.d.ts @@ -1,3 +1,6 @@ +import type { ClientRequestArgs } from "http"; +import type { ClientOptions } from "ws"; + // Same as exported type in @hapi/hapi v20 type HTTP_METHODS = 'ACL' | 'BIND' | 'CHECKOUT' | 'CONNECT' | 'COPY' | 'DELETE' | 'GET' | 'HEAD' | 'LINK' | 'LOCK' | 'M-SEARCH' | 'MERGE' | 'MKACTIVITY' | 'MKCALENDAR' | 'MKCOL' | 'MOVE' | 'NOTIFY' | 'OPTIONS' | 'PATCH' | 'POST' | @@ -173,7 +176,7 @@ export class Client { constructor( url: `ws://${string}` | `wss://${string}`, options?: { - ws?: string | string[]; + ws?: ClientOptions | ClientRequestArgs; timeout?: number | boolean; }); @@ -369,4 +372,4 @@ export class Client { * `client.connect()` */ reauthenticate(auth: ClientConnectOptions['auth']): Promise; -} \ No newline at end of file +} diff --git a/package.json b/package.json index 30ee07e..be6894e 100755 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@hapi/iron": "^7.0.1", "@hapi/teamwork": "^6.0.0", "@hapi/validate": "^2.0.1", + "@types/ws": "^8.5.13", "ws": "^8.17.1" }, "devDependencies": { From 66dd37a928d66dea5043aa6fdd4acb613f1ede9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominykas=20Blyz=CC=8Ce=CC=87?= Date: Tue, 7 Jan 2025 12:46:37 +0200 Subject: [PATCH 2/2] test: add Ws.Client constructor types test --- test/types/client.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/types/client.ts b/test/types/client.ts index d8bdf77..293379f 100644 --- a/test/types/client.ts +++ b/test/types/client.ts @@ -7,7 +7,13 @@ import { Client } from '../../lib/client'; const init = () => { - const client = new Client('ws://localhost'); + const client = new Client('ws://localhost', { + ws: { // optional + origin: 'http://localhost:12345', + maxPayload: 1000, + headers: { cookie: 'xnes=123' } + } + }); client.connect()