mirror of
https://github.com/honojs/hono.git
synced 2024-11-25 05:07:03 +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:
parent
3b8e72a8ff
commit
7389b4cad7
@ -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 () => {
|
||||
|
@ -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())
|
||||
|
Loading…
Reference in New Issue
Block a user