feat: Allow `context.jsonT` to take interface as an argument (#1162)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ayame113 · committed by GitHub · parent aaa1c6d · Browse files · 5 files·+103 added−10 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+103 added−10 removed
- tests:+15 added−0 removed
- other:+88 added−10 removed
Provenance
- Commit
81d2712- Tree
c359ba4- Author
ayame113 <40050810+ayame113@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
aaa1c6d
5 files changed+103 added−10 removed
modified:deno_dist/utils/types.ts+2 added
19 unmodified lines20212223242519 unmodified linesexport type JSONPrimitive = string | boolean | number | null | undefinedexport type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject }export type JSONValue = JSONObject | JSONArray | JSONPrimitive
export type InterfaceToType<T> = T extends Function ? T : { [K in keyof T]: InterfaceToType<T[K]> }modified:deno_dist/context.ts+42 added-5 removed
2 unmodified lines3 import type { Env, NotFoundHandler, Input } from './types.ts'4 import type { CookieOptions } from './utils/cookie.ts'5 import { serialize } from './utils/cookie.ts'6 import type { StatusCode } from './utils/http-status.ts'7-import type { JSONValue } from './utils/types.ts'7+import type { JSONValue, InterfaceToType } from './utils/types.ts'8 9 type Runtime = 'node' | 'deno' | 'bun' | 'workerd' | 'fastly' | 'edge-light' | 'lagon' | 'other'10 type HeaderRecord = Record<string, string | string[]>11 type Data = string | ArrayBuffer | ReadableStream35 unmodified lines47 <T>(48 object: T extends JSONValue ? T : JSONValue,49 status?: StatusCode,50 headers?: HeaderRecord51- ): TypedResponse<T extends JSONValue ? (JSONValue extends T ? never : T) : never>51+ ): TypedResponse<52+ InterfaceToType<T> extends JSONValue53+ ? JSONValue extends InterfaceToType<T>54+ ? never55+ : T56+ : never57+ >58+ <T>(59+ object: InterfaceToType<T> extends JSONValue ? T : JSONValue,60+ status?: StatusCode,61+ headers?: HeaderRecord62+ ): TypedResponse<63+ InterfaceToType<T> extends JSONValue64+ ? JSONValue extends InterfaceToType<T>65+ ? never66+ : T67+ : never68+ >69 <T>(object: T extends JSONValue ? T : JSONValue, init?: ResponseInit): TypedResponse<53- T extends JSONValue ? (JSONValue extends T ? never : T) : never70+ InterfaceToType<T> extends JSONValue71+ ? JSONValue extends InterfaceToType<T>72+ ? never73+ : T74+ : never75 >76+ <T>(77+ object: InterfaceToType<T> extends JSONValue ? T : JSONValue,78+ init?: ResponseInit79+ ): TypedResponse<80+ InterfaceToType<T> extends JSONValue81+ ? JSONValue extends InterfaceToType<T>82+ ? never83+ : T84+ : never85+ >86 }87 88 interface HTMLRespond {89 (html: string, status?: StatusCode, headers?: HeaderRecord): Response249 unmodified lines339 : this.newResponse(body, arg)340 }341 342 jsonT: JSONTRespond = <T>(312- object: T extends JSONValue ? T : JSONValue,343+ object: InterfaceToType<T> extends JSONValue ? T : JSONValue,344 arg?: StatusCode | RequestInit,345 headers?: HeaderRecord315- ): TypedResponse<T extends JSONValue ? (JSONValue extends T ? never : T) : never> => {346+ ): TypedResponse<347+ InterfaceToType<T> extends JSONValue348+ ? JSONValue extends InterfaceToType<T>349+ ? never350+ : T351+ : never352+ > => {353 return {354 response: typeof arg === 'number' ? this.json(object, arg, headers) : this.json(object, arg),355 // eslint-disable-next-line @typescript-eslint/no-explicit-any356 data: object as any,80 unmodified linesmodified:src/client/client.test.ts+15 added
357 unmodified lines358 type verify = Expect<Equal<true, typeof data.ok>>359 expect(data.ok).toBe(true)360 })361 362+ it('Should have correct types - with interface', async () => {363+ interface Result {364+ ok: boolean365+ }366+ const result: Result = { ok: true }367+ const base = new Hono<Env>().basePath('/api')368+ const app = base.get('/search', (c) => c.jsonT(result))369+ type AppType = typeof app370+ const client = hc<AppType>('http://localhost')371+ const res = await client.api.search.$get()372+ const data = await res.json()373+ type verify = Expect<Equal<Result, typeof data>>374+ expect(data.ok).toBe(true)375+ })376+377 it('Should not allow the incorrect JSON type', async () => {378 const app = new Hono()379 // @ts-ignore380 const route = app.get('/api/foo', (c) => c.jsonT({ datetime: new Date() }))55 unmodified linesmodified:src/utils/types.ts+2 added
19 unmodified lines20212223242519 unmodified linesexport type JSONPrimitive = string | boolean | number | null | undefinedexport type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject }export type JSONValue = JSONObject | JSONArray | JSONPrimitive
export type InterfaceToType<T> = T extends Function ? T : { [K in keyof T]: InterfaceToType<T[K]> }modified:src/context.ts+42 added-5 removed
2 unmodified lines3 import type { Env, NotFoundHandler, Input } from './types'4 import type { CookieOptions } from './utils/cookie'5 import { serialize } from './utils/cookie'6 import type { StatusCode } from './utils/http-status'7-import type { JSONValue } from './utils/types'7+import type { JSONValue, InterfaceToType } from './utils/types'8 9 type Runtime = 'node' | 'deno' | 'bun' | 'workerd' | 'fastly' | 'edge-light' | 'lagon' | 'other'10 type HeaderRecord = Record<string, string | string[]>11 type Data = string | ArrayBuffer | ReadableStream35 unmodified lines47 <T>(48 object: T extends JSONValue ? T : JSONValue,49 status?: StatusCode,50 headers?: HeaderRecord51- ): TypedResponse<T extends JSONValue ? (JSONValue extends T ? never : T) : never>51+ ): TypedResponse<52+ InterfaceToType<T> extends JSONValue53+ ? JSONValue extends InterfaceToType<T>54+ ? never55+ : T56+ : never57+ >58+ <T>(59+ object: InterfaceToType<T> extends JSONValue ? T : JSONValue,60+ status?: StatusCode,61+ headers?: HeaderRecord62+ ): TypedResponse<63+ InterfaceToType<T> extends JSONValue64+ ? JSONValue extends InterfaceToType<T>65+ ? never66+ : T67+ : never68+ >69 <T>(object: T extends JSONValue ? T : JSONValue, init?: ResponseInit): TypedResponse<53- T extends JSONValue ? (JSONValue extends T ? never : T) : never70+ InterfaceToType<T> extends JSONValue71+ ? JSONValue extends InterfaceToType<T>72+ ? never73+ : T74+ : never75 >76+ <T>(77+ object: InterfaceToType<T> extends JSONValue ? T : JSONValue,78+ init?: ResponseInit79+ ): TypedResponse<80+ InterfaceToType<T> extends JSONValue81+ ? JSONValue extends InterfaceToType<T>82+ ? never83+ : T84+ : never85+ >86 }87 88 interface HTMLRespond {89 (html: string, status?: StatusCode, headers?: HeaderRecord): Response249 unmodified lines339 : this.newResponse(body, arg)340 }341 342 jsonT: JSONTRespond = <T>(312- object: T extends JSONValue ? T : JSONValue,343+ object: InterfaceToType<T> extends JSONValue ? T : JSONValue,344 arg?: StatusCode | RequestInit,345 headers?: HeaderRecord315- ): TypedResponse<T extends JSONValue ? (JSONValue extends T ? never : T) : never> => {346+ ): TypedResponse<347+ InterfaceToType<T> extends JSONValue348+ ? JSONValue extends InterfaceToType<T>349+ ? never350+ : T351+ : never352+ > => {353 return {354 response: typeof arg === 'number' ? this.json(object, arg, headers) : this.json(object, arg),355 // eslint-disable-next-line @typescript-eslint/no-explicit-any356 data: object as any,80 unmodified lines