Skip to content
Myr

index.ts

This file

Path

src/middleware/pretty-json/index.ts

68 lines · 1.9 KiB · TypeScript

Last changed by

fix(pretty-json): don't break responses with unparseable JSON bodies (#5377)Neil Daquioagbefore adoptionf950277

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–27why, one step back
1/**
2 * @module3 * Pretty JSON Middleware for Hono.4 */56import type { MiddlewareHandler } from '../../types'78interface PrettyOptions {9 /**10 * Number of spaces for indentation.11 * @default 212 */13 space?: number1415 /**16 * Query conditions for when to Pretty.17 * @default 'pretty'18 */19 query?: string2021 /**22 * Force prettification of JSON responses regardless of query parameters.23 * @default false24 */25 force?: boolean26}27

feat(pretty-json): support structured JSON content-types (+json) (#5226)ADITYA KUMAR JHAbefore adoption5ad469a

lines 28–29why, one step back
28const jsonContentTypeRegex = /^application\/(?:[a-z0-9._-]+\+)?json(?=$|[;\s])/i
29

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 30–52why, one step back
30/**
31 * Pretty JSON Middleware for Hono.32 *33 * @see {@link https://hono.dev/docs/middleware/builtin/pretty-json}34 *35 * @param options - The options for the pretty JSON middleware.36 * @returns {MiddlewareHandler} The middleware handler function.37 *38 * @example39 * ```ts40 * const app = new Hono()41 *42 * app.use(prettyJSON()) // With options: prettyJSON({ space: 4 })43 * app.get('/', (c) => {44 * return c.json({ message: 'Hono!' })45 * })46 * ```47 */48export const prettyJSON = (options?: PrettyOptions): MiddlewareHandler => {49 const targetQuery = options?.query ?? 'pretty'50 return async function prettyJSON(c, next) {51 const pretty = options?.force || c.req.query(targetQuery) || c.req.query(targetQuery) === ''52 await next()

feat(pretty-json): support structured JSON content-types (+json) (#5226)ADITYA KUMAR JHAbefore adoption5ad469a

lines 53–54why, one step back
53 const contentType = c.res.headers.get('Content-Type')
54 if (pretty && contentType && jsonContentTypeRegex.test(contentType)) {

fix(pretty-json): don't break responses with unparseable JSON bodies (#5377)Neil Daquioagbefore adoptionf950277

lines 55–62why, one step back
55 // Read from a clone so that the response is kept intact when the body is not parseable as JSON
56 let obj: unknown57 try {58 obj = await c.res.clone().json()59 } catch {60 // e.g. an empty 204 response or a body that is not valid JSON61 return62 }

Merge commit from forkYusuke Wadabefore adoptionf0b094d

line 63why, one step back
63 c.res = new Response(JSON.stringify(obj, null, options?.space ?? 2), c.res)

fix(pretty-json): don't break responses with unparseable JSON bodies (#5377)Neil Daquioagbefore adoptionf950277

lines 64–65why, one step back
64 // The length of the body has changed, so remove the stale Content-Length
65 c.res.headers.delete('Content-Length')

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 66–68why, one step back
66 }
67 }68}