Kind: Function
Source: src/middleware/pretty-json/index.ts
Part of: Middleware
Pretty JSON Middleware for Hono.
prettyJSON creates Hono middleware that formats JSON response bodies with indentation when the request asks for pretty output. Register it before route handlers so it can inspect downstream JSON responses and replace them with formatted JSON when appropriate.
Signature
tsfunction prettyJSON(options: PrettyOptions): MiddlewareHandler
Parameters
| Name | Type |
|---|---|
options | PrettyOptions |
Returns: MiddlewareHandler
Diagram
mermaidgraph LR Request[Incoming request] --> Middleware[prettyJSON middleware] Middleware --> Route[Route handler] Route --> Response[JSON response] Response --> Middleware Middleware --> Formatted[Formatted JSON response]
Usage
tsimport { Hono } from 'hono'
import { prettyJSON } from 'hono/pretty-json'
const app = new Hono()
app.use('*', prettyJSON())
app.get('/users', (c) => {
return c.json({
users: [{ id: 'user-a', name: 'Ada' }],
})
})
export default app
Request the route with ?pretty to receive formatted JSON.
AI Coding Instructions
- Register
prettyJSON()before routes whose JSON responses should support formatted output. - Return JSON through Hono response helpers such as
c.json()so the response has a JSON content type. - Preserve the middleware order:
prettyJSONmust wrap downstream route handling to format the completed response. - Avoid applying assumptions about non-JSON response bodies; the middleware should only reformat valid JSON responses.
Was this page helpful?