Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): return query params in $url #3541

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,27 @@ describe('$url() with a param option', () => {
})
})

describe('$url() with a query option', () => {
const app = new Hono().get(
'/posts',
validator('query', () => {
return {} as { filter: 'test' }
}),
(c) => c.json({ ok: true })
)
type AppType = typeof app
const client = hc<AppType>('http://localhost')

it('Should return the correct path - /posts?filter=test', async () => {
const url = client.posts.$url({
query: {
filter: 'test',
},
})
expect(url.search).toBe('?filter=test')
})
})

describe('Client can be awaited', () => {
it('Can be awaited without side effects', async () => {
const client = hc('http://localhost')
Expand Down
28 changes: 11 additions & 17 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { serialize } from '../utils/cookie'
import type { UnionToIntersection } from '../utils/types'
import type { Callback, Client, ClientRequestOptions } from './types'
import {
buildSearchParams,
deepMerge,
mergePath,
removeIndexString,
Expand Down Expand Up @@ -49,20 +50,7 @@ class ClientRequestImpl {
) => {
if (args) {
if (args.query) {
for (const [k, v] of Object.entries(args.query)) {
if (v === undefined) {
continue
}

this.queryParams ||= new URLSearchParams()
if (Array.isArray(v)) {
for (const v2 of v) {
this.queryParams.append(k, v2)
}
} else {
this.queryParams.set(k, v)
}
}
this.queryParams = buildSearchParams(args.query)
}

if (args.form) {
Expand Down Expand Up @@ -172,10 +160,16 @@ export const hc = <T extends Hono<any, any, any>>(
const path = parts.join('/')
const url = mergePath(baseUrl, path)
if (method === 'url') {
if (opts.args[0] && opts.args[0].param) {
return new URL(replaceUrlParam(url, opts.args[0].param))
let result = url
if (opts.args[0]) {
if (opts.args[0].param) {
result = replaceUrlParam(url, opts.args[0].param)
}
if (opts.args[0].query) {
result = result + '?' + buildSearchParams(opts.args[0].query).toString()
}
}
return new URL(url)
return new URL(result)
}
if (method === 'ws') {
const webSocketUrl = replaceUrlProtocol(
Expand Down
6 changes: 5 additions & 1 deletion src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export type ClientRequest<S extends Schema> = {
$url: (
arg?: S[keyof S] extends { input: infer R }
? R extends { param: infer P }
? { param: P }
? R extends { query: infer Q }
? { param: P; query: Q }
: { param: P }
: R extends { query: infer Q }
? { query: Q }
: {}
: {}
) => URL
Expand Down
13 changes: 13 additions & 0 deletions src/client/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
buildSearchParams,
deepMerge,
mergePath,
removeIndexString,
Expand Down Expand Up @@ -59,6 +60,18 @@ describe('replaceUrlParams', () => {
})
})

describe('buildSearchParams', () => {
it('Should build URLSearchParams correctly', () => {
const query = {
id: '123',
type: 'test',
tag: ['a', 'b'],
}
const searchParams = buildSearchParams(query)
expect(searchParams.toString()).toBe('id=123&type=test&tag=a&tag=b')
})
})

describe('replaceUrlProtocol', () => {
it('Should replace http to ws', () => {
const url = 'http://localhost'
Expand Down
20 changes: 20 additions & 0 deletions src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ export const replaceUrlParam = (urlString: string, params: Record<string, string
return urlString
}

export const buildSearchParams = (query: Record<string, string | string[]>) => {
const searchParams = new URLSearchParams()

for (const [k, v] of Object.entries(query)) {
if (v === undefined) {
continue
}

if (Array.isArray(v)) {
for (const v2 of v) {
searchParams.append(k, v2)
}
} else {
searchParams.set(k, v)
}
}

return searchParams
}

export const replaceUrlProtocol = (urlString: string, protocol: 'ws' | 'http') => {
switch (protocol) {
case 'ws':
Expand Down
Loading