2022-10-31 15:07:56 +00:00
|
|
|
import type { MiddlewareHandler } from '../../types.ts'
|
2022-07-02 06:09:45 +00:00
|
|
|
|
|
|
|
type prettyOptions = {
|
|
|
|
space: number
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:17:20 +00:00
|
|
|
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()
|
2023-08-05 09:13:39 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|