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
Provenance
- Commit
94df5fc- Tree
d1d7c97- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
05ee837
5 files changed+35 added−9 removed
modified:deno_dist/client/index.ts+1 added-1 removed
122export { hc } from './client.ts'export type { InferResponseType } from './types.ts'export type { InferResponseType, InferRequestType } from './types.ts'modified:deno_dist/client/types.ts+3 added
25 unmodified lines262728293031323334353626 unmodified lines25 unmodified lines json(): Promise<T>}
export type InferResponseType<T> = T extends () => Promise<ClientResponse<infer O>> ? O : neverexport type InferRequestType<T> = T extends (args: infer R) => Promise<ClientResponse<unknown>> ? NonNullable<R> : never
type PathToChain< Path extends string, E extends Endpoint,26 unmodified linesmodified:src/client/client.test.ts+27 added-7 removed
6 unmodified lines7 import { Hono } from '../hono'8 import type { Equal, Expect } from '../utils/types'9 import { validator } from '../validator'10 import { hc } from './client'11-import type { InferResponseType } from './types'11+import type { InferRequestType, InferResponseType } from './types'12 13 // @ts-ignore14 global.fetch = _fetch15 // @ts-ignore210 unmodified lines226 })227 228 describe('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 )244 245 type AppType = typeof route246 239- 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.$get250 251 type Actual = InferResponseType<typeof req>2 unmodified lines254 title: string255 }256 type verify = Expect<Equal<Expected, Actual>>257 })258+259+ it('Should infer request type the type correctly', () => {260+ const client = hc<AppType>('/')261+ const req = client.index.$get262+263+ 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
122export { hc } from './client'export type { InferResponseType } from './types'export type { InferResponseType, InferRequestType } from './types'modified:src/client/types.ts+3 added
25 unmodified lines262728293031323334353626 unmodified lines25 unmodified lines json(): Promise<T>}
export type InferResponseType<T> = T extends () => Promise<ClientResponse<infer O>> ? O : neverexport type InferRequestType<T> = T extends (args: infer R) => Promise<ClientResponse<unknown>> ? NonNullable<R> : never
type PathToChain< Path extends string, E extends Endpoint,26 unmodified lines