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
Provenance
- Commit
42912df- Tree
e568ea0- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
8028437
5 files changed+10 added−9 removed
modified:deno_dist/context.ts+3 added-3 removed
47 unmodified lines48 }49 50 interface 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 lines304 : this.newResponse(text, arg)305 }306 307 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 lines331 * 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 linesmodified:src/adapter/vercel/handler.test.ts+1 added-1 removed
11 unmodified lines1213141516161718192027 unmodified lines11 unmodified lines * Checking if the `waitUntil` value is passed. */ // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore waitUntil: c.executionCtx.waitUntil(), waitUntil: c.executionCtx.waitUntil() as any, }) }) const handler = handle(app) const req = new Request('http://localhost/api/foo')27 unmodified linesmodified:src/client/client.test.ts+1 added
500 unmodified lines501 })502 503 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 linesmodified:src/context.ts+3 added-3 removed
47 unmodified lines48 }49 50 interface 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 lines304 : this.newResponse(text, arg)305 }306 307 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 lines331 * 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 linesmodified:src/hono.test.ts+2 added-2 removed
1923 unmodified lines1924 describe('Parse Body', () => {1925 const app = new Hono()1926 1927 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 })1933 1934 it('POST with JSON', async () => {1935 const req = new Request('http://localhost/json', {1265 unmodified lines