Skip to content
Myr

fix(context): `c.json()` allows object and returns JSONParsed<T> (#1806)

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

* fix(context): `c.json()` allows object and returns JSONParsed<T> * denoify

authored by Yusuke Wada · committed by GitHub · parent 87e256b · Browse files · 5 files+19 added−14 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+19 added−14 removed

  • tests:+3 added−4 removed
  • other:+16 added−10 removed

5 files+19 added−14 removed

modified:deno_dist/utils/types.ts+4 added-1 removed
modified:deno_dist/context.ts+4 added-4 removed
4 unmodified lines
5import { serialize } from './utils/cookie.ts'6import { resolveStream } from './utils/html.ts'7import type { StatusCode } from './utils/http-status.ts'8import { StreamingApi } from './utils/stream.ts'9import type { JSONValue, InterfaceToType } from './utils/types.ts'9import type { JSONValue, InterfaceToType, JSONParsed } from './utils/types.ts'1011type HeaderRecord = Record<string, string | string[]>12type Data = string | ArrayBuffer | ReadableStream13
42 unmodified lines
56 TypedResponse<57 InterfaceToType<T> extends JSONValue58 ? JSONValue extends InterfaceToType<T>59 ? never60 : T60 : JSONParsed<T>61 : never62 >63 <T>(object: InterfaceToType<T> extends JSONValue ? T : JSONValue, init?: ResponseInit): Response &64 TypedResponse<65 InterfaceToType<T> extends JSONValue66 ? JSONValue extends InterfaceToType<T>67 ? never68 : T68 : JSONParsed<T>69 : never70 >71}72
234 unmodified lines
307 TypedResponse<308 InterfaceToType<T> extends JSONValue309 ? JSONValue extends InterfaceToType<T>310 ? never311 : T311 : JSONParsed<T>312 : never313 > => {314 const body = JSON.stringify(object)315 this.#preparedHeaders ??= {}
159 unmodified lines
modified:src/client/client.test.ts+3 added-4 removed
498 unmodified lines
499 type verify = Expect<Equal<Result, typeof data>>500 expect(data.ok).toBe(true)501 })502503 it('Should not allow the incorrect JSON type', async () => {503 it('Should allow a Date object and return it as a string', async () => {504 const app = new Hono()505 // @ts-expect-error505 const route = app.get('/api/foo', (c) => c.json({ datetime: new Date() }))506 type AppType = typeof route507 const client = hc<AppType>('http://localhost')508 const res = await client.api.foo.$get()510 const data = await res.json()511 type verify = Expect<Equal<never, typeof data>>509 const { datetime } = await res.json()510 type verify = Expect<Equal<string, typeof datetime>>511 })512513 describe('Multiple endpoints', () => {514 const api = new Hono()
93 unmodified lines
modified:src/utils/types.ts+4 added-1 removed
modified:src/context.ts+4 added-4 removed
4 unmodified lines
5import { serialize } from './utils/cookie'6import { resolveStream } from './utils/html'7import type { StatusCode } from './utils/http-status'8import { StreamingApi } from './utils/stream'9import type { JSONValue, InterfaceToType } from './utils/types'9import type { JSONValue, InterfaceToType, JSONParsed } from './utils/types'1011type HeaderRecord = Record<string, string | string[]>12type Data = string | ArrayBuffer | ReadableStream13
42 unmodified lines
56 TypedResponse<57 InterfaceToType<T> extends JSONValue58 ? JSONValue extends InterfaceToType<T>59 ? never60 : T60 : JSONParsed<T>61 : never62 >63 <T>(object: InterfaceToType<T> extends JSONValue ? T : JSONValue, init?: ResponseInit): Response &64 TypedResponse<65 InterfaceToType<T> extends JSONValue66 ? JSONValue extends InterfaceToType<T>67 ? never68 : T68 : JSONParsed<T>69 : never70 >71}72
234 unmodified lines
307 TypedResponse<308 InterfaceToType<T> extends JSONValue309 ? JSONValue extends InterfaceToType<T>310 ? never311 : T311 : JSONParsed<T>312 : never313 > => {314 const body = JSON.stringify(object)315 this.#preparedHeaders ??= {}
159 unmodified lines