feat: add JSON pretty option (#149)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
You can set a JSON pretty option with `c.pretty(true)`.
authored by Yusuke Wada · committed by GitHub · parent 107aece · Browse files · 2 files·+24 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+24 added−7 removed
- tests:+18 added−6 removed
- other:+6 added−1 removed
Provenance
- Commit
3a73fef- Tree
9a5dd1d- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
107aece
2 files changed+24 added−7 removed
modified:src/context.test.ts+18 added-6 removed
123455678910111213145 unmodified lines202122232021242526272829303132333435363738394033 unmodified lines74757677667879808182838473858687888917 unmodified lines10710810911099111112113114115116import { Context } from './context'
describe('Context', () => { const req = new Request('http://localhost/') const c = new Context(req)
let c: Context beforeEach(() => { c = new Context(req) })
it('c.text()', async () => { const res = c.text('text in c', 201, { 'X-Custom': 'Message' }) expect(res.status).toBe(201) expect(res.headers.get('Content-Type')).toBe('text/plain; charset=UTF-8')5 unmodified lines const res = c.json({ message: 'Hello' }, 201, { 'X-Custom': 'Message' }) expect(res.status).toBe(201) expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8') const text = await res.text() const object = JSON.parse(text) expect(object.message).toBe('Hello') expect(text).toBe('{"message":"Hello"}') expect(res.headers.get('X-Custom')).toBe('Message') })
it('c.json() with pretty', async () => { c.pretty(true) 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')33 unmodified lines const obj: { [key: string]: string } = await res.json() expect(obj['hono']).toBe('great app') })
it('Content-Length', async () => { it('Should return `Content-Length`', async () => { let res = c.text('abcdefg') expect(res.headers.get('Content-Length')).toBe('7') res = c.text('炎') expect(res.headers.get('Content-Length')).toBe('3') })
it('Headers, status, statusText', async () => { it('Has headers, status, and statusText', async () => { c.header('X-Custom1', 'Message1') c.header('X-Custom2', 'Message2') c.status(200) const res = c.newResponse('this is body', {17 unmodified lines expect(c.res.status).toBe(201) expect(c.res.statusText).toBe('OK') })
it('Default status code is 200', async () => { it('Should return 200 response', async () => { const res = c.text('Text') expect(res.status).toBe(200) expect(res.statusText).toBe('OK') })})modified:src/context.ts+6 added-1 removed
12 unmodified lines13 event: FetchEvent14 private _headers: Headers15 private _status: number16 private _statusText: string17+ private _pretty: boolean18 19 render: (template: string, params?: object, options?: object) => Promise<Response>20 notFound: () => Response | Promise<Response>21 32 unmodified lines54 this._status = number55 this._statusText = getStatusText(number)56 }57 58+ pretty(prettyJSON: boolean): void {59+ this._pretty = prettyJSON60+ }61+62 newResponse(data: Data, init: ResponseInit = {}): Response {63 init.status = init.status || this._status || 20064 init.statusText = init.statusText || this._statusText || getStatusText(init.status)65 32 unmodified lines98 json(object: object, status: number = this._status, headers: Headers = {}): Response {99 if (typeof object !== 'object') {100 throw new TypeError('json method arg must be a object!')101 }97- const body = JSON.stringify(object)102+ const body = this._pretty ? JSON.stringify(object, null, 2) : JSON.stringify(object)103 headers['Content-Type'] ||= 'application/json; charset=UTF-8'104 return this.body(body, status, headers)105 }106 25 unmodified lines