Skip to content
Myr

fix(context): Strict type checking `c.json()` (#1776)

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

* fix(context): Strict type checking `c.json()` * denoify

authored by Yusuke Wada · committed by GitHub · parent 8028437 · Browse files · 5 files+10 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+10 added−9 removed

  • tests:+4 added−3 removed
  • other:+6 added−6 removed

5 files+10 added−9 removed

modified:deno_dist/context.ts+3 added-3 removed
47 unmodified lines
48}4950interface JSONRespond {51 <T>(52 object: InterfaceToType<T> extends JSONValue ? T : T extends JSONValue ? JSONValue : T,52 object: InterfaceToType<T> extends JSONValue ? T : JSONValue,53 status?: StatusCode,54 headers?: HeaderRecord55 ): Response &56 TypedResponse<
247 unmodified lines
304 : this.newResponse(text, arg)305 }306307 json: JSONRespond = <T>(308 object: InterfaceToType<T> extends JSONValue ? T : T extends JSONValue ? JSONValue : T,308 object: InterfaceToType<T> extends JSONValue ? T : JSONValue,309 arg?: StatusCode | ResponseInit,310 headers?: HeaderRecord311 ): Response &312 TypedResponse<
18 unmodified lines
331 * Use `c.json()` instead of `c.jsonT()`.332 * `c.json()` now returns data type, so you can just replace `c.jsonT()` to `c.json()`.333 */334 jsonT: JSONRespond = <T>(335 object: InterfaceToType<T> extends JSONValue ? T : T extends JSONValue ? JSONValue : T,335 object: InterfaceToType<T> extends JSONValue ? T : JSONValue,336 arg?: StatusCode | ResponseInit,337 headers?: HeaderRecord338 ): Response &339 TypedResponse<
140 unmodified lines
modified:src/adapter/vercel/handler.test.ts+1 added-1 removed
modified:src/client/client.test.ts+1 added
500 unmodified lines
501 })502503 it('Should not allow the incorrect JSON type', async () => {504 const app = new Hono()505 // @ts-expect-error506 const route = app.get('/api/foo', (c) => c.json({ datetime: new Date() }))507 type AppType = typeof route508 const client = hc<AppType>('http://localhost')509 const res = await client.api.foo.$get()
81 unmodified lines
modified:src/context.ts+3 added-3 removed
47 unmodified lines
48}4950interface JSONRespond {51 <T>(52 object: InterfaceToType<T> extends JSONValue ? T : T extends JSONValue ? JSONValue : T,52 object: InterfaceToType<T> extends JSONValue ? T : JSONValue,53 status?: StatusCode,54 headers?: HeaderRecord55 ): Response &56 TypedResponse<
247 unmodified lines
304 : this.newResponse(text, arg)305 }306307 json: JSONRespond = <T>(308 object: InterfaceToType<T> extends JSONValue ? T : T extends JSONValue ? JSONValue : T,308 object: InterfaceToType<T> extends JSONValue ? T : JSONValue,309 arg?: StatusCode | ResponseInit,310 headers?: HeaderRecord311 ): Response &312 TypedResponse<
18 unmodified lines
331 * Use `c.json()` instead of `c.jsonT()`.332 * `c.json()` now returns data type, so you can just replace `c.jsonT()` to `c.json()`.333 */334 jsonT: JSONRespond = <T>(335 object: InterfaceToType<T> extends JSONValue ? T : T extends JSONValue ? JSONValue : T,335 object: InterfaceToType<T> extends JSONValue ? T : JSONValue,336 arg?: StatusCode | ResponseInit,337 headers?: HeaderRecord338 ): Response &339 TypedResponse<
140 unmodified lines
modified:src/hono.test.ts+2 added-2 removed
1923 unmodified lines
1924describe('Parse Body', () => {1925 const app = new Hono()19261927 app.post('/json', async (c) => {1928 return c.json(await c.req.parseBody(), 200)1928 return c.json<{}>(await c.req.parseBody(), 200)1929 })1930 app.post('/form', async (c) => {1931 return c.json(await c.req.parseBody(), 200)1931 return c.json<{}>(await c.req.parseBody(), 200)1932 })19331934 it('POST with JSON', async () => {1935 const req = new Request('http://localhost/json', {
1265 unmodified lines