0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 18:18:57 +01:00

fix(client): support array values for query in ws (#3169)

Co-authored-by: Alber Tenez <albert@zenettech.com>
This commit is contained in:
Yusuke Wada 2024-07-20 23:01:34 +09:00 committed by GitHub
parent 3b8e72a8ff
commit 7389b4cad7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 3 deletions

View File

@ -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 () => {

View File

@ -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())