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
Provenance
- Commit
976b2c3- Tree
a9a1ac9- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
5133a93
4 files changed+34 added−32 removed
modified:deno_dist/utils/body.ts+6 added-2 removed
11234567895 unmodified lines15161718151920export async function parseBody(r: Request | Response): Promise<Record<string, string | File>> {export type BodyData = Record<string, string | number | boolean | File>
export async function parseBody<BodyType extends BodyData>( r: Request | Response): Promise<BodyType> { let body: Record<string, string | File> = {} const contentType = r.headers.get('Content-Type') if ( contentType &&5 unmodified lines acc[cur[0]] = cur[1] return acc }, form) } return body return body as BodyType}modified:deno_dist/request.ts+11 added-14 removed
12345626 unmodified lines3334353636373839373839414243404142434472 unmodified lines11711811912012412112212612312412812512613012712812913313013113213313413813513614013713813914014115 unmodified linesimport { parseBody } from './utils/body.ts'import type { BodyData } from './utils/body.ts'import type { Cookie } from './utils/cookie.ts'import { parse } from './utils/cookie.ts'import { getQueryStringFromURL } from './utils/url.ts'
26 unmodified lines cookie: { (name: string): string (): Cookie } bodyData?: Record<string, string | File> parseBody: { (): Promise<Record<string, string | File>> } bodyData?: BodyData parseBody<BodyType extends BodyData>(): Promise<BodyType> jsonData?: any json: { (): Promise<any> } json<JSONData = any>(): Promise<JSONData> data: Data valid: { (key: string, value: any): Data (): Data72 unmodified lines return obj } } as InstanceType<typeof Request>['cookie']
Request.prototype.parseBody = async function ( Request.prototype.parseBody = async function <BodyType extends BodyData>( this: Request ): Promise<Record<string, string | File>> { ): Promise<BodyType> { // Cache the parsed body let body: Record<string, string | File> let body: BodyType if (!this.bodyData) { body = await parseBody(this) body = await parseBody<BodyType>(this) this.bodyData = body } else { body = this.bodyData body = this.bodyData as BodyType } return body } as InstanceType<typeof Request>['parseBody']
Request.prototype.json = async function (this: Request): Promise<any> { Request.prototype.json = async function <JSONData>(this: Request): Promise<JSONData> { // Cache the JSON body let jsonData: any let jsonData: JSONData if (!this.jsonData) { jsonData = JSON.parse(await this.text()) this.jsonData = jsonData } else {15 unmodified linesmodified:src/utils/body.ts+6 added-2 removed
11234567895 unmodified lines15161718151920export async function parseBody(r: Request | Response): Promise<Record<string, string | File>> {export type BodyData = Record<string, string | number | boolean | File>
export async function parseBody<BodyType extends BodyData>( r: Request | Response): Promise<BodyType> { let body: Record<string, string | File> = {} const contentType = r.headers.get('Content-Type') if ( contentType &&5 unmodified lines acc[cur[0]] = cur[1] return acc }, form) } return body return body as BodyType}modified:src/request.ts+11 added-14 removed
1 import { parseBody } from './utils/body'2+import type { BodyData } from './utils/body'3 import type { Cookie } from './utils/cookie'4 import { parse } from './utils/cookie'5 import { getQueryStringFromURL } from './utils/url'6 26 unmodified lines33 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 (): Data72 unmodified lines117 return obj118 }119 } as InstanceType<typeof Request>['cookie']120 124- 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']134 138- 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