From 7389b4cad723cd472335f4034c03bd771676a200 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Sat, 20 Jul 2024 23:01:34 +0900 Subject: [PATCH] fix(client): support array values for `query` in `ws` (#3169) Co-authored-by: Alber Tenez --- src/client/client.test.ts | 3 ++- src/client/client.ts | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/client/client.test.ts b/src/client/client.test.ts index e01931a1..e9c91877 100644 --- a/src/client/client.test.ts +++ b/src/client/client.test.ts @@ -1079,9 +1079,10 @@ describe('WebSocket URL Protocol Translation with Query Parameters', () => { query: { id: '123', type: 'test', + tag: ['a', 'b'], }, }) - expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index?id=123&type=test') + expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index?id=123&type=test&tag=a&tag=b') }) it('Translates HTTPS to wss and includes query parameters', async () => { diff --git a/src/client/client.ts b/src/client/client.ts index a5c29602..acd0ddad 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -183,8 +183,16 @@ export const hc = >( 'ws' ) const targetUrl = new URL(webSocketUrl) - for (const key in opts.args[0]?.query) { - targetUrl.searchParams.set(key, opts.args[0].query[key]) + + const queryParams: Record | undefined = opts.args[0]?.query + if (queryParams) { + Object.entries(queryParams).forEach(([key, value]) => { + if (Array.isArray(value)) { + value.forEach((item) => targetUrl.searchParams.append(key, item)) + } else { + targetUrl.searchParams.set(key, value) + } + }) } return new WebSocket(targetUrl.toString())