Skip to content
Myr

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

5 files+70 added−18 removed

modified:src/client/client.test.ts+21 added
845 unmodified lines
846 expect(url.pathname).toBe('/something/123/456')847 })848})849850describe('$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')860861 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})870871describe('Client can be awaited', () => {872 it('Can be awaited without side effects', async () => {873 const client = hc('http://localhost')874
451 unmodified lines
modified:src/client/client.ts+11 added-17 removed
2 unmodified lines
3import { serialize } from '../utils/cookie'4import type { UnionToIntersection } from '../utils/types'5import type { Callback, Client, ClientRequestOptions } from './types'6import {7 buildSearchParams,8 deepMerge,9 mergePath,10 removeIndexString,11 replaceUrlParam,
37 unmodified lines
49 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 }5657 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 }5556 if (args.form) {57 const form = new FormData()
101 unmodified lines
159160 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 lines
modified:src/client/types.ts+5 added-1 removed
modified:src/client/utils.test.ts+13 added
1import {2 buildSearchParams,3 deepMerge,4 mergePath,5 removeIndexString,6 replaceUrlParam,
52 unmodified lines
59 expect(replacedUrl).toBe('http://localhost/something/123/456')60 })61})6263describe('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})7475describe('replaceUrlProtocol', () => {76 it('Should replace http to ws', () => {77 const url = 'http://localhost'78 const newUrl = replaceUrlProtocol(url, 'ws')
63 unmodified lines
modified:src/client/utils.ts+20 added
13 unmodified lines
14 }15 return urlString16}1718export const buildSearchParams = (query: Record<string, string | string[]>) => {19 const searchParams = new URLSearchParams()2021 for (const [k, v] of Object.entries(query)) {22 if (v === undefined) {23 continue24 }2526 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 }3435 return searchParams36}3738export const replaceUrlProtocol = (urlString: string, protocol: 'ws' | 'http') => {39 switch (protocol) {40 case 'ws':41 return urlString.replace(/^http/, 'ws')
33 unmodified lines