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
Provenance
- Commit
da0a7ce- Tree
e122333- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
7103fe8
4 files changed+46 added−23 removed
modified:deno_dist/utils/encode.ts+1 added-1 removed
11234572 unmodified linesimport { Buffer } from "https://deno.land/std@0.171.0/node/buffer.ts";import { Buffer } from "https://deno.land/std@0.173.0/node/buffer.ts";export const encodeBase64 = (str: string): string => { if (str === null) { throw new TypeError('1st argument of "encodeBase64" should not be null.') }72 unmodified linesmodified:deno_dist/context.ts+15 added-11 removed
67 unmodified lines68 this._res = _res69 this.finalized = true70 }71 72- 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()75 76 let shouldAppend = false18 unmodified lines95 }96 }97 }98 99- status(status: StatusCode): void {99+ status = (status: StatusCode): void => {100 this._status = status101 }102 103 set<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void13 unmodified lines117 }118 return this._map[key]119 }120 121- pretty(prettyJSON: boolean, space: number = 2): void {121+ pretty = (prettyJSON: boolean, space: number = 2): void => {122 this._pretty = prettyJSON123 this._prettySpace = space124 }125 126- 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 lines158 }159 return finalizedHeaders160 }161 162- 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 }169 166- 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 lines178 headers['content-type'] = 'text/plain; charset=UTF-8'179 return this.newResponse(text, status, headers)180 }181 178- 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 }189 186- 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 }194 191- 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 }200 197- 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 }205 202- notFound(): Response | Promise<Response> {206+ notFound = (): Response | Promise<Response> => {207 return this.notFoundHandler(this as unknown as Context<string, E>)208 }209 210 get runtime(): Runtime {34 unmodified linesmodified:src/context.ts+15 added-11 removed
67 unmodified lines68 this._res = _res69 this.finalized = true70 }71 72- 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()75 76 let shouldAppend = false18 unmodified lines95 }96 }97 }98 99- status(status: StatusCode): void {99+ status = (status: StatusCode): void => {100 this._status = status101 }102 103 set<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void13 unmodified lines117 }118 return this._map[key]119 }120 121- pretty(prettyJSON: boolean, space: number = 2): void {121+ pretty = (prettyJSON: boolean, space: number = 2): void => {122 this._pretty = prettyJSON123 this._prettySpace = space124 }125 126- 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 lines158 }159 return finalizedHeaders160 }161 162- 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 }169 166- 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 lines178 headers['content-type'] = 'text/plain; charset=UTF-8'179 return this.newResponse(text, status, headers)180 }181 178- 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 }189 186- 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 }194 191- 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 }200 197- 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 }205 202- notFound(): Response | Promise<Response> {206+ notFound = (): Response | Promise<Response> => {207 return this.notFoundHandler(this as unknown as Context<string, E>)208 }209 210 get runtime(): Runtime {34 unmodified linesmodified:src/hono.test.ts+15 added
100 unmodified lines101 })102 })103 })104 105+describe('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+})119+120 describe('Routing', () => {121 it('Return it self', async () => {122 const app = new Hono()123 1266 unmodified lines