Skip to content

Commit

Permalink
res.text() and res.json() return corret types
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe committed Apr 30, 2024
1 parent 77d2469 commit 9d690a6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
44 changes: 44 additions & 0 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,47 @@ describe('Client can be console.log in react native', () => {
expect(client.posts.toString()).toMatch('function proxyCallback')
})
})

describe('Text response', () => {
const text = 'My name is Hono'
const obj = { ok: true }
const server = setupServer(
rest.get('http://localhost/about/me', async (_req, res, ctx) => {
return res(ctx.text(text))
}),
rest.get('http://localhost/api', async (_req, res, ctx) => {
return res(ctx.json(obj))
})
)

beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())

const app = new Hono().get('/about/me', (c) => c.text(text)).get('/api', (c) => c.json(obj))
const client = hc<typeof app>('http://localhost/')

it('Should be never with res.json() - /about/me', async () => {
const res = await client.about.me.$get()
type Actual = ReturnType<typeof res.json>
type Expected = Promise<never>
type verify = Expect<Equal<Expected, Actual>>
})

it('Should be "Hello, World!" with res.text() - /about/me', async () => {
const res = await client.about.me.$get()
const data = await res.text()
expectTypeOf(data).toEqualTypeOf<'My name is Hono'>()
expect(data).toBe(text)
})

/**
* Also check the type of JSON response with res.text().
*/
it('Should be never with res.text() - /api', async () => {
const res = await client.api.$get()
type Actual = ReturnType<typeof res.text>
type Expected = Promise<never>
type verify = Expect<Equal<Expected, Actual>>
})
})
4 changes: 2 additions & 2 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export interface ClientResponse<T> {
url: string
redirect(url: string, status: number): Response
clone(): Response
json(): Promise<BlankRecordToNever<T>>
text(): Promise<string>
json(): T extends string ? Promise<never> : Promise<BlankRecordToNever<T>>
text(): T extends string ? Promise<T> : Promise<never>
blob(): Promise<Blob>
formData(): Promise<FormData>
arrayBuffer(): Promise<ArrayBuffer>
Expand Down
5 changes: 3 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ interface NewResponse {
interface BodyRespond extends NewResponse {}

interface TextRespond {
<T extends string>(text: T, status?: StatusCode, headers?: HeaderRecord): Response & TypedResponse<T>
<T extends string>(text: T, init?: ResponseInit): Response & TypedResponse<T>
<T extends string>(text: T, status?: StatusCode, headers?: HeaderRecord): Response &
TypedResponse<T>
<T extends string>(text: T, init?: ResponseInit): Response & TypedResponse<T>
}

interface JSONRespond {
Expand Down

0 comments on commit 9d690a6

Please sign in to comment.