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): correct inferring empty object fromc.json({}) #3873

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Loading