Skip to content
Myr

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

5 files+103 added−10 removed

modified:deno_dist/utils/types.ts+2 added
modified:deno_dist/context.ts+42 added-5 removed
2 unmodified lines
3import type { Env, NotFoundHandler, Input } from './types.ts'4import type { CookieOptions } from './utils/cookie.ts'5import { serialize } from './utils/cookie.ts'6import type { StatusCode } from './utils/http-status.ts'7import type { JSONValue } from './utils/types.ts'7import type { JSONValue, InterfaceToType } from './utils/types.ts'89type Runtime = 'node' | 'deno' | 'bun' | 'workerd' | 'fastly' | 'edge-light' | 'lagon' | 'other'10type HeaderRecord = Record<string, string | string[]>11type Data = string | ArrayBuffer | ReadableStream
35 unmodified lines
47 <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}8788interface HTMLRespond {89 (html: string, status?: StatusCode, headers?: HeaderRecord): Response
249 unmodified lines
339 : this.newResponse(body, arg)340 }341342 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
modified:src/client/client.test.ts+15 added
357 unmodified lines
358 type verify = Expect<Equal<true, typeof data.ok>>359 expect(data.ok).toBe(true)360 })361362 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 })376377 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 lines
modified:src/utils/types.ts+2 added
modified:src/context.ts+42 added-5 removed
2 unmodified lines
3import type { Env, NotFoundHandler, Input } from './types'4import type { CookieOptions } from './utils/cookie'5import { serialize } from './utils/cookie'6import type { StatusCode } from './utils/http-status'7import type { JSONValue } from './utils/types'7import type { JSONValue, InterfaceToType } from './utils/types'89type Runtime = 'node' | 'deno' | 'bun' | 'workerd' | 'fastly' | 'edge-light' | 'lagon' | 'other'10type HeaderRecord = Record<string, string | string[]>11type Data = string | ArrayBuffer | ReadableStream
35 unmodified lines
47 <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}8788interface HTMLRespond {89 (html: string, status?: StatusCode, headers?: HeaderRecord): Response
249 unmodified lines
339 : this.newResponse(body, arg)340 }341342 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