Skip to content
Myr

feat: `c.req.body` and `c.req.json` accept generics (#529)

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 5133a93 · Browse files · 4 files+34 added−32 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+34 added−32 removed

  • other:+34 added−32 removed

4 files+34 added−32 removed

modified:deno_dist/utils/body.ts+6 added-2 removed
modified:deno_dist/request.ts+11 added-14 removed
modified:src/utils/body.ts+6 added-2 removed
modified:src/request.ts+11 added-14 removed
1import { parseBody } from './utils/body'2import type { BodyData } from './utils/body'3import type { Cookie } from './utils/cookie'4import { parse } from './utils/cookie'5import { getQueryStringFromURL } from './utils/url'6
26 unmodified lines
33 cookie: {34 (name: string): string35 (): Cookie36 }36 bodyData?: Record<string, string | File>37 parseBody: {38 (): Promise<Record<string, string | File>>39 }37 bodyData?: BodyData38 parseBody<BodyType extends BodyData>(): Promise<BodyType>39 jsonData?: any41 json: {42 (): Promise<any>43 }40 json<JSONData = any>(): Promise<JSONData>41 data: Data42 valid: {43 (key: string, value: any): Data44 (): Data
72 unmodified lines
117 return obj118 }119 } as InstanceType<typeof Request>['cookie']120124 Request.prototype.parseBody = async function (121 Request.prototype.parseBody = async function <BodyType extends BodyData>(122 this: Request126 ): Promise<Record<string, string | File>> {123 ): Promise<BodyType> {124 // Cache the parsed body128 let body: Record<string, string | File>125 let body: BodyType126 if (!this.bodyData) {130 body = await parseBody(this)127 body = await parseBody<BodyType>(this)128 this.bodyData = body129 } else {133 body = this.bodyData130 body = this.bodyData as BodyType131 }132 return body133 } as InstanceType<typeof Request>['parseBody']134138 Request.prototype.json = async function (this: Request): Promise<any> {135 Request.prototype.json = async function <JSONData>(this: Request): Promise<JSONData> {136 // Cache the JSON body140 let jsonData: any137 let jsonData: JSONData138 if (!this.jsonData) {139 jsonData = JSON.parse(await this.text())140 this.jsonData = jsonData141 } else {
15 unmodified lines