Skip to content

Commit

Permalink
fix(client): correct inferring empty object c.json({})
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe committed Jan 31, 2025
1 parent d72aa4b commit 85239bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('Basic - JSON', () => {
)
.get('/hello-not-found', (c) => c.notFound())
.get('/null', (c) => c.json(null))
.get('/empty', (c) => c.json({}))

type AppType = typeof route

Expand Down Expand Up @@ -77,6 +78,9 @@ describe('Basic - JSON', () => {
http.get('http://localhost/null', () => {
return HttpResponse.json(null)
}),
http.get('http://localhost/empty', () => {
return HttpResponse.json({})
}),
http.get('http://localhost/api/string', () => {
return HttpResponse.json('a-string')
}),
Expand Down Expand Up @@ -139,6 +143,14 @@ describe('Basic - JSON', () => {
expect(data).toBe(null)
})

it('Should get a `{}` content', async () => {
const client = hc<AppType>('http://localhost')
const res = await client.empty.$get()
const data = await res.json()
expectTypeOf(data).toMatchTypeOf<{}>()
expect(data).toStrictEqual({})
})

it('Should have correct types - primitives', async () => {
const app = new Hono()
const route = app
Expand Down
15 changes: 1 addition & 14 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@ export type ClientRequest<S extends Schema> = {
: {}
: {})

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type BlankRecordToNever<T> = T extends any
? T extends null
? null
: keyof T extends never
? never
: T
: never

type ClientResponseOfEndpoint<T extends Endpoint = Endpoint> = T extends {
output: infer O
outputFormat: infer F
Expand All @@ -89,11 +80,7 @@ export interface ClientResponse<
url: string
redirect(url: string, status: number): Response
clone(): Response
json(): F extends 'text'
? Promise<never>
: F extends 'json'
? Promise<BlankRecordToNever<T>>
: Promise<unknown>
json(): F extends 'text' ? Promise<never> : F extends 'json' ? Promise<T> : Promise<unknown>
text(): F extends 'text' ? (T extends string ? Promise<T> : Promise<never>) : Promise<string>
blob(): Promise<Blob>
formData(): Promise<FormData>
Expand Down

0 comments on commit 85239bf

Please sign in to comment.