mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 10:51:01 +00:00
17 lines
536 B
TypeScript
17 lines
536 B
TypeScript
import type { MiddlewareHandler } from '../../types.ts'
|
|
|
|
type prettyOptions = {
|
|
space: number
|
|
}
|
|
|
|
export const prettyJSON = (options: prettyOptions = { space: 2 }): MiddlewareHandler => {
|
|
return async (c, next) => {
|
|
const pretty = c.req.query('pretty') || c.req.query('pretty') === '' ? true : false
|
|
await next()
|
|
if (pretty && c.res.headers.get('Content-Type')?.startsWith('application/json')) {
|
|
const obj = await c.res.json()
|
|
c.res = new Response(JSON.stringify(obj, null, options.space), c.res)
|
|
}
|
|
}
|
|
}
|