Skip to content
Myr

feat(hc): add `InferRequestType` (#894)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat(hc): add `InferRequestType` * denoify

authored by Yusuke Wada · committed by Yusuke Wada · parent 05ee837 · Browse files · 5 files+35 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+35 added−9 removed

  • tests:+27 added−7 removed
  • other:+8 added−2 removed

5 files+35 added−9 removed

modified:deno_dist/client/index.ts+1 added-1 removed
modified:deno_dist/client/types.ts+3 added
modified:src/client/client.test.ts+27 added-7 removed
6 unmodified lines
7import { Hono } from '../hono'8import type { Equal, Expect } from '../utils/types'9import { validator } from '../validator'10import { hc } from './client'11import type { InferResponseType } from './types'11import type { InferRequestType, InferResponseType } from './types'1213// @ts-ignore14global.fetch = _fetch15// @ts-ignore
210 unmodified lines
226})227228describe('Infer the request type', () => {229 const app = new Hono()230 const route = app.get(231 '/',232 validator('query', () => {233 return {234 name: 'dummy',235 age: 'dummy',236 }237 }),230 const route = app.get('/', (c) =>231 c.jsonT({232 id: 123,233 title: 'Morning!',234 })238 (c) =>239 c.jsonT({240 id: 123,241 title: 'Morning!',242 })243 )244245 type AppType = typeof route246239 it('Should infer the type correctly', () => {247 it('Should infer response type the type correctly', () => {248 const client = hc<AppType>('/')249 const req = client.index.$get250251 type Actual = InferResponseType<typeof req>
2 unmodified lines
254 title: string255 }256 type verify = Expect<Equal<Expected, Actual>>257 })258259 it('Should infer request type the type correctly', () => {260 const client = hc<AppType>('/')261 const req = client.index.$get262263 type Actual = InferRequestType<typeof req>264 type Expected = {265 age: string266 name: string267 }268 type verify = Expect<Equal<Expected, Actual['query']>>269 })270})
modified:src/client/index.ts+1 added-1 removed
modified:src/client/types.ts+3 added