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
Provenance
- Commit
73794bd- Tree
ad03696- Author
Ersa Oktavian Ramadan <124137772+Ersaoktaviannn@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
28a9c12
2 files changed+67 added−2 removed
modified:src/client/client.test.ts+60 added
1839 unmodified lines1840 expect(requestedUrl).toBe('http://localhost/search?tag=a&tag=b')1841 })1842 })1843 1844+describe('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 } }1851+1852+ 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+ })1860+1861+ await client.search.$get(input)1862+1863+ 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+ })1867+1868+ 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' } }1878+1879+ await client.search.$get(customInput)1880+1881+ 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+ })1885+1886+ 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: '' } }1895+1896+ await client.search.$get(emptyStringInput)1897+1898+ 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+})1903+1904 describe('Custom buildSearchParams', () => {1905 const app = new Hono()1906 const route = app.get(1907 '/search',327 unmodified linesmodified:src/client/client.ts+7 added-2 removed
28 unmodified lines29 })30 return proxy31 }32 33+const appendQueryParams = (url: string, searchParams: URLSearchParams): string => {34+ const queryString = searchParams.toString()35+ return queryString ? `${url}?${queryString}` : url36+}37+38 class ClientRequestImpl {39 private url: string40 private method: string41 private buildSearchParams: BuildSearchParamsFn89 unmodified lines131 url = removeIndexString(url)132 url = replaceUrlParam(url, this.pathParams)133 134 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 lines192 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