mirror of
https://github.com/honojs/hono.git
synced 2024-11-22 11:17:33 +01:00
feat: c.req.body
and c.req.json
accept generics (#529)
This commit is contained in:
parent
5133a93925
commit
976b2c310e
@ -1,4 +1,5 @@
|
||||
import { 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'
|
||||
@ -33,14 +34,10 @@ declare global {
|
||||
(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
|
||||
@ -121,23 +118,23 @@ export function extendRequestPrototype() {
|
||||
}
|
||||
} 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
|
||||
|
@ -1,4 +1,8 @@
|
||||
export 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 (
|
||||
@ -12,5 +16,5 @@ export async function parseBody(r: Request | Response): Promise<Record<string, s
|
||||
return acc
|
||||
}, form)
|
||||
}
|
||||
return body
|
||||
return body as BodyType
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { parseBody } from './utils/body'
|
||||
import type { BodyData } from './utils/body'
|
||||
import type { Cookie } from './utils/cookie'
|
||||
import { parse } from './utils/cookie'
|
||||
import { getQueryStringFromURL } from './utils/url'
|
||||
@ -33,14 +34,10 @@ declare global {
|
||||
(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
|
||||
@ -121,23 +118,23 @@ export function extendRequestPrototype() {
|
||||
}
|
||||
} 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
|
||||
|
@ -1,4 +1,8 @@
|
||||
export 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 (
|
||||
@ -12,5 +16,5 @@ export async function parseBody(r: Request | Response): Promise<Record<string, s
|
||||
return acc
|
||||
}, form)
|
||||
}
|
||||
return body
|
||||
return body as BodyType
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user