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
Provenance
- Commit
af8d16b- Tree
810e6c2- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
87e256b
5 files changed+19 added−14 removed
modified:deno_dist/utils/types.ts+4 added-1 removed
23 unmodified lines24252627282829303132333435369 unmodified lines23 unmodified lines : {}
export type JSONPrimitive = string | boolean | number | null | undefinedexport type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject }export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject | object }export type JSONValue = JSONObject | JSONArray | JSONPrimitiveexport type JSONParsed<T> = { [k in keyof T]: T[k] extends JSONValue ? T[k] : string}
export type InterfaceToType<T> = T extends Function ? T : { [K in keyof T]: InterfaceToType<T[K]> }
export type RequiredKeysOf<BaseType extends object> = Exclude<9 unmodified linesmodified:deno_dist/context.ts+4 added-4 removed
4 unmodified lines5 import { serialize } from './utils/cookie.ts'6 import { resolveStream } from './utils/html.ts'7 import type { StatusCode } from './utils/http-status.ts'8 import { StreamingApi } from './utils/stream.ts'9-import type { JSONValue, InterfaceToType } from './utils/types.ts'9+import type { JSONValue, InterfaceToType, JSONParsed } from './utils/types.ts'10 11 type HeaderRecord = Record<string, string | string[]>12 type Data = string | ArrayBuffer | ReadableStream13 42 unmodified lines56 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 lines307 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 linesmodified:src/client/client.test.ts+3 added-4 removed
498 unmodified lines499 type verify = Expect<Equal<Result, typeof data>>500 expect(data.ok).toBe(true)501 })502 503- 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 })512 513 describe('Multiple endpoints', () => {514 const api = new Hono()93 unmodified linesmodified:src/utils/types.ts+4 added-1 removed
23 unmodified lines24252627282829303132333435369 unmodified lines23 unmodified lines : {}
export type JSONPrimitive = string | boolean | number | null | undefinedexport type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject }export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject | object }export type JSONValue = JSONObject | JSONArray | JSONPrimitiveexport type JSONParsed<T> = { [k in keyof T]: T[k] extends JSONValue ? T[k] : string}
export type InterfaceToType<T> = T extends Function ? T : { [K in keyof T]: InterfaceToType<T[K]> }
export type RequiredKeysOf<BaseType extends object> = Exclude<9 unmodified linesmodified:src/context.ts+4 added-4 removed
4 unmodified lines5 import { serialize } from './utils/cookie'6 import { resolveStream } from './utils/html'7 import type { StatusCode } from './utils/http-status'8 import { StreamingApi } from './utils/stream'9-import type { JSONValue, InterfaceToType } from './utils/types'9+import type { JSONValue, InterfaceToType, JSONParsed } from './utils/types'10 11 type HeaderRecord = Record<string, string | string[]>12 type Data = string | ArrayBuffer | ReadableStream13 42 unmodified lines56 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 lines307 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