Skip to content
Draft
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
87 changes: 87 additions & 0 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,93 @@ describe('Infer the response/request type', () => {
type verify = Expect<Equal<Expected, Actual>>
})

it('Should infer response type with Promise.then in arrow expression', () => {
const route = new Hono().get('/', (c) =>
Promise.resolve({ hello: 'world' }).then((d) => c.json(d))
)
type AppType = typeof route

const client = hc<AppType>('/')
const req = client.index.$get

type Actual = InferResponseType<typeof req>
type verify = Expect<
Actual extends { hello: string } ? ({ hello: string } extends Actual ? true : false) : false
>
})

it('Should infer response type for post handler with Promise.then', () => {
const route = new Hono().post('/', (c) =>
Promise.resolve({ hello: 'world' }).then((d) => c.json(d))
)
type AppType = typeof route

const client = hc<AppType>('/')
const req = client.index.$post

type Actual = InferResponseType<typeof req>
type verify = Expect<
Actual extends { hello: string } ? ({ hello: string } extends Actual ? true : false) : false
>
})

it('Should infer response type for on handler with Promise.then', () => {
const route = new Hono().on('GET', '/', (c) =>
Promise.resolve({ hello: 'world' }).then((d) => c.json(d))
)
type AppType = typeof route

const client = hc<AppType>('/')
const req = client.index.$get

type Actual = InferResponseType<typeof req>
type verify = Expect<
Actual extends { hello: string } ? ({ hello: string } extends Actual ? true : false) : false
>
})

it('Should infer response type with mixed handlers and Promise.then', () => {
const route = new Hono().post(
'/',
async (_c, next) => {
await next()
},
async (_c, next) => {
await next()
},
(c) => Promise.resolve({ hello: 'world' }).then((d) => c.json(d))
)
type AppType = typeof route

const client = hc<AppType>('/')
const req = client.index.$post

type Actual = InferResponseType<typeof req>
type verify = Expect<
Actual extends { hello: string } ? ({ hello: string } extends Actual ? true : false) : false
>
})

it('Should infer response type with mixed on handlers and Promise.then', () => {
const route = new Hono().on(
'GET',
'/',
async (_c, next) => {
await next()
},
(c) => Promise.resolve({ hello: 'world' }).then((d) => c.json(d))
)
type AppType = typeof route

const client = hc<AppType>('/')
const req = client.index.$get

type Actual = InferResponseType<typeof req>
type verify = Expect<
Actual extends { hello: string } ? ({ hello: string } extends Actual ? true : false) : false
>
})

it('Should infer request type the type correctly', () => {
const client = hc<AppType>('/')
const req = client.index.$get
Expand Down
Loading
Loading