fix: enable passing Generics to c.req.parseBody, default is any (#481)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent ddfe07e · Browse files · 5 files·+48 added−35 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+48 added−35 removed
- tests:+8 added−3 removed
- other:+40 added−32 removed
Provenance
- Commit
c7ee2e7- Tree
ef0c167- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
ddfe07e
5 files changed+48 added−35 removed
modified:deno_dist/utils/body.ts+11 added-10 removed
112334567789910111112131314151516171819202122222324252625262728export type Body = string | object | Record<string, string | File> | ArrayBufferexport async function parseBody<T = any>(r: Request | Response): Promise<T> { let body: any
export const parseBody = async (r: Request | Response): Promise<Body> => { const contentType = r.headers.get('Content-Type') || ''
if (contentType.includes('application/json')) { let body = {} let jsonBody = {} try { body = await r.json() jsonBody = await r.json() } catch {} // Do nothing return body body = jsonBody } else if (contentType.includes('application/text')) { return await r.text() body = await r.text() } else if (contentType.startsWith('text')) { return await r.text() body = await r.text() } else if (contentType.includes('form')) { const form: Record<string, string | File> = {} const data = [...(await r.formData())].reduce((acc, cur) => { acc[cur[0]] = cur[1] return acc }, form) return data body = data } else { body = await r.arrayBuffer() }
const arrayBuffer = await r.arrayBuffer() return arrayBuffer return body as T}modified:deno_dist/request.ts+9 added-6 removed
12234518 unmodified lines2425262729282931303132333462 unmodified lines979899100102101102103104104105106107108106109110111import { parseBody } from './utils/body.ts'import type { Body } from './utils/body.ts'import type { Cookie } from './utils/cookie.ts'import { parse } from './utils/cookie.ts'
declare global {18 unmodified lines cookie: { (name: string): string (): Cookie } parsedBody?: Promise<Body> parsedBody?: Promise<any> parseBody: { (): Promise<Body> <T = any>(): Promise<T> } }}
62 unmodified lines return obj } } as InstanceType<typeof Request>['cookie']
Request.prototype.parseBody = function (this: Request) { Request.prototype.parseBody = function <T = any>(this: Request): Promise<T> { let body: Promise<T> if (!this.parsedBody) { this.parsedBody = parseBody(this) body = parseBody<T>(this) this.parsedBody = body } else { body = this.parsedBody } return this.parsedBody return body } as InstanceType<typeof Request>['parseBody']}modified:src/utils/body.ts+11 added-10 removed
112334567789910111112131314151516171819202122222324252625262728export type Body = string | object | Record<string, string | File> | ArrayBufferexport async function parseBody<T = any>(r: Request | Response): Promise<T> { let body: any
export const parseBody = async (r: Request | Response): Promise<Body> => { const contentType = r.headers.get('Content-Type') || ''
if (contentType.includes('application/json')) { let body = {} let jsonBody = {} try { body = await r.json() jsonBody = await r.json() } catch {} // Do nothing return body body = jsonBody } else if (contentType.includes('application/text')) { return await r.text() body = await r.text() } else if (contentType.startsWith('text')) { return await r.text() body = await r.text() } else if (contentType.includes('form')) { const form: Record<string, string | File> = {} const data = [...(await r.formData())].reduce((acc, cur) => { acc[cur[0]] = cur[1] return acc }, form) return data body = data } else { body = await r.arrayBuffer() }
const arrayBuffer = await r.arrayBuffer() return arrayBuffer return body as T}modified:src/hono.test.ts+8 added-3 removed
961 unmodified lines962 963 describe('Parse Body', () => {964 const app = new Hono()965 966+ type Payload = {967+ message: string968+ }969+970 app.post('/json', async (c) => {971 return c.json(await c.req.parseBody(), 200)972 })973 app.post('/text', async (c) => {3 unmodified lines977 return c.json(await c.req.parseBody(), 200)978 })979 980 it('POST with JSON', async () => {977- const payload = { message: 'hello hono' }981+ const payload: Payload = { message: 'hello hono' }982 const req = new Request('http://localhost/json', {983 method: 'POST',984 body: JSON.stringify(payload),985 headers: new Headers({ 'Content-Type': 'application/json' }),986 })987 const res = await app.request(req)988 expect(res).not.toBeNull()989 expect(res.status).toBe(200)986- expect(await req.parseBody()).toEqual(payload)987- expect(await res.json()).toEqual(payload)990+ const body = await req.parseBody<Payload>()991+ expect(body.message).toBe('hello hono')992+ expect(body).toEqual(payload)993 })994 995 it('POST with text', async () => {996 const payload = 'hello'48 unmodified linesmodified:src/request.ts+9 added-6 removed
1 import { parseBody } from './utils/body'2-import type { Body } from './utils/body'2 import type { Cookie } from './utils/cookie'3 import { parse } from './utils/cookie'4 5 declare global {18 unmodified lines24 cookie: {25 (name: string): string26 (): Cookie27 }29- parsedBody?: Promise<Body>28+ parsedBody?: Promise<any>29 parseBody: {31- (): Promise<Body>30+ <T = any>(): Promise<T>31 }32 }33 }34 62 unmodified lines97 return obj98 }99 } as InstanceType<typeof Request>['cookie']100 102- Request.prototype.parseBody = function (this: Request) {101+ Request.prototype.parseBody = function <T = any>(this: Request): Promise<T> {102+ let body: Promise<T>103 if (!this.parsedBody) {104- this.parsedBody = parseBody(this)104+ body = parseBody<T>(this)105+ this.parsedBody = body106+ } else {107+ body = this.parsedBody108 }106- return this.parsedBody109+ return body110 } as InstanceType<typeof Request>['parseBody']111 }