Skip to content
Myr

feat(`jsonT`): check JSON type (#939)

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

* feat(`jsonT`): check JSON type * denoify * use `JSONValue` * create `TrueAndFalseToBoolean` and add a comment

authored by Yusuke Wada · committed by GitHub · parent 73860a1 · Browse files · 5 files+59 added−8 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+59 added−8 removed

  • tests:+11 added−0 removed
  • other:+48 added−8 removed

5 files+59 added−8 removed

modified:deno_dist/utils/types.ts+18 added
modified:deno_dist/context.ts+6 added-4 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 { PrettyJSON, JSONValue } 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
225 unmodified lines
237 this._preparedHeaders['content-type'] = 'application/json; charset=UTF-8'238 return this.newResponse(body, status, headers)239 }240240 jsonT = <T = object>(241 object: T,241 jsonT = <T>(242 object: T extends JSONValue ? T : JSONValue,243 status: StatusCode = this._status,244 headers?: HeaderRecord244 ): TypedResponse<T> => {245 ): TypedResponse<T extends JSONValue ? (JSONValue extends T ? never : PrettyJSON<T>) : never> => {246 return {247 response: this.json(object, status, headers),248 // eslint-disable-next-line @typescript-eslint/no-explicit-any247 data: object,249 data: object as any,250 format: 'json',251 }252 }253
65 unmodified lines
modified:src/client/client.test.ts+11 added
330 unmodified lines
331 type verify = Expect<Equal<boolean, typeof data.ok>>332 expect(data.ok).toBe(true)333 })334335 it('Should not allow the incorrect JSON type', async () => {336 const app = new Hono()337 // @ts-ignore338 const route = app.get('/api/foo', (c) => c.jsonT({ datetime: new Date() }))339 type AppType = typeof route340 const client = hc<AppType>('http://localhost')341 const res = await client.api.foo.$get()342 const data = await res.json()343 type verify = Expect<Equal<never, typeof data>>344 })345346 describe('Multiple endpoints', () => {347 const api = new Hono()348 .get('/foo', (c) => c.jsonT({ foo: '' }))349 .post('/bar', (c) => c.jsonT({ bar: 0 }))
44 unmodified lines
modified:src/utils/types.ts+18 added
modified:src/context.ts+6 added-4 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 { PrettyJSON, JSONValue } 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
225 unmodified lines
237 this._preparedHeaders['content-type'] = 'application/json; charset=UTF-8'238 return this.newResponse(body, status, headers)239 }240240 jsonT = <T = object>(241 object: T,241 jsonT = <T>(242 object: T extends JSONValue ? T : JSONValue,243 status: StatusCode = this._status,244 headers?: HeaderRecord244 ): TypedResponse<T> => {245 ): TypedResponse<T extends JSONValue ? (JSONValue extends T ? never : PrettyJSON<T>) : never> => {246 return {247 response: this.json(object, status, headers),248 // eslint-disable-next-line @typescript-eslint/no-explicit-any247 data: object,249 data: object as any,250 format: 'json',251 }252 }253
65 unmodified lines