Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pretty-json): support custom query #3300

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/middleware/pretty-json/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,28 @@ describe('JSON pretty by Middleware', () => {
"message": "Hono!"
}`)
})

it('Should return pretty JSON output when middleware received custom query', async () => {
const targetQuery = 'format'

const app = new Hono()
app.use(
'*',
prettyJSON({
query: targetQuery,
})
)
app.get('/', (c) =>
c.json({
message: 'Hono!',
})
)

const prettyText = await (await app.request(`?${targetQuery}`)).text()
expect(prettyText).toBe(`{
"message": "Hono!"
}`)
const nonPrettyText = await (await app.request('?pretty')).text()
expect(nonPrettyText).toBe('{"message":"Hono!"}')
})
})
24 changes: 17 additions & 7 deletions src/middleware/pretty-json/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@

import type { MiddlewareHandler } from '../../types'

type prettyOptions = {
space: number
interface PrettyOptions {
/**
* Number of spaces for indentation.
* @default 2
*/
space?: number

/**
* Query conditions for when to Pretty.
* @default 'pretty'
*/
query?: string
}

/**
* Pretty JSON Middleware for Hono.
*
* @see {@link https://hono.dev/docs/middleware/builtin/pretty-json}
*
* @param {prettyOptions} [options] - The options for the pretty JSON middleware.
* @param {number} [options.space=2] - Number of spaces for indentation.
* @param options - The options for the pretty JSON middleware.
* @returns {MiddlewareHandler} The middleware handler function.
*
* @example
Expand All @@ -28,13 +37,14 @@ type prettyOptions = {
* })
* ```
*/
export const prettyJSON = (options: prettyOptions = { space: 2 }): MiddlewareHandler => {
export const prettyJSON = (options?: PrettyOptions): MiddlewareHandler => {
const targetQuery = options?.query ?? 'pretty'
return async function prettyJSON(c, next) {
const pretty = c.req.query('pretty') || c.req.query('pretty') === ''
const pretty = c.req.query(targetQuery) || c.req.query(targetQuery) === ''
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)
c.res = new Response(JSON.stringify(obj, null, options?.space ?? 2), c.res)
}
}
}