Skip to content

Commit

Permalink
fix(client): support array values for query in ws (#3169)
Browse files Browse the repository at this point in the history
Co-authored-by: Alber Tenez <albert@zenettech.com>
  • Loading branch information
yusukebe and AlbertSabate authored Jul 20, 2024
1 parent 3b8e72a commit 7389b4c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
12 changes: 10 additions & 2 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,16 @@ export const hc = <T extends Hono<any, any, any>>(
'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<string, string | string[]> | 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())
Expand Down

0 comments on commit 7389b4c

Please sign in to comment.