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
Provenance
- Commit
b1c5059- Tree
077b17e- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
73860a1
5 files changed+59 added−8 removed
modified:deno_dist/utils/types.ts+18 added
8 unmodified lines91011121314151617181920212223242526272829308 unmodified lines k: infer I) => void ? I : never
export type JSONPrimitive = string | boolean | number | null | undefinedexport type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject }export type JSONValue = JSONObject | JSONArray | JSONPrimitive
// `boolean` will be `true` or `false` because it's an alias of them.// See: https://github.com/microsoft/TypeScript/issues/22596// This type converts `true | false` to `boolean` that we expect.type TrueAndFalseToBoolean<T> = T extends true ? boolean : T extends false ? boolean : T
export type PrettyJSON<T> = T extends JSONPrimitive ? TrueAndFalseToBoolean<T> : T extends JSONArray ? PrettyJSON<T[number]> : T extends JSONObject ? { [K in keyof T]: PrettyJSON<T[K]> } : nevermodified:deno_dist/context.ts+6 added-4 removed
2 unmodified lines3 import type { Env, NotFoundHandler, Input } from './types.ts'4 import type { CookieOptions } from './utils/cookie.ts'5 import { serialize } from './utils/cookie.ts'6 import type { StatusCode } from './utils/http-status.ts'7+import type { PrettyJSON, JSONValue } from './utils/types.ts'8 9 type Runtime = 'node' | 'deno' | 'bun' | 'workerd' | 'fastly' | 'edge-light' | 'lagon' | 'other'10 type HeaderRecord = Record<string, string | string[]>11 type Data = string | ArrayBuffer | ReadableStream225 unmodified lines237 this._preparedHeaders['content-type'] = 'application/json; charset=UTF-8'238 return this.newResponse(body, status, headers)239 }240 240- 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 linesmodified:src/client/client.test.ts+11 added
330 unmodified lines331 type verify = Expect<Equal<boolean, typeof data.ok>>332 expect(data.ok).toBe(true)333 })334 335+ 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+ })345+346 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 linesmodified:src/utils/types.ts+18 added
8 unmodified lines91011121314151617181920212223242526272829308 unmodified lines k: infer I) => void ? I : never
export type JSONPrimitive = string | boolean | number | null | undefinedexport type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject }export type JSONValue = JSONObject | JSONArray | JSONPrimitive
// `boolean` will be `true` or `false` because it's an alias of them.// See: https://github.com/microsoft/TypeScript/issues/22596// This type converts `true | false` to `boolean` that we expect.type TrueAndFalseToBoolean<T> = T extends true ? boolean : T extends false ? boolean : T
export type PrettyJSON<T> = T extends JSONPrimitive ? TrueAndFalseToBoolean<T> : T extends JSONArray ? PrettyJSON<T[number]> : T extends JSONObject ? { [K in keyof T]: PrettyJSON<T[K]> } : nevermodified:src/context.ts+6 added-4 removed
2 unmodified lines3 import type { Env, NotFoundHandler, Input } from './types'4 import type { CookieOptions } from './utils/cookie'5 import { serialize } from './utils/cookie'6 import type { StatusCode } from './utils/http-status'7+import type { PrettyJSON, JSONValue } from './utils/types'8 9 type Runtime = 'node' | 'deno' | 'bun' | 'workerd' | 'fastly' | 'edge-light' | 'lagon' | 'other'10 type HeaderRecord = Record<string, string | string[]>11 type Data = string | ArrayBuffer | ReadableStream225 unmodified lines237 this._preparedHeaders['content-type'] = 'application/json; charset=UTF-8'238 return this.newResponse(body, status, headers)239 }240 240- 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