Skip to content
Myr

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

2 files+24 added−7 removed

modified:src/context.test.ts+18 added-6 removed
modified:src/context.ts+6 added-1 removed
12 unmodified lines
13 event: FetchEvent14 private _headers: Headers15 private _status: number16 private _statusText: string17 private _pretty: boolean1819 render: (template: string, params?: object, options?: object) => Promise<Response>20 notFound: () => Response | Promise<Response>21
32 unmodified lines
54 this._status = number55 this._statusText = getStatusText(number)56 }5758 pretty(prettyJSON: boolean): void {59 this._pretty = prettyJSON60 }6162 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 lines
98 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