fix(client): return query params in $url (#3541)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Alex Grover · committed by GitHub · parent 267186d · Browse files · 5 files·+70 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+70 added−18 removed
- tests:+34 added−0 removed
- other:+36 added−18 removed
Provenance
- Commit
3d8abbc- Tree
2c3f2d3- Author
Alex Grover <hello@alexgrover.me>- Committer
GitHub <noreply@github.com>- Parent
267186d
5 files changed+70 added−18 removed
modified:src/client/client.test.ts+21 added
845 unmodified lines846 expect(url.pathname).toBe('/something/123/456')847 })848 })849 850+describe('$url() with a query option', () => {851+ const app = new Hono().get(852+ '/posts',853+ validator('query', () => {854+ return {} as { filter: 'test' }855+ }),856+ (c) => c.json({ ok: true })857+ )858+ type AppType = typeof app859+ const client = hc<AppType>('http://localhost')860+861+ it('Should return the correct path - /posts?filter=test', async () => {862+ const url = client.posts.$url({863+ query: {864+ filter: 'test',865+ },866+ })867+ expect(url.search).toBe('?filter=test')868+ })869+})870+871 describe('Client can be awaited', () => {872 it('Can be awaited without side effects', async () => {873 const client = hc('http://localhost')874 451 unmodified linesmodified:src/client/client.ts+11 added-17 removed
2 unmodified lines3 import { serialize } from '../utils/cookie'4 import type { UnionToIntersection } from '../utils/types'5 import type { Callback, Client, ClientRequestOptions } from './types'6 import {7+ buildSearchParams,8 deepMerge,9 mergePath,10 removeIndexString,11 replaceUrlParam,37 unmodified lines49 opt?: ClientRequestOptions50 ) => {51 if (args) {52 if (args.query) {52- for (const [k, v] of Object.entries(args.query)) {53- if (v === undefined) {54- continue55- }56-57- this.queryParams ||= new URLSearchParams()58- if (Array.isArray(v)) {59- for (const v2 of v) {60- this.queryParams.append(k, v2)61- }62- } else {63- this.queryParams.set(k, v)64- }65- }53+ this.queryParams = buildSearchParams(args.query)54 }55 56 if (args.form) {57 const form = new FormData()101 unmodified lines159 160 const path = parts.join('/')161 const url = mergePath(baseUrl, path)162 if (method === 'url') {175- if (opts.args[0] && opts.args[0].param) {176- return new URL(replaceUrlParam(url, opts.args[0].param))163+ let result = url164+ if (opts.args[0]) {165+ if (opts.args[0].param) {166+ result = replaceUrlParam(url, opts.args[0].param)167+ }168+ if (opts.args[0].query) {169+ result = result + '?' + buildSearchParams(opts.args[0].query).toString()170+ }171 }178- return new URL(url)172+ return new URL(result)173 }174 if (method === 'ws') {175 const webSocketUrl = replaceUrlProtocol(176 opts.args[0] && opts.args[0].param ? replaceUrlParam(url, opts.args[0].param) : url,32 unmodified linesmodified:src/client/types.ts+5 added-1 removed
36 unmodified lines3738394041424143444546474849136 unmodified linesExpand all36 unmodified lines} & { $url: ( arg?: S[keyof S] extends { input: infer R } ? R extends { param: infer P } ? R extends { query: infer Q } ? { param: P; query: Q } ? { param: P } : { param: P } : R extends { query: infer Q } ? { query: Q } : {} : {} ) => URL} & (S['$get'] extends { outputFormat: 'ws' }136 unmodified linesExpand allmodified:src/client/utils.test.ts+13 added
1 import {2+ buildSearchParams,3 deepMerge,4 mergePath,5 removeIndexString,6 replaceUrlParam,52 unmodified lines59 expect(replacedUrl).toBe('http://localhost/something/123/456')60 })61 })62 63+describe('buildSearchParams', () => {64+ it('Should build URLSearchParams correctly', () => {65+ const query = {66+ id: '123',67+ type: 'test',68+ tag: ['a', 'b'],69+ }70+ const searchParams = buildSearchParams(query)71+ expect(searchParams.toString()).toBe('id=123&type=test&tag=a&tag=b')72+ })73+})74+75 describe('replaceUrlProtocol', () => {76 it('Should replace http to ws', () => {77 const url = 'http://localhost'78 const newUrl = replaceUrlProtocol(url, 'ws')63 unmodified linesmodified:src/client/utils.ts+20 added
13 unmodified lines14 }15 return urlString16 }17 18+export const buildSearchParams = (query: Record<string, string | string[]>) => {19+ const searchParams = new URLSearchParams()20+21+ for (const [k, v] of Object.entries(query)) {22+ if (v === undefined) {23+ continue24+ }25+26+ if (Array.isArray(v)) {27+ for (const v2 of v) {28+ searchParams.append(k, v2)29+ }30+ } else {31+ searchParams.set(k, v)32+ }33+ }34+35+ return searchParams36+}37+38 export const replaceUrlProtocol = (urlString: string, protocol: 'ws' | 'http') => {39 switch (protocol) {40 case 'ws':41 return urlString.replace(/^http/, 'ws')33 unmodified lines