Skip to content
Myr

fix(client): normalize root WebSocket URLs (#5291)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The index segment is a client-side alias for root routes. Apply the existing normalization before path parameter substitution so $ws() targets the declared endpoint while preserving dynamic values named index.

authored by Ersa Oktavian Ramadan · committed by GitHub · parent 50b8788 · Browse files · 2 files+24 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+24 added−11 removed

  • tests:+22 added−10 removed
  • other:+2 added−1 removed

2 files+24 added−11 removed

modified:src/client/client.test.ts+22 added-10 removed
1610 unmodified lines
16111612 it('Translates HTTP to ws', async () => {1613 const client = hc<AppType>('http://localhost')1614 client.index.$ws()1615 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index')1615 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/')1616 })16171618 it('Translates HTTPS to wss', async () => {1619 const client = hc<AppType>('https://localhost')1620 client.index.$ws()1621 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index')1621 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/')1622 })16231624 it('Keeps ws unchanged', async () => {1625 const client = hc<AppType>('ws://localhost')1626 client.index.$ws()1627 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index')1627 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/')1628 })16291630 it('Keeps wss unchanged', async () => {1631 const client = hc<AppType>('wss://localhost')1632 client.index.$ws()1633 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index')1633 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/')1634 })16351636 it('Preserves an index path parameter value', async () => {1637 const dynamicRoute = new Hono().get(1638 '/:id',1639 upgradeWebSocket(() => ({}))1640 )1641 const client = hc<typeof dynamicRoute>('http://localhost')16421643 client[':id'].$ws({ param: { id: 'index' } })16441645 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index')1646 })1647})16481649describe('WebSocket URL Protocol Translation with Query Parameters', () => {1650 const app = new Hono()
32 unmodified lines
1683 type: 'test',1684 tag: ['a', 'b'],1685 },1686 })1675 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index?id=123&type=test&tag=a&tag=b')1687 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/?id=123&type=test&tag=a&tag=b')1688 })16891690 it('Translates HTTPS to wss and includes query parameters', async () => {1691 const client = hc<AppType>('https://localhost')
2 unmodified lines
1694 id: '456',1695 type: 'secure',1696 },1697 })1686 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index?id=456&type=secure')1698 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/?id=456&type=secure')1699 })17001701 it('Keeps ws unchanged and includes query parameters', async () => {1702 const client = hc<AppType>('ws://localhost')
2 unmodified lines
1705 id: '789',1706 type: 'plain',1707 },1708 })1697 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index?id=789&type=plain')1709 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/?id=789&type=plain')1710 })17111712 it('Keeps wss unchanged and includes query parameters', async () => {1713 const client = hc<AppType>('wss://localhost')
2 unmodified lines
1716 id: '1011',1717 type: 'secure',1718 },1719 })1708 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index?id=1011&type=secure')1720 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/?id=1011&type=secure')1721 })1722})17231724describe('Client can be console.log in react native', () => {
146 unmodified lines
1871 {1872 description: 'should initialize the WebSocket provider correctly',1873 url: 'http://localhost',1874 query: undefined,1863 expectedUrl: 'ws://localhost/index',1875 expectedUrl: 'ws://localhost/',1876 },1877 {1878 description: 'should correctly add query parameters to the WebSocket URL',1879 url: 'http://localhost',1880 query: { id: '123', type: 'test', tag: ['a', 'b'] },1869 expectedUrl: 'ws://localhost/index?id=123&type=test&tag=a&tag=b',1881 expectedUrl: 'ws://localhost/?id=123&type=test&tag=a&tag=b',1882 },1883 ])('$description', ({ url, expectedUrl, query }) => {1884 const webSocketMock = vi.fn()1885 const client = hc<AppType>(url, {
425 unmodified lines
modified:src/client/client.ts+2 added-1 removed
202 unmodified lines
203 }204 return result.slice(baseUrl.replace(/\/+$/, '').length).replace(/^\/?/, '/')205 }206 if (method === 'ws') {207 const normalizedUrl = removeIndexString(url)208 const webSocketUrl = replaceUrlProtocol(208 opts.args[0]?.param ? replaceUrlParam(url, opts.args[0].param) : url,209 opts.args[0]?.param ? replaceUrlParam(normalizedUrl, opts.args[0].param) : normalizedUrl,210 'ws'211 )212 const targetUrl = new URL(webSocketUrl)213
36 unmodified lines