Skip to content
Myr

refactor(json-pretty): don't use `c.pretty()` (#1288)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by Yusuke Wada · parent 09368ac · Browse files · 5 files+10 added−36 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+10 added−36 removed

  • tests:+0 added−18 removed
  • other:+10 added−18 removed

5 files+10 added−36 removed

modified:deno_dist/middleware/pretty-json/index.ts+4 added-1 removed
modified:deno_dist/context.ts+1 added-8 removed
93 unmodified lines
94 // eslint-disable-next-line @typescript-eslint/no-explicit-any95 private _req?: HonoRequest<any, any>96 private _status: StatusCode = 20097 private _exCtx: FetchEventLike | ExecutionContext | undefined // _executionCtx98 private _pre: boolean = false // _pretty99 private _preS: number = 2 // _prettySpace98 private _map: Record<string, unknown> | undefined99 private _h: Headers | undefined = undefined // _headers100 private _pH: Record<string, string> | undefined = undefined // _preparedHeaders101 private _res: Response | undefined
112 unmodified lines
214 get: Get<E> = (key: string) => {215 return this._map ? this._map[key] : undefined216 }217220 pretty = (prettyJSON: boolean, space: number = 2): void => {221 this._pre = prettyJSON222 this._preS = space223 }224218 newResponse: NewResponse = (219 data: Data | null,220 arg?: StatusCode | ResponseInit,221 headers?: HeaderRecord
87 unmodified lines
309 object: T,310 arg?: StatusCode | RequestInit,311 headers?: HeaderRecord312 ) => {320 const body = this._pre ? JSON.stringify(object, null, this._preS) : JSON.stringify(object)313 const body = JSON.stringify(object)314 this._pH ??= {}315 this._pH['content-type'] = 'application/json; charset=UTF-8'316 return typeof arg === 'number'317 ? this.newResponse(body, arg, headers)
98 unmodified lines
modified:src/middleware/pretty-json/index.ts+4 added-1 removed
modified:src/context.test.ts-18 removed
23 unmodified lines
24 expect(text).toBe('{"message":"Hello"}')25 expect(res.headers.get('X-Custom')).toBe('Message')26 })2728 it('c.json() with c.pretty(true)', async () => {29 c.pretty(true)30 const res = c.json({ message: 'Hello' })31 const text = await res.text()32 expect(text).toBe(`{33 "message": "Hello"34}`)35 })3637 it('c.json() with c.pretty(true, 4)', async () => {38 c.pretty(true, 4)39 const res = c.json({ message: 'Hello' })40 const text = await res.text()41 expect(text).toBe(`{42 "message": "Hello"43}`)44 })4528 it('c.html()', async () => {29 const res = c.html('<h1>Hello! Hono!</h1>', 201, { 'X-Custom': 'Message' })30 expect(res.status).toBe(201)31 expect(res.headers.get('Content-Type')).toMatch('text/html')
230 unmodified lines
modified:src/context.ts+1 added-8 removed
93 unmodified lines
94 // eslint-disable-next-line @typescript-eslint/no-explicit-any95 private _req?: HonoRequest<any, any>96 private _status: StatusCode = 20097 private _exCtx: FetchEventLike | ExecutionContext | undefined // _executionCtx98 private _pre: boolean = false // _pretty99 private _preS: number = 2 // _prettySpace98 private _map: Record<string, unknown> | undefined99 private _h: Headers | undefined = undefined // _headers100 private _pH: Record<string, string> | undefined = undefined // _preparedHeaders101 private _res: Response | undefined
112 unmodified lines
214 get: Get<E> = (key: string) => {215 return this._map ? this._map[key] : undefined216 }217220 pretty = (prettyJSON: boolean, space: number = 2): void => {221 this._pre = prettyJSON222 this._preS = space223 }224218 newResponse: NewResponse = (219 data: Data | null,220 arg?: StatusCode | ResponseInit,221 headers?: HeaderRecord
87 unmodified lines
309 object: T,310 arg?: StatusCode | RequestInit,311 headers?: HeaderRecord312 ) => {320 const body = this._pre ? JSON.stringify(object, null, this._preS) : JSON.stringify(object)313 const body = JSON.stringify(object)314 this._pH ??= {}315 this._pH['content-type'] = 'application/json; charset=UTF-8'316 return typeof arg === 'number'317 ? this.newResponse(body, arg, headers)
98 unmodified lines