Skip to content
Myr

fix(context): enable destruction such `json` (#813)

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

This will fix #812

authored by Yusuke Wada · committed by GitHub · parent 7103fe8 · Browse files · 4 files+46 added−23 removed
This commit

State

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

Diff stats

4 files+46 added−23 removed

  • tests:+15 added−0 removed
  • other:+31 added−23 removed

4 files+46 added−23 removed

modified:deno_dist/utils/encode.ts+1 added-1 removed
modified:deno_dist/context.ts+15 added-11 removed
67 unmodified lines
68 this._res = _res69 this.finalized = true70 }7172 header(name: string, value: string, options?: { append?: boolean }): void {72 header = (name: string, value: string, options?: { append?: boolean }): void => {73 this._headers ||= {}74 const key = name.toLowerCase()7576 let shouldAppend = false
18 unmodified lines
95 }96 }97 }9899 status(status: StatusCode): void {99 status = (status: StatusCode): void => {100 this._status = status101 }102103 set<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void
13 unmodified lines
117 }118 return this._map[key]119 }120121 pretty(prettyJSON: boolean, space: number = 2): void {121 pretty = (prettyJSON: boolean, space: number = 2): void => {122 this._pretty = prettyJSON123 this._prettySpace = space124 }125126 newResponse(data: Data | null, status: StatusCode, headers: Headers = {}): Response {126 newResponse = (data: Data | null, status: StatusCode, headers: Headers = {}): Response => {127 return new Response(data, {128 status,129 headers: this._finalizeHeaders(headers),130 })
27 unmodified lines
158 }159 return finalizedHeaders160 }161162 body(data: Data | null, status: StatusCode = this._status, headers: Headers = {}): Response {162 body = (163 data: Data | null,164 status: StatusCode = this._status,165 headers: Headers = {}166 ): Response => {167 return this.newResponse(data, status, headers)168 }169166 text(text: string, status?: StatusCode, headers?: Headers): Response {170 text = (text: string, status?: StatusCode, headers?: Headers): Response => {171 // If the header is empty, return Response immediately.172 // Content-Type will be added automatically as `text/plain`.173 if (!headers && !status && !this._res && !this._headers) {174 return new Response(text)
3 unmodified lines
178 headers['content-type'] = 'text/plain; charset=UTF-8'179 return this.newResponse(text, status, headers)180 }181178 json<T>(object: T, status: StatusCode = this._status, headers: Headers = {}): Response {182 json = <T>(object: T, status: StatusCode = this._status, headers: Headers = {}): Response => {183 const body = this._pretty184 ? JSON.stringify(object, null, this._prettySpace)185 : JSON.stringify(object)186 headers['content-type'] = 'application/json; charset=UTF-8'187 return this.newResponse(body, status, headers)188 }189186 html(html: string, status: StatusCode = this._status, headers: Headers = {}): Response {190 html = (html: string, status: StatusCode = this._status, headers: Headers = {}): Response => {191 headers['content-type'] = 'text/html; charset=UTF-8'192 return this.newResponse(html, status, headers)193 }194191 redirect(location: string, status: StatusCode = 302): Response {195 redirect = (location: string, status: StatusCode = 302): Response => {196 return this.newResponse(null, status, {197 Location: location,198 })199 }200197 cookie(name: string, value: string, opt?: CookieOptions): void {201 cookie = (name: string, value: string, opt?: CookieOptions): void => {202 const cookie = serialize(name, value, opt)203 this.header('set-cookie', cookie, { append: true })204 }205202 notFound(): Response | Promise<Response> {206 notFound = (): Response | Promise<Response> => {207 return this.notFoundHandler(this as unknown as Context<string, E>)208 }209210 get runtime(): Runtime {
34 unmodified lines
modified:src/context.ts+15 added-11 removed
67 unmodified lines
68 this._res = _res69 this.finalized = true70 }7172 header(name: string, value: string, options?: { append?: boolean }): void {72 header = (name: string, value: string, options?: { append?: boolean }): void => {73 this._headers ||= {}74 const key = name.toLowerCase()7576 let shouldAppend = false
18 unmodified lines
95 }96 }97 }9899 status(status: StatusCode): void {99 status = (status: StatusCode): void => {100 this._status = status101 }102103 set<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void
13 unmodified lines
117 }118 return this._map[key]119 }120121 pretty(prettyJSON: boolean, space: number = 2): void {121 pretty = (prettyJSON: boolean, space: number = 2): void => {122 this._pretty = prettyJSON123 this._prettySpace = space124 }125126 newResponse(data: Data | null, status: StatusCode, headers: Headers = {}): Response {126 newResponse = (data: Data | null, status: StatusCode, headers: Headers = {}): Response => {127 return new Response(data, {128 status,129 headers: this._finalizeHeaders(headers),130 })
27 unmodified lines
158 }159 return finalizedHeaders160 }161162 body(data: Data | null, status: StatusCode = this._status, headers: Headers = {}): Response {162 body = (163 data: Data | null,164 status: StatusCode = this._status,165 headers: Headers = {}166 ): Response => {167 return this.newResponse(data, status, headers)168 }169166 text(text: string, status?: StatusCode, headers?: Headers): Response {170 text = (text: string, status?: StatusCode, headers?: Headers): Response => {171 // If the header is empty, return Response immediately.172 // Content-Type will be added automatically as `text/plain`.173 if (!headers && !status && !this._res && !this._headers) {174 return new Response(text)
3 unmodified lines
178 headers['content-type'] = 'text/plain; charset=UTF-8'179 return this.newResponse(text, status, headers)180 }181178 json<T>(object: T, status: StatusCode = this._status, headers: Headers = {}): Response {182 json = <T>(object: T, status: StatusCode = this._status, headers: Headers = {}): Response => {183 const body = this._pretty184 ? JSON.stringify(object, null, this._prettySpace)185 : JSON.stringify(object)186 headers['content-type'] = 'application/json; charset=UTF-8'187 return this.newResponse(body, status, headers)188 }189186 html(html: string, status: StatusCode = this._status, headers: Headers = {}): Response {190 html = (html: string, status: StatusCode = this._status, headers: Headers = {}): Response => {191 headers['content-type'] = 'text/html; charset=UTF-8'192 return this.newResponse(html, status, headers)193 }194191 redirect(location: string, status: StatusCode = 302): Response {195 redirect = (location: string, status: StatusCode = 302): Response => {196 return this.newResponse(null, status, {197 Location: location,198 })199 }200197 cookie(name: string, value: string, opt?: CookieOptions): void {201 cookie = (name: string, value: string, opt?: CookieOptions): void => {202 const cookie = serialize(name, value, opt)203 this.header('set-cookie', cookie, { append: true })204 }205202 notFound(): Response | Promise<Response> {206 notFound = (): Response | Promise<Response> => {207 return this.notFoundHandler(this as unknown as Context<string, E>)208 }209210 get runtime(): Runtime {
34 unmodified lines
modified:src/hono.test.ts+15 added
100 unmodified lines
101 })102 })103})104105describe('Destruct functions in context', () => {106 it('Should return 200 response - text', async () => {107 const app = new Hono()108 app.get('/text', ({ text }) => text('foo'))109 const res = await app.request('http://localhost/text')110 expect(res.status).toBe(200)111 })112 it('Should return 200 response - json', async () => {113 const app = new Hono()114 app.get('/json', ({ json }) => json({ foo: 'bar' }))115 const res = await app.request('http://localhost/json')116 expect(res.status).toBe(200)117 })118})119120describe('Routing', () => {121 it('Return it self', async () => {122 const app = new Hono()123
1266 unmodified lines