Skip to content
Myr

fix(client): support custom buildSearchParams and filter undefined query in $ws() (#5256)

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

* fix(client): support custom buildSearchParams and filter undefined query in () * test(client): restore default buildSearchParams test

authored by ADITYA KUMAR JHA · committed by GitHub · parent c4a4407 · Browse files · 2 files+34 added−7 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+34 added−7 removed

  • tests:+30 added−0 removed
  • other:+4 added−7 removed

2 files+34 added−7 removed

modified:src/client/client.test.ts+30 added
1917 unmodified lines
1918 const url = client.search.$url({ query: { q: 'test', tags: ['tag1', 'tag2'] } })19191920 expect(url.href).toBe('http://localhost/search?q=test&tags=tag1&tags=tag2')1921 })19221923 it('Should use custom buildSearchParams in $ws() method', () => {1924 const webSocketMock = vi.fn()1925 const client = hc<AppType>('http://localhost', {1926 buildSearchParams: customBuildSearchParams,1927 webSocket(url, options) {1928 return webSocketMock(url, options)1929 },1930 })1931 // @ts-expect-error search route does not explicitly define $ws in mock AppType1932 client.search.$ws({ query: { q: 'test', tags: ['tag1', 'tag2'] } })19331934 expect(webSocketMock).toHaveBeenCalledWith(1935 'ws://localhost/search?q=test&tags%5B%5D=tag1&tags%5B%5D=tag2',1936 undefined1937 )1938 })19391940 it('Should filter out undefined query parameters in $ws()', () => {1941 const webSocketMock = vi.fn()1942 const client = hc<AppType>('http://localhost', {1943 webSocket(url, options) {1944 return webSocketMock(url, options)1945 },1946 })1947 // @ts-expect-error search route does not explicitly define $ws in mock AppType1948 client.search.$ws({ query: { q: 'test', tags: undefined as any } })19491950 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/search?q=test', undefined)1951 })1952})19531954describe('ApplyGlobalResponse Type Helper', () => {1955 const server = setupServer(
219 unmodified lines
modified:src/client/client.ts+4 added-7 removed
197 unmodified lines
198 return result.slice(baseUrl.replace(/\/+$/, '').length).replace(/^\/?/, '/')199 }200 if (method === 'ws') {201 const webSocketUrl = replaceUrlProtocol(202 opts.args[0] && opts.args[0].param ? replaceUrlParam(url, opts.args[0].param) : url,202 opts.args[0]?.param ? replaceUrlParam(url, opts.args[0].param) : url,203 'ws'204 )205 const targetUrl = new URL(webSocketUrl)206207 const queryParams: Record<string, string | string[]> | undefined = opts.args[0]?.query208 if (queryParams) {209 Object.entries(queryParams).forEach(([key, value]) => {210 if (Array.isArray(value)) {211 value.forEach((item) => targetUrl.searchParams.append(key, item))212 } else {213 targetUrl.searchParams.set(key, value)214 }209 const searchParams = buildSearchParamsOption(queryParams)210 searchParams.forEach((value, key) => {211 targetUrl.searchParams.append(key, value)212 })213 }214 const establishWebSocket = (...args: ConstructorParameters<typeof WebSocket>) => {215 if (options?.webSocket !== undefined && typeof options.webSocket === 'function') {
27 unmodified lines