Skip to content
Myr

fix(client): omit empty query delimiter (#5283)

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

authored by Ersa Oktavian Ramadan · committed by GitHub · parent 28a9c12 · Browse files · 2 files+67 added−2 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+67 added−2 removed

  • tests:+60 added−0 removed
  • other:+7 added−2 removed

2 files+67 added−2 removed

modified:src/client/client.test.ts+60 added
1839 unmodified lines
1840 expect(requestedUrl).toBe('http://localhost/search?tag=a&tag=b')1841 })1842})18431844describe('Empty serialized query', () => {1845 const app = new Hono().get(1846 '/search',1847 validator('query', () => ({}) as { q?: string }),1848 (c) => c.json({ ok: true })1849 )1850 const input = { query: { q: undefined } }18511852 it('Should omit the query delimiter when no query values are serialized', async () => {1853 let requestedUrl = ''1854 const client = hc<typeof app>('http://localhost', {1855 fetch: (input: RequestInfo | URL) => {1856 requestedUrl = input.toString()1857 return Promise.resolve(new Response('{}'))1858 },1859 })18601861 await client.search.$get(input)18621863 expect(requestedUrl).toBe('http://localhost/search')1864 expect(client.search.$url(input).href).toBe('http://localhost/search')1865 expect(client.search.$path(input)).toBe('/search')1866 })18671868 it('Should omit the query delimiter when a custom serializer returns no values', async () => {1869 let requestedUrl = ''1870 const client = hc<typeof app>('http://localhost', {1871 buildSearchParams: () => new URLSearchParams(),1872 fetch: (input: RequestInfo | URL) => {1873 requestedUrl = input.toString()1874 return Promise.resolve(new Response('{}'))1875 },1876 })1877 const customInput = { query: { q: 'ignored' } }18781879 await client.search.$get(customInput)18801881 expect(requestedUrl).toBe('http://localhost/search')1882 expect(client.search.$url(customInput).href).toBe('http://localhost/search')1883 expect(client.search.$path(customInput)).toBe('/search')1884 })18851886 it('Should preserve empty-string query values', async () => {1887 let requestedUrl = ''1888 const client = hc<typeof app>('http://localhost', {1889 fetch: (input: RequestInfo | URL) => {1890 requestedUrl = input.toString()1891 return Promise.resolve(new Response('{}'))1892 },1893 })1894 const emptyStringInput = { query: { q: '' } }18951896 await client.search.$get(emptyStringInput)18971898 expect(requestedUrl).toBe('http://localhost/search?q=')1899 expect(client.search.$url(emptyStringInput).href).toBe('http://localhost/search?q=')1900 expect(client.search.$path(emptyStringInput)).toBe('/search?q=')1901 })1902})19031904describe('Custom buildSearchParams', () => {1905 const app = new Hono()1906 const route = app.get(1907 '/search',
327 unmodified lines
modified:src/client/client.ts+7 added-2 removed
28 unmodified lines
29 })30 return proxy31}3233const appendQueryParams = (url: string, searchParams: URLSearchParams): string => {34 const queryString = searchParams.toString()35 return queryString ? `${url}?${queryString}` : url36}3738class ClientRequestImpl {39 private url: string40 private method: string41 private buildSearchParams: BuildSearchParamsFn
89 unmodified lines
131 url = removeIndexString(url)132 url = replaceUrlParam(url, this.pathParams)133134 if (this.queryParams) {130 url = url + '?' + this.queryParams.toString()135 url = appendQueryParams(url, this.queryParams)136 }137 methodUpperCase = this.method.toUpperCase()138 const setBody = !(methodUpperCase === 'GET' || methodUpperCase === 'HEAD')139
52 unmodified lines
192 if (opts.args[0].param) {193 result = replaceUrlParam(url, opts.args[0].param)194 }195 if (opts.args[0].query) {191 result = result + '?' + buildSearchParamsOption(opts.args[0].query).toString()196 result = appendQueryParams(result, buildSearchParamsOption(opts.args[0].query))197 }198 }199 result = removeIndexString(result)200 if (method === 'url') {
47 unmodified lines