Skip to content

Commit 5ad469a

Browse files
feat(pretty-json): support structured JSON content-types (+json) (#5226)
* feat(pretty-json): support structured JSON content types and custom replacer * refactor(pretty-json): remove replacer option and keep structured json content-type support * add boundary to content-type regex and add tests --------- Co-authored-by: Yusuke Wada <[email protected]>
1 parent c91ec9b commit 5ad469a

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎src/middleware/pretty-json/index.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,50 @@ describe('JSON pretty by Middleware', () => {
7373
"message": "Hono!"
7474
}`)
7575
})
76+
77+
it('Should return pretty JSON output for structured JSON content-types (+json)', async () => {
78+
const app = new Hono()
79+
app.use('*', prettyJSON({ force: true }))
80+
app.get('/problem', (c) => {
81+
return c.newResponse(JSON.stringify({ type: 'about:blank', title: 'Bad Request' }), 400, {
82+
'Content-Type': 'application/problem+json',
83+
})
84+
})
85+
app.get('/jsonapi', (c) => {
86+
return c.newResponse(JSON.stringify({ data: { id: '1', type: 'articles' } }), 200, {
87+
'Content-Type': 'application/vnd.api+json',
88+
})
89+
})
90+
91+
const problemRes = await app.request('http://localhost/problem')
92+
expect(await problemRes.text()).toBe(`{
93+
"type": "about:blank",
94+
"title": "Bad Request"
95+
}`)
96+
97+
const jsonApiRes = await app.request('http://localhost/jsonapi')
98+
expect(await jsonApiRes.text()).toBe(`{
99+
"data": {
100+
"id": "1",
101+
"type": "articles"
102+
}
103+
}`)
104+
})
105+
106+
it('Should not touch responses with non-JSON content-types', async () => {
107+
const app = new Hono()
108+
app.use('*', prettyJSON({ force: true }))
109+
app.get('/text', (c) => c.text('{"message":"Hono!"}'))
110+
app.get('/json-seq', (c) => {
111+
return c.body('{"message":"Hono!"}', 200, {
112+
'Content-Type': 'application/json-seq',
113+
})
114+
})
115+
116+
const textRes = await app.request('http://localhost/text')
117+
expect(await textRes.text()).toBe('{"message":"Hono!"}')
118+
119+
const jsonSeqRes = await app.request('http://localhost/json-seq')
120+
expect(await jsonSeqRes.text()).toBe('{"message":"Hono!"}')
121+
})
76122
})

‎src/middleware/pretty-json/index.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface PrettyOptions {
2525
force?: boolean
2626
}
2727

28+
const jsonContentTypeRegex = /^application\/(?:[a-z0-9._-]+\+)?json(?=$|[;\s])/i
29+
2830
/**
2931
* Pretty JSON Middleware for Hono.
3032
*
@@ -48,7 +50,8 @@ export const prettyJSON = (options?: PrettyOptions): MiddlewareHandler => {
4850
return async function prettyJSON(c, next) {
4951
const pretty = options?.force || c.req.query(targetQuery) || c.req.query(targetQuery) === ''
5052
await next()
51-
if (pretty && c.res.headers.get('Content-Type')?.startsWith('application/json')) {
53+
const contentType = c.res.headers.get('Content-Type')
54+
if (pretty && contentType && jsonContentTypeRegex.test(contentType)) {
5255
const obj = await c.res.json()
5356
c.res = new Response(JSON.stringify(obj, null, options?.space ?? 2), c.res)
5457
}

0 commit comments

Comments
 (0)