feat: add JSON pretty-print middleware (#150)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat: add JSON pretty-print middleware * exports json pretty middleware
authored by Yusuke Wada · committed by GitHub · parent cb81a95 · Browse files · 5 files·+65 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+65 added−3 removed
- tests:+42 added−1 removed
- other:+23 added−2 removed
Provenance
- Commit
ea93cf8- Tree
5d38db2- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
cb81a95
5 files changed+65 added−3 removed
added:src/middleware/pretty-json/pretty-json.test.ts+32 added
1234567891011121314151617181920212223242526272829303132import { Hono } from '../../hono'import { prettyJSON } from './pretty-json'
describe('JSON pretty by Middleware', () => { const app = new Hono()
app.get('/', (c) => { return c.json({ message: 'Hono!' }) })
it('Should return pretty JSON output', async () => { app.use('*', prettyJSON()) const req = new Request('http://localhost/?pretty') const res = await app.dispatch(req) expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe(`{ "message": "Hono!"}`) })
it('Should return pretty JSON output with 4 spaces', async () => { app.use('*', prettyJSON({ space: 4 })) const req = new Request('http://localhost/?pretty') const res = await app.dispatch(req) expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe(`{ "message": "Hono!"}`) })})added:src/middleware/pretty-json/pretty-json.ts+13 added
12345678910111213import type { Context } from '../../context'
type prettyOptions = { space: number}
export const prettyJSON = (options: prettyOptions = { space: 2 }) => { return async (c: Context, next: Function) => { const pretty = c.req.query('pretty') || c.req.query('pretty') === '' ? true : false c.pretty(pretty, options.space) await next() }}modified:src/context.test.ts+10 added-1 removed
23 unmodified lines24252627282829303132333435363738394041424344454647484976 unmodified lines23 unmodified lines expect(text).toBe('{"message":"Hello"}') expect(res.headers.get('X-Custom')).toBe('Message') })
it('c.json() with pretty', async () => { it('c.json() with c.pretty(true)', async () => { c.pretty(true) const res = c.json({ message: 'Hello' }) const text = await res.text() expect(text).toBe(`{ "message": "Hello"}`) })
it('c.json() with c.pretty(true, 4)', async () => { c.pretty(true, 4) const res = c.json({ message: 'Hello' }) const text = await res.text() expect(text).toBe(`{ "message": "Hello"}`) })
it('c.html()', async () => { const res = c.html('<h1>Hello! Hono!</h1>', 201, { 'X-Custom': 'Message' }) expect(res.status).toBe(201) expect(res.headers.get('Content-Type')).toMatch('text/html')76 unmodified linesmodified:src/context.ts+6 added-2 removed
13 unmodified lines14 private _headers: Headers15 private _status: number16 private _statusText: string17 private _pretty: boolean18+ private _prettySpace: number = 219 20 render: (template: string, params?: object, options?: object) => Promise<Response>21 notFound: () => Response | Promise<Response>22 32 unmodified lines55 this._status = number56 this._statusText = getStatusText(number)57 }58 58- pretty(prettyJSON: boolean): void {59+ pretty(prettyJSON: boolean, space: number = 2): void {60 this._pretty = prettyJSON61+ this._prettySpace = space62 }63 64 newResponse(data: Data, init: ResponseInit = {}): Response {65 init.status = init.status || this._status || 20034 unmodified lines100 json(object: object, status: number = this._status, headers: Headers = {}): Response {101 if (typeof object !== 'object') {102 throw new TypeError('json method arg must be a object!')103 }102- const body = this._pretty ? JSON.stringify(object, null, 2) : JSON.stringify(object)104+ const body = this._pretty105+ ? JSON.stringify(object, null, this._prettySpace)106+ : JSON.stringify(object)107 headers['Content-Type'] ||= 'application/json; charset=UTF-8'108 return this.body(body, status, headers)109 }110 25 unmodified linesmodified:package.json+4 added
23 unmodified lines24 "./graphql-server": "./dist/middleware/graphql-server/graphql-server.js",25 "./logger": "./dist/middleware/logger/logger.js",26 "./mustache": "./dist/middleware/mustache/mustache.js",27 "./powered-by": "./dist/middleware/powered-by/powered-by.js",28+ "./pretty-json": "./dist/middleware/pretty-json/pretty-json.js",29 "./serve-static": "./dist/middleware/serve-static/serve-static.js",30 "./router/trie-router": "./dist/router/trie-router/router.js",31 "./router/reg-exp-router": "./dist/router/reg-exp-router/index.js",32 "./utils/cloudflare": "./dist/utils/cloudflare.js",28 unmodified lines61 ],62 "powered-by": [63 "./dist/middleware/powered-by/powered-by.d.ts"64 ],65+ "pretty-json": [66+ "./dist/middleware/pretty-json/pretty-json.d.ts"67+ ],68 "serve-static": [69 "./dist/middleware/serve-static/serve-static.d.ts"70 ],71 "router/trie-router": [67 unmodified lines