0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 10:51:01 +00:00
hono/deno_dist/middleware/pretty-json/index.ts

17 lines
536 B
TypeScript
Raw Normal View History

import type { MiddlewareHandler } from '../../types.ts'
2022-07-02 06:09:45 +00:00
type prettyOptions = {
space: number
}
export const prettyJSON = (options: prettyOptions = { space: 2 }): MiddlewareHandler => {
return async (c, next) => {
2022-07-02 06:09:45 +00:00
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)
}
2022-07-02 06:09:45 +00:00
}
}