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
Provenance
- Commit
8b0f189- Tree
0c85a01- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
Yusuke Wada <yusuke@cloudflare.com>- Parent
09368ac
5 files changed+10 added−36 removed
modified:deno_dist/middleware/pretty-json/index.ts+4 added-1 removed
5 unmodified lines678910101112131415165 unmodified lines
export const prettyJSON = (options: prettyOptions = { space: 2 }): MiddlewareHandler => { return async (c, next) => { const pretty = c.req.query('pretty') || c.req.query('pretty') === '' ? true : false c.pretty(pretty, options.space) await next() if (pretty && c.res.headers.get('Content-Type')?.startsWith('application/json')) { const obj = await c.res.json() c.res = new Response(JSON.stringify(obj, null, options.space), c.res) } }}modified:deno_dist/context.ts+1 added-8 removed
93 unmodified lines94 // 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 | undefined112 unmodified lines214 get: Get<E> = (key: string) => {215 return this._map ? this._map[key] : undefined216 }217 220- pretty = (prettyJSON: boolean, space: number = 2): void => {221- this._pre = prettyJSON222- this._preS = space223- }224-218 newResponse: NewResponse = (219 data: Data | null,220 arg?: StatusCode | ResponseInit,221 headers?: HeaderRecord87 unmodified lines309 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 linesmodified:src/middleware/pretty-json/index.ts+4 added-1 removed
5 unmodified lines678910101112131415165 unmodified lines
export const prettyJSON = (options: prettyOptions = { space: 2 }): MiddlewareHandler => { return async (c, next) => { const pretty = c.req.query('pretty') || c.req.query('pretty') === '' ? true : false c.pretty(pretty, options.space) await next() if (pretty && c.res.headers.get('Content-Type')?.startsWith('application/json')) { const obj = await c.res.json() c.res = new Response(JSON.stringify(obj, null, options.space), c.res) } }}modified:src/context.test.ts-18 removed
23 unmodified lines24 expect(text).toBe('{"message":"Hello"}')25 expect(res.headers.get('X-Custom')).toBe('Message')26 })27 28- 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- })36-37- 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- })45-28 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 linesmodified:src/context.ts+1 added-8 removed
93 unmodified lines94 // 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 | undefined112 unmodified lines214 get: Get<E> = (key: string) => {215 return this._map ? this._map[key] : undefined216 }217 220- pretty = (prettyJSON: boolean, space: number = 2): void => {221- this._pre = prettyJSON222- this._preS = space223- }224-218 newResponse: NewResponse = (219 data: Data | null,220 arg?: StatusCode | ResponseInit,221 headers?: HeaderRecord87 unmodified lines309 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